From 0fca86edc1750db8cd06d0ca4e2ca488095e757c Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Sat, 2 Jul 2022 11:13:18 -0400 Subject: [PATCH 1/5] changed update and delete to return the affected rows --- src/masoniteorm/connections/BaseConnection.py | 3 +++ src/masoniteorm/query/QueryBuilder.py | 13 ++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/masoniteorm/connections/BaseConnection.py b/src/masoniteorm/connections/BaseConnection.py index f21484f4..8c1f4eae 100644 --- a/src/masoniteorm/connections/BaseConnection.py +++ b/src/masoniteorm/connections/BaseConnection.py @@ -73,3 +73,6 @@ def select_many(self, query, bindings, amount): yield result result = self.format_cursor_results(self._cursor.fetchmany(amount)) + + def get_row_count(self): + return self._cursor.rowcount \ No newline at end of file diff --git a/src/masoniteorm/query/QueryBuilder.py b/src/masoniteorm/query/QueryBuilder.py index a27b7e84..92085c6a 100644 --- a/src/masoniteorm/query/QueryBuilder.py +++ b/src/masoniteorm/query/QueryBuilder.py @@ -579,12 +579,14 @@ 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") - result = self.new_connection().query(self.to_qmark(), self._bindings) + connection = self.new_connection() + + connection.query(self.to_qmark(), self._bindings) if model: self.observe_events(model, "deleted") - return result + return connection.get_row_count() def where(self, column, *args): """Specifies a where expression. @@ -1313,7 +1315,7 @@ def update(self, updates: dict, dry=False, force=False): # do not perform update query if no changes if len(updates.keys()) == 0: - return model if model else self + return 0 self._updates = (UpdateQueryExpression(updates),) self.set_action("update") @@ -1321,13 +1323,14 @@ def update(self, updates: dict, dry=False, force=False): return self additional.update(updates) + connection = self.new_connection() - self.new_connection().query(self.to_qmark(), self._bindings) + connection.query(self.to_qmark(), self._bindings) if model: model.fill(updates) self.observe_events(model, "updated") model.fill_original(updates) - return model + return connection.get_row_count() return additional def force_update(self, updates: dict, dry=False): From e397168947cbc38a708ac33c6557f6c2b66dee21 Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Sat, 2 Jul 2022 11:19:24 -0400 Subject: [PATCH 2/5] fixed response on update --- src/masoniteorm/query/QueryBuilder.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/masoniteorm/query/QueryBuilder.py b/src/masoniteorm/query/QueryBuilder.py index 92085c6a..19d6bb3a 100644 --- a/src/masoniteorm/query/QueryBuilder.py +++ b/src/masoniteorm/query/QueryBuilder.py @@ -1315,6 +1315,8 @@ def update(self, updates: dict, dry=False, force=False): # do not perform update query if no changes if len(updates.keys()) == 0: + if dry or self.dry: + return self return 0 self._updates = (UpdateQueryExpression(updates),) From 1d3e5bb84a3959a0b26ceb2cdf53e9fc0a757f12 Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Sat, 2 Jul 2022 11:20:09 -0400 Subject: [PATCH 3/5] linted --- src/masoniteorm/connections/BaseConnection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/masoniteorm/connections/BaseConnection.py b/src/masoniteorm/connections/BaseConnection.py index 8c1f4eae..3b080774 100644 --- a/src/masoniteorm/connections/BaseConnection.py +++ b/src/masoniteorm/connections/BaseConnection.py @@ -75,4 +75,4 @@ def select_many(self, query, bindings, amount): result = self.format_cursor_results(self._cursor.fetchmany(amount)) def get_row_count(self): - return self._cursor.rowcount \ No newline at end of file + return self._cursor.rowcount From ea4feaaa0161d7a9dd35848b098a0d82db89fa53 Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Sat, 2 Jul 2022 11:23:16 -0400 Subject: [PATCH 4/5] fixed save response --- src/masoniteorm/models/Model.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/masoniteorm/models/Model.py b/src/masoniteorm/models/Model.py index 2a7e5063..2b6edc13 100644 --- a/src/masoniteorm/models/Model.py +++ b/src/masoniteorm/models/Model.py @@ -814,7 +814,7 @@ def save(self, query=False): if not query: if self.is_loaded(): - result = builder.update(self.__dirty_attributes__) + builder.update(self.__dirty_attributes__) else: result = self.create( self.__dirty_attributes__, @@ -824,6 +824,8 @@ def save(self, query=False): self.observe_events(self, "saved") self.fill(result.__attributes__) self.__dirty_attributes__ = {} + if self.is_loaded(): + return self return result if self.is_loaded(): From 1ee3ef2d43e8f4cf1640b2279f7c1e38ea35941e Mon Sep 17 00:00:00 2001 From: Joe Mancuso Date: Sat, 2 Jul 2022 11:25:56 -0400 Subject: [PATCH 5/5] removed fill --- src/masoniteorm/models/Model.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/masoniteorm/models/Model.py b/src/masoniteorm/models/Model.py index 2b6edc13..583237ae 100644 --- a/src/masoniteorm/models/Model.py +++ b/src/masoniteorm/models/Model.py @@ -822,7 +822,6 @@ def save(self, query=False): id_key=self.get_primary_key(), ) self.observe_events(self, "saved") - self.fill(result.__attributes__) self.__dirty_attributes__ = {} if self.is_loaded(): return self