~syleam/openobject-server/exit-properly

« back to all changes in this revision

Viewing changes to bin/sql_db.py

  • Committer: pinky
  • Date: 2006-12-07 13:41:40 UTC
  • Revision ID: pinky-3f10ee12cea3c4c75cef44ab04ad33ef47432907
New trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2004 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
4
#
 
5
# $Id: pooler.py 1310 2005-09-08 20:40:15Z pinky $
 
6
#
 
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
 
12
# Service Company
 
13
#
 
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.
 
18
#
 
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.
 
23
#
 
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
#
 
28
##############################################################################
 
29
 
 
30
import psycopg
 
31
import tools
 
32
import sys,os
 
33
 
 
34
#try:
 
35
#       import decimal
 
36
#except ImportError:
 
37
#       from tools import decimal
 
38
 
 
39
 
 
40
import re
 
41
 
 
42
class fake_cursor:
 
43
        nbr = 0
 
44
        _tables = {}
 
45
        def __init__(self, db, con, dbname):
 
46
                self.db = db
 
47
                self.obj = db.cursor()
 
48
                self.con = con
 
49
                self.dbname = dbname
 
50
 
 
51
        def execute_dont_run(self,*args):
 
52
                if not fake_cursor.nbr % 100:
 
53
                        print 'sql: ',fake_cursor.nbr
 
54
                res = re.match('^select.* from ([a-zA-Z_]+) .*$', args[0], re.I)
 
55
                if res:
 
56
                        fake_cursor._tables.setdefault(res.group(1), 0)
 
57
                        fake_cursor._tables[res.group(1)] += 1
 
58
 
 
59
                #else:
 
60
                #       if len(args)>1:
 
61
                #               print 'sql: ',fake_cursor.nbr, args[0], args[1]
 
62
                #       else:
 
63
                #               print 'sql: ',fake_cursor.nbr, args[0]
 
64
 
 
65
                if not fake_cursor.nbr % 5000:
 
66
                        ct = []
 
67
                        for t,c in fake_cursor._tables.items():
 
68
                                ct.append([c,t])
 
69
                        ct.sort()
 
70
                        ct.reverse()
 
71
                        print 'After %d queries' % (fake_cursor.nbr,)
 
72
                        for line in ct[:50]:
 
73
                                print '    %s: %d' % (line[1], line[0])
 
74
 
 
75
                #if len(args)>1:
 
76
                #       print 'sql: ',fake_cursor.nbr, args[0], args[1]
 
77
                #else:
 
78
                #       print 'sql: ',fake_cursor.nbr, args[0]
 
79
 
 
80
                fake_cursor.nbr += 1
 
81
                return self.obj.execute(*args)
 
82
 
 
83
        def close(self):
 
84
#               print "close cursors fno:", [i.fileno() for i in self.db.cursors]
 
85
                self.obj.close()
 
86
 
 
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.
 
92
                del self.obj
 
93
#               print "after close cursors fno:", [i.fileno() for i in self.db.cursors]
 
94
 
 
95
        def __getattr__(self, name):
 
96
#               print 'LOOK',name
 
97
                return getattr(self.obj, name)
 
98
 
 
99
class fakedb:
 
100
        def __init__(self, truedb, dbname):
 
101
                self.truedb = truedb
 
102
                self.dbname = dbname
 
103
                
 
104
        def cursor(self):
 
105
                return fake_cursor(self.truedb, {}, self.dbname)
 
106
 
 
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)
 
112
 
 
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)
 
121
        return fdb
 
122
 
 
123
def init():
 
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))
 
129
 
 
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))
 
133