2008-07-23 15:01:27 +00:00
|
|
|
# -*- encoding: utf-8 -*-
|
2006-12-07 13:41:40 +00:00
|
|
|
##############################################################################
|
|
|
|
#
|
2008-06-16 07:24:04 +00:00
|
|
|
# Copyright (c) 2004-2008 Tiny SPRL (http://tiny.be) All Rights Reserved.
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
2008-06-16 06:44:24 +00:00
|
|
|
# $Id$
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
|
|
|
# WARNING: This program as such is intended to be used by professional
|
|
|
|
# programmers who take the whole responsability of assessing all potential
|
|
|
|
# consequences resulting from its eventual inadequacies and bugs
|
|
|
|
# End users who are looking for a ready-to-use solution with commercial
|
|
|
|
# garantees and support are strongly adviced to contract a Free Software
|
|
|
|
# Service Company
|
|
|
|
#
|
|
|
|
# This program is Free Software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
2008-06-16 07:24:04 +00:00
|
|
|
###############################################################################
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
import psycopg
|
|
|
|
import tools
|
|
|
|
import sys,os
|
|
|
|
|
|
|
|
#try:
|
2008-07-22 14:24:36 +00:00
|
|
|
# import decimal
|
2006-12-07 13:41:40 +00:00
|
|
|
#except ImportError:
|
2008-07-22 14:24:36 +00:00
|
|
|
# from tools import decimal
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
2007-07-26 08:31:38 +00:00
|
|
|
from mx import DateTime as mdt
|
2007-08-30 14:22:06 +00:00
|
|
|
re_from = re.compile('.* from "?([a-zA-Z_0-9]+)"? .*$');
|
|
|
|
re_into = re.compile('.* into "?([a-zA-Z_0-9]+)"? .*$');
|
2007-07-26 08:31:38 +00:00
|
|
|
|
2006-12-07 13:41:40 +00:00
|
|
|
class fake_cursor:
|
2008-08-05 08:39:45 +00:00
|
|
|
IN_MAX = 1000
|
2008-07-22 14:24:36 +00:00
|
|
|
nbr = 0
|
|
|
|
_tables = {}
|
|
|
|
sql_from_log = {}
|
|
|
|
sql_into_log = {}
|
|
|
|
sql_log = False
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
def __init__(self, db, con, dbname):
|
|
|
|
self.db = db
|
|
|
|
self.obj = db.cursor()
|
|
|
|
self.con = con
|
|
|
|
self.dbname = dbname
|
|
|
|
|
|
|
|
def execute(self, sql, params=None):
|
|
|
|
if not params:
|
|
|
|
params=()
|
|
|
|
def base_string(s):
|
|
|
|
if isinstance(s, unicode):
|
|
|
|
return s.encode('utf-8')
|
|
|
|
return s
|
|
|
|
p=map(base_string, params)
|
|
|
|
if isinstance(sql, unicode):
|
|
|
|
sql = sql.encode('utf-8')
|
|
|
|
if self.sql_log:
|
|
|
|
now = mdt.now()
|
2008-08-04 15:32:50 +00:00
|
|
|
print "SQL LOG query:", sql
|
|
|
|
print "SQL LOG params:", repr(p)
|
2008-07-22 14:24:36 +00:00
|
|
|
if p:
|
|
|
|
res = self.obj.execute(sql, p)
|
|
|
|
else:
|
|
|
|
res = self.obj.execute(sql)
|
|
|
|
if self.sql_log:
|
|
|
|
self.count+=1
|
|
|
|
res_from = re_from.match(sql.lower())
|
|
|
|
if res_from:
|
|
|
|
self.sql_from_log.setdefault(res_from.group(1), [0, 0])
|
|
|
|
self.sql_from_log[res_from.group(1)][0] += 1
|
|
|
|
self.sql_from_log[res_from.group(1)][1] += mdt.now() - now
|
|
|
|
res_into = re_into.match(sql.lower())
|
|
|
|
if res_into:
|
|
|
|
self.sql_into_log.setdefault(res_into.group(1), [0, 0])
|
|
|
|
self.sql_into_log[res_into.group(1)][0] += 1
|
|
|
|
self.sql_into_log[res_into.group(1)][1] += mdt.now() - now
|
|
|
|
return res
|
|
|
|
|
|
|
|
def print_log(self, type='from'):
|
|
|
|
print "SQL LOG %s:" % (type,)
|
|
|
|
if type == 'from':
|
|
|
|
logs = self.sql_from_log.items()
|
|
|
|
else:
|
|
|
|
logs = self.sql_into_log.items()
|
|
|
|
logs.sort(lambda x, y: cmp(x[1][1], y[1][1]))
|
|
|
|
sum=0
|
|
|
|
for r in logs:
|
|
|
|
print "table:", r[0], ":", str(r[1][1]), "/", r[1][0]
|
|
|
|
sum+= r[1][1]
|
|
|
|
print "SUM:%s/%d"% (sum, self.count)
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
if self.sql_log:
|
|
|
|
self.print_log('from')
|
|
|
|
self.print_log('into')
|
|
|
|
self.obj.close()
|
|
|
|
|
2008-09-11 07:29:45 +00:00
|
|
|
# This force the cursor to be freed, and thus, available again. It is
|
|
|
|
# important because otherwise we can overload the server very easily
|
2008-07-22 14:24:36 +00:00
|
|
|
# because of a cursor shortage (because cursors are not garbage
|
2008-09-11 07:29:45 +00:00
|
|
|
# collected as fast as they should). The problem is probably due in
|
2008-07-22 14:24:36 +00:00
|
|
|
# part because browse records keep a reference to the cursor.
|
|
|
|
del self.obj
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
return getattr(self.obj, name)
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
class fakedb:
|
2008-07-22 14:24:36 +00:00
|
|
|
def __init__(self, truedb, dbname):
|
|
|
|
self.truedb = truedb
|
|
|
|
self.dbname = dbname
|
2008-09-11 07:29:45 +00:00
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
def cursor(self):
|
|
|
|
return fake_cursor(self.truedb, {}, self.dbname)
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
def decimalize(symb):
|
2008-07-22 14:24:36 +00:00
|
|
|
if symb is None: return None
|
|
|
|
if isinstance(symb, float):
|
|
|
|
return decimal.Decimal('%f' % symb)
|
|
|
|
return decimal.Decimal(symb)
|
2006-12-07 13:41:40 +00:00
|
|
|
|
2007-11-21 15:24:36 +00:00
|
|
|
def db_connect(db_name, serialize=0):
|
2008-07-22 14:24:36 +00:00
|
|
|
host = tools.config['db_host'] and "host=%s" % tools.config['db_host'] or ''
|
|
|
|
port = tools.config['db_port'] and "port=%s" % tools.config['db_port'] or ''
|
|
|
|
name = "dbname=%s" % db_name
|
|
|
|
user = tools.config['db_user'] and "user=%s" % tools.config['db_user'] or ''
|
|
|
|
password = tools.config['db_password'] and "password=%s" % tools.config['db_password'] or ''
|
|
|
|
maxconn = int(tools.config['db_maxconn']) or 64
|
|
|
|
tdb = psycopg.connect('%s %s %s %s %s' % (host, port, name, user, password),
|
|
|
|
serialize=serialize, maxconn=maxconn)
|
|
|
|
fdb = fakedb(tdb, db_name)
|
|
|
|
return fdb
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
def init():
|
2008-07-22 14:24:36 +00:00
|
|
|
#define DATEOID 1082, define TIMESTAMPOID 1114 see pgtypes.h
|
|
|
|
psycopg.register_type(psycopg.new_type((1082,), "date", lambda x:x))
|
|
|
|
psycopg.register_type(psycopg.new_type((1083,), "time", lambda x:x))
|
|
|
|
psycopg.register_type(psycopg.new_type((1114,), "datetime", lambda x:x))
|
|
|
|
#psycopg.register_type(psycopg.new_type((700, 701, 1700), 'decimal', decimalize))
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
psycopg.register_type(psycopg.new_type((1082,), "date", lambda x:x))
|
|
|
|
psycopg.register_type(psycopg.new_type((1083,), "time", lambda x:x))
|
|
|
|
psycopg.register_type(psycopg.new_type((1114,), "datetime", lambda x:x))
|
|
|
|
|
2008-07-23 15:01:27 +00:00
|
|
|
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
|
|
|
|