Skip to content

Commit 43768f5

Browse files
committed
refactor like() & not_like() methods
1 parent 5657a32 commit 43768f5

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

simple_query_builder/querybuilder.py

+24-12
Original file line numberDiff line numberDiff line change
@@ -376,20 +376,32 @@ def having(self, having: Union[str, list]):
376376

377377
return self
378378

379-
def like(self, cond: Union[str, tuple, list] = ()):
380-
if cond:
381-
if isinstance(cond, str):
382-
self.where(cond)
383-
elif isinstance(cond, tuple) or isinstance(cond, list):
384-
self.where([[cond[0], "LIKE", cond[1]]])
379+
def like(self, field: Union[str, tuple, list] = (), value: str = ""):
380+
if not field:
381+
self.set_error(f"Empty field in {inspect.stack()[0][3]} method")
382+
return self
383+
384+
if isinstance(field, str) and field and isinstance(value, str) and value:
385+
self.where([[field, "LIKE", value]])
386+
elif isinstance(field, str) and not value:
387+
self.where(field)
388+
elif isinstance(field, tuple) or isinstance(field, list):
389+
self.where([[field[0], "LIKE", field[1]]])
390+
385391
return self
386392

387-
def not_like(self, cond: Union[str, tuple, list] = ()):
388-
if cond:
389-
if isinstance(cond, str):
390-
self.where(cond)
391-
elif isinstance(cond, tuple) or isinstance(cond, list):
392-
self.where([[cond[0], "NOT LIKE", cond[1]]])
393+
def not_like(self, field: Union[str, tuple, list] = (), value: str = ""):
394+
if not field:
395+
self.set_error(f"Empty field in {inspect.stack()[0][3]} method")
396+
return self
397+
398+
if isinstance(field, str) and isinstance(value, str) and value:
399+
self.where([[field, "NOT LIKE", value]])
400+
elif isinstance(field, str) and not value:
401+
self.where(field)
402+
elif isinstance(field, tuple) or isinstance(field, list):
403+
self.where([[field[0], "NOT LIKE", field[1]]])
404+
393405
return self
394406

395407
def is_null(self, field: str = ""):

0 commit comments

Comments
 (0)