1 ##############################################################################
3 # Copyright (c) 2004-2008 Tiny SPRL (http://tiny.be) All Rights Reserved.
7 # WARNING: This program as such is intended to be used by professional
8 # programmers who take the whole responsability of assessing all potential
9 # consequences resulting from its eventual inadequacies and bugs
10 # End users who are looking for a ready-to-use solution with commercial
11 # garantees and support are strongly adviced to contract a Free Software
14 # This program is Free Software; you can redistribute it and/or
15 # modify it under the terms of the GNU General Public License
16 # as published by the Free Software Foundation; either version 2
17 # of the License, or (at your option) any later version.
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 ###############################################################################
36 # from tools import decimal
41 from mx import DateTime as mdt
42 re_from = re.compile('.* from "?([a-zA-Z_0-9]+)"? .*$');
43 re_into = re.compile('.* into "?([a-zA-Z_0-9]+)"? .*$');
53 def __init__(self, db, con, dbname):
55 self.obj = db.cursor()
59 def execute(self, sql, params=None):
63 if isinstance(s, unicode):
64 return s.encode('utf-8')
66 p=map(base_string, params)
67 if isinstance(sql, unicode):
68 sql = sql.encode('utf-8')
72 res = self.obj.execute(sql, p)
74 res = self.obj.execute(sql)
76 print "SQL LOG query:", sql
77 print "SQL LOG params:", repr(p)
79 res_from = re_from.match(sql.lower())
81 self.sql_from_log.setdefault(res_from.group(1), [0, 0])
82 self.sql_from_log[res_from.group(1)][0] += 1
83 self.sql_from_log[res_from.group(1)][1] += mdt.now() - now
84 res_into = re_into.match(sql.lower())
86 self.sql_into_log.setdefault(res_into.group(1), [0, 0])
87 self.sql_into_log[res_into.group(1)][0] += 1
88 self.sql_into_log[res_into.group(1)][1] += mdt.now() - now
91 def print_log(self, type='from'):
92 print "SQL LOG %s:" % (type,)
94 logs = self.sql_from_log.items()
96 logs = self.sql_into_log.items()
97 logs.sort(lambda x, y: cmp(x[1][1], y[1][1]))
100 print "table:", r[0], ":", str(r[1][1]), "/", r[1][0]
102 print "SUM:%s/%d"% (sum, self.count)
106 self.print_log('from')
107 self.print_log('into')
110 # This force the cursor to be freed, and thus, available again. It is
111 # important because otherwise we can overload the server very easily
112 # because of a cursor shortage (because cursors are not garbage
113 # collected as fast as they should). The problem is probably due in
114 # part because browse records keep a reference to the cursor.
117 def __getattr__(self, name):
118 return getattr(self.obj, name)
121 def __init__(self, truedb, dbname):
126 return fake_cursor(self.truedb, {}, self.dbname)
128 def decimalize(symb):
129 if symb is None: return None
130 if isinstance(symb, float):
131 return decimal.Decimal('%f' % symb)
132 return decimal.Decimal(symb)
134 def db_connect(db_name, serialize=0):
135 host = tools.config['db_host'] and "host=%s" % tools.config['db_host'] or ''
136 port = tools.config['db_port'] and "port=%s" % tools.config['db_port'] or ''
137 name = "dbname=%s" % db_name
138 user = tools.config['db_user'] and "user=%s" % tools.config['db_user'] or ''
139 password = tools.config['db_password'] and "password=%s" % tools.config['db_password'] or ''
140 maxconn = int(tools.config['db_maxconn']) or 64
141 tdb = psycopg.connect('%s %s %s %s %s' % (host, port, name, user, password),
142 serialize=serialize, maxconn=maxconn)
143 fdb = fakedb(tdb, db_name)
147 #define DATEOID 1082, define TIMESTAMPOID 1114 see pgtypes.h
148 psycopg.register_type(psycopg.new_type((1082,), "date", lambda x:x))
149 psycopg.register_type(psycopg.new_type((1083,), "time", lambda x:x))
150 psycopg.register_type(psycopg.new_type((1114,), "datetime", lambda x:x))
151 #psycopg.register_type(psycopg.new_type((700, 701, 1700), 'decimal', decimalize))
153 psycopg.register_type(psycopg.new_type((1082,), "date", lambda x:x))
154 psycopg.register_type(psycopg.new_type((1083,), "time", lambda x:x))
155 psycopg.register_type(psycopg.new_type((1114,), "datetime", lambda x:x))