1 ##############################################################################
3 # Copyright (c) 2004 TINY SPRL. (http://tiny.be) All Rights Reserved.
5 # $Id: pooler.py 1310 2005-09-08 20:40:15Z pinky $
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.
28 ##############################################################################
37 # from tools import decimal
45 def __init__(self, db, con, dbname):
47 self.obj = db.cursor()
51 def execute_not_run(self,*args):
52 #if not fake_cursor.nbr % 1:
53 # print 'sql: ',fake_cursor.nbr, args
54 res = re.match('^select.* from ([a-zA-Z_]+) .*$', args[0], re.I)
56 fake_cursor._tables.setdefault(res.group(1), 0)
57 fake_cursor._tables[res.group(1)] += 1
61 # print 'sql: ',fake_cursor.nbr, args[0], args[1]
63 # print 'sql: ',fake_cursor.nbr, args[0]
65 #if not fake_cursor.nbr % 5000:
67 # for t,c in fake_cursor._tables.items():
71 # print 'After %d queries' % (fake_cursor.nbr,)
72 # for line in ct[:50]:
73 # print ' %s: %d' % (line[1], line[0])
76 # print 'sql: ',fake_cursor.nbr, args[0], args[1]
78 # print 'sql: ',fake_cursor.nbr, args[0]
81 return self.obj.execute(*args)
84 # print "close cursors fno:", [i.fileno() for i in self.db.cursors]
87 # This force the cursor to be freed, and thus, available again. It is
88 # important because otherwise we can overload the server very easily
89 # because of a cursor shortage (because cursors are not garbage
90 # collected as fast as they should). The problem is probably due in
91 # part because browse records keep a reference to the cursor.
93 # print "after close cursors fno:", [i.fileno() for i in self.db.cursors]
95 def __getattr__(self, name):
97 return getattr(self.obj, name)
100 def __init__(self, truedb, dbname):
105 return fake_cursor(self.truedb, {}, self.dbname)
107 def decimalize(symb):
108 if symb is None: return None
109 if isinstance(symb, float):
110 return decimal.Decimal('%f' % symb)
111 return decimal.Decimal(symb)
113 def db_connect(db_name):
114 host = tools.config['db_host'] and "host=%s" % tools.config['db_host'] or ''
115 port = tools.config['db_port'] and "port=%s" % tools.config['db_port'] or ''
116 name = "dbname=%s" % db_name
117 user = tools.config['db_user'] and "user=%s" % tools.config['db_user'] or ''
118 password = tools.config['db_password'] and "password=%s" % tools.config['db_password'] or ''
119 tdb = psycopg.connect('%s %s %s %s %s' % (host, port, name, user, password), serialize=0)
120 fdb = fakedb(tdb, db_name)
124 #define DATEOID 1082, define TIMESTAMPOID 1114 see pgtypes.h
125 psycopg.register_type(psycopg.new_type((1082,), "date", lambda x:x))
126 psycopg.register_type(psycopg.new_type((1083,), "time", lambda x:x))
127 psycopg.register_type(psycopg.new_type((1114,), "datetime", lambda x:x))
128 #psycopg.register_type(psycopg.new_type((700, 701, 1700), 'decimal', decimalize))
130 psycopg.register_type(psycopg.new_type((1082,), "date", lambda x:x))
131 psycopg.register_type(psycopg.new_type((1083,), "time", lambda x:x))
132 psycopg.register_type(psycopg.new_type((1114,), "datetime", lambda x:x))