Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
  • Loading branch information...
commit 5ce050a5f5beab38e89b1e6a9c8b1c946252cd7f 1 parent 5a64264
@adrianholovaty adrianholovaty authored
View
10 django/db/backends/__init__.py
@@ -99,3 +99,13 @@ def fulltext_search_sql(self, field_name):
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
View
8 django/db/backends/ado_mssql/base.py
@@ -63,6 +63,10 @@ def date_trunc_sql(self, lookup_type, field_name):
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 @@ def quote_name(name):
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
View
1  django/db/backends/dummy/base.py
@@ -43,7 +43,6 @@ def close(self):
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
View
3  django/db/backends/mysql/base.py
@@ -155,9 +155,6 @@ def quote_name(name):
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:
View
3  django/db/backends/mysql_old/base.py
@@ -174,9 +174,6 @@ def quote_name(name):
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:
View
10 django/db/backends/oracle/base.py
@@ -57,6 +57,11 @@ def datetime_cast_sql(self):
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 @@ def quote_name(name):
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)"
View
8 django/db/backends/postgresql/base.py
@@ -69,6 +69,10 @@ def date_trunc_sql(self, lookup_type, field_name):
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:
View
8 django/db/backends/postgresql_psycopg2/base.py
@@ -31,6 +31,10 @@ def date_trunc_sql(self, lookup_type, field_name):
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 @@ def quote_name(name):
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:
View
3  django/db/backends/sqlite3/base.py
@@ -108,9 +108,6 @@ def quote_name(name):
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)
View
2  django/db/models/base.py
@@ -254,7 +254,7 @@ def save(self, raw=False):
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.
Please sign in to comment.
Something went wrong with that request. Please try again.