From fc65c2cd3666a1d6fb146fdd8aa4a325a5d9cfb4 Mon Sep 17 00:00:00 2001 From: Aradhya Date: Thu, 25 Nov 2021 16:20:06 +0530 Subject: [PATCH] fix: fixed pluck in execute --- frappe/database/database.py | 69 +++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 15 deletions(-) diff --git a/frappe/database/database.py b/frappe/database/database.py index bfdaebde75..411587aa7e 100644 --- a/frappe/database/database.py +++ b/frappe/database/database.py @@ -336,7 +336,7 @@ class Database(object): return self.get_value(doctype, filters, "*", as_dict=as_dict, cache=cache) def get_value(self, doctype, filters=None, fieldname="name", ignore=None, as_dict=False, - debug=False, order_by="KEEP_DEFAULT_ORDERING", cache=False, for_update=False, run=True): + debug=False, order_by="KEEP_DEFAULT_ORDERING", cache=False, for_update=False, run=True, pluck=False): """Returns a document property or list of properties. :param doctype: DocType name. @@ -363,7 +363,7 @@ class Database(object): """ ret = self.get_values(doctype, filters, fieldname, ignore, as_dict, debug, - order_by, cache=cache, for_update=for_update, run=run) + order_by, cache=cache, for_update=for_update, run=run, pluck=pluck) if not run: return ret @@ -371,7 +371,8 @@ class Database(object): return ((len(ret[0]) > 1 or as_dict) and ret[0] or ret[0][0]) if ret else None def get_values(self, doctype, filters=None, fieldname="name", ignore=None, as_dict=False, - debug=False, order_by="KEEP_DEFAULT_ORDERING", update=None, cache=False, for_update=False, run=True): + debug=False, order_by="KEEP_DEFAULT_ORDERING", update=None, cache=False, for_update=False, + run=True, pluck=False): """Returns multiple document properties. :param doctype: DocType name. @@ -396,7 +397,7 @@ class Database(object): return self.value_cache[(doctype, filters, fieldname)] if isinstance(filters, list): - out = self._get_value_for_many_names(doctype, filters, fieldname, order_by, debug=debug, run=run) + out = self._get_value_for_many_names(doctype, filters, fieldname, order_by, debug=debug, run=run, pluck=pluck) else: fields = fieldname @@ -411,7 +412,16 @@ class Database(object): if order_by: order_by = "modified" if order_by == "KEEP_DEFAULT_ORDERING" else order_by out = self._get_values_from_table( - fields, filters, doctype, as_dict, debug, order_by, update, for_update=for_update, run=run + fields, + filters, + doctype, + as_dict, + debug, + order_by, + update, + for_update=for_update, + run=run, + pluck=pluck, ) except Exception as e: if ignore and (frappe.db.is_missing_column(e) or frappe.db.is_table_missing(e)): @@ -424,14 +434,24 @@ class Database(object): else: raise else: - out = self.get_values_from_single(fields, filters, doctype, as_dict, debug, update, run=run) + out = self.get_values_from_single(fields, filters, doctype, as_dict, debug, update, run=run, pluck=pluck) if cache and isinstance(filters, str): self.value_cache[(doctype, filters, fieldname)] = out return out - def get_values_from_single(self, fields, filters, doctype, as_dict=False, debug=False, update=None, run=True): + def get_values_from_single( + self, + fields, + filters, + doctype, + as_dict=False, + debug=False, + update=None, + run=True, + pluck=False, + ): """Get values from `tabSingles` (Single DocTypes) (internal). :param fields: List of fields, @@ -457,10 +477,16 @@ class Database(object): return [map(values.get, fields)] else: - r = self.sql("""select field, value + r = self.sql( + """select field, value from `tabSingles` where field in (%s) and doctype=%s""" - % (', '.join(['%s'] * len(fields)), '%s'), - tuple(fields) + (doctype,), as_dict=False, debug=debug, run=run) + % (", ".join(["%s"] * len(fields)), "%s"), + tuple(fields) + (doctype,), + as_dict=False, + debug=debug, + run=run, + pluck=pluck, + ) if not run: return r if as_dict: @@ -540,8 +566,19 @@ class Database(object): """Alias for get_single_value""" return self.get_single_value(*args, **kwargs) - def _get_values_from_table(self, fields, filters, doctype, as_dict, debug, order_by=None, - update=None, for_update=False, run=True, **kwargs): + def _get_values_from_table( + self, + fields, + filters, + doctype, + as_dict, + debug, + order_by=None, + update=None, + for_update=False, + run=True, + pluck=False, + ): field_objects = [] if not isinstance(fields, Criterion): @@ -558,7 +595,6 @@ class Database(object): for_update=for_update, field_objects=field_objects, fields=fields, - **kwargs, ) if ( fields == "*" @@ -567,10 +603,12 @@ class Database(object): ): as_dict = True - r = self.sql(query, as_dict=as_dict, debug=debug, update=update, run=run, **kwargs) + r = self.sql( + query, as_dict=as_dict, debug=debug, update=update, run=run, pluck=pluck + ) return r - def _get_value_for_many_names(self, doctype, names, field, order_by, debug=False, run=True): + def _get_value_for_many_names(self, doctype, names, field, order_by, debug=False, run=True, pluck=False): names = list(filter(None, names)) if names: return self.get_all( @@ -578,6 +616,7 @@ class Database(object): fields=field, filters=names, order_by=order_by, + pluck=pluck, debug=debug, as_list=1, run=run,