mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	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:
		| @@ -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 | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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: | ||||
|   | ||||
| @@ -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: | ||||
|   | ||||
| @@ -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)" | ||||
|   | ||||
| @@ -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: | ||||
|   | ||||
| @@ -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: | ||||
|   | ||||
| @@ -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) | ||||
|   | ||||
| @@ -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. | ||||
|   | ||||
		Reference in New Issue
	
	Block a user