fix: Ported subqry to branch for compatibility
Via https://github.com/frappe/frappe/pull/16107
This commit is contained in:
parent
71d3f1c0d1
commit
2d806b5d6d
2 changed files with 16 additions and 3 deletions
|
|
@ -1,10 +1,12 @@
|
|||
from datetime import timedelta
|
||||
from typing import Any, Dict, Optional
|
||||
from frappe.utils.data import format_timedelta
|
||||
|
||||
from pypika.terms import Function, ValueWrapper
|
||||
from pypika.queries import QueryBuilder
|
||||
from pypika.terms import Criterion, Function, ValueWrapper
|
||||
from pypika.utils import format_alias_sql
|
||||
|
||||
from frappe.utils.data import format_timedelta
|
||||
|
||||
|
||||
class NamedParameterWrapper:
|
||||
"""Utility class to hold parameter values and keys"""
|
||||
|
|
@ -100,3 +102,12 @@ class ParameterizedFunction(Function):
|
|||
)
|
||||
|
||||
return function_sql
|
||||
|
||||
class subqry(Criterion):
|
||||
def __init__(self, subq: QueryBuilder, alias: Optional[str] = None,) -> None:
|
||||
super().__init__(alias)
|
||||
self.subq = subq
|
||||
|
||||
def get_sql(self, **kwg: Any) -> str:
|
||||
kwg["subquery"] = True
|
||||
return self.subq.get_sql(**kwg)
|
||||
|
|
|
|||
|
|
@ -295,6 +295,8 @@ class NestedSet(Document):
|
|||
def get_root_of(doctype):
|
||||
"""Get root element of a DocType with a tree structure"""
|
||||
from frappe.query_builder.functions import Count
|
||||
from frappe.query_builder.terms import subqry
|
||||
|
||||
Table = DocType(doctype)
|
||||
t1 = Table.as_("t1")
|
||||
t2 = Table.as_("t2")
|
||||
|
|
@ -303,7 +305,7 @@ def get_root_of(doctype):
|
|||
(t2.lft < t1.lft) & (t2.rgt > t1.rgt)
|
||||
)
|
||||
result = frappe.qb.from_(t1).select(t1.name).where(
|
||||
(subq == 0) & (t1.rgt > t1.lft) # depends on https://github.com/frappe/frappe/pull/16107
|
||||
(subqry(subq) == 0) & (t1.rgt > t1.lft)
|
||||
).run()
|
||||
|
||||
return result[0][0] if result else None
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue