Revert "[optimize] remove count(*) from queries" (#2353)

This commit is contained in:
Rushabh Mehta 2016-11-22 10:55:13 +05:30 committed by GitHub
parent 188c7390eb
commit a099690e8f
15 changed files with 21 additions and 21 deletions

View file

@ -94,7 +94,7 @@ def get_allowed_pages():
# pages where role is not set are also allowed
for p in frappe.db.sql("""select name, modified, title
from `tabPage` where
(select count(name) from `tabPage Role`
(select count(*) from `tabPage Role`
where `tabPage Role`.parent=tabPage.name) = 0""", as_dict=1):
page_info[p.name] = {"modified":p.modified, "title":p.title}

View file

@ -586,7 +586,7 @@ def sign_up(email, full_name, redirect_to):
else:
return _("Already Registered")
else:
if frappe.db.sql("""select count(name) from tabUser where
if frappe.db.sql("""select count(*) from tabUser where
HOUR(TIMEDIFF(CURRENT_TIMESTAMP, TIMESTAMP(modified)))=1""")[0][0] > 300:
frappe.respond_as_web_page(_('Temperorily Disabled'),
@ -676,19 +676,19 @@ def get_system_users(exclude_users=None, limit=None):
def get_active_users():
"""Returns No. of system users who logged in, in the last 3 days"""
return frappe.db.sql("""select count(name) from `tabUser`
return frappe.db.sql("""select count(*) from `tabUser`
where enabled = 1 and user_type != 'Website User'
and name not in ({})
and hour(timediff(now(), last_active)) < 72""".format(", ".join(["%s"]*len(STANDARD_USERS))), STANDARD_USERS)[0][0]
def get_website_users():
"""Returns total no. of website users"""
return frappe.db.sql("""select count(name) from `tabUser`
return frappe.db.sql("""select count(*) from `tabUser`
where enabled = 1 and user_type = 'Website User'""")[0][0]
def get_active_website_users():
"""Returns No. of website users who logged in, in the last 3 days"""
return frappe.db.sql("""select count(name) from `tabUser`
return frappe.db.sql("""select count(*) from `tabUser`
where enabled = 1 and user_type = 'Website User'
and hour(timediff(now(), last_active)) < 72""")[0][0]

View file

@ -44,7 +44,7 @@ def get_todays_events(as_list=False):
def get_unread_messages():
"returns unread (docstatus-0 messages for a user)"
return frappe.db.sql("""\
select count(name)
SELECT count(*)
FROM `tabCommunication`
WHERE communication_type in ('Chat', 'Notification')
AND reference_doctype = 'User'
@ -55,7 +55,7 @@ def get_unread_messages():
def get_unseen_likes():
"""Returns count of unseen likes"""
return frappe.db.sql("""select count(name) from `tabCommunication`
return frappe.db.sql("""select count(*) from `tabCommunication`
where
communication_type='Comment'
and modified >= DATE_SUB(NOW(),INTERVAL 1 YEAR)

View file

@ -77,7 +77,7 @@ def get_fields_label(doctype=None):
def create_custom_field_if_values_exist(doctype, df):
df = frappe._dict(df)
if df.fieldname in frappe.db.get_table_columns(doctype) and \
frappe.db.sql("""select count(name) from `tab{doctype}`
frappe.db.sql("""select count(*) from `tab{doctype}`
where ifnull({fieldname},'')!=''""".format(doctype=doctype, fieldname=df.fieldname))[0][0]:
create_custom_field(doctype, df)

View file

@ -777,10 +777,10 @@ class Database:
"""Returns `COUNT(*)` for given DocType and filters."""
if filters:
conditions, filters = self.build_conditions(filters)
return frappe.db.sql("""select count(name)
return frappe.db.sql("""select count(*)
from `tab%s` where %s""" % (dt, conditions), filters, debug=debug)[0][0]
else:
return frappe.db.sql("""select count(name)
return frappe.db.sql("""select count(*)
from `tab%s`""" % (dt,))[0][0]

View file

@ -110,7 +110,7 @@ def add_user_icon(_doctype, label=None, link=None, type='link', standard=0):
else:
idx = frappe.db.sql('select max(idx) from `tabDesktop Icon` where owner=%s',
frappe.session.user)[0][0] or \
frappe.db.sql('select count(name) from `tabDesktop Icon` where standard=1')[0][0]
frappe.db.sql('select count(*) from `tabDesktop Icon` where standard=1')[0][0]
module = frappe.db.get_value('DocType', _doctype, 'module')
module_icon = frappe.get_value('Desktop Icon', {'standard':1, 'module_name':module},

View file

@ -61,7 +61,7 @@ def get_list(arg=None):
@frappe.whitelist()
def get_active_users():
data = frappe.db.sql("""select name,
(select count(name) from tabSessions where user=tabUser.name
(select count(*) from tabSessions where user=tabUser.name
and timediff(now(), lastupdate) < time("01:00:00")) as has_session
from tabUser
where enabled=1 and

View file

@ -35,7 +35,7 @@ class AutoEmailReport(Document):
def validate_report_count(self):
'''check that there are only 3 enabled reports per user'''
count = frappe.db.sql('select count(name) from `tabAuto Email Report` where user=%s and enabled=1', self.user)[0][0]
count = frappe.db.sql('select count(*) from `tabAuto Email Report` where user=%s and enabled=1', self.user)[0][0]
if count > max_reports_per_user:
frappe.throw(_('Only {0} emailed reports are allowed per user').format(max_reports_per_user))

View file

@ -49,7 +49,7 @@ class EmailGroup(Document):
return self.total_subscribers
def get_total_subscribers(self):
return frappe.db.sql("""select count(name) from `tabEmail Group Member`
return frappe.db.sql("""select count(*) from `tabEmail Group Member`
where email_group=%s""", self.name)[0][0]
def on_trash(self):

View file

@ -29,7 +29,7 @@ def execute():
# reset file size
for folder in frappe.db.sql("""select name from tabFile f1 where is_folder = 1 and
(select count(name) from tabFile f2 where f2.folder = f1.name and f2.is_folder = 1) = 0"""):
(select count(*) from tabFile f2 where f2.folder = f1.name and f2.is_folder = 1) = 0"""):
folder = frappe.get_doc("File", folder[0])
folder.save()

View file

@ -10,7 +10,7 @@ from frappe.model.document import Document
class LetterHead(Document):
def validate(self):
if not self.is_default:
if not frappe.db.sql("""select count(name) from `tabLetter Head` where ifnull(is_default,0)=1"""):
if not frappe.db.sql("""select count(*) from `tabLetter Head` where ifnull(is_default,0)=1"""):
self.is_default = 1
def on_update(self):

View file

@ -88,7 +88,7 @@ class CountBot(BotParser):
if self.startswith('how many'):
self.tables = self.reply.identify_tables(self.query.split(None, 1)[1])
if self.tables:
return str(frappe.db.sql('select count(name) from `tab{0}`'.format(self.get_doctype()))[0][0])
return str(frappe.db.sql('select count(*) from `tab{0}`'.format(self.get_doctype()))[0][0])
class FindBot(BotParser):
def get_reply(self):

View file

@ -224,7 +224,7 @@ class NestedSet(Document):
def validate_one_root(self):
if not self.get(self.nsm_parent_field):
if frappe.db.sql("""select count(name) from `tab%s` where
if frappe.db.sql("""select count(*) from `tab%s` where
ifnull(%s, '')=''""" % (self.doctype, self.nsm_parent_field))[0][0] > 1:
frappe.throw(_("""Multiple root nodes not allowed."""), NestedSetMultipleRootsError)
@ -240,7 +240,7 @@ class NestedSet(Document):
def get_root_of(doctype):
"""Get root element of a DocType with a tree structure"""
return frappe.db.sql("""select t1.name from `tab{0}` t1 where
(select count(name) from `tab{1}` t2 where
(select count(*) from `tab{1}` t2 where
t2.lft < t1.lft and t2.rgt > t1.rgt) = 0""".format(doctype, doctype))[0][0]
def get_ancestors_of(doctype, name):

View file

@ -41,7 +41,7 @@ class BlogPost(WebsiteGenerator):
self.published_on = today()
# update posts
frappe.db.sql("""update tabBlogger set posts=(select count(name) from `tabBlog Post`
frappe.db.sql("""update tabBlogger set posts=(select count(*) from `tabBlog Post`
where ifnull(blogger,'')=tabBlogger.name)
where name=%s""", (self.blogger,))

View file

@ -38,7 +38,7 @@ def send_message(subject="Website Query", message="", sender=""):
return
# guest method, cap max writes per hour
if frappe.db.sql("""select count(name) from `tabCommunication`
if frappe.db.sql("""select count(*) from `tabCommunication`
where `sent_or_received`="Received"
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"