-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
87 lines (80 loc) · 3.19 KB
/
database.py
File metadata and controls
87 lines (80 loc) · 3.19 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Handle database connections / requests and initial database creation / schema tracking
# The schema set up like this might be mostly useless after initial db creation...
# might still be good to have for schema tracking??
import sqlite3
db_schema = {
'members': [
'id INTEGER PRIMARY KEY AUTOINCREMENT', #autoincrement this instead of rowid so that id linking is not broken if a rowid is reused after a delete or db change
'first_name VARCHAR(25)', #apparently sqlite doesn't use the length of a varchar, just treats column as text
'last_name VARCHAR(25)',
'email VARCHAR(50)',
'start_date INTEGER', #integer for date fields will use unix time
'expiration_date INTEGER',
'admin INTEGER DEFAULT 0', #has admin privileges
'enabled INTEGER DEFAULT 1'
],
'rfid_tokens': [
'id INTEGER PRIMARY KEY AUTOINCREMENT',
'member_id INTEGER',
'token VARCHAR(50)', #the token id stored on the card
'enabled INTEGER DEFAULT 1',
'FOREIGN KEY( member_id ) REFERENCES members( id )'
],
'items': [
'id INTEGER PRIMARY KEY AUTOINCREMENT',
'description TEXT',
'enabled INTEGER DEFAULT 0',
'disabled_reason TEXT'
],
'training': [
'id INTEGER PRIMARY KEY AUTOINCREMENT',
'description TEXT',
'valid_length INTEGER DEFAULT 0'
],
'required_training': [
'id INTEGER PRIMARY KEY AUTOINCREMENT',
'training_id INTEGER',
'item_id INTEGER',
'FOREIGN KEY( training_id ) REFERENCES training( id )',
'FOREIGN KEY( item_id ) REFERENCES items( id )'
],
'completed_training': [
'id INTEGER PRIMARY KEY AUTOINCREMENT',
'training_id INTEGER',
'member_id INTEGER',
'complete_date INTEGER',
'expiration_date INTEGER',
'FOREIGN KEY( training_id ) REFERENCES training( id )',
'FOREIGN KEY( member_id ) REFERENCES members( id )'
]
}
class Database:
# pass in schema of None to just connect and not initialize the database
def __init__( self, database, schema ):
self.con = None
self.database = database
self.schema = schema
self.connected = False
self.__connect()
if schema != None:
self.__initialize()
#after connecting to database must execute
#PRAGMA foreign_keys = ON;
#disabled by defaul for backwards compat with sqlite2.
def __connect( self ):
#TODO: probably need to add a try / catch here to confirm connection
self.con = sqlite3.connect( self.database )
self.con.execute( 'PRAGMA foreign_keys = ON;' )
self.con.commit()
self.connected = True
def __initialize( self ):
#initialize database creating non existant tables
for table in self.schema:
self.con.execute( 'CREATE TABLE IF NOT EXISTS %s ( %s )' % ( table, ', '.join( self.schema[ table ] ) ) )
self.con.commit()
def commit( self ):
self.con.commit()
def execute( self, sql ):
self.con.execute( sql )
def cursor( self ):
return self.con.cursor()