1
##############################################################################
3
# Zope Public License (ZPL) Version 1.0
4
# -------------------------------------
6
# Copyright (c) Digital Creations. All rights reserved.
8
# This license has been certified as Open Source(tm).
10
# Redistribution and use in source and binary forms, with or without
11
# modification, are permitted provided that the following conditions are
14
# 1. Redistributions in source code must retain the above copyright
15
# notice, this list of conditions, and the following disclaimer.
17
# 2. Redistributions in binary form must reproduce the above copyright
18
# notice, this list of conditions, and the following disclaimer in
19
# the documentation and/or other materials provided with the
22
# 3. Digital Creations requests that attribution be given to Zope
23
# in any manner possible. Zope includes a "Powered by Zope"
24
# button that is installed by default. While it is not a license
25
# violation to remove this button, it is requested that the
26
# attribution remain. A significant investment has been put
27
# into Zope, and this effort will continue if the Zope community
28
# continues to grow. This is one way to assure that growth.
30
# 4. All advertising materials and documentation mentioning
31
# features derived from or use of this software must display
32
# the following acknowledgement:
34
# "This product includes software developed by Digital Creations
35
# for use in the Z Object Publishing Environment
36
# (http://www.zope.org/)."
38
# In the event that the product being advertised includes an
39
# intact Zope distribution (with copyright and license included)
40
# then this clause is waived.
42
# 5. Names associated with Zope or Digital Creations must not be used to
43
# endorse or promote products derived from this software without
44
# prior written permission from Digital Creations.
46
# 6. Modified redistributions of any form whatsoever must retain
47
# the following acknowledgment:
49
# "This product includes software developed by Digital Creations
50
# for use in the Z Object Publishing Environment
51
# (http://www.zope.org/)."
53
# Intact (re-)distributions of any official Zope release do not
54
# require an external acknowledgement.
56
# 7. Modifications are encouraged but must be packaged separately as
57
# patches to official Zope releases. Distributions that do not
58
# clearly separate the patches from the original work must be clearly
59
# labeled as unofficial distributions. Modifications which do not
60
# carry the name Zope may be packaged in any form, as long as they
61
# conform to all of the clauses above.
66
# THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
67
# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
69
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
70
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
71
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
72
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
73
# USE, DATA, OR PROFITS; OR BUSINESS INTERUPTION) HOWEVER CAUSED AND
74
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
75
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
76
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
80
# This software consists of contributions made by Digital Creations and
81
# many individuals on behalf of Digital Creations. Specific
82
# attributions are listed in the accompanying credits file.
84
##############################################################################
86
'''$Id: db.py 532 2004-09-27 18:35:25Z fog $'''
87
__version__='$Revision: 1.31.2.7 $'[11:-2]
1
# ZPsycopgDA/db.py - query execution
3
# Copyright (C) 2004 Federico Di Gregorio <fog@initd.org>
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by the
7
# Free Software Foundation; either version 2, or (at your option) any later
10
# Or, at your option this program (ZPsycopgDA) can be distributed under the
11
# Zope Public License (ZPL) Version 1.0, as published on the Zope web site,
12
# http://www.zope.org/Resources/ZPL.
14
# This program is distributed in the hope that it will be useful, but
15
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
16
# or FITNESS FOR A PARTICULAR PURPOSE.
18
# See the LICENSE file for details.
90
20
from Shared.DC.ZRDB.TM import TM
91
21
from Shared.DC.ZRDB import dbi_db
94
from string import strip, split, find
96
from types import ListType
23
from ZODB.POSException import ConflictError
100
from psycopg import NUMBER, STRING, INTEGER, FLOAT, DATETIME
101
from psycopg import BOOLEAN, ROWID, LONGINTEGER
102
from ZODB.POSException import ConflictError
104
class DB(TM,dbi_db.DB):
30
from psycopg.extensions import INTEGER, LONGINTEGER, FLOAT, BOOLEAN
31
from psycopg import NUMBER, STRING, ROWID, DATETIME
35
# the DB object, managing all the real query work
37
class DB(TM, dbi_db.DB):
106
39
_p_oid = _p_changed = _registered = None
108
def __init__(self, connection, tilevel, enc='utf-8'):
109
self.connection = connection
41
def __init__(self, dsn, tilevel, enc='utf-8'):
110
43
self.tilevel = tilevel
112
self.db = self.connect(self.connection)
116
def connect(self, connection):
117
o = psycopg.connect(connection)
118
o.set_isolation_level(int(self.tilevel))
48
def getconn(self, create=True):
49
conn = pool.getconn(self.dsn)
50
conn.set_isolation_level(int(self.tilevel))
53
def putconn(self, close=False):
55
conn = pool.getconn(self.dsn, False)
56
except AttributeError:
58
pool.putconn(self.dsn, conn, close)
121
64
def _finish(self, *ignored):
122
if hasattr(self, 'db') and self.db:
66
conn = self.getconn(False)
69
except AttributeError:
125
72
def _abort(self, *ignored):
126
if hasattr(self, 'db') and self.db:
130
"""Obtains a cursor in a safe way."""
131
if not hasattr(self, 'db') or not self.db:
132
self.db = self.connect(self.connection)
133
return self.db.cursor()
74
conn = self.getconn(False)
77
except AttributeError:
81
# this will create a new pool for our DSN if not already existing,
82
# then get and immediately release a connection
87
# FIXME: if this connection is closed we flush all the pool associated
88
# with the current DSN; does this makes sense?
89
pool.flushpool(self.dsn)
135
96
def tables(self, rdb=0, _care=('TABLE', 'VIEW')):
138
c.execute('SELECT t.tablename AS NAME, '
139
'\'TABLE\' AS TYPE FROM pg_tables t '
140
'WHERE tableowner <> \'postgres\' '
141
'UNION SELECT v.viewname AS NAME, '
142
'\'VIEW\' AS TYPE FROM pg_views v '
143
'WHERE viewowner <> \'postgres\' '
144
'UNION SELECT t.tablename AS NAME, '
145
'\'SYSTEM_TABLE\' AS TYPE FROM pg_tables t '
146
'WHERE tableowner = \'postgres\' '
147
'UNION SELECT v.viewname AS NAME, '
148
'\'SYSTEM_TABLE\' AS TYPE FROM pg_views v '
149
'WHERE viewowner = \'postgres\' ' )
100
"SELECT t.tablename AS NAME, 'TABLE' AS TYPE "
101
" FROM pg_tables t WHERE tableowner <> 'postgres' "
102
"UNION SELECT v.viewname AS NAME, 'VIEW' AS TYPE "
103
" FROM pg_views v WHERE viewowner <> 'postgres' "
104
"UNION SELECT t.tablename AS NAME, 'SYSTEM_TABLE\' AS TYPE "
105
" FROM pg_tables t WHERE tableowner = 'postgres' "
106
"UNION SELECT v.viewname AS NAME, 'SYSTEM_TABLE' AS TYPE "
107
"FROM pg_views v WHERE viewowner = 'postgres'")
152
109
for name, typ in c.fetchall():
154
a({'TABLE_NAME': name, 'TABLE_TYPE': typ})
111
res.append({'TABLE_NAME': name, 'TABLE_TYPE': typ})
158
115
def columns(self, table_name):
162
r = c.execute('select * from "%s" where 1=0' % table_name)
119
r = c.execute('SELECT * FROM "%s" WHERE 1=0' % table_name)
168
for name, type, width, ds, p, scale, null_ok in desc:
123
for name, type, width, ds, p, scale, null_ok in c.description:
169
124
if type == NUMBER:
170
125
if type == INTEGER: