fix(search): make QB DB-Aware when using Locate (#35796)
* fix: make QB DB-Aware when choosing Locate * fix(test): adjust test to check smarter qb choice based on db
This commit is contained in:
parent
02420bde3f
commit
59b440cb28
2 changed files with 30 additions and 6 deletions
|
|
@ -25,8 +25,21 @@ class Concat_ws(Function):
|
|||
|
||||
|
||||
class Locate(Function):
|
||||
def __init__(self, *terms, **kwargs):
|
||||
super().__init__("LOCATE", *terms, **kwargs)
|
||||
def __init__(self, needle, haystack, **kwargs):
|
||||
super().__init__("LOCATE", needle, haystack, **kwargs)
|
||||
|
||||
|
||||
class Strpos(Function):
|
||||
def __init__(self, needle, haystack, **kwargs):
|
||||
super().__init__("STRPOS", haystack, needle, **kwargs)
|
||||
|
||||
|
||||
class Instr(Function):
|
||||
def __init__(self, needle, haystack, **kwargs):
|
||||
super().__init__("INSTR", haystack, needle, **kwargs)
|
||||
|
||||
|
||||
Locate = ImportMapper({db_type_is.MARIADB: Locate, db_type_is.POSTGRES: Strpos, db_type_is.SQLITE: Instr})
|
||||
|
||||
|
||||
# for backward compatibility
|
||||
|
|
|
|||
|
|
@ -1812,10 +1812,21 @@ class TestQuery(IntegrationTestCase):
|
|||
],
|
||||
)
|
||||
sql = query.get_sql()
|
||||
self.assertIn(
|
||||
self.normalize_sql("1/NULLIF(LOCATE('test',`name`),0) `relevance`"),
|
||||
self.normalize_sql(sql),
|
||||
)
|
||||
if frappe.db.db_type == "mariadb":
|
||||
self.assertIn(
|
||||
self.normalize_sql("1/NULLIF(LOCATE('test',`name`),0) `relevance`"),
|
||||
self.normalize_sql(sql),
|
||||
)
|
||||
elif frappe.db.db_type == "postgres":
|
||||
self.assertIn(
|
||||
self.normalize_sql("1/NULLIF(STRPOS(`name`,'test'),0) `relevance`"),
|
||||
self.normalize_sql(sql),
|
||||
)
|
||||
elif frappe.db.db_type == "sqlite":
|
||||
self.assertIn(
|
||||
self.normalize_sql("1/NULLIF(INSTR(`name`,'test'),0) `relevance`"),
|
||||
self.normalize_sql(sql),
|
||||
)
|
||||
|
||||
# Test multiple operators in fields
|
||||
query = frappe.qb.get_query(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue