Skip to content

Commit 07ffe3b

Browse files
committed
Delete with_cursor decorator
reason being it's currently impossible to return a correct type with mypy python/typing#193
1 parent 5571e25 commit 07ffe3b

File tree

2 files changed

+12
-23
lines changed

2 files changed

+12
-23
lines changed

examples/basic.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ def __init__(self) -> None:
1616
# we can use any connection string here
1717
super().__init__("sqlite://:memory:", path.join(path.dirname(__file__), "migrations"))
1818

19-
@BaseDb.with_cursor
2019
def select_all_basic(self, cursor: Cursor) -> List[Basic]:
21-
cursor.execute("SELECT * FROM basic")
22-
return [Basic(*x) for x in cursor.fetchall()]
20+
with self.get_cursor() as cursor:
21+
cursor.execute("SELECT * FROM basic")
22+
return [Basic(*x) for x in cursor.fetchall()]
2323

2424
def select_basic_by_id(self, basic_id: int) -> Optional[Basic]:
2525
with self.get_cursor() as cursor:
@@ -37,13 +37,14 @@ def insert_basic(self, basic: Basic) -> None:
3737
basic.name,
3838
))
3939

40-
@BaseDb.with_cursor
4140
def update_basic(self, basic: Basic, cursor: Cursor) -> None:
42-
cursor.execute("UPDATE basic SET name = ? WHERE id = ?", (
43-
basic.name,
44-
basic.id,
45-
))
41+
with self.get_cursor() as cursor:
42+
cursor.execute("UPDATE basic SET name = ? WHERE id = ?", (
43+
basic.name,
44+
basic.id,
45+
))
4646

47-
@BaseDb.with_cursor
4847
def delete_basic_by_id(self, basic_id: int, cursor: Connection) -> None:
49-
cursor.execute("DELETE FROM basic WHERE id = ?", (basic_id,))
48+
with self.get_cursor() as cursor:
49+
cursor.execute("DELETE FROM basic WHERE id = ?", (basic_id,))
50+

msql/database.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,6 @@ def migrate(self) -> None:
4040
def connection(self) -> Connection:
4141
return connection(self.conn_str)
4242

43-
@staticmethod
44-
def with_cursor(f: Callable) -> Callable:
45-
46-
@wraps(f)
47-
def wrapper(*args: Tuple) -> None:
48-
db = cast(Database, args[0])
49-
with db.connection() as conn:
50-
f(cursor=conn.cursor())
51-
if db.auto_commit:
52-
conn.commit()
53-
54-
return wrapper
55-
5643
def get_cursor(self) -> _ContextHelper:
5744
return _ContextHelper(self)
45+

0 commit comments

Comments
 (0)