diff --git a/frappe/__init__.py b/frappe/__init__.py index 39e21efb9e..24faf840bd 100644 --- a/frappe/__init__.py +++ b/frappe/__init__.py @@ -28,7 +28,7 @@ from .exceptions import * from .utils.jinja import (get_jenv, get_template, render_template, get_email_from_template, get_jloader) from .utils.lazy_loader import lazy_import -from frappe.query_builder import query_builder +from frappe.query_builder import get_query_builder # Lazy imports faker = lazy_import('faker') @@ -205,7 +205,7 @@ def init(site, sites_path=None, new_site=False): local.form_dict = _dict() local.session = _dict() local.dev_server = _dev_server - local.qb = query_builder(local.conf.db_type) + local.qb = get_query_builder(local.conf.db_type) setup_module_map() diff --git a/frappe/query_builder/__init__.py b/frappe/query_builder/__init__.py index da1748beec..00356ab679 100644 --- a/frappe/query_builder/__init__.py +++ b/frappe/query_builder/__init__.py @@ -1 +1 @@ -from frappe.query_builder.qb import qb as query_builder +from frappe.query_builder.builder import get_query_builder diff --git a/frappe/query_builder/qb.py b/frappe/query_builder/builder.py similarity index 80% rename from frappe/query_builder/qb.py rename to frappe/query_builder/builder.py index 2c1e3e103a..db16047ce9 100644 --- a/frappe/query_builder/qb.py +++ b/frappe/query_builder/builder.py @@ -1,15 +1,25 @@ -from pypika import MySQLQuery, Order, PostgreSQLQuery +from pypika import MySQLQuery, Order, PostgreSQLQuery, Query from pypika import functions as fn from pypika import terms from pypika.queries import Schema, Table -import frappe.query_builder.custom_functions as SpecialFuncs +import frappe.query_builder.functions as SpecialFuncs -def qb(db_type): + +def get_query_builder(db_type: str) -> Query: + """[return the query builder object] + + Args: + db_type (str): [string value of the db used] + + Returns: + Query: [Query object] + """ if not db_type: db_type = "mariadb" selecter = {"mariadb": MariaDB, "postgres": Postgres} return selecter[db_type] + class common: fn = fn terms = terms @@ -17,24 +27,24 @@ class common: Schema = Schema @staticmethod - def Table(classname:str, *args, **kwargs): + def Table(classname: str, *args, **kwargs) -> Table: if not classname.startswith("__"): - classname = "tab" + classname + classname = f"tab{classname}" return Table(classname, *args, **kwargs) -class MariaDB(common, MySQLQuery,): + +class MariaDB(common, MySQLQuery): Field = terms.Field GROUP_CONCAT = SpecialFuncs.GROUP_CONCAT Match = SpecialFuncs.Match - def __init__(self) -> None: super().__init__() @classmethod def from_(cls, class_name, *args, **kwargs): - if isinstance(class_name,str): - class_name = "tab"+class_name + if isinstance(class_name, str): + class_name = f"tab{class_name}" return super().from_(class_name, *args, **kwargs) @staticmethod @@ -50,7 +60,7 @@ class MariaDB(common, MySQLQuery,): return f"ALTER TABLE `{tb}` MODIFY `{col}` {type} NOT NULL" -class Postgres(common, PostgreSQLQuery,): +class Postgres(common, PostgreSQLQuery): postgres_field = {"table_name": "relname", "table_rows": "n_tup_ins"} information_schema_translation = {"tables": "pg_stat_all_tables"} GROUP_CONCAT = SpecialFuncs.STRING_AGG diff --git a/frappe/query_builder/custom_functions.py b/frappe/query_builder/functions.py similarity index 100% rename from frappe/query_builder/custom_functions.py rename to frappe/query_builder/functions.py index 6b6dd137ab..3b45d78726 100644 --- a/frappe/query_builder/custom_functions.py +++ b/frappe/query_builder/functions.py @@ -2,7 +2,6 @@ from pypika import functions as fn from pypika.utils import builder - class GROUP_CONCAT(fn.DistinctOptionFunction): def __init__(self, col, alias=None): super(GROUP_CONCAT, self).__init__("GROUP_CONCAT", col, alias=alias) @@ -12,6 +11,7 @@ class STRING_AGG(fn.DistinctOptionFunction): def __init__(self, col, val=",", alias=None): super(STRING_AGG, self).__init__("STRING_AGG", col, val, alias=alias) + class Match(fn.DistinctOptionFunction): def __init__(self, col, *args, **kwargs): alias = kwargs.get("alias")