Skip to content

Commit dc4ee99

Browse files
charettesfelixxm
authored andcommitted
Refs #27222 -- Implemented BaseDatabaseOperations.return_insert_columns()/fetch_returned_insert_rows().
1 parent 41ff30f commit dc4ee99

4 files changed

Lines changed: 18 additions & 63 deletions

File tree

django/db/backends/base/operations.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,24 @@ def return_insert_columns(self, fields):
364364
return the SQL and params to append to the INSERT query. The returned
365365
fragment should contain a format string to hold the appropriate column.
366366
"""
367-
pass
367+
if not fields:
368+
return "", ()
369+
columns = [
370+
"%s.%s"
371+
% (
372+
self.quote_name(field.model._meta.db_table),
373+
self.quote_name(field.column),
374+
)
375+
for field in fields
376+
]
377+
return "RETURNING %s" % ", ".join(columns), ()
378+
379+
def fetch_returned_insert_rows(self, cursor):
380+
"""
381+
Given a cursor object that has just performed an INSERT...RETURNING
382+
statement into a table, return the tuple of returned data.
383+
"""
384+
return cursor.fetchall()
368385

369386
def compiler(self, compiler_name):
370387
"""

django/db/backends/mysql/operations.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,6 @@ def time_trunc_sql(self, lookup_type, sql, params, tzname=None):
148148
else:
149149
return f"TIME({sql})", params
150150

151-
def fetch_returned_insert_rows(self, cursor):
152-
"""
153-
Given a cursor object that has just performed an INSERT...RETURNING
154-
statement into a table, return the tuple of returned data.
155-
"""
156-
return cursor.fetchall()
157-
158151
def format_for_duration_arithmetic(self, sql):
159152
return "INTERVAL %s MICROSECOND" % sql
160153

@@ -182,20 +175,6 @@ def quote_name(self, name):
182175
return name # Quoting once is enough.
183176
return "`%s`" % name
184177

185-
def return_insert_columns(self, fields):
186-
# MySQL doesn't support an INSERT...RETURNING statement.
187-
if not fields:
188-
return "", ()
189-
columns = [
190-
"%s.%s"
191-
% (
192-
self.quote_name(field.model._meta.db_table),
193-
self.quote_name(field.column),
194-
)
195-
for field in fields
196-
]
197-
return "RETURNING %s" % ", ".join(columns), ()
198-
199178
def sql_flush(self, style, tables, *, reset_sequences=False, allow_cascade=False):
200179
if not tables:
201180
return []

django/db/backends/postgresql/operations.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,6 @@ def bulk_insert_sql(self, fields, placeholder_rows):
155155
return f"SELECT * FROM {placeholder_rows}"
156156
return super().bulk_insert_sql(fields, placeholder_rows)
157157

158-
def fetch_returned_insert_rows(self, cursor):
159-
"""
160-
Given a cursor object that has just performed an INSERT...RETURNING
161-
statement into a table, return the tuple of returned data.
162-
"""
163-
return cursor.fetchall()
164-
165158
def lookup_cast(self, lookup_type, internal_type=None):
166159
lookup = "%s"
167160
# Cast text lookups to text to allow things like filter(x__contains=4)
@@ -324,19 +317,6 @@ def last_executed_query(self, cursor, sql, params):
324317
return cursor.query.decode()
325318
return None
326319

327-
def return_insert_columns(self, fields):
328-
if not fields:
329-
return "", ()
330-
columns = [
331-
"%s.%s"
332-
% (
333-
self.quote_name(field.model._meta.db_table),
334-
self.quote_name(field.column),
335-
)
336-
for field in fields
337-
]
338-
return "RETURNING %s" % ", ".join(columns), ()
339-
340320
if is_psycopg3:
341321

342322
def adapt_integerfield_value(self, value, internal_type):

django/db/backends/sqlite3/operations.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,6 @@ def date_extract_sql(self, lookup_type, sql, params):
8484
"""
8585
return f"django_date_extract(%s, {sql})", (lookup_type.lower(), *params)
8686

87-
def fetch_returned_insert_rows(self, cursor):
88-
"""
89-
Given a cursor object that has just performed an INSERT...RETURNING
90-
statement into a table, return the list of returned data.
91-
"""
92-
return cursor.fetchall()
93-
9487
def format_for_duration_arithmetic(self, sql):
9588
"""Do nothing since formatting is handled in the custom function."""
9689
return sql
@@ -399,20 +392,6 @@ def insert_statement(self, on_conflict=None):
399392
return "INSERT OR IGNORE INTO"
400393
return super().insert_statement(on_conflict=on_conflict)
401394

402-
def return_insert_columns(self, fields):
403-
# SQLite < 3.35 doesn't support an INSERT...RETURNING statement.
404-
if not fields:
405-
return "", ()
406-
columns = [
407-
"%s.%s"
408-
% (
409-
self.quote_name(field.model._meta.db_table),
410-
self.quote_name(field.column),
411-
)
412-
for field in fields
413-
]
414-
return "RETURNING %s" % ", ".join(columns), ()
415-
416395
def on_conflict_suffix_sql(self, fields, on_conflict, update_fields, unique_fields):
417396
if (
418397
on_conflict == OnConflict.UPDATE

0 commit comments

Comments
 (0)