forked from Screenly/Anthias
-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.py
40 lines (30 loc) · 977 Bytes
/
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
import sqlite3
from contextlib import contextmanager
from datetime import time
import queries
def adapt_timeobj(timeobj):
return ((3600*timeobj.hour + 60*timeobj.minute + timeobj.second)*10**6
+ timeobj.microsecond)
def convert_timeobj(val):
val = int(val)
hour, val = divmod(val, 3600*10**6)
minute, val = divmod(val, 60*10**6)
second, val = divmod(val, 10**6)
microsecond = int(val)
return time(hour, minute, second, microsecond)
# Converts datetime.time to TEXT when inserting
sqlite3.register_adapter(time, adapt_timeobj)
# Converts TEXT to datetime.time when selecting
sqlite3.register_converter("time", convert_timeobj)
conn = lambda db: sqlite3.connect(db, detect_types=sqlite3.PARSE_DECLTYPES)
@contextmanager
def cursor(connection):
cur = connection.cursor()
yield cur
cur.close()
@contextmanager
def commit(connection):
cur = connection.cursor()
yield cur
connection.commit()
cur.close()