-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
156 lines (125 loc) · 3.98 KB
/
model.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
from factory import DatabaseFactory
import MySQLdb
class Model:
def __init__(self, tablename, idname = 'id', connection = 'default'):
dbname = DatabaseFactory.get_property(connection, 'dbname')
self.tablename = dbname + '.' + tablename
self.idname = idname
self.db = DatabaseFactory.instance(connection)
def get_all(self, start = None, count = None):
sql = 'select * from %s' % self
if start is not None:
sql += ' limit %s' % start
if count is not None:
sql += ', %s' % count
return self.do_query(sql)
def get_by(self, key, value):
sql = 'select * from %s' % self
sql += ' where %s = %s' % (key, '%s')
return self.do_query(sql, str(value))
def get(self, id):
result = self.get_by(self.idname, id)
if result is None:
return None
return result[0]
def get_unique(self, unique, value):
result = self.get_by(unique, value)
if result is None:
return None
return result[0]
def insert(self, *fields):
sql = 'insert into %s values(' % self
temp = []
for field in fields:
temp.append('%s')
sql += ', '.join(temp) + ')'
return self.do_non_query(sql, *fields)
def delete_by(self, key, value):
sql = 'delete from %s where %s = %s' % (self, key, '%s')
return self.do_non_query(sql, value)
def delete(self, id):
return self.delete_by(self.idname, id) > 0
pass
def delete_all(self):
return self.do_non_query('delete from %s' % self)
def update_by(self, key, value, update_key, update_value):
sql = 'update %s set %s = %s where %s = %s' % (self, update_key, '%s', key, '%s')
return self.do_non_query(sql, update_value, value)
def update(self, id, key, value):
return self.update_by(self.idname, id, key, value) > 0
def exists_by(self, key, value):
sql = 'select count(*) from %s where %s = %s' % (self, key, '%s')
return self.do_scalar(sql, value) > 0
def exists(self, id):
return self.exists_by(self.idname, id)
def last_insert_id(self):
return self.db.insert_id()
def error(self):
pass
def version(self):
return self.do_scalar('select version()')
def begin(self):
self.db.autocommit = False
def commit(self):
self.db.commit()
self.db.autocommit = True
def now(self):
return self.do_scalar('select now()')
def today(self):
return self.do_scalar('select current_date')
def count(self, what = None, distinct = False):
count = '*'
if what is not None:
count = what
if distinct:
count = 'distinct %s' % count
sql = 'select count(%s) from %s' % (count, self)
return self.do_scalar(sql)
def max(self, expr):
return self.do_scalar('select max(%s) from %s' % (expr, self))
def min(self, expr):
return self.do_scalar('select min(%s) from %s' % (expr, self))
def sum(self, expr):
return self.do_scalar('select sum(%s) from %s' % (expr, self))
def avg(self, expr):
return self.do_scalar('select avg(%s) from %s' % (expr, self))
def distinct(self, *columns):
what = ', '.join(columns)
sql = 'select distinct %s from %s' % (what, self)
return self.do_query(sql)
def get_like(self, _dict, empty_gets_all = True):
if _dict is None or len(_dict) is 0:
return self.get_all() if empty_gets_all else None
sql = 'select * from %s where ' % self
criteria = []
params = []
for key in _dict:
criteria.append('%s like %s' % (key, '%s'))
params.append('%' + _dict[key] + '%')
sql += ' or '.join(criteria)
return self.do_query(sql, *params)
def do_query(self, sql, *params):
cursor = self.db.cursor()
cursor.execute(sql, params)
result = cursor.fetchall()
cursor.close()
if len(result) is 0:
return None
return result
def do_non_query(self, sql, *params):
cursor = self.db.cursor()
cursor.execute(sql, params)
rowcount = cursor.rowcount
cursor.close()
return rowcount
def do_scalar(self, sql, *params):
cursor = self.db.cursor()
cursor.execute(sql, params)
result = cursor.fetchone()
cursor.close()
if result is None or len(result) is 0:
return None
for key in result:
return result[key] #return just the first value
def __str__(self):
return self.tablename