Add smart cursor stuff
This commit is contained in:
@@ -1,9 +1,29 @@
|
||||
from flask import g
|
||||
import sqlite3
|
||||
from sqlite3 import connect, Connection, Cursor
|
||||
|
||||
|
||||
def get_db():
|
||||
db = getattr(g, "_database", None)
|
||||
if db is None:
|
||||
db = sqlite3.connect("/data/tables.db")
|
||||
db = connect("/data/tables.db")
|
||||
return db
|
||||
|
||||
|
||||
class SmartCursor:
|
||||
connection: Connection
|
||||
curosr: Cursor
|
||||
autoClose: bool
|
||||
|
||||
def __init__(self, cursor: Cursor = None, connection: Connectoin = None):
|
||||
self.cursor = cursor
|
||||
self.connection = connection
|
||||
|
||||
def __enter__(self):
|
||||
if self.cursor != None:
|
||||
self.cursor = self.connection.cursor()
|
||||
self.autoClose = True
|
||||
return self.cursor
|
||||
|
||||
def __exit__(self, exc_type, exc_value, traceback):
|
||||
if self.autoClose:
|
||||
self.cursor.close()
|
||||
|
||||
Reference in New Issue
Block a user