2 # PostgreSQL database interface.
3 # Sort of like DBI(3) (Database independent interface for Perl).
5 # Mark Huang <mlhuang@cs.princeton.edu>
6 # Copyright (C) 2006 The Trustees of Princeton University
10 import psycopg2.extensions
11 psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
12 # UNICODEARRAY not exported yet
13 psycopg2.extensions.register_type(psycopg2._psycopg.UNICODEARRAY)
16 from types import StringTypes, NoneType
20 from pprint import pformat
22 from PLC.Debug import profile, log
23 from PLC.Faults import *
24 from datetime import datetime as DateTimeType
27 def __init__(self, api):
31 self.connection = None
34 if self.connection is None:
35 # (Re)initialize database connection
37 # Try UNIX socket first
38 self.connection = psycopg2.connect(user = self.api.config.PLC_DB_USER,
39 password = self.api.config.PLC_DB_PASSWORD,
40 database = self.api.config.PLC_DB_NAME)
41 except psycopg2.OperationalError:
43 self.connection = psycopg2.connect(user = self.api.config.PLC_DB_USER,
44 password = self.api.config.PLC_DB_PASSWORD,
45 database = self.api.config.PLC_DB_NAME,
46 host = self.api.config.PLC_DB_HOST,
47 port = self.api.config.PLC_DB_PORT)
48 self.connection.set_client_encoding("UNICODE")
50 (self.rowcount, self.description, self.lastrowid) = \
53 return self.connection.cursor()
56 if self.connection is not None:
57 self.connection.close()
58 self.connection = None
61 # From pgdb, and simplify code
63 if isinstance(x, DateTimeType):
65 elif isinstance(x, unicode):
66 x = x.encode( 'utf-8' )
68 if isinstance(x, types.StringType):
69 x = "'%s'" % str(x).replace("\\", "\\\\").replace("'", "''")
70 elif isinstance(x, (types.IntType, types.LongType, types.FloatType)):
74 elif isinstance(x, (types.ListType, types.TupleType, set)):
75 x = 'ARRAY[%s]' % ', '.join(map(lambda x: str(_quote(x)), x))
76 elif hasattr(x, '__pg_repr__'):
79 raise PLCDBError, 'Cannot quote type %s' % type(x)
83 def quote(self, value):
85 Returns quoted version of the specified value.
87 return PostgreSQL._quote (value)
89 # following is an unsuccessful attempt to re-use lib code as much as possible
90 # def quote(self, value):
91 # # The pgdb._quote function is good enough for general SQL
92 # # quoting, except for array types.
93 # if isinstance (value, (types.ListType, types.TupleType, set)):
94 # 'ARRAY[%s]' % ', '.join( [ str(self.quote(x)) for x in value ] )
97 # # up to PyGreSQL-3.x, function was pgdb._quote
99 # return pgdb._quote(value)
101 # # with PyGreSQL-4.x, use psycopg2's adapt
102 # from psycopg2.extensions import adapt
103 # return adapt (value)
106 def param(self, name, value):
107 # None is converted to the unquoted string NULL
108 if isinstance(value, NoneType):
110 # True and False are also converted to unquoted strings
111 elif isinstance(value, bool):
113 elif isinstance(value, float):
115 elif not isinstance(value, StringTypes):
120 return '%(' + name + ')' + conversion
122 def begin_work(self):
123 # Implicit in pgdb.connect()
127 self.connection.commit()
130 self.connection.rollback()
132 def do(self, query, params = None):
133 cursor = self.execute(query, params)
137 def next_id(self, table_name, primary_key):
138 sequence = "%(table_name)s_%(primary_key)s_seq" % locals()
139 sql = "SELECT nextval('%(sequence)s')" % locals()
140 rows = self.selectall(sql, hashref = False)
146 def last_insert_id(self, table_name, primary_key):
147 if isinstance(self.lastrowid, int):
148 sql = "SELECT %s FROM %s WHERE oid = %d" % \
149 (primary_key, table_name, self.lastrowid)
150 rows = self.selectall(sql, hashref = False)
156 # modified for psycopg2-2.0.7
157 # executemany is undefined for SELECT's
158 # see http://www.python.org/dev/peps/pep-0249/
159 # accepts either None, a single dict, a tuple of single dict - in which case it execute's
160 # or a tuple of several dicts, in which case it executemany's
161 def execute(self, query, params = None):
163 cursor = self.cursor()
166 # psycopg2 requires %()s format for all parameters,
167 # regardless of type.
168 # this needs to be done carefully though as with pattern-based filters
169 # we might have percents embedded in the query
170 # so e.g. GetPersons({'email':'*fake*'}) was resulting in .. LIKE '%sake%'
172 query = re.sub(r'(%\([^)]*\)|%)[df]', r'\1s', query)
173 # rewrite wildcards set by Filter.py as '***' into '%'
174 query = query.replace ('***','%')
178 print >> log,'execute0',query
179 cursor.execute(query)
180 elif isinstance(params,dict):
182 print >> log,'execute-dict: params',params,'query',query%params
183 cursor.execute(query,params)
184 elif isinstance(params,tuple) and len(params)==1:
186 print >> log,'execute-tuple',query%params[0]
187 cursor.execute(query,params[0])
191 for params in param_seq:
192 print >> log,'executemany',query%params
193 cursor.executemany(query, param_seq)
194 (self.rowcount, self.description, self.lastrowid) = \
195 (cursor.rowcount, cursor.description, cursor.lastrowid)
201 uuid = commands.getoutput("uuidgen")
202 print >> log, "Database error %s:" % uuid
204 print >> log, "Query:"
206 print >> log, "Params:"
207 print >> log, pformat(params)
208 raise PLCDBError("Please contact " + \
209 self.api.config.PLC_NAME + " Support " + \
210 "<" + self.api.config.PLC_MAIL_SUPPORT_ADDRESS + ">" + \
211 " and reference " + uuid)
215 def selectall(self, query, params = None, hashref = True, key_field = None):
217 Return each row as a dictionary keyed on field name (like DBI
218 selectrow_hashref()). If key_field is specified, return rows
219 as a dictionary keyed on the specified field (like DBI
220 selectall_hashref()).
222 If params is specified, the specified parameters will be bound
226 cursor = self.execute(query, params)
227 rows = cursor.fetchall()
230 if hashref or key_field is not None:
231 # Return each row as a dictionary keyed on field name
232 # (like DBI selectrow_hashref()).
233 labels = [column[0] for column in self.description]
234 rows = [dict(zip(labels, row)) for row in rows]
236 if key_field is not None and key_field in labels:
237 # Return rows as a dictionary keyed on the specified field
238 # (like DBI selectall_hashref()).
239 return dict([(row[key_field], row) for row in rows])
243 def fields(self, table, notnull = None, hasdef = None):
245 Return the names of the fields of the specified table.
248 if hasattr(self, 'fields_cache'):
249 if self.fields_cache.has_key((table, notnull, hasdef)):
250 return self.fields_cache[(table, notnull, hasdef)]
252 self.fields_cache = {}
254 sql = "SELECT attname FROM pg_attribute, pg_class" \
255 " WHERE pg_class.oid = attrelid" \
256 " AND attnum > 0 AND relname = %(table)s"
258 if notnull is not None:
259 sql += " AND attnotnull is %(notnull)s"
261 if hasdef is not None:
262 sql += " AND atthasdef is %(hasdef)s"
264 rows = self.selectall(sql, locals(), hashref = False)
266 self.fields_cache[(table, notnull, hasdef)] = [row[0] for row in rows]
268 return self.fields_cache[(table, notnull, hasdef)]