From 271a8210bbbde4edc1d4d049ce73bd1295044e80 Mon Sep 17 00:00:00 2001 From: Mark Huang Date: Tue, 3 Oct 2006 19:27:07 +0000 Subject: [PATCH] - make last_insert_id() return the primary key value of the last inserted row, like DBI - add notnull and hasdef keywords to fields() so that we know which columns must be set --- PLC/PostgreSQL.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/PLC/PostgreSQL.py b/PLC/PostgreSQL.py index 9eff5849..7fe5ad2d 100644 --- a/PLC/PostgreSQL.py +++ b/PLC/PostgreSQL.py @@ -5,7 +5,7 @@ # Mark Huang # Copyright (C) 2006 The Trustees of Princeton University # -# $Id: PostgreSQL.py,v 1.1 2006/09/06 15:36:07 mlhuang Exp $ +# $Id: PostgreSQL.py,v 1.2 2006/09/25 15:12:20 mlhuang Exp $ # import pgdb @@ -80,8 +80,15 @@ class PostgreSQL: self.execute(query, params) return self.rowcount - def last_insert_id(self): - return self.lastrowid + def last_insert_id(self, table_name, primary_key): + if isinstance(self.lastrowid, int): + sql = "SELECT %s FROM %s WHERE oid = %d" % \ + (primary_key, table_name, self.lastrowid) + rows = self.selectall(sql, hashref = False) + if rows: + return rows[0][0] + + return None def execute(self, query, params = None): self.execute_array(query, (params,)) @@ -134,15 +141,21 @@ class PostgreSQL: else: return rows - def fields(self, table): + def fields(self, table, notnull = None, hasdef = None): """ Return the names of the fields of the specified table. """ - rows = self.selectall("SELECT attname FROM pg_attribute, pg_class" \ - " WHERE pg_class.oid = attrelid" \ - " AND attnum > 0 AND relname = %(table)s", - locals(), - hashref = False) + sql = "SELECT attname FROM pg_attribute, pg_class" \ + " WHERE pg_class.oid = attrelid" \ + " AND attnum > 0 AND relname = %(table)s" + + if notnull is not None: + sql += " AND attnotnull is %(notnull)s" + + if hasdef is not None: + sql += " AND atthasdef is %(hasdef)s" + + rows = self.selectall(sql, locals(), hashref = False) return [row[0] for row in rows] -- 2.47.0