-
Notifications
You must be signed in to change notification settings - Fork 0
/
mbdb.py
179 lines (137 loc) · 5.29 KB
/
mbdb.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import inspect
import sqlite3
#from data import customconfig #working
#import customconfig
class mbdb():
# @staticmethod
# def get_db_path(pWhichDb):
# #print(pTableClass._db_)
# return customconfig.db[pWhichDb]
@staticmethod
def executeCursor(db_path, psql, pparams):
try:
db = sqlite3.connect(db_path)
cursor = db.cursor()
cursor.execute(psql, pparams)
return cursor
except Exception as ex:
print(ex)
return None
@staticmethod
def NonQuery(db_path, psql, pparams):
try:
db = sqlite3.connect(db_path)
cursor = db.cursor()
cursor.execute(psql, pparams)
intr = cursor.rowcount
cursor.close()
if intr>0:
db.commit()
return intr
except Exception as ex:
print(ex)
return 0
@staticmethod
def Scalar(db_path, psql, pparams):
try:
db = sqlite3.connect(db_path)
cursor = db.cursor()
cursor.execute(psql, pparams)
rec = cursor.fetchone()
cursor.close()
return rec
except Exception as ex:
print(ex)
return None
@staticmethod
def insertTable(pcls:any):
'''
inserts a record into sqlite table.
'''
fields = []
vals = []
# getmembers() returns all the members of an object
for it in inspect.getmembers(pcls):
# to remove private and protected functions
if not it[0].startswith('_'):
# To remove other methods that doesnot start with a underscore
if not inspect.ismethod(it[1]):
if(it[0]=="id"):
id_record=it[1]
continue
fields.append(it[0]) # + "=?")
vals.append(it[1])
#print(it[0], it[1])
#print(pcls.__class__.__name__)
table_name = type(pcls).__name__
q = "INSERT OR IGNORE INTO " + table_name + " (" + ", ".join(fields) + ") VALUES ("
q += "?, " * (len(fields)-1)
q += "? )"
return mbdb.NonQuery(pcls._db_, q, vals)
@staticmethod
def updateTable(pcls:any):
fields = []
vals = []
# getmembers() returns all the members of an object
for it in inspect.getmembers(pcls):
# to remove private and protected functions
if not it[0].startswith('_'):
# To remove other methods that doesnot start with a underscore
if not inspect.ismethod(it[1]):
if(it[0]=="id"):
id_record=it[1]
continue
fields.append(it[0] + "=?")
vals.append(it[1])
#print(it[0], it[1])
#print(pcls.__class__.__name__)
table_name = type(pcls).__name__
q="UPDATE " + table_name + " SET " + ", ".join(fields) + " WHERE id=?"
vals.append(id_record)
return mbdb.NonQuery(pcls._db_, q, vals)
@staticmethod
def deleteRecord(pcls:any):
# getmembers() returns all the members of an object
for it in inspect.getmembers(pcls):
# to remove private and protected functions
if not it[0].startswith('_'):
# To remove other methods that doesnot start with a underscore
if not inspect.ismethod(it[1]):
if(it[0]=="id"):
id_record=it[1]
break
#print(pcls.__class__.__name__)
table_name = type(pcls).__name__
q="DELETE FROM " + table_name + " WHERE id=?"
return mbdb.NonQuery(pcls._db_, q, (id_record, ))
@staticmethod
def getOne(pcls:any, pcriteria, pvals=()):
table_name = type(pcls).__name__
q="SELECT * FROM " + table_name + " WHERE " + pcriteria + " LIMIT 1"
values = pvals
#if(pid > 0):
#q="SELECT * FROM tblurl WHERE id = ?"
#values = (pid,)
mycursor = mbdb.executeCursor(pcls._db_, q, values)
#return mycursor.fetchone()
rec = mycursor.fetchone()
if rec==None:
return False
map = {}
for d in mycursor.description:
map[d[0]] = len(map)
#print(rec[map["id"]])
# getmembers() returns all the members of an object
for it in inspect.getmembers(pcls):
# to remove private and protected functions
if not it[0].startswith('_'):
# To remove other methods that doesnot start with a underscore
if not inspect.ismethod(it[1]):
pcls.__dict__[it[0]]=rec[map[it[0]]]
#print(it)
return True
@staticmethod
def getAll(db_path, query, pvals=()):
values = pvals
mycursor = mbdb.executeCursor(db_path, query, values)
return mycursor.fetchall()