Skip to content

Commit

Permalink
Delete with_cursor decorator
Browse files Browse the repository at this point in the history
reason being it's currently impossible to return a correct
type with mypy python/typing#193
  • Loading branch information
BonusPlay committed Oct 20, 2020
1 parent 55cc51c commit 2a3e953
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
21 changes: 11 additions & 10 deletions examples/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ def __init__(self) -> None:
# we can use any connection string here
super().__init__("sqlite://:memory:", path.join(path.dirname(__file__), "migrations"))

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

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

@BaseDb.with_cursor
def update_basic(self, basic: Basic, cursor: Cursor) -> None:
cursor.execute("UPDATE basic SET name = ? WHERE id = ?", (
basic.name,
basic.id,
))
with self.get_cursor() as cursor:
cursor.execute("UPDATE basic SET name = ? WHERE id = ?", (
basic.name,
basic.id,
))

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

14 changes: 1 addition & 13 deletions msql/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,6 @@ def migrate(self) -> None:
def connection(self) -> Connection:
return connection(self.conn_str)

@staticmethod
def with_cursor(f: Callable) -> Callable:

@wraps(f)
def wrapper(*args: Tuple) -> None:
db = cast(Database, args[0])
with db.connection() as conn:
f(cursor=conn.cursor())
if db.auto_commit:
conn.commit()

return wrapper

def get_cursor(self) -> _ContextHelper:
return _ContextHelper(self)

0 comments on commit 2a3e953

Please sign in to comment.