refactor: move to get_query_builder

This commit is contained in:
saxenabhishek 2021-07-23 18:39:27 +05:30
parent c05a032811
commit dd36b2a528
4 changed files with 24 additions and 14 deletions

View file

@ -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()

View file

@ -1 +1 @@
from frappe.query_builder.qb import qb as query_builder
from frappe.query_builder.builder import get_query_builder

View file

@ -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

View file

@ -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")