diff --git a/orm/db/__init__.py b/orm/db/__init__.py index 6a0a3f0..839f00b 100644 --- a/orm/db/__init__.py +++ b/orm/db/__init__.py @@ -217,10 +217,12 @@ def delete_bulk(self, instance: Model, filters: dict = {}): return affected_rows def delete_one(self, instance: Model, filters: dict = {}): - sql, params = instance._get_delete_where_stm(filters) - affected_rows = self._execute_sql( - sql, args=params, affected_rows=True, fetchall=True - ) + pk = None + for name, field in inspect.getmembers(instance): + if isinstance(field, PrimaryKeyColumn): + pk = name + sql, params = instance._get_delete_where_stm(pk=pk, args=filters) + affected_rows = self._execute_sql(sql, args=params, affected_rows=True) return affected_rows def delete_by_pk(self, instance: Model, pk): diff --git a/orm/model/model.py b/orm/model/model.py index a7ccc5b..31c8f30 100644 --- a/orm/model/model.py +++ b/orm/model/model.py @@ -264,7 +264,7 @@ def _get_select_where_stm(cls, fields: list = [], args: dict = {}): return sql, fields, params @classmethod - def _get_delete_where_stm(cls, args: dict = {}): + def _get_delete_where_stm(cls, pk: str = "id", args: dict = {}): params = [] filters = [] for key, value in args.items(): @@ -276,8 +276,7 @@ def _get_delete_where_stm(cls, args: dict = {}): ) else: sql = Statements.DELETE_ONE_WHERE_COMMAND.format( - table_name=cls._get_name(), - filters=" AND ".join(filters), + table_name=cls._get_name(), filters=" AND ".join(filters), pk=pk ) return sql, params diff --git a/orm/model/statements.py b/orm/model/statements.py index b937b2a..6df9e5b 100644 --- a/orm/model/statements.py +++ b/orm/model/statements.py @@ -1,7 +1,11 @@ class Statements: # delete DELETE_BY_PK = "DELETE FROM {table_name} WHERE {pk_name} = {pk};" - DELETE_ONE_WHERE_COMMAND = "DELETE FROM {table_name} WHERE {filters};" + DELETE_ONE_WHERE_COMMAND = """ + DELETE FROM {table_name} WHERE {pk} = ( + SELECT {pk} FROM {table_name} WHERE {filters} LIMIT 1 + ); + """ DELETE_BULK_WHERE_COMMAND = "DELETE FROM {table_name} WHERE {filters};" DELETE_ALL_COMMAND = "DELETE FROM {table_name};" # select diff --git a/orm/tests/test_delete.py b/orm/tests/test_delete.py index 863991e..b18e1ea 100644 --- a/orm/tests/test_delete.py +++ b/orm/tests/test_delete.py @@ -48,13 +48,15 @@ class User(Model): User(name="Crispen", username="heyy"), ] ) - affected_rows_1 = db.delete_one(User, {"name": "Crispen"}) - affected_rows_2 = db.delete_one(User, {"name": "Crispen", "id": 9}) - affected_rows_3 = db.delete_one(User, {"name": "Crispen", "id": 2}) - - assert affected_rows_1 == 3 - assert affected_rows_3 == 0 - assert affected_rows_2 == 0 + db.delete_one(User, {"name": "Crispen"}) + rows_1 = db.find_many(User, {"name": "Crispen"}) + db.delete_one(User, {"name": "Crispen", "id": 9}) + rows_2 = db.find_many(User, {"name": "Crispen"}) + db.delete_one(User, {"name": "Crispen", "id": 2}) + rows_3 = db.find_many(User, {"name": "Crispen"}) + assert len(rows_1) == 2 + assert len(rows_2) == 2 + assert len(rows_3) == 1 conn.close() def test_delete_bulk_fn(self): @@ -81,11 +83,20 @@ class User(Model): User(name="Crispen", username="heyy"), ] ) - affected_rows_1 = db.delete_one(User, {"name": "Crispen"}) - affected_rows_2 = db.delete_one(User, {"name": "Crispen", "id": 9}) - affected_rows_3 = db.delete_one(User, {"name": "Crispen", "id": 2}) - - assert affected_rows_1 == 3 - assert affected_rows_3 == 0 - assert affected_rows_2 == 0 + db.delete_bulk(User, {"name": "Crispen"}) + rows_1 = db.find_many(User, {"name": "Crispen"}) + db.commit_bulk( + [ + User(name="Crispen", username="heyy"), + User(name="Crispen", username="heyy"), + User(name="Crispen", username="heyy"), + ] + ) + db.delete_bulk(User, {"name": "Crispen", "id": 99}) + rows_2 = db.find_many(User, {"name": "Crispen"}) + db.delete_bulk(User, {"name": "Crispen", "id": 5}) + rows_3 = db.find_many(User, {"name": "Crispen"}) + assert len(rows_1) == 0 + assert len(rows_2) == 3 + assert len(rows_3) == 2 conn.close() diff --git a/playground.py b/playground.py index 127971c..3043cbd 100644 --- a/playground.py +++ b/playground.py @@ -63,7 +63,8 @@ def to_dict(self): # Post(userId=1, title="What are you thinking"), # ) -now = db.delete_bulk(User, {"name": "Crispen"}) +now = db.delete_one(User, {"name": "Crispen"}) +print(now) # print(f"now: {now}")