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
This commit is contained in:
parent
5a6426448f
commit
5ce050a5f5
|
@ -99,3 +99,13 @@ class BaseDatabaseOperations(object):
|
||||||
contain a '%s' placeholder for the value being searched against.
|
contain a '%s' placeholder for the value being searched against.
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError('Full-text search is not implemented for this database backend')
|
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
|
||||||
|
|
|
@ -63,6 +63,10 @@ class DatabaseOperations(BaseDatabaseOperations):
|
||||||
def deferrable_sql(self):
|
def deferrable_sql(self):
|
||||||
return " DEFERRABLE INITIALLY DEFERRED"
|
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):
|
class DatabaseWrapper(BaseDatabaseWrapper):
|
||||||
ops = DatabaseOperations()
|
ops = DatabaseOperations()
|
||||||
|
|
||||||
|
@ -96,10 +100,6 @@ dictfetchone = util.dictfetchone
|
||||||
dictfetchmany = util.dictfetchmany
|
dictfetchmany = util.dictfetchmany
|
||||||
dictfetchall = util.dictfetchall
|
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):
|
def get_limit_offset_sql(limit, offset=None):
|
||||||
# TODO: This is a guess. Make sure this is correct.
|
# TODO: This is a guess. Make sure this is correct.
|
||||||
sql = "LIMIT %s" % limit
|
sql = "LIMIT %s" % limit
|
||||||
|
|
|
@ -43,7 +43,6 @@ quote_name = complain
|
||||||
dictfetchone = complain
|
dictfetchone = complain
|
||||||
dictfetchmany = complain
|
dictfetchmany = complain
|
||||||
dictfetchall = complain
|
dictfetchall = complain
|
||||||
get_last_insert_id = complain
|
|
||||||
get_limit_offset_sql = complain
|
get_limit_offset_sql = complain
|
||||||
get_random_function_sql = complain
|
get_random_function_sql = complain
|
||||||
get_pk_default_value = complain
|
get_pk_default_value = complain
|
||||||
|
|
|
@ -155,9 +155,6 @@ dictfetchone = util.dictfetchone
|
||||||
dictfetchmany = util.dictfetchmany
|
dictfetchmany = util.dictfetchmany
|
||||||
dictfetchall = util.dictfetchall
|
dictfetchall = util.dictfetchall
|
||||||
|
|
||||||
def get_last_insert_id(cursor, table_name, pk_name):
|
|
||||||
return cursor.lastrowid
|
|
||||||
|
|
||||||
def get_limit_offset_sql(limit, offset=None):
|
def get_limit_offset_sql(limit, offset=None):
|
||||||
sql = "LIMIT "
|
sql = "LIMIT "
|
||||||
if offset and offset != 0:
|
if offset and offset != 0:
|
||||||
|
|
|
@ -174,9 +174,6 @@ dictfetchone = util.dictfetchone
|
||||||
dictfetchmany = util.dictfetchmany
|
dictfetchmany = util.dictfetchmany
|
||||||
dictfetchall = util.dictfetchall
|
dictfetchall = util.dictfetchall
|
||||||
|
|
||||||
def get_last_insert_id(cursor, table_name, pk_name):
|
|
||||||
return cursor.lastrowid
|
|
||||||
|
|
||||||
def get_limit_offset_sql(limit, offset=None):
|
def get_limit_offset_sql(limit, offset=None):
|
||||||
sql = "LIMIT "
|
sql = "LIMIT "
|
||||||
if offset and offset != 0:
|
if offset and offset != 0:
|
||||||
|
|
|
@ -57,6 +57,11 @@ class DatabaseOperations(BaseDatabaseOperations):
|
||||||
def deferrable_sql(self):
|
def deferrable_sql(self):
|
||||||
return " DEFERRABLE INITIALLY DEFERRED"
|
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):
|
class DatabaseWrapper(BaseDatabaseWrapper):
|
||||||
ops = DatabaseOperations()
|
ops = DatabaseOperations()
|
||||||
|
|
||||||
|
@ -167,11 +172,6 @@ dictfetchone = util.dictfetchone
|
||||||
dictfetchmany = util.dictfetchmany
|
dictfetchmany = util.dictfetchmany
|
||||||
dictfetchall = util.dictfetchall
|
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):
|
def get_field_cast_sql(db_type):
|
||||||
if db_type.endswith('LOB'):
|
if db_type.endswith('LOB'):
|
||||||
return "DBMS_LOB.SUBSTR(%s%s)"
|
return "DBMS_LOB.SUBSTR(%s%s)"
|
||||||
|
|
|
@ -69,6 +69,10 @@ class DatabaseOperations(BaseDatabaseOperations):
|
||||||
def deferrable_sql(self):
|
def deferrable_sql(self):
|
||||||
return " DEFERRABLE INITIALLY DEFERRED"
|
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):
|
class DatabaseWrapper(BaseDatabaseWrapper):
|
||||||
ops = DatabaseOperations()
|
ops = DatabaseOperations()
|
||||||
|
|
||||||
|
@ -127,10 +131,6 @@ def dictfetchall(cursor):
|
||||||
"Returns all rows from a cursor as a dict"
|
"Returns all rows from a cursor as a dict"
|
||||||
return cursor.dictfetchall()
|
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):
|
def get_limit_offset_sql(limit, offset=None):
|
||||||
sql = "LIMIT %s" % limit
|
sql = "LIMIT %s" % limit
|
||||||
if offset and offset != 0:
|
if offset and offset != 0:
|
||||||
|
|
|
@ -31,6 +31,10 @@ class DatabaseOperations(BaseDatabaseOperations):
|
||||||
def deferrable_sql(self):
|
def deferrable_sql(self):
|
||||||
return " DEFERRABLE INITIALLY DEFERRED"
|
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):
|
class DatabaseWrapper(BaseDatabaseWrapper):
|
||||||
ops = DatabaseOperations()
|
ops = DatabaseOperations()
|
||||||
|
|
||||||
|
@ -81,10 +85,6 @@ dictfetchone = util.dictfetchone
|
||||||
dictfetchmany = util.dictfetchmany
|
dictfetchmany = util.dictfetchmany
|
||||||
dictfetchall = util.dictfetchall
|
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):
|
def get_limit_offset_sql(limit, offset=None):
|
||||||
sql = "LIMIT %s" % limit
|
sql = "LIMIT %s" % limit
|
||||||
if offset and offset != 0:
|
if offset and offset != 0:
|
||||||
|
|
|
@ -108,9 +108,6 @@ dictfetchone = util.dictfetchone
|
||||||
dictfetchmany = util.dictfetchmany
|
dictfetchmany = util.dictfetchmany
|
||||||
dictfetchall = util.dictfetchall
|
dictfetchall = util.dictfetchall
|
||||||
|
|
||||||
def get_last_insert_id(cursor, table_name, pk_name):
|
|
||||||
return cursor.lastrowid
|
|
||||||
|
|
||||||
def _sqlite_extract(lookup_type, dt):
|
def _sqlite_extract(lookup_type, dt):
|
||||||
try:
|
try:
|
||||||
dt = util.typecast_timestamp(dt)
|
dt = util.typecast_timestamp(dt)
|
||||||
|
|
|
@ -254,7 +254,7 @@ class Model(object):
|
||||||
backend.quote_name(self._meta.pk.column),
|
backend.quote_name(self._meta.pk.column),
|
||||||
backend.get_pk_default_value()))
|
backend.get_pk_default_value()))
|
||||||
if self._meta.has_auto_field and not pk_set:
|
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()
|
transaction.commit_unless_managed()
|
||||||
|
|
||||||
# Run any post-save hooks.
|
# Run any post-save hooks.
|
||||||
|
|
Loading…
Reference in New Issue