forked from orisi/orisi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_classes.py
90 lines (70 loc) · 2.2 KB
/
db_classes.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import sqlite3
class GeneralDb:
def __init__(self, filename):
self._filename = filename
self.connect()
def connect(self):
self.conn = sqlite3.connect(self._filename, detect_types=sqlite3.PARSE_COLNAMES)
self.conn.row_factory = sqlite3.Row
def commit(self):
self.conn.commit()
def execute(self, sql):
cursor = self.conn.cursor()
cursor.execute(sql)
self.conn.commit()
def get_cursor(self):
if not self.conn:
self.connect()
try:
return self.conn.cursor()
except:
self.connect()
return self.conn.cursor()
class TableDb(object):
"""
TableDb is class designed as wrapper for new tables and database requests.
It creates table when needed, so no need to worry about it
"""
table_name = "TableDB"
exist_sql = "select name from sqlite_master where type='table' and name='{0}'"
def __init__(self, db):
self.db = db
if not self.table_exists():
self.create_table()
def table_exists(self):
cursor = self.db.get_cursor()
sql = self.exist_sql.format(self.table_name)
cursor = cursor.execute(sql)
results = cursor.fetchall()
return len(results) > 0
def create_table(self):
cursor = self.db.get_cursor()
sql = self.create_sql.format(self.table_name)
cursor.execute(sql)
self.db.commit()
def args_for_obj(self, obj):
raise NotImplementedError()
def args_for_obj_update(self, obj):
raise NotImplementedError()
def args_for_obj_delete(self, obj):
raise NotImplementedError()
def insert_with_sql(self, sql, args):
cursor = self.db.get_cursor()
cursor.execute(sql, args)
self.db.commit()
def execute_sql_properly(self, sql, args):
cursor = self.db.get_cursor()
cursor.execute(sql, args)
self.db.commit()
def save(self, obj):
sql = self.insert_sql.format(self.table_name)
args = self.args_for_obj(obj)
self.execute_sql_properly(sql, args)
def update(self, obj):
sql = self.update_sql.format(self.table_name)
args = self.args_for_obj_update(obj)
self.execute_sql_properly(sql, args)
def delete(self, obj):
sql = self.delete_sql.format(self.table_name)
args = self.args_for_obj_delete(obj)
self.execute_sql_properly(sql, args)