dabo Commit
Revision 3177
Date: 2007-06-14 10:37:17 -0700 (Thu, 14 Jun 2007)
Author: Johnf
Trac: http://svn.dabodev.com/trac/dabo/changeset/3177

Changed:
U   trunk/dabo/db/dbPostgreSQL.py

Log:
Added routine ( getLastInsertID) to support getting the current value for the 
PK.  Thanks to Larry Long for pointing out that saveAll() was not working and 
Ed Leafe for pointing to the right method.

Diff:
Modified: trunk/dabo/db/dbPostgreSQL.py
===================================================================
--- trunk/dabo/db/dbPostgreSQL.py       2007-06-13 16:30:34 UTC (rev 3176)
+++ trunk/dabo/db/dbPostgreSQL.py       2007-06-14 17:37:17 UTC (rev 3177)
@@ -222,3 +222,49 @@
                to the database written to disk.
                """
                self.commitTransaction(cursor)
+               
+       def getLastInsertID(self, cursor):
+               """ Return the ID of the last inserted row, or None.
+               
+               When inserting a new record in a table that auto-generates a PK 
(such 
+               as a serial data type) value, different databases have their 
own way of retrieving that value.
+               With Postgres a sequence is created.  The SQL statement 
determines the sequence name 
+               ('table_pkid_seq') and needs three parameters the schema name, 
table name, and the primary
+               key field for the table.
+               cursor.KeyField = primary field
+               cursor.Table = returns 'schema.table' for the cursor
+               
+               Postgres uses 'currval(sequence_name)' to determine the last 
value of the session.
+               If two different sessions are open (two users accessing the 
same table for example)
+               currval() will return the correct value for each session.
+               
+               """
+               tableNameBreak=cursor.Table.split('.',1)
+               localSchemaName = tableNameBreak[0]
+               localTableName = tableNameBreak[1]
+               
+               tempCursor = self._connection.cursor()
+               sqltablestr = """SELECT seq.relname::text
+               FROM pg_class src, pg_class seq, pg_namespace, pg_attribute,
+               pg_depend
+               WHERE
+               pg_depend.refobjsubid = pg_attribute.attnum AND
+               pg_depend.refobjid = src.oid AND
+               seq.oid = pg_depend.objid AND
+               src.relnamespace = pg_namespace.oid AND
+               pg_attribute.attrelid = src.oid AND
+               pg_namespace.nspname = '%s' AND
+               src.relname = '%s' AND
+               pg_attribute.attname = '%s'""" % 
(localSchemaName,localTableName,cursor.KeyField)
+               
+               
+               tempCursor.execute(sqltablestr)
+               rs = tempCursor.fetchall()
+               sqlWithseq_name="""select currval('%s') as curval""" % 
(rs[0][0],)
+               tempCursor.execute(sqlWithseq_name) 
+               rs = tempCursor.fetchall()
+               if rs[0][0]:
+                       return rs[0][0]
+               else:
+                       raise AttributeError, "Unable to determine the sequence 
used or the sequence return a strange value."
+




_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev
Searchable Archives: http://leafe.com/archives/search/dabo-dev
This message: http://leafe.com/archives/byMID/dabo-dev/[EMAIL PROTECTED]

Reply via email to