[optimize] remove count(*) from queriesversion-14
@@ -94,7 +94,7 @@ def get_allowed_pages(): | |||||
# pages where role is not set are also allowed | # pages where role is not set are also allowed | ||||
for p in frappe.db.sql("""select name, modified, title | for p in frappe.db.sql("""select name, modified, title | ||||
from `tabPage` where | from `tabPage` where | ||||
(select count(*) from `tabPage Role` | |||||
(select count(name) from `tabPage Role` | |||||
where `tabPage Role`.parent=tabPage.name) = 0""", as_dict=1): | where `tabPage Role`.parent=tabPage.name) = 0""", as_dict=1): | ||||
page_info[p.name] = {"modified":p.modified, "title":p.title} | page_info[p.name] = {"modified":p.modified, "title":p.title} | ||||
@@ -586,7 +586,7 @@ def sign_up(email, full_name, redirect_to): | |||||
else: | else: | ||||
return _("Already Registered") | return _("Already Registered") | ||||
else: | else: | ||||
if frappe.db.sql("""select count(*) from tabUser where | |||||
if frappe.db.sql("""select count(name) from tabUser where | |||||
HOUR(TIMEDIFF(CURRENT_TIMESTAMP, TIMESTAMP(modified)))=1""")[0][0] > 300: | HOUR(TIMEDIFF(CURRENT_TIMESTAMP, TIMESTAMP(modified)))=1""")[0][0] > 300: | ||||
frappe.respond_as_web_page(_('Temperorily Disabled'), | frappe.respond_as_web_page(_('Temperorily Disabled'), | ||||
@@ -676,19 +676,19 @@ def get_system_users(exclude_users=None, limit=None): | |||||
def get_active_users(): | def get_active_users(): | ||||
"""Returns No. of system users who logged in, in the last 3 days""" | """Returns No. of system users who logged in, in the last 3 days""" | ||||
return frappe.db.sql("""select count(*) from `tabUser` | |||||
return frappe.db.sql("""select count(name) from `tabUser` | |||||
where enabled = 1 and user_type != 'Website User' | where enabled = 1 and user_type != 'Website User' | ||||
and name not in ({}) | and name not in ({}) | ||||
and hour(timediff(now(), last_active)) < 72""".format(", ".join(["%s"]*len(STANDARD_USERS))), STANDARD_USERS)[0][0] | and hour(timediff(now(), last_active)) < 72""".format(", ".join(["%s"]*len(STANDARD_USERS))), STANDARD_USERS)[0][0] | ||||
def get_website_users(): | def get_website_users(): | ||||
"""Returns total no. of website users""" | """Returns total no. of website users""" | ||||
return frappe.db.sql("""select count(*) from `tabUser` | |||||
return frappe.db.sql("""select count(name) from `tabUser` | |||||
where enabled = 1 and user_type = 'Website User'""")[0][0] | where enabled = 1 and user_type = 'Website User'""")[0][0] | ||||
def get_active_website_users(): | def get_active_website_users(): | ||||
"""Returns No. of website users who logged in, in the last 3 days""" | """Returns No. of website users who logged in, in the last 3 days""" | ||||
return frappe.db.sql("""select count(*) from `tabUser` | |||||
return frappe.db.sql("""select count(name) from `tabUser` | |||||
where enabled = 1 and user_type = 'Website User' | where enabled = 1 and user_type = 'Website User' | ||||
and hour(timediff(now(), last_active)) < 72""")[0][0] | and hour(timediff(now(), last_active)) < 72""")[0][0] | ||||
@@ -44,7 +44,7 @@ def get_todays_events(as_list=False): | |||||
def get_unread_messages(): | def get_unread_messages(): | ||||
"returns unread (docstatus-0 messages for a user)" | "returns unread (docstatus-0 messages for a user)" | ||||
return frappe.db.sql("""\ | return frappe.db.sql("""\ | ||||
SELECT count(*) | |||||
select count(name) | |||||
FROM `tabCommunication` | FROM `tabCommunication` | ||||
WHERE communication_type in ('Chat', 'Notification') | WHERE communication_type in ('Chat', 'Notification') | ||||
AND reference_doctype = 'User' | AND reference_doctype = 'User' | ||||
@@ -55,7 +55,7 @@ def get_unread_messages(): | |||||
def get_unseen_likes(): | def get_unseen_likes(): | ||||
"""Returns count of unseen likes""" | """Returns count of unseen likes""" | ||||
return frappe.db.sql("""select count(*) from `tabCommunication` | |||||
return frappe.db.sql("""select count(name) from `tabCommunication` | |||||
where | where | ||||
communication_type='Comment' | communication_type='Comment' | ||||
and modified >= DATE_SUB(NOW(),INTERVAL 1 YEAR) | and modified >= DATE_SUB(NOW(),INTERVAL 1 YEAR) | ||||
@@ -77,7 +77,7 @@ def get_fields_label(doctype=None): | |||||
def create_custom_field_if_values_exist(doctype, df): | def create_custom_field_if_values_exist(doctype, df): | ||||
df = frappe._dict(df) | df = frappe._dict(df) | ||||
if df.fieldname in frappe.db.get_table_columns(doctype) and \ | if df.fieldname in frappe.db.get_table_columns(doctype) and \ | ||||
frappe.db.sql("""select count(*) from `tab{doctype}` | |||||
frappe.db.sql("""select count(name) from `tab{doctype}` | |||||
where ifnull({fieldname},'')!=''""".format(doctype=doctype, fieldname=df.fieldname))[0][0]: | where ifnull({fieldname},'')!=''""".format(doctype=doctype, fieldname=df.fieldname))[0][0]: | ||||
create_custom_field(doctype, df) | create_custom_field(doctype, df) | ||||
@@ -777,10 +777,10 @@ class Database: | |||||
"""Returns `COUNT(*)` for given DocType and filters.""" | """Returns `COUNT(*)` for given DocType and filters.""" | ||||
if filters: | if filters: | ||||
conditions, filters = self.build_conditions(filters) | conditions, filters = self.build_conditions(filters) | ||||
return frappe.db.sql("""select count(*) | |||||
return frappe.db.sql("""select count(name) | |||||
from `tab%s` where %s""" % (dt, conditions), filters, debug=debug)[0][0] | from `tab%s` where %s""" % (dt, conditions), filters, debug=debug)[0][0] | ||||
else: | else: | ||||
return frappe.db.sql("""select count(*) | |||||
return frappe.db.sql("""select count(name) | |||||
from `tab%s`""" % (dt,))[0][0] | from `tab%s`""" % (dt,))[0][0] | ||||
@@ -110,7 +110,7 @@ def add_user_icon(_doctype, label=None, link=None, type='link', standard=0): | |||||
else: | else: | ||||
idx = frappe.db.sql('select max(idx) from `tabDesktop Icon` where owner=%s', | idx = frappe.db.sql('select max(idx) from `tabDesktop Icon` where owner=%s', | ||||
frappe.session.user)[0][0] or \ | frappe.session.user)[0][0] or \ | ||||
frappe.db.sql('select count(*) from `tabDesktop Icon` where standard=1')[0][0] | |||||
frappe.db.sql('select count(name) from `tabDesktop Icon` where standard=1')[0][0] | |||||
module = frappe.db.get_value('DocType', _doctype, 'module') | module = frappe.db.get_value('DocType', _doctype, 'module') | ||||
module_icon = frappe.get_value('Desktop Icon', {'standard':1, 'module_name':module}, | module_icon = frappe.get_value('Desktop Icon', {'standard':1, 'module_name':module}, | ||||
@@ -61,7 +61,7 @@ def get_list(arg=None): | |||||
@frappe.whitelist() | @frappe.whitelist() | ||||
def get_active_users(): | def get_active_users(): | ||||
data = frappe.db.sql("""select name, | data = frappe.db.sql("""select name, | ||||
(select count(*) from tabSessions where user=tabUser.name | |||||
(select count(name) from tabSessions where user=tabUser.name | |||||
and timediff(now(), lastupdate) < time("01:00:00")) as has_session | and timediff(now(), lastupdate) < time("01:00:00")) as has_session | ||||
from tabUser | from tabUser | ||||
where enabled=1 and | where enabled=1 and | ||||
@@ -35,7 +35,7 @@ class AutoEmailReport(Document): | |||||
def validate_report_count(self): | def validate_report_count(self): | ||||
'''check that there are only 3 enabled reports per user''' | '''check that there are only 3 enabled reports per user''' | ||||
count = frappe.db.sql('select count(*) from `tabAuto Email Report` where user=%s and enabled=1', self.user)[0][0] | |||||
count = frappe.db.sql('select count(name) from `tabAuto Email Report` where user=%s and enabled=1', self.user)[0][0] | |||||
if count > max_reports_per_user: | if count > max_reports_per_user: | ||||
frappe.throw(_('Only {0} emailed reports are allowed per user').format(max_reports_per_user)) | frappe.throw(_('Only {0} emailed reports are allowed per user').format(max_reports_per_user)) | ||||
@@ -49,7 +49,7 @@ class EmailGroup(Document): | |||||
return self.total_subscribers | return self.total_subscribers | ||||
def get_total_subscribers(self): | def get_total_subscribers(self): | ||||
return frappe.db.sql("""select count(*) from `tabEmail Group Member` | |||||
return frappe.db.sql("""select count(name) from `tabEmail Group Member` | |||||
where email_group=%s""", self.name)[0][0] | where email_group=%s""", self.name)[0][0] | ||||
def on_trash(self): | def on_trash(self): | ||||
@@ -29,7 +29,7 @@ def execute(): | |||||
# reset file size | # reset file size | ||||
for folder in frappe.db.sql("""select name from tabFile f1 where is_folder = 1 and | for folder in frappe.db.sql("""select name from tabFile f1 where is_folder = 1 and | ||||
(select count(*) from tabFile f2 where f2.folder = f1.name and f2.is_folder = 1) = 0"""): | |||||
(select count(name) from tabFile f2 where f2.folder = f1.name and f2.is_folder = 1) = 0"""): | |||||
folder = frappe.get_doc("File", folder[0]) | folder = frappe.get_doc("File", folder[0]) | ||||
folder.save() | folder.save() | ||||
@@ -10,7 +10,7 @@ from frappe.model.document import Document | |||||
class LetterHead(Document): | class LetterHead(Document): | ||||
def validate(self): | def validate(self): | ||||
if not self.is_default: | if not self.is_default: | ||||
if not frappe.db.sql("""select count(*) from `tabLetter Head` where ifnull(is_default,0)=1"""): | |||||
if not frappe.db.sql("""select count(name) from `tabLetter Head` where ifnull(is_default,0)=1"""): | |||||
self.is_default = 1 | self.is_default = 1 | ||||
def on_update(self): | def on_update(self): | ||||
@@ -88,7 +88,7 @@ class CountBot(BotParser): | |||||
if self.startswith('how many'): | if self.startswith('how many'): | ||||
self.tables = self.reply.identify_tables(self.query.split(None, 1)[1]) | self.tables = self.reply.identify_tables(self.query.split(None, 1)[1]) | ||||
if self.tables: | if self.tables: | ||||
return str(frappe.db.sql('select count(*) from `tab{0}`'.format(self.get_doctype()))[0][0]) | |||||
return str(frappe.db.sql('select count(name) from `tab{0}`'.format(self.get_doctype()))[0][0]) | |||||
class FindBot(BotParser): | class FindBot(BotParser): | ||||
def get_reply(self): | def get_reply(self): | ||||
@@ -224,7 +224,7 @@ class NestedSet(Document): | |||||
def validate_one_root(self): | def validate_one_root(self): | ||||
if not self.get(self.nsm_parent_field): | if not self.get(self.nsm_parent_field): | ||||
if frappe.db.sql("""select count(*) from `tab%s` where | |||||
if frappe.db.sql("""select count(name) from `tab%s` where | |||||
ifnull(%s, '')=''""" % (self.doctype, self.nsm_parent_field))[0][0] > 1: | ifnull(%s, '')=''""" % (self.doctype, self.nsm_parent_field))[0][0] > 1: | ||||
frappe.throw(_("""Multiple root nodes not allowed."""), NestedSetMultipleRootsError) | frappe.throw(_("""Multiple root nodes not allowed."""), NestedSetMultipleRootsError) | ||||
@@ -240,7 +240,7 @@ class NestedSet(Document): | |||||
def get_root_of(doctype): | def get_root_of(doctype): | ||||
"""Get root element of a DocType with a tree structure""" | """Get root element of a DocType with a tree structure""" | ||||
return frappe.db.sql("""select t1.name from `tab{0}` t1 where | return frappe.db.sql("""select t1.name from `tab{0}` t1 where | ||||
(select count(*) from `tab{1}` t2 where | |||||
(select count(name) from `tab{1}` t2 where | |||||
t2.lft < t1.lft and t2.rgt > t1.rgt) = 0""".format(doctype, doctype))[0][0] | t2.lft < t1.lft and t2.rgt > t1.rgt) = 0""".format(doctype, doctype))[0][0] | ||||
def get_ancestors_of(doctype, name): | def get_ancestors_of(doctype, name): | ||||
@@ -41,7 +41,7 @@ class BlogPost(WebsiteGenerator): | |||||
self.published_on = today() | self.published_on = today() | ||||
# update posts | # update posts | ||||
frappe.db.sql("""update tabBlogger set posts=(select count(*) from `tabBlog Post` | |||||
frappe.db.sql("""update tabBlogger set posts=(select count(name) from `tabBlog Post` | |||||
where ifnull(blogger,'')=tabBlogger.name) | where ifnull(blogger,'')=tabBlogger.name) | ||||
where name=%s""", (self.blogger,)) | where name=%s""", (self.blogger,)) | ||||
@@ -38,7 +38,7 @@ def send_message(subject="Website Query", message="", sender=""): | |||||
return | return | ||||
# guest method, cap max writes per hour | # guest method, cap max writes per hour | ||||
if frappe.db.sql("""select count(*) from `tabCommunication` | |||||
if frappe.db.sql("""select count(name) from `tabCommunication` | |||||
where `sent_or_received`="Received" | where `sent_or_received`="Received" | ||||
and TIMEDIFF(%s, modified) < '01:00:00'""", now())[0][0] > max_communications_per_hour: | and TIMEDIFF(%s, modified) < '01:00:00'""", now())[0][0] > max_communications_per_hour: | ||||
frappe.response["message"] = "Sorry: we believe we have received an unreasonably high number of requests of this kind. Please try later" | frappe.response["message"] = "Sorry: we believe we have received an unreasonably high number of requests of this kind. Please try later" | ||||