From 771575717c5cc323338b4f0e37ca33f40c9053da Mon Sep 17 00:00:00 2001 From: Kieren Eaton <499977+circulon@users.noreply.github.com> Date: Fri, 27 Mar 2026 06:54:48 +0800 Subject: [PATCH 1/2] fixed QueryBuilder connection caching new_connection() now just creates a connection and returns it get_connection() handles the internal connection var --- src/masoniteorm/query/QueryBuilder.py | 42 ++++++++++++++------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/masoniteorm/query/QueryBuilder.py b/src/masoniteorm/query/QueryBuilder.py index fb1a2adc..27564290 100644 --- a/src/masoniteorm/query/QueryBuilder.py +++ b/src/masoniteorm/query/QueryBuilder.py @@ -257,7 +257,7 @@ def begin(self): Returns: self """ - return self.new_connection().begin() + return self.get_connection().begin() def begin_transaction(self, *args, **kwargs): return self.begin(*args, **kwargs) @@ -462,7 +462,7 @@ def add_select(self, alias, callable): def statement(self, query, bindings=None): if bindings is None: bindings = [] - result = self.new_connection().query(query, bindings) + result = self.get_connection().query(query, bindings) return self.prepare_result(result) def select_raw(self, query): @@ -504,7 +504,7 @@ def bulk_create( if model: model = model.hydrate(self._creates) if not self.dry: - connection = self.new_connection() + connection = self.get_connection() query_result = connection.query( self.to_qmark(), self._bindings, results=1 ) @@ -561,7 +561,7 @@ def create( self._creates.update(model.get_dirty_attributes()) if not self.dry: - connection = self.new_connection() + connection = self.get_connection() query_result = connection.query( self.to_qmark(), self._bindings, results=1 @@ -616,7 +616,7 @@ def delete(self, column=None, value=None, query=False): self.where(model.get_primary_key(), model.get_primary_key_value()) self.observe_events(model, "deleting") - connection = self.new_connection() + connection = self.get_connection() connection.query(self.to_qmark(), self._bindings) @@ -973,7 +973,7 @@ def or_where_null(self, column): return self def chunk(self, chunk_amount): - chunk_connection = self.new_connection() + chunk_connection = self.get_connection() for result in chunk_connection.select_many( self.to_sql(), (), chunk_amount ): @@ -1574,7 +1574,7 @@ def update( return self additional.update(updates) - connection = self.new_connection() + connection = self.get_connection() connection.query(self.to_qmark(), self._bindings) if model: @@ -1640,7 +1640,7 @@ def increment(self, column, value=1, dry=False): if dry or self.dry: return self - results = self.new_connection().query(self.to_qmark(), self._bindings) + results = self.get_connection().query(self.to_qmark(), self._bindings) processed_results = self.get_processor().get_column_value( self, column, results, id_key, id_value ) @@ -1684,7 +1684,7 @@ def decrement(self, column, value=1, dry=False): if dry or self.dry: return self - result = self.new_connection().query(self.to_qmark(), self._bindings) + result = self.get_connection().query(self.to_qmark(), self._bindings) processed_results = self.get_processor().get_column_value( self, column, result, id_key, id_value ) @@ -1725,7 +1725,7 @@ def count(self, column=None, dry=False): return self if not column: - result = self.new_connection().query( + result = self.get_connection().query( self.to_qmark(), self._bindings, results=1 ) @@ -1845,7 +1845,7 @@ def first(self, fields=None, query=False): if query: return self - result = self.new_connection().query( + result = self.get_connection().query( self.to_qmark(), self._bindings, results=1 ) @@ -1912,7 +1912,7 @@ def last(self, column=None, query=False): if query: return self - result = self.new_connection().query( + result = self.get_connection().query( self.to_qmark(), self._bindings, results=1, @@ -2132,7 +2132,7 @@ def all(self, selects=[], query=False): return self result = ( - self.new_connection().query(self.to_qmark(), self._bindings) or [] + self.get_connection().query(self.to_qmark(), self._bindings) or [] ) return self.prepare_result(result, collection=True) @@ -2144,24 +2144,26 @@ def get(self, selects=[]): self """ self.select(*selects) - result = self.new_connection().query(self.to_qmark(), self._bindings) + result = self.get_connection().query(self.to_qmark(), self._bindings) return self.prepare_result(result, collection=True) def new_connection(self): - if self._connection: - return self._connection - - self._connection = ( + """Create a new connection""" + return ( self.connection_class( **self.get_connection_information(), name=self.connection ) .set_schema(self._schema) .make_connection() ) - return self._connection def get_connection(self): + """Get the current connection""" + if self._connection: + return self._connection + + self._connection = self.new_connection() return self._connection def without_eager(self): @@ -2386,7 +2388,7 @@ def truncate(self, foreign_keys=False, dry=False): if dry or self.dry: return sql - return self.new_connection().query(sql, ()) + return self.get_connection().query(sql, ()) def exists(self): """Determine if rows exist for the current query. From 22012d1d30cf44dcf13a0efc987659f4a403dab7 Mon Sep 17 00:00:00 2001 From: Kieren Eaton <499977+circulon@users.noreply.github.com> Date: Fri, 27 Mar 2026 07:48:54 +0800 Subject: [PATCH 2/2] added tests --- tests/query/test_querybuilder.py | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tests/query/test_querybuilder.py diff --git a/tests/query/test_querybuilder.py b/tests/query/test_querybuilder.py new file mode 100644 index 00000000..cba9ca70 --- /dev/null +++ b/tests/query/test_querybuilder.py @@ -0,0 +1,35 @@ +import unittest + +from src.masoniteorm.query import QueryBuilder +from tests.integrations.config.database import DATABASES +from tests.utils import MockSQLiteConnection + + +class TestQueryBuilder(unittest.TestCase): + maxDiff = None + + def get_builder(self): + return QueryBuilder( + connection="dev", + connection_class=MockSQLiteConnection, + connection_details=DATABASES, + ) + + def test_returned_connection_is_same(self): + builder = self.get_builder() + first_connection = builder.get_connection() + self.assertIsNotNone(first_connection) + second_connection = builder.get_connection() + self.assertIsNotNone(second_connection) + self.assertEqual(first_connection, second_connection) + + def test_new_connection_does_not_change_existing_connection(self): + builder = self.get_builder() + first_connection = builder.get_connection() + self.assertIsNotNone(first_connection) + new_connection = builder.new_connection() + self.assertIsNotNone(new_connection) + second_connection = builder.get_connection() + self.assertIsNotNone(second_connection) + self.assertEqual(first_connection, second_connection) + self.assertNotEqual(new_connection, first_connection)