# Mark Huang <mlhuang@cs.princeton.edu>
# Copyright (C) 2006 The Trustees of Princeton University
#
-# $Id: PostgreSQL.py,v 1.9 2006/11/08 22:43:02 mlhuang Exp $
+# $Id$
#
import psycopg2
import psycopg2.extensions
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
+# UNICODEARRAY not exported yet
+psycopg2.extensions.register_type(psycopg2._psycopg.UNICODEARRAY)
import pgdb
from types import StringTypes, NoneType
class PostgreSQL:
def __init__(self, api):
self.api = api
+ self.debug = False
# Initialize database connection
if psycopg2:
def execute_array(self, query, param_seq):
cursor = self.cursor
try:
+ if self.debug:
+ for params in param_seq:
+ if params:
+ print >> log, query % params
+ else:
+ print >> log, query
+
# psycopg2 requires %()s format for all parameters,
# regardless of type.
if psycopg2:
Return the names of the fields of the specified table.
"""
+ if hasattr(self, 'fields_cache'):
+ if self.fields_cache.has_key((table, notnull, hasdef)):
+ return self.fields_cache[(table, notnull, hasdef)]
+ else:
+ self.fields_cache = {}
+
sql = "SELECT attname FROM pg_attribute, pg_class" \
" WHERE pg_class.oid = attrelid" \
" AND attnum > 0 AND relname = %(table)s"
rows = self.selectall(sql, locals(), hashref = False)
- return [row[0] for row in rows]
+ self.fields_cache[(table, notnull, hasdef)] = [row[0] for row in rows]
+
+ return self.fields_cache[(table, notnull, hasdef)]