-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor.py
106 lines (87 loc) · 2.83 KB
/
cursor.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
import sys
import time
import traceback
import orm
normal = "\033[0m"
cyan = "\033[36m"
query_count = [0]
class Cursor(object):
def __init__(self, database, auto_commit=1):
self.auto_commit = auto_commit
self.database = database
self.conn = database.get_connection()
self.cursor = self.conn.cursor()
def execute(self, q, vars=[], log_query=1):
query_count[0] += 1
q = self.database.db_connector.translate(q)
vars = map(self.database.db_connector.translate_data, vars)
start = time.time()
try:
if vars:
self.cursor.execute(q, vars)
else:
self.cursor.execute(q)
if self.auto_commit:
self.conn.commit()
end = time.time()
if getattr(self.cursor, 'query', None):
if orm.verbose_level() > 0:
print cyan, '%.5f' % (end-start), normal, '\t', self.cursor.query
else:
if orm.verbose_level() > 0:
print cyan, '%.5f' % (end-start), normal, '\t', q
except Exception, e:
self.conn.rollback()
print >>sys.stderr, 'ERROR: ', q, vars
traceback.print_exc()
raise e
def d_fetchall(self, data):
colnames = [t[0] for t in self.cursor.description]
rows = [dict(zip(colnames, tup)) for tup in data]
return rows
def fetchall(self):
if self.cursor.rowcount:
res = self.cursor.fetchall()
else:
res = {}
if res:
if type(res[0]) == dict:
return res
else:
return self.d_fetchall(res)
else:
return {}
def fetchone(self):
colnames = [t[0] for t in self.cursor.description]
x = self.cursor.fetchone()
if x:
if type(x) == dict:
return x
else:
return dict(zip(colnames, x))
else:
return {}
def commit(self):
try:
start = time.time()
self.conn.commit()
end = time.time()
if orm.verbose_level() > 0:
print 'Commit: %.10f' % (end-start)
except:
import traceback
traceback.print_exc()
self.conn.rollback()
def rollback(self):
try:
start = time.time()
self.conn.rollback()
end = time.time()
if orm.verbose_level() > 0:
print 'Rollback: %.10f' % (end-start)
except:
import traceback
traceback.print_exc()
self.conn.commit()
def __del__(self):
self.database.return_connection(self.conn)