2004-10-29 20:15:45 +04:00
|
|
|
# ZPsycopgDA/db.py - query execution
|
|
|
|
#
|
|
|
|
# Copyright (C) 2004 Federico Di Gregorio <fog@initd.org>
|
|
|
|
#
|
|
|
|
# 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, or (at your option) any later
|
|
|
|
# version.
|
|
|
|
#
|
|
|
|
# Or, at your option this program (ZPsycopgDA) can be distributed under the
|
|
|
|
# Zope Public License (ZPL) Version 1.0, as published on the Zope web site,
|
|
|
|
# http://www.zope.org/Resources/ZPL.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but
|
|
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
|
|
|
|
# or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
#
|
|
|
|
# See the LICENSE file for details.
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
from Shared.DC.ZRDB.TM import TM
|
|
|
|
from Shared.DC.ZRDB import dbi_db
|
|
|
|
|
2004-10-29 20:15:45 +04:00
|
|
|
from ZODB.POSException import ConflictError
|
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
import site
|
2004-10-29 20:15:45 +04:00
|
|
|
import pool
|
2004-10-19 07:17:12 +04:00
|
|
|
|
2005-08-22 05:49:18 +04:00
|
|
|
import psycopg2
|
2006-06-09 04:05:42 +04:00
|
|
|
from psycopg2.extensions import INTEGER, LONGINTEGER, FLOAT, BOOLEAN, DATE, TIME
|
2005-08-22 05:49:18 +04:00
|
|
|
from psycopg2 import NUMBER, STRING, ROWID, DATETIME
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
|
2004-10-29 20:15:45 +04:00
|
|
|
# the DB object, managing all the real query work
|
|
|
|
|
|
|
|
class DB(TM, dbi_db.DB):
|
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
_p_oid = _p_changed = _registered = None
|
|
|
|
|
2004-10-29 20:15:45 +04:00
|
|
|
def __init__(self, dsn, tilevel, enc='utf-8'):
|
|
|
|
self.dsn = dsn
|
2004-10-19 07:17:12 +04:00
|
|
|
self.tilevel = tilevel
|
2004-10-29 20:15:45 +04:00
|
|
|
self.encoding = enc
|
2004-10-19 07:17:12 +04:00
|
|
|
self.failures = 0
|
|
|
|
self.calls = 0
|
2006-06-15 16:39:56 +04:00
|
|
|
self.make_mappings()
|
2004-10-19 07:17:12 +04:00
|
|
|
|
2004-10-29 20:15:45 +04:00
|
|
|
def getconn(self, create=True):
|
|
|
|
conn = pool.getconn(self.dsn)
|
|
|
|
conn.set_isolation_level(int(self.tilevel))
|
|
|
|
return conn
|
|
|
|
|
|
|
|
def putconn(self, close=False):
|
|
|
|
try:
|
|
|
|
conn = pool.getconn(self.dsn, False)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
pool.putconn(self.dsn, conn, close)
|
|
|
|
|
|
|
|
def getcursor(self):
|
|
|
|
conn = self.getconn()
|
|
|
|
return conn.cursor()
|
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
def _finish(self, *ignored):
|
2004-10-29 20:15:45 +04:00
|
|
|
try:
|
|
|
|
conn = self.getconn(False)
|
|
|
|
conn.commit()
|
|
|
|
self.putconn()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
def _abort(self, *ignored):
|
2004-10-29 20:15:45 +04:00
|
|
|
try:
|
|
|
|
conn = self.getconn(False)
|
|
|
|
conn.rollback()
|
|
|
|
self.putconn()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def open(self):
|
|
|
|
# this will create a new pool for our DSN if not already existing,
|
|
|
|
# then get and immediately release a connection
|
|
|
|
self.getconn()
|
|
|
|
self.putconn()
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
# FIXME: if this connection is closed we flush all the pool associated
|
|
|
|
# with the current DSN; does this makes sense?
|
|
|
|
pool.flushpool(self.dsn)
|
|
|
|
|
|
|
|
def sortKey(self):
|
|
|
|
return 1
|
|
|
|
|
2006-06-15 16:39:56 +04:00
|
|
|
def make_mappings(self):
|
|
|
|
"""Generate the mappings used later by self.convert_description()."""
|
|
|
|
self.type_mappings = {}
|
|
|
|
for t, s in [(INTEGER,'i'), (LONGINTEGER, 'i'), (NUMBER, 'n'),
|
|
|
|
(BOOLEAN,'n'), (ROWID, 'i'),
|
|
|
|
(DATETIME, 'd'), (DATE, 'd'), (TIME, 'd')]:
|
|
|
|
for v in t.values:
|
|
|
|
self.type_mappings[v] = (t, s)
|
|
|
|
|
2005-05-26 11:34:27 +04:00
|
|
|
def convert_description(self, desc, use_psycopg_types=False):
|
|
|
|
"""Convert DBAPI-2.0 description field to Zope format."""
|
|
|
|
items = []
|
|
|
|
for name, typ, width, ds, p, scale, null_ok in desc:
|
2006-06-15 16:39:56 +04:00
|
|
|
m = self.type_mappings.get(typ, (STRING, 's'))
|
2005-05-26 11:34:27 +04:00
|
|
|
items.append({
|
|
|
|
'name': name,
|
2006-06-15 16:39:56 +04:00
|
|
|
'type': use_psycopg_types and m[0] or m[1],
|
2005-05-26 11:34:27 +04:00
|
|
|
'width': width,
|
|
|
|
'precision': p,
|
|
|
|
'scale': scale,
|
|
|
|
'null': null_ok,
|
|
|
|
})
|
|
|
|
return items
|
|
|
|
|
2004-10-29 20:15:45 +04:00
|
|
|
## tables and rows ##
|
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
def tables(self, rdb=0, _care=('TABLE', 'VIEW')):
|
|
|
|
self._register()
|
2004-10-29 20:15:45 +04:00
|
|
|
c = self.getcursor()
|
|
|
|
c.execute(
|
|
|
|
"SELECT t.tablename AS NAME, 'TABLE' AS TYPE "
|
|
|
|
" FROM pg_tables t WHERE tableowner <> 'postgres' "
|
|
|
|
"UNION SELECT v.viewname AS NAME, 'VIEW' AS TYPE "
|
|
|
|
" FROM pg_views v WHERE viewowner <> 'postgres' "
|
|
|
|
"UNION SELECT t.tablename AS NAME, 'SYSTEM_TABLE\' AS TYPE "
|
|
|
|
" FROM pg_tables t WHERE tableowner = 'postgres' "
|
|
|
|
"UNION SELECT v.viewname AS NAME, 'SYSTEM_TABLE' AS TYPE "
|
|
|
|
"FROM pg_views v WHERE viewowner = 'postgres'")
|
|
|
|
res = []
|
2004-10-19 07:17:12 +04:00
|
|
|
for name, typ in c.fetchall():
|
|
|
|
if typ in _care:
|
2004-10-29 20:15:45 +04:00
|
|
|
res.append({'TABLE_NAME': name, 'TABLE_TYPE': typ})
|
|
|
|
self.putconn()
|
|
|
|
return res
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
def columns(self, table_name):
|
|
|
|
self._register()
|
2004-10-29 20:15:45 +04:00
|
|
|
c = self.getcursor()
|
2004-10-19 07:17:12 +04:00
|
|
|
try:
|
2004-10-29 20:15:45 +04:00
|
|
|
r = c.execute('SELECT * FROM "%s" WHERE 1=0' % table_name)
|
2004-10-19 07:17:12 +04:00
|
|
|
except:
|
|
|
|
return ()
|
2004-10-29 20:15:45 +04:00
|
|
|
self.putconn()
|
2005-05-26 11:34:27 +04:00
|
|
|
return self.convert_description(c.description, True)
|
2004-10-29 20:15:45 +04:00
|
|
|
|
|
|
|
## query execution ##
|
2004-10-19 07:17:12 +04:00
|
|
|
|
|
|
|
def query(self, query_string, max_rows=None, query_data=None):
|
|
|
|
self._register()
|
|
|
|
self.calls = self.calls+1
|
2004-10-29 20:15:45 +04:00
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
desc = ()
|
2004-10-29 20:15:45 +04:00
|
|
|
res = []
|
2004-10-19 07:17:12 +04:00
|
|
|
nselects = 0
|
2004-10-29 20:15:45 +04:00
|
|
|
|
|
|
|
c = self.getcursor()
|
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
try:
|
2004-10-29 20:15:45 +04:00
|
|
|
for qs in [x for x in query_string.split('\0') if x]:
|
2004-10-19 07:17:12 +04:00
|
|
|
if type(qs) == unicode:
|
|
|
|
if self.encoding:
|
|
|
|
qs = qs.encode(self.encoding)
|
|
|
|
try:
|
2005-01-20 08:49:40 +03:00
|
|
|
if query_data:
|
2004-10-29 20:15:45 +04:00
|
|
|
c.execute(qs, query_data)
|
2004-10-19 07:17:12 +04:00
|
|
|
else:
|
2004-10-29 20:15:45 +04:00
|
|
|
c.execute(qs)
|
2005-09-12 06:23:17 +04:00
|
|
|
except psycopg2.OperationalError, e:
|
2005-01-20 08:49:40 +03:00
|
|
|
try:
|
|
|
|
self.close()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
self.open()
|
|
|
|
try:
|
|
|
|
if query_data:
|
|
|
|
c.execute(qs, query_data)
|
|
|
|
else:
|
|
|
|
c.execute(qs)
|
2005-08-22 05:49:18 +04:00
|
|
|
except (psycopg2.ProgrammingError,
|
|
|
|
psycopg2.IntegrityError), e:
|
2005-01-20 08:49:40 +03:00
|
|
|
if e.args[0].find("concurrent update") > -1:
|
|
|
|
raise ConflictError
|
|
|
|
raise e
|
2005-08-22 05:49:18 +04:00
|
|
|
except (psycopg2.ProgrammingError, psycopg2.IntegrityError), e:
|
2004-10-29 20:15:45 +04:00
|
|
|
if e.args[0].find("concurrent update") > -1:
|
2004-10-19 07:17:12 +04:00
|
|
|
raise ConflictError
|
2004-10-29 20:15:45 +04:00
|
|
|
raise e
|
2004-10-19 07:17:12 +04:00
|
|
|
if c.description is not None:
|
2004-10-29 20:15:45 +04:00
|
|
|
nselects += 1
|
2004-10-19 07:17:12 +04:00
|
|
|
if c.description != desc and nselects > 1:
|
2005-08-22 05:49:18 +04:00
|
|
|
raise psycopg2.ProgrammingError(
|
2004-10-29 20:15:45 +04:00
|
|
|
'multiple selects in single query not allowed')
|
2004-10-19 07:17:12 +04:00
|
|
|
if max_rows:
|
2004-10-29 20:15:45 +04:00
|
|
|
res = c.fetchmany(max_rows)
|
2004-10-19 07:17:12 +04:00
|
|
|
else:
|
2004-10-29 20:15:45 +04:00
|
|
|
res = c.fetchall()
|
2004-10-19 07:17:12 +04:00
|
|
|
desc = c.description
|
|
|
|
self.failures = 0
|
2004-10-29 20:15:45 +04:00
|
|
|
|
2004-10-19 07:17:12 +04:00
|
|
|
except StandardError, err:
|
|
|
|
self._abort()
|
|
|
|
raise err
|
|
|
|
|
2005-05-26 11:34:27 +04:00
|
|
|
return self.convert_description(desc), res
|