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:
Aarol D'Souza 2026-01-12 12:06:28 +05:30 committed by GitHub
parent 02420bde3f
commit 59b440cb28
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 30 additions and 6 deletions

View file

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

View file

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