Skip to content

Commit

Permalink
Merge pull request #266 from MasoniteFramework/feature/265
Browse files Browse the repository at this point in the history
Adds where like to query builder
  • Loading branch information
josephmancuso authored Dec 17, 2020
2 parents e0a7ed3 + aa9dbc2 commit ef4c5c8
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/masoniteorm/query/QueryBuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,9 @@ def where_raw(self, query: str, bindings=()):
Returns:
self
"""
self._wheres += ((QueryExpression(query, "=", None, "value", raw=True)),)
self._wheres += (
(QueryExpression(query, "=", None, "value", raw=True, bindings=bindings)),
)
return self

def or_where(self, column: [str, int], *args) -> "self":
Expand Down Expand Up @@ -1355,7 +1357,7 @@ def min(self, column):

def _extract_operator_value(self, *args):

operators = ["=", ">", ">=", "<", "<=", "!=", "<>"]
operators = ["=", ">", ">=", "<", "<=", "!=", "<>", "like", "not like"]

operator = operators[0]

Expand Down
12 changes: 11 additions & 1 deletion src/masoniteorm/query/grammars/BaseGrammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,13 @@ def process_wheres(self, qmark=False, strip_first_where=False):
keyword=keyword, query=where.column
)

self.add_binding(where.bindings)
if not isinstance(where.bindings, (list, tuple)):
raise ValueError(
f"Binings must be tuple or list. Received {type(where.bindings)}"
)

if where.bindings:
self.add_binding(*where.bindings)

continue

Expand Down Expand Up @@ -427,6 +433,10 @@ def process_wheres(self, qmark=False, strip_first_where=False):
sql_string = self.where_not_null_string()
elif equality == "EXISTS":
sql_string = self.where_exists_string()
elif equality.upper() == "LIKE":
sql_string = self.where_like_string()
elif equality.upper() == "NOT LIKE":
sql_string = self.where_not_like_string()
else:
sql_string = self.where_string()

Expand Down
6 changes: 6 additions & 0 deletions src/masoniteorm/query/grammars/MSSQLGrammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ def having_string(self):
def where_exists_string(self):
return "{keyword} EXISTS {value}"

def where_like_string(self):
return "{keyword} {column} LIKE {value}"

def where_not_like_string(self):
return "{keyword} {column} NOT LIKE {value}"

def having_equality_string(self):
return "HAVING {column} {equality} {value}"

Expand Down
6 changes: 6 additions & 0 deletions src/masoniteorm/query/grammars/MySQLGrammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ def not_between_string(self):
def where_exists_string(self):
return "{keyword} EXISTS {value}"

def where_like_string(self):
return "{keyword} {column} LIKE {value}"

def where_not_like_string(self):
return "{keyword} {column} NOT LIKE {value}"

def subquery_alias_string(self):
return "AS {alias}"

Expand Down
6 changes: 6 additions & 0 deletions src/masoniteorm/query/grammars/PostgresGrammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ def not_between_string(self):
def where_exists_string(self):
return "{keyword} EXISTS {value}"

def where_like_string(self):
return "{keyword} {column} LIKE {value}"

def where_not_like_string(self):
return "{keyword} {column} NOT LIKE {value}"

def subquery_alias_string(self):
return "AS {alias}"

Expand Down
6 changes: 6 additions & 0 deletions src/masoniteorm/query/grammars/SQLiteGrammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ def not_between_string(self):
def where_exists_string(self):
return "{keyword} EXISTS {value}"

def where_like_string(self):
return "{keyword} {column} LIKE {value}"

def where_not_like_string(self):
return "{keyword} {column} NOT LIKE {value}"

def subquery_alias_string(self):
return "AS {alias}"

Expand Down
14 changes: 14 additions & 0 deletions src/masoniteorm/testing/BaseTestCaseSelectGrammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,17 @@ def test_can_compile_first_or_fail(self):
self, inspect.currentframe().f_code.co_name.replace("test_", "")
)()
self.assertEqual(to_sql, sql)

def test_where_like(self):
to_sql = self.builder.where("age", "like", "%name%").to_sql()
sql = getattr(
self, inspect.currentframe().f_code.co_name.replace("test_", "")
)()
self.assertEqual(to_sql, sql)

def test_where_not_like(self):
to_sql = self.builder.where("age", "not like", "%name%").to_sql()
sql = getattr(
self, inspect.currentframe().f_code.co_name.replace("test_", "")
)()
self.assertEqual(to_sql, sql)
14 changes: 14 additions & 0 deletions tests/mssql/grammar/test_mssql_select_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,17 @@ def can_compile_first_or_fail(self):
builder.where("is_admin", "=", True).first_or_fail()
"""
return """SELECT TOP 1 * FROM [users] WHERE [users].[is_admin] = '1'"""

def where_like(self):
"""
builder = self.get_builder()
builder.where("age", "like", "%name%")
"""
return """SELECT * FROM [users] WHERE [users].[age] LIKE '%name%'"""

def where_not_like(self):
"""
builder = self.get_builder()
builder.where("age", "like", "%name%")
"""
return """SELECT * FROM [users] WHERE [users].[age] NOT LIKE '%name%'"""
32 changes: 32 additions & 0 deletions tests/mysql/builder/test_query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ def test_with_(self):
)()
self.assertEqual(builder.to_sql(), sql)

def test_where_like(self):
builder = self.get_builder()
builder.where("age", "like", "%name%")

sql = getattr(
self, inspect.currentframe().f_code.co_name.replace("test_", "")
)()
self.assertEqual(builder.to_sql(), sql)

def test_where_not_like(self):
builder = self.get_builder()
builder.where("age", "not like", "%name%")

sql = getattr(
self, inspect.currentframe().f_code.co_name.replace("test_", "")
)()
self.assertEqual(builder.to_sql(), sql)

def test_max(self):
builder = self.get_builder()
builder.max("age")
Expand Down Expand Up @@ -653,3 +671,17 @@ def or_where(self):
return (
"SELECT * FROM `users` WHERE `users`.`age` = '20' OR `users`.`age` < '20'"
)

def where_like(self):
"""
builder = self.get_builder()
builder.where("age", "like", "%name%")
"""
return "SELECT * FROM `users` WHERE `users`.`age` LIKE '%name%'"

def where_not_like(self):
"""
builder = self.get_builder()
builder.where("age", "not like", "%name%")
"""
return "SELECT * FROM `users` WHERE `users`.`age` NOT LIKE '%name%'"
14 changes: 14 additions & 0 deletions tests/mysql/grammar/test_mysql_select_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,17 @@ def can_compile_first_or_fail(self):
builder.where("is_admin", "=", True).first_or_fail()
"""
return """SELECT * FROM `users` WHERE `users`.`is_admin` = '1' LIMIT 1"""

def where_not_like(self):
"""
builder = self.get_builder()
builder.where("age", "not like", "%name%").to_sql()
"""
return "SELECT * FROM `users` WHERE `users`.`age` NOT LIKE '%name%'"

def where_like(self):
"""
builder = self.get_builder()
builder.where("age", "not like", "%name%").to_sql()
"""
return "SELECT * FROM `users` WHERE `users`.`age` LIKE '%name%'"
32 changes: 32 additions & 0 deletions tests/postgres/builder/test_postgres_query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ def test_sum(self):
)()
self.assertEqual(builder.to_sql(), sql)

def test_where_like(self):
builder = self.get_builder()
builder.where("age", "like", "%name%")

sql = getattr(
self, inspect.currentframe().f_code.co_name.replace("test_", "")
)()
self.assertEqual(builder.to_sql(), sql)

def test_where_not_like(self):
builder = self.get_builder()
builder.where("age", "not like", "%name%")

sql = getattr(
self, inspect.currentframe().f_code.co_name.replace("test_", "")
)()
self.assertEqual(builder.to_sql(), sql)

def test_max(self):
builder = self.get_builder()
builder.max("age")
Expand Down Expand Up @@ -635,3 +653,17 @@ def or_where(self):
builder.where('age', '20').or_where('age','<', 20)
"""
return """SELECT * FROM "users" WHERE "users"."age" = '20' OR "users"."age" < '20'"""

def where_like(self):
"""
builder = self.get_builder()
builder.where("age", "like", "%name%")
"""
return """SELECT * FROM "users" WHERE "users"."age" LIKE '%name%'"""

def where_not_like(self):
"""
builder = self.get_builder()
builder.where("age", "not like", "%name%")
"""
return """SELECT * FROM "users" WHERE "users"."age" NOT LIKE '%name%'"""
14 changes: 14 additions & 0 deletions tests/postgres/grammar/test_select_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,17 @@ def can_compile_first_or_fail(self):
builder.where("is_admin", "=", True).first_or_fail()
"""
return """SELECT * FROM "users" WHERE "users"."is_admin" = '1' LIMIT 1"""

def where_not_like(self):
"""
builder = self.get_builder()
builder.where("age", "not like", "%name%").to_sql()
"""
return """SELECT * FROM "users" WHERE "users"."age" NOT LIKE '%name%'"""

def where_like(self):
"""
builder = self.get_builder()
builder.where("age", "not like", "%name%").to_sql()
"""
return """SELECT * FROM "users" WHERE "users"."age" LIKE '%name%'"""
32 changes: 32 additions & 0 deletions tests/sqlite/builder/test_sqlite_query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ def test_sum(self):
)()
self.assertEqual(builder.to_sql(), sql)

def test_where_like(self):
builder = self.get_builder()
builder.where("age", "like", "%name%")

sql = getattr(
self, inspect.currentframe().f_code.co_name.replace("test_", "")
)()
self.assertEqual(builder.to_sql(), sql)

def test_where_not_like(self):
builder = self.get_builder()
builder.where("age", "not like", "%name%")

sql = getattr(
self, inspect.currentframe().f_code.co_name.replace("test_", "")
)()
self.assertEqual(builder.to_sql(), sql)

def test_max(self):
builder = self.get_builder()
builder.max("age")
Expand Down Expand Up @@ -636,3 +654,17 @@ def or_where(self):
builder.where('age', '20').or_where('age','<', 20)
"""
return """SELECT * FROM "users" WHERE "users"."age" = '20' OR "users"."age" < '20'"""

def where_like(self):
"""
builder = self.get_builder()
builder.where("age", "like", "%name%")
"""
return """SELECT * FROM "users" WHERE "users"."age" LIKE '%name%'"""

def where_not_like(self):
"""
builder = self.get_builder()
builder.where("age", "like", "%name%")
"""
return """SELECT * FROM "users" WHERE "users"."age" NOT LIKE '%name%'"""
14 changes: 14 additions & 0 deletions tests/sqlite/grammar/test_sqlite_select_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,17 @@ def can_compile_first_or_fail(self):
builder.where("is_admin", "=", True).first_or_fail()
"""
return """SELECT * FROM "users" WHERE "users"."is_admin" = '1' LIMIT 1"""

def where_not_like(self):
"""
builder = self.get_builder()
builder.where("age", "not like", "%name%").to_sql()
"""
return """SELECT * FROM "users" WHERE "users"."age" NOT LIKE '%name%'"""

def where_like(self):
"""
builder = self.get_builder()
builder.where("age", "not like", "%name%").to_sql()
"""
return """SELECT * FROM "users" WHERE "users"."age" LIKE '%name%'"""

0 comments on commit ef4c5c8

Please sign in to comment.