-
Notifications
You must be signed in to change notification settings - Fork 2
/
compy_db.py
67 lines (59 loc) · 1.94 KB
/
compy_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import flask
import sqlite3
import logging
import glob
class CompyDB:
def __init__(self, app):
self.app_ = app
self.db_ = None
self.app_.teardown_appcontext(self.close_db)
self.last_index_ = -1
@property
def db(self):
if not self.is_open():
if "db" not in flask.g:
logging.debug("Connecting to database:" + flask.current_app.config["DATABASE"])
flask.g.db = sqlite3.connect(
flask.current_app.config["DATABASE"],
detect_types=sqlite3.PARSE_DECLTYPES,
)
flask.g.db.row_factory = sqlite3.Row
self.db_ = flask.g.db
return self.db_
@property
def cursor(self):
return self.db.cursor()
@property
def last_index(self):
return self.last_index_
def execute(self, cmd, args = ()):
if type(args) is not tuple:
args = (args, )
logging.debug("Execute: '" + cmd + "' args: " + str(args))
cursor = self.cursor
data = cursor.execute(cmd, args).fetchall()
self.last_index_ = cursor.lastrowid
self.db.commit()
if len(data) == 0:
return None
else:
return data
def is_open(self):
try:
self.db_.cursor()
return True
except Exception as ex:
return False
def init_db(self):
schemas = glob.glob("schemas/*.sql")
with self.app_.app_context():
for schema in schemas:
logging.debug("Initializing database entry from: " + schema)
with flask.current_app.open_resource(schema) as f:
self.db.executescript(f.read().decode("utf-8"))
logging.info("Database initialized")
def close_db(self, e=None):
if self.db_ is not None:
logging.debug("Closing database")
self.db_.close()
self.db_ = None