##// END OF EJS Templates
timerprox sqlformatting update for update and delete keywords
marcink -
r236:5ba66bb4 default
parent child Browse files
Show More
@@ -1,54 +1,56 b''
1 from sqlalchemy.interfaces import ConnectionProxy
1 from sqlalchemy.interfaces import ConnectionProxy
2 import time
2 import time
3 import logging
3 import logging
4 log = logging.getLogger('timerproxy')
4 log = logging.getLogger('timerproxy')
5 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38)
5 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38)
6
6
7 def color_sql(sql):
7 def color_sql(sql):
8 COLOR_SEQ = "\033[1;%dm"
8 COLOR_SEQ = "\033[1;%dm"
9 COLOR_SQL = YELLOW
9 COLOR_SQL = YELLOW
10 normal = '\x1b[0m'
10 normal = '\x1b[0m'
11 return COLOR_SEQ % COLOR_SQL + sql + normal
11 return COLOR_SEQ % COLOR_SQL + sql + normal
12
12
13 def one_space_trim(s):
13 def one_space_trim(s):
14 if s.find(" ") == -1:
14 if s.find(" ") == -1:
15 return s
15 return s
16 else:
16 else:
17 s = s.replace(' ', ' ')
17 s = s.replace(' ', ' ')
18 return one_space_trim(s)
18 return one_space_trim(s)
19
19
20 def format_sql(sql):
20 def format_sql(sql):
21 sql = color_sql(sql)
21 sql = color_sql(sql)
22 sql = sql.replace('\n', '')
22 sql = sql.replace('\n', '')
23 sql = one_space_trim(sql)
23 sql = one_space_trim(sql)
24 sql = sql\
24 sql = sql\
25 .replace('SELECT', '\n\tSELECT \n\t')\
25 .replace('SELECT', '\n\tSELECT \n\t')\
26 .replace('UPDATE', '\n\tUPDATE \n\t')\
27 .replace('DELETE', '\n\tDELETE \n\t')\
26 .replace('FROM', '\n\tFROM')\
28 .replace('FROM', '\n\tFROM')\
27 .replace('ORDER BY', '\n\tORDER BY')\
29 .replace('ORDER BY', '\n\tORDER BY')\
28 .replace('LIMIT', '\n\tLIMIT')\
30 .replace('LIMIT', '\n\tLIMIT')\
29 .replace('WHERE', '\n\tWHERE')\
31 .replace('WHERE', '\n\tWHERE')\
30 .replace('AND', '\n\tAND')\
32 .replace('AND', '\n\tAND')\
31 .replace('LEFT', '\n\tLEFT')\
33 .replace('LEFT', '\n\tLEFT')\
32 .replace('INNER', '\n\tINNER')\
34 .replace('INNER', '\n\tINNER')\
33 .replace('INSERT', '\n\tINSERT')\
35 .replace('INSERT', '\n\tINSERT')\
34 .replace('DELETE', '\n\tDELETE')
36 .replace('DELETE', '\n\tDELETE')
35 return sql
37 return sql
36
38
37
39
38 class TimerProxy(ConnectionProxy):
40 class TimerProxy(ConnectionProxy):
39 def cursor_execute(self, execute, cursor, statement, parameters, context, executemany):
41 def cursor_execute(self, execute, cursor, statement, parameters, context, executemany):
40 now = time.time()
42 now = time.time()
41 try:
43 try:
42 log.info(">>>>> STARTING QUERY >>>>>")
44 log.info(">>>>> STARTING QUERY >>>>>")
43 return execute(cursor, statement, parameters, context)
45 return execute(cursor, statement, parameters, context)
44 finally:
46 finally:
45 total = time.time() - now
47 total = time.time() - now
46 try:
48 try:
47 log.info(format_sql("Query: %s" % statement % parameters))
49 log.info(format_sql("Query: %s" % statement % parameters))
48 except TypeError:
50 except TypeError:
49 log.info(format_sql("Query: %s %s" % (statement, parameters)))
51 log.info(format_sql("Query: %s %s" % (statement, parameters)))
50 log.info("<<<<< TOTAL TIME: %f <<<<<" % total)
52 log.info("<<<<< TOTAL TIME: %f <<<<<" % total)
51
53
52
54
53
55
54
56
General Comments 0
You need to be logged in to leave comments. Login now