Skip to content

Commit

Permalink
Merge pull request #353 from MasoniteFramework/fix/speed
Browse files Browse the repository at this point in the history
Performance: Changes tuple to list to speed up qmark compiling 600%
  • Loading branch information
josephmancuso authored Jan 30, 2021
2 parents 35982c1 + 07d1e8e commit 62de8fb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
5 changes: 2 additions & 3 deletions src/masoniteorm/query/grammars/BaseGrammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(
self._connection_details = connection_details or {}
self._column = None

self._bindings = ()
self._bindings = []

self._sql = ""

Expand Down Expand Up @@ -273,7 +273,6 @@ def _compile_key_value_equals(self, qmark=False):
self._bindings += (value,)

sql = sql.rstrip(", ")

return sql

def process_aggregates(self):
Expand Down Expand Up @@ -587,7 +586,7 @@ def add_binding(self, binding):
Arguments:
binding {string} -- A value to bind.
"""
self._bindings += (binding,)
self._bindings.append(binding)

def column_exists(self, column):
"""Check if a column exists
Expand Down
8 changes: 4 additions & 4 deletions tests/mssql/grammar/test_mssql_qmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ def test_can_compile_select(self):

sql = "SELECT [users].[username] FROM [users] WHERE [users].[name] = '?'"
self.assertEqual(mark.to_qmark(), sql)
self.assertEqual(mark._bindings, ("Joe",))
self.assertEqual(mark._bindings, ["Joe"])

def test_can_compile_update(self):
mark = self.builder.update({"name": "Bob"}, dry=True).where("name", "Joe")

sql = "UPDATE [users] SET [users].[name] = '?' WHERE [users].[name] = '?'"
self.assertEqual(mark.to_qmark(), sql)
self.assertEqual(mark._bindings, ("Bob", "Joe"))
self.assertEqual(mark._bindings, ["Bob", "Joe"])

def test_can_compile_insert(self):
mark = self.builder.create({"name": "Bob"}, query=True)

sql = "INSERT INTO [users] ([users].[name]) VALUES ('?')"
self.assertEqual(mark.to_qmark(), sql)
self.assertEqual(mark._bindings, ("Bob",))
self.assertEqual(mark._bindings, ["Bob"])

def test_can_compile_where_in(self):
mark = self.builder.where_in("id", [1, 2, 3])

sql = "SELECT * FROM [users] WHERE [users].[id] IN ('?', '?', '?')"
self.assertEqual(mark.to_qmark(), sql)
self.assertEqual(mark._bindings, ("1", "2", "3"))
self.assertEqual(mark._bindings, ["1", "2", "3"])
10 changes: 5 additions & 5 deletions tests/mysql/grammar/test_mysql_qmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_can_compile_where_not_null(self):
self, inspect.currentframe().f_code.co_name.replace("test_", "")
)()
self.assertEqual(mark.to_qmark(), sql)
self.assertEqual(mark._bindings, bindings)
self.assertEqual(mark._bindings, [])

# def test_can_create_with_falsy_values(self):
# mark = self.builder.create(
Expand Down Expand Up @@ -80,14 +80,14 @@ def can_compile_select(self):
"""
return (
"SELECT `users`.`username` FROM `users` WHERE `users`.`name` = '?'",
("Joe",),
["Joe"],
)

def can_compile_delete(self):
"""
self.builder.where('name', 'Joe').delete()
"""
return "DELETE FROM `users` WHERE `users`.`name` = '?'", ("Joe",)
return "DELETE FROM `users` WHERE `users`.`name` = '?'", ["Joe"]

def can_compile_update(self):
"""
Expand All @@ -97,7 +97,7 @@ def can_compile_update(self):
"""
return (
"UPDATE `users` SET `users`.`name` = '?' WHERE `users`.`name` = '?'",
("Bob", "Joe"),
["Bob", "Joe"],
)

def can_compile_where_in(self):
Expand All @@ -106,7 +106,7 @@ def can_compile_where_in(self):
"""
return (
"SELECT * FROM `users` WHERE `users`.`id` IN ('?', '?', '?')",
("1", "2", "3"),
["1", "2", "3"],
)

def can_compile_where_not_null(self):
Expand Down

0 comments on commit 62de8fb

Please sign in to comment.