Mercurial > public > src > rhodecode
annotate pylons_app/lib/timerproxy.py @ 504:298546182b41
more test suites on login, fixed strange detached instance bug found during in tests.
on __repr__ function
fixed timerproxy query formatting
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Sun, 05 Sep 2010 23:40:08 +0200 |
parents | 5ba66bb4ca95 |
children | a5a17000e45b |
rev | line source |
---|---|
49
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
1 from sqlalchemy.interfaces import ConnectionProxy |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
2 import time |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
3 import logging |
90
0c22a870bb79
logging proxy update
Marcin Kuzminski <marcin@python-works.com>
parents:
49
diff
changeset
|
4 log = logging.getLogger('timerproxy') |
153
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
90
diff
changeset
|
5 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38) |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
90
diff
changeset
|
6 |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
90
diff
changeset
|
7 def color_sql(sql): |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
90
diff
changeset
|
8 COLOR_SEQ = "\033[1;%dm" |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
90
diff
changeset
|
9 COLOR_SQL = YELLOW |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
90
diff
changeset
|
10 normal = '\x1b[0m' |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
90
diff
changeset
|
11 return COLOR_SEQ % COLOR_SQL + sql + normal |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
90
diff
changeset
|
12 |
172
83c7ee1b5f5c
improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents:
153
diff
changeset
|
13 def one_space_trim(s): |
83c7ee1b5f5c
improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents:
153
diff
changeset
|
14 if s.find(" ") == -1: |
83c7ee1b5f5c
improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents:
153
diff
changeset
|
15 return s |
83c7ee1b5f5c
improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents:
153
diff
changeset
|
16 else: |
83c7ee1b5f5c
improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents:
153
diff
changeset
|
17 s = s.replace(' ', ' ') |
83c7ee1b5f5c
improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents:
153
diff
changeset
|
18 return one_space_trim(s) |
83c7ee1b5f5c
improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents:
153
diff
changeset
|
19 |
153
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
90
diff
changeset
|
20 def format_sql(sql): |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
90
diff
changeset
|
21 sql = color_sql(sql) |
172
83c7ee1b5f5c
improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents:
153
diff
changeset
|
22 sql = sql.replace('\n', '') |
83c7ee1b5f5c
improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents:
153
diff
changeset
|
23 sql = one_space_trim(sql) |
83c7ee1b5f5c
improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents:
153
diff
changeset
|
24 sql = sql\ |
504
298546182b41
more test suites on login, fixed strange detached instance bug found during in tests.
Marcin Kuzminski <marcin@python-works.com>
parents:
236
diff
changeset
|
25 .replace(',',',\n\t')\ |
172
83c7ee1b5f5c
improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents:
153
diff
changeset
|
26 .replace('SELECT', '\n\tSELECT \n\t')\ |
236
5ba66bb4ca95
timerprox sqlformatting update for update and delete keywords
Marcin Kuzminski <marcin@python-works.com>
parents:
172
diff
changeset
|
27 .replace('UPDATE', '\n\tUPDATE \n\t')\ |
5ba66bb4ca95
timerprox sqlformatting update for update and delete keywords
Marcin Kuzminski <marcin@python-works.com>
parents:
172
diff
changeset
|
28 .replace('DELETE', '\n\tDELETE \n\t')\ |
172
83c7ee1b5f5c
improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents:
153
diff
changeset
|
29 .replace('FROM', '\n\tFROM')\ |
83c7ee1b5f5c
improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents:
153
diff
changeset
|
30 .replace('ORDER BY', '\n\tORDER BY')\ |
83c7ee1b5f5c
improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents:
153
diff
changeset
|
31 .replace('LIMIT', '\n\tLIMIT')\ |
83c7ee1b5f5c
improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents:
153
diff
changeset
|
32 .replace('WHERE', '\n\tWHERE')\ |
83c7ee1b5f5c
improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents:
153
diff
changeset
|
33 .replace('AND', '\n\tAND')\ |
83c7ee1b5f5c
improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents:
153
diff
changeset
|
34 .replace('LEFT', '\n\tLEFT')\ |
83c7ee1b5f5c
improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents:
153
diff
changeset
|
35 .replace('INNER', '\n\tINNER')\ |
83c7ee1b5f5c
improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents:
153
diff
changeset
|
36 .replace('INSERT', '\n\tINSERT')\ |
83c7ee1b5f5c
improved timerproxy with sqllogging, and new way of sqlformat queries
Marcin Kuzminski <marcin@python-works.com>
parents:
153
diff
changeset
|
37 .replace('DELETE', '\n\tDELETE') |
153
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
90
diff
changeset
|
38 return sql |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
90
diff
changeset
|
39 |
49
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
40 |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
41 class TimerProxy(ConnectionProxy): |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
42 def cursor_execute(self, execute, cursor, statement, parameters, context, executemany): |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
43 now = time.time() |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
44 try: |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
45 log.info(">>>>> STARTING QUERY >>>>>") |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
46 return execute(cursor, statement, parameters, context) |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
47 finally: |
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
48 total = time.time() - now |
90
0c22a870bb79
logging proxy update
Marcin Kuzminski <marcin@python-works.com>
parents:
49
diff
changeset
|
49 try: |
153
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
90
diff
changeset
|
50 log.info(format_sql("Query: %s" % statement % parameters)) |
90
0c22a870bb79
logging proxy update
Marcin Kuzminski <marcin@python-works.com>
parents:
49
diff
changeset
|
51 except TypeError: |
153
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
90
diff
changeset
|
52 log.info(format_sql("Query: %s %s" % (statement, parameters))) |
49
3ada2f409c1c
Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff
changeset
|
53 log.info("<<<<< TOTAL TIME: %f <<<<<" % total) |
153
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
90
diff
changeset
|
54 |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
90
diff
changeset
|
55 |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
90
diff
changeset
|
56 |
a5a3bcc5ee89
Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents:
90
diff
changeset
|
57 |