style: typecast to string inside db.sql
This commit is contained in:
parent
dd36b2a528
commit
ee3c84beef
7 changed files with 42 additions and 30 deletions
|
|
@ -141,13 +141,13 @@ def build_table_count_cache():
|
|||
return
|
||||
|
||||
_cache = frappe.cache()
|
||||
name = frappe.qb.Field("table_name").as_("name")
|
||||
count = frappe.qb.Field("table_rows").as_("count")
|
||||
table_name = frappe.qb.Field("table_name").as_("name")
|
||||
table_rows = frappe.qb.Field("table_rows").as_("count")
|
||||
information_schema = frappe.qb.Schema("information_schema")
|
||||
|
||||
q = frappe.qb.from_(information_schema.tables).select(name, count).get_sql()
|
||||
query = frappe.qb.from_(information_schema.tables).select(table_name, table_rows)
|
||||
|
||||
data = frappe.db.sql(q, as_dict=1)
|
||||
data = frappe.db.sql(query, as_dict=1)
|
||||
|
||||
counts = {d.get('name').lstrip('tab'): d.get('count', None) for d in data}
|
||||
_cache.set_value("information_schema:counts", counts)
|
||||
|
|
|
|||
|
|
@ -43,9 +43,9 @@ def get_all_empty_tables_by_module():
|
|||
table_name = frappe.qb.Field("table_name")
|
||||
information_schema = frappe.qb.Schema("information_schema")
|
||||
|
||||
q = frappe.qb.from_(information_schema.tables).select(table_name).where(table_rows == 0).get_sql()
|
||||
query = frappe.qb.from_(information_schema.tables).select(table_name).where(table_rows == 0)
|
||||
|
||||
empty_tables = set(r[0] for r in frappe.db.sql(q))
|
||||
empty_tables = set(r[0] for r in frappe.db.sql(query))
|
||||
|
||||
results = frappe.get_all("DocType", fields=["name", "module"])
|
||||
empty_tables_by_module = {}
|
||||
|
|
|
|||
|
|
@ -104,6 +104,7 @@ class Database(object):
|
|||
{"name": "a%", "owner":"test@example.com"})
|
||||
|
||||
"""
|
||||
query = str(query)
|
||||
if re.search(r'ifnull\(', query, flags=re.IGNORECASE):
|
||||
# replaces ifnull in query with coalesce
|
||||
query = re.sub(r'ifnull\(', 'coalesce(', query, flags=re.IGNORECASE)
|
||||
|
|
|
|||
|
|
@ -297,6 +297,7 @@ class PostgresDatabase(Database):
|
|||
def modify_query(query):
|
||||
""""Modifies query according to the requirements of postgres"""
|
||||
# replace ` with " for definitions
|
||||
query = str(query)
|
||||
query = query.replace('`', '"')
|
||||
query = replace_locate_with_strpos(query)
|
||||
# select from requires ""
|
||||
|
|
|
|||
|
|
@ -7,24 +7,23 @@ def execute():
|
|||
ToDo = frappe.qb.Table("ToDo")
|
||||
assignees = frappe.qb.GROUP_CONCAT("owner").distinct().as_("assignees")
|
||||
|
||||
q = (
|
||||
query = (
|
||||
frappe.qb.from_(ToDo)
|
||||
.select(ToDo.name, ToDo.reference_type, assignees)
|
||||
.where(frappe.qb.fn.Coalesce(ToDo.reference_type, "") != "")
|
||||
.where(frappe.qb.fn.Coalesce(ToDo.reference_name, "") != "")
|
||||
.where(ToDo.status != "Cancelled")
|
||||
.groupby(ToDo.reference_type, ToDo.reference_name)
|
||||
.get_sql()
|
||||
)
|
||||
|
||||
assignments = frappe.db.sql(q, as_dict=True)
|
||||
assignments = frappe.db.sql(query, as_dict=True)
|
||||
|
||||
for doc in assignments:
|
||||
assignments = doc.assignees.split(',')
|
||||
assignments = doc.assignees.split(",")
|
||||
frappe.db.set_value(
|
||||
doc.reference_type,
|
||||
doc.reference_name,
|
||||
'_assign',
|
||||
"_assign",
|
||||
frappe.as_json(assignments),
|
||||
update_modified=False
|
||||
)
|
||||
)
|
||||
|
|
@ -411,14 +411,16 @@ def search(text, start=0, limit=20, doctype=""):
|
|||
:param limit: number of results to return, default 20
|
||||
:return: Array of result objects
|
||||
"""
|
||||
from frappe.desk.doctype.global_search_settings.global_search_settings import get_doctypes_for_global_search
|
||||
from frappe.desk.doctype.global_search_settings.global_search_settings import (
|
||||
get_doctypes_for_global_search,
|
||||
)
|
||||
|
||||
results = []
|
||||
sorted_results = []
|
||||
|
||||
allowed_doctypes = get_doctypes_for_global_search()
|
||||
|
||||
for text in set(text.split('&')):
|
||||
for text in set(text.split("&")):
|
||||
text = text.strip()
|
||||
if not text:
|
||||
continue
|
||||
|
|
@ -427,21 +429,24 @@ def search(text, start=0, limit=20, doctype=""):
|
|||
|
||||
rank = frappe.qb.Match(global_search.content).Against(text).as_("rank")
|
||||
|
||||
q = (frappe.qb.from_(global_search)
|
||||
.select(global_search.doctype,global_search.name,global_search.content,rank)
|
||||
query = (
|
||||
frappe.qb.from_(global_search)
|
||||
.select(
|
||||
global_search.doctype, global_search.name, global_search.content, rank
|
||||
)
|
||||
.orderby("rank", order=frappe.qb.desc)
|
||||
.limit(limit))
|
||||
.limit(limit)
|
||||
)
|
||||
|
||||
if doctype:
|
||||
q = q.where(global_search.doctype == doctype)
|
||||
query = query.where(global_search.doctype == doctype)
|
||||
elif allowed_doctypes:
|
||||
q = q.where(global_search.doctype.isin(allowed_doctypes))
|
||||
query = query.where(global_search.doctype.isin(allowed_doctypes))
|
||||
|
||||
if int(start) > 0:
|
||||
q = q.offset(int(start))
|
||||
if start > 0:
|
||||
query = query.offset(start)
|
||||
|
||||
|
||||
result = frappe.db.sql(q.get_sql(),as_dict=True)
|
||||
result = frappe.db.sql(query, as_dict=True)
|
||||
|
||||
results.extend(result)
|
||||
|
||||
|
|
@ -452,7 +457,9 @@ def search(text, start=0, limit=20, doctype=""):
|
|||
try:
|
||||
meta = frappe.get_meta(r.doctype)
|
||||
if meta.image_field:
|
||||
r.image = frappe.db.get_value(r.doctype, r.name, meta.image_field)
|
||||
r.image = frappe.db.get_value(
|
||||
r.doctype, r.name, meta.image_field
|
||||
)
|
||||
except Exception:
|
||||
frappe.clear_messages()
|
||||
|
||||
|
|
|
|||
|
|
@ -56,17 +56,21 @@ class WebsiteAnalytics(object):
|
|||
]
|
||||
|
||||
def get_data(self):
|
||||
Web_Page_View = frappe.qb.Table("Web Page View")
|
||||
WebPageView = frappe.qb.Table("Web Page View")
|
||||
count_all = frappe.qb.fn.Count("*").as_("count")
|
||||
case = frappe.qb.terms.Case().when(Web_Page_View.is_unique == "1", "1")
|
||||
case = frappe.qb.terms.Case().when(WebPageView.is_unique == "1", "1")
|
||||
count_is_unique = frappe.qb.fn.Count(case).as_("unique_count")
|
||||
|
||||
curr = (
|
||||
frappe.qb.from_(Web_Page_View)
|
||||
frappe.qb.from_(WebPageView)
|
||||
.select("path", count_all, count_is_unique)
|
||||
.where(frappe.qb.fn.Coalesce(Web_Page_View.creation, "0001-01-01")[self.filters.from_date:self.filters.to_date])
|
||||
.groupby(Web_Page_View.path)
|
||||
.orderby("count", Order=frappe.qb.desc).get_sql()
|
||||
.where(
|
||||
frappe.qb.fn.Coalesce(WebPageView.creation, "0001-01-01")[
|
||||
self.filters.from_date : self.filters.to_date
|
||||
]
|
||||
)
|
||||
.groupby(WebPageView.path)
|
||||
.orderby("count", Order=frappe.qb.desc)
|
||||
)
|
||||
return frappe.db.sql(curr)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue