Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Adrian Holovaty authored August 19, 2007
10  django/db/backends/__init__.py
@@ -99,3 +99,13 @@ def fulltext_search_sql(self, field_name):
99 99
         contain a '%s' placeholder for the value being searched against.
100 100
         """
101 101
         raise NotImplementedError('Full-text search is not implemented for this database backend')
  102
+
  103
+    def last_insert_id(self, cursor, table_name, pk_name):
  104
+        """
  105
+        Given a cursor object that has just performed an INSERT statement into
  106
+        a table that has an auto-incrementing ID, returns the newly created ID.
  107
+
  108
+        This method also receives the table name and the name of the primary-key
  109
+        column.
  110
+        """
  111
+        return cursor.lastrowid
8  django/db/backends/ado_mssql/base.py
@@ -63,6 +63,10 @@ def date_trunc_sql(self, lookup_type, field_name):
63 63
     def deferrable_sql(self):
64 64
         return " DEFERRABLE INITIALLY DEFERRED"
65 65
 
  66
+    def last_insert_id(self, cursor, table_name, pk_name):
  67
+        cursor.execute("SELECT %s FROM %s WHERE %s = @@IDENTITY" % (pk_name, table_name, pk_name))
  68
+        return cursor.fetchone()[0]
  69
+
66 70
 class DatabaseWrapper(BaseDatabaseWrapper):
67 71
     ops = DatabaseOperations()
68 72
 
@@ -96,10 +100,6 @@ def quote_name(name):
96 100
 dictfetchmany = util.dictfetchmany
97 101
 dictfetchall  = util.dictfetchall
98 102
 
99  
-def get_last_insert_id(cursor, table_name, pk_name):
100  
-    cursor.execute("SELECT %s FROM %s WHERE %s = @@IDENTITY" % (pk_name, table_name, pk_name))
101  
-    return cursor.fetchone()[0]
102  
-
103 103
 def get_limit_offset_sql(limit, offset=None):
104 104
     # TODO: This is a guess. Make sure this is correct.
105 105
     sql = "LIMIT %s" % limit
1  django/db/backends/dummy/base.py
@@ -43,7 +43,6 @@ def close(self):
43 43
 dictfetchone = complain
44 44
 dictfetchmany = complain
45 45
 dictfetchall = complain
46  
-get_last_insert_id = complain
47 46
 get_limit_offset_sql = complain
48 47
 get_random_function_sql = complain
49 48
 get_pk_default_value = complain
3  django/db/backends/mysql/base.py
@@ -155,9 +155,6 @@ def quote_name(name):
155 155
 dictfetchmany = util.dictfetchmany
156 156
 dictfetchall  = util.dictfetchall
157 157
 
158  
-def get_last_insert_id(cursor, table_name, pk_name):
159  
-    return cursor.lastrowid
160  
-
161 158
 def get_limit_offset_sql(limit, offset=None):
162 159
     sql = "LIMIT "
163 160
     if offset and offset != 0:
3  django/db/backends/mysql_old/base.py
@@ -174,9 +174,6 @@ def quote_name(name):
174 174
 dictfetchmany = util.dictfetchmany
175 175
 dictfetchall  = util.dictfetchall
176 176
 
177  
-def get_last_insert_id(cursor, table_name, pk_name):
178  
-    return cursor.lastrowid
179  
-
180 177
 def get_limit_offset_sql(limit, offset=None):
181 178
     sql = "LIMIT "
182 179
     if offset and offset != 0:
10  django/db/backends/oracle/base.py
@@ -57,6 +57,11 @@ def datetime_cast_sql(self):
57 57
     def deferrable_sql(self):
58 58
         return " DEFERRABLE INITIALLY DEFERRED"
59 59
 
  60
+    def last_insert_id(self, cursor, table_name, pk_name):
  61
+        sq_name = util.truncate_name(table_name, get_max_name_length()-3)
  62
+        cursor.execute('SELECT %s_sq.currval FROM dual' % sq_name)
  63
+        return cursor.fetchone()[0]
  64
+
60 65
 class DatabaseWrapper(BaseDatabaseWrapper):
61 66
     ops = DatabaseOperations()
62 67
 
@@ -167,11 +172,6 @@ def quote_name(name):
167 172
 dictfetchmany = util.dictfetchmany
168 173
 dictfetchall  = util.dictfetchall
169 174
 
170  
-def get_last_insert_id(cursor, table_name, pk_name):
171  
-    sq_name = util.truncate_name(table_name, get_max_name_length()-3)
172  
-    cursor.execute('SELECT %s_sq.currval FROM dual' % sq_name)
173  
-    return cursor.fetchone()[0]
174  
-
175 175
 def get_field_cast_sql(db_type):
176 176
     if db_type.endswith('LOB'):
177 177
         return "DBMS_LOB.SUBSTR(%s%s)"
8  django/db/backends/postgresql/base.py
@@ -69,6 +69,10 @@ def date_trunc_sql(self, lookup_type, field_name):
69 69
     def deferrable_sql(self):
70 70
         return " DEFERRABLE INITIALLY DEFERRED"
71 71
 
  72
+    def last_insert_id(self, cursor, table_name, pk_name):
  73
+        cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name))
  74
+        return cursor.fetchone()[0]
  75
+
72 76
 class DatabaseWrapper(BaseDatabaseWrapper):
73 77
     ops = DatabaseOperations()
74 78
 
@@ -127,10 +131,6 @@ def dictfetchall(cursor):
127 131
     "Returns all rows from a cursor as a dict"
128 132
     return cursor.dictfetchall()
129 133
 
130  
-def get_last_insert_id(cursor, table_name, pk_name):
131  
-    cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name))
132  
-    return cursor.fetchone()[0]
133  
-
134 134
 def get_limit_offset_sql(limit, offset=None):
135 135
     sql = "LIMIT %s" % limit
136 136
     if offset and offset != 0:
8  django/db/backends/postgresql_psycopg2/base.py
@@ -31,6 +31,10 @@ def date_trunc_sql(self, lookup_type, field_name):
31 31
     def deferrable_sql(self):
32 32
         return " DEFERRABLE INITIALLY DEFERRED"
33 33
 
  34
+    def last_insert_id(self, cursor, table_name, pk_name):
  35
+        cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name))
  36
+        return cursor.fetchone()[0]
  37
+
34 38
 class DatabaseWrapper(BaseDatabaseWrapper):
35 39
     ops = DatabaseOperations()
36 40
 
@@ -81,10 +85,6 @@ def quote_name(name):
81 85
 dictfetchmany = util.dictfetchmany
82 86
 dictfetchall = util.dictfetchall
83 87
 
84  
-def get_last_insert_id(cursor, table_name, pk_name):
85  
-    cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name))
86  
-    return cursor.fetchone()[0]
87  
-
88 88
 def get_limit_offset_sql(limit, offset=None):
89 89
     sql = "LIMIT %s" % limit
90 90
     if offset and offset != 0:
3  django/db/backends/sqlite3/base.py
@@ -108,9 +108,6 @@ def quote_name(name):
108 108
 dictfetchmany = util.dictfetchmany
109 109
 dictfetchall  = util.dictfetchall
110 110
 
111  
-def get_last_insert_id(cursor, table_name, pk_name):
112  
-    return cursor.lastrowid
113  
-
114 111
 def _sqlite_extract(lookup_type, dt):
115 112
     try:
116 113
         dt = util.typecast_timestamp(dt)
2  django/db/models/base.py
@@ -254,7 +254,7 @@ def save(self, raw=False):
254 254
                      backend.quote_name(self._meta.pk.column),
255 255
                      backend.get_pk_default_value()))
256 256
             if self._meta.has_auto_field and not pk_set:
257  
-                setattr(self, self._meta.pk.attname, backend.get_last_insert_id(cursor, self._meta.db_table, self._meta.pk.column))
  257
+                setattr(self, self._meta.pk.attname, connection.ops.last_insert_id(cursor, self._meta.db_table, self._meta.pk.column))
258 258
         transaction.commit_unless_managed()
259 259
 
260 260
         # Run any post-save hooks.

0 notes on commit 5ce050a

Please sign in to comment.
Something went wrong with that request. Please try again.