From 5ce050a5f5beab38e89b1e6a9c8b1c946252cd7f Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Sun, 19 Aug 2007 23:18:43 +0000 Subject: [PATCH] Refactored get_last_insert_id() to DatabaseOperations.last_insert_id(). Refs #5106 git-svn-id: http://code.djangoproject.com/svn/django/trunk@5958 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/backends/__init__.py | 10 ++++++++++ django/db/backends/ado_mssql/base.py | 8 ++++---- django/db/backends/dummy/base.py | 1 - django/db/backends/mysql/base.py | 3 --- django/db/backends/mysql_old/base.py | 3 --- django/db/backends/oracle/base.py | 10 +++++----- django/db/backends/postgresql/base.py | 8 ++++---- django/db/backends/postgresql_psycopg2/base.py | 8 ++++---- django/db/backends/sqlite3/base.py | 3 --- django/db/models/base.py | 2 +- 10 files changed, 28 insertions(+), 28 deletions(-) diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index 8113f6f877..3f012eadff 100644 --- a/django/db/backends/__init__.py +++ b/django/db/backends/__init__.py @@ -99,3 +99,13 @@ class BaseDatabaseOperations(object): contain a '%s' placeholder for the value being searched against. """ raise NotImplementedError('Full-text search is not implemented for this database backend') + + def last_insert_id(self, cursor, table_name, pk_name): + """ + Given a cursor object that has just performed an INSERT statement into + a table that has an auto-incrementing ID, returns the newly created ID. + + This method also receives the table name and the name of the primary-key + column. + """ + return cursor.lastrowid diff --git a/django/db/backends/ado_mssql/base.py b/django/db/backends/ado_mssql/base.py index 6c9b50bd4b..02795fd0c1 100644 --- a/django/db/backends/ado_mssql/base.py +++ b/django/db/backends/ado_mssql/base.py @@ -63,6 +63,10 @@ class DatabaseOperations(BaseDatabaseOperations): def deferrable_sql(self): return " DEFERRABLE INITIALLY DEFERRED" + def last_insert_id(self, cursor, table_name, pk_name): + cursor.execute("SELECT %s FROM %s WHERE %s = @@IDENTITY" % (pk_name, table_name, pk_name)) + return cursor.fetchone()[0] + class DatabaseWrapper(BaseDatabaseWrapper): ops = DatabaseOperations() @@ -96,10 +100,6 @@ dictfetchone = util.dictfetchone dictfetchmany = util.dictfetchmany dictfetchall = util.dictfetchall -def get_last_insert_id(cursor, table_name, pk_name): - cursor.execute("SELECT %s FROM %s WHERE %s = @@IDENTITY" % (pk_name, table_name, pk_name)) - return cursor.fetchone()[0] - def get_limit_offset_sql(limit, offset=None): # TODO: This is a guess. Make sure this is correct. sql = "LIMIT %s" % limit diff --git a/django/db/backends/dummy/base.py b/django/db/backends/dummy/base.py index 9e49840f9e..e6547fa765 100644 --- a/django/db/backends/dummy/base.py +++ b/django/db/backends/dummy/base.py @@ -43,7 +43,6 @@ quote_name = complain dictfetchone = complain dictfetchmany = complain dictfetchall = complain -get_last_insert_id = complain get_limit_offset_sql = complain get_random_function_sql = complain get_pk_default_value = complain diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py index 900b995c1b..a9ca8e046a 100644 --- a/django/db/backends/mysql/base.py +++ b/django/db/backends/mysql/base.py @@ -155,9 +155,6 @@ dictfetchone = util.dictfetchone dictfetchmany = util.dictfetchmany dictfetchall = util.dictfetchall -def get_last_insert_id(cursor, table_name, pk_name): - return cursor.lastrowid - def get_limit_offset_sql(limit, offset=None): sql = "LIMIT " if offset and offset != 0: diff --git a/django/db/backends/mysql_old/base.py b/django/db/backends/mysql_old/base.py index 519670ad74..8497cc5983 100644 --- a/django/db/backends/mysql_old/base.py +++ b/django/db/backends/mysql_old/base.py @@ -174,9 +174,6 @@ dictfetchone = util.dictfetchone dictfetchmany = util.dictfetchmany dictfetchall = util.dictfetchall -def get_last_insert_id(cursor, table_name, pk_name): - return cursor.lastrowid - def get_limit_offset_sql(limit, offset=None): sql = "LIMIT " if offset and offset != 0: diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py index 28b798fa2d..f35586cbbc 100644 --- a/django/db/backends/oracle/base.py +++ b/django/db/backends/oracle/base.py @@ -57,6 +57,11 @@ class DatabaseOperations(BaseDatabaseOperations): def deferrable_sql(self): return " DEFERRABLE INITIALLY DEFERRED" + def last_insert_id(self, cursor, table_name, pk_name): + sq_name = util.truncate_name(table_name, get_max_name_length()-3) + cursor.execute('SELECT %s_sq.currval FROM dual' % sq_name) + return cursor.fetchone()[0] + class DatabaseWrapper(BaseDatabaseWrapper): ops = DatabaseOperations() @@ -167,11 +172,6 @@ dictfetchone = util.dictfetchone dictfetchmany = util.dictfetchmany dictfetchall = util.dictfetchall -def get_last_insert_id(cursor, table_name, pk_name): - sq_name = util.truncate_name(table_name, get_max_name_length()-3) - cursor.execute('SELECT %s_sq.currval FROM dual' % sq_name) - return cursor.fetchone()[0] - def get_field_cast_sql(db_type): if db_type.endswith('LOB'): return "DBMS_LOB.SUBSTR(%s%s)" diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py index cb83200776..1642a51d7e 100644 --- a/django/db/backends/postgresql/base.py +++ b/django/db/backends/postgresql/base.py @@ -69,6 +69,10 @@ class DatabaseOperations(BaseDatabaseOperations): def deferrable_sql(self): return " DEFERRABLE INITIALLY DEFERRED" + def last_insert_id(self, cursor, table_name, pk_name): + cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name)) + return cursor.fetchone()[0] + class DatabaseWrapper(BaseDatabaseWrapper): ops = DatabaseOperations() @@ -127,10 +131,6 @@ def dictfetchall(cursor): "Returns all rows from a cursor as a dict" return cursor.dictfetchall() -def get_last_insert_id(cursor, table_name, pk_name): - cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name)) - return cursor.fetchone()[0] - def get_limit_offset_sql(limit, offset=None): sql = "LIMIT %s" % limit if offset and offset != 0: diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py index d60210b772..9ba5c8a170 100644 --- a/django/db/backends/postgresql_psycopg2/base.py +++ b/django/db/backends/postgresql_psycopg2/base.py @@ -31,6 +31,10 @@ class DatabaseOperations(BaseDatabaseOperations): def deferrable_sql(self): return " DEFERRABLE INITIALLY DEFERRED" + def last_insert_id(self, cursor, table_name, pk_name): + cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name)) + return cursor.fetchone()[0] + class DatabaseWrapper(BaseDatabaseWrapper): ops = DatabaseOperations() @@ -81,10 +85,6 @@ dictfetchone = util.dictfetchone dictfetchmany = util.dictfetchmany dictfetchall = util.dictfetchall -def get_last_insert_id(cursor, table_name, pk_name): - cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name)) - return cursor.fetchone()[0] - def get_limit_offset_sql(limit, offset=None): sql = "LIMIT %s" % limit if offset and offset != 0: diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py index 51ef11d346..b1d7dc8db1 100644 --- a/django/db/backends/sqlite3/base.py +++ b/django/db/backends/sqlite3/base.py @@ -108,9 +108,6 @@ dictfetchone = util.dictfetchone dictfetchmany = util.dictfetchmany dictfetchall = util.dictfetchall -def get_last_insert_id(cursor, table_name, pk_name): - return cursor.lastrowid - def _sqlite_extract(lookup_type, dt): try: dt = util.typecast_timestamp(dt) diff --git a/django/db/models/base.py b/django/db/models/base.py index 1a0de4c827..3ae3c26fb3 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -254,7 +254,7 @@ class Model(object): backend.quote_name(self._meta.pk.column), backend.get_pk_default_value())) if self._meta.has_auto_field and not pk_set: - setattr(self, self._meta.pk.attname, backend.get_last_insert_id(cursor, self._meta.db_table, self._meta.pk.column)) + setattr(self, self._meta.pk.attname, connection.ops.last_insert_id(cursor, self._meta.db_table, self._meta.pk.column)) transaction.commit_unless_managed() # Run any post-save hooks.