From 912f72a6f057bb39f63d66c7fcf0eab7bf28c7b5 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Thu, 14 Mar 2024 05:29:49 +0100 Subject: [PATCH] Refs #35295 -- Added BaseDatabaseOperations.bulk_insert_sql(). Co-authored-by: Nick Pope --- django/db/backends/base/operations.py | 5 +++++ django/db/backends/mysql/operations.py | 5 ----- django/db/backends/postgresql/operations.py | 5 ----- django/db/backends/sqlite3/operations.py | 5 ----- 4 files changed, 5 insertions(+), 15 deletions(-) diff --git a/django/db/backends/base/operations.py b/django/db/backends/base/operations.py index 9f40ec5e4f..889e4d87b4 100644 --- a/django/db/backends/base/operations.py +++ b/django/db/backends/base/operations.py @@ -276,6 +276,11 @@ class BaseDatabaseOperations: if sql ) + def bulk_insert_sql(self, fields, placeholder_rows): + placeholder_rows_sql = (", ".join(row) for row in placeholder_rows) + values_sql = ", ".join([f"({sql})" for sql in placeholder_rows_sql]) + return f"VALUES {values_sql}" + def last_executed_query(self, cursor, sql, params): """ Return a string of the query last executed by the given cursor, with diff --git a/django/db/backends/mysql/operations.py b/django/db/backends/mysql/operations.py index 7fabb0e05e..9741e6a985 100644 --- a/django/db/backends/mysql/operations.py +++ b/django/db/backends/mysql/operations.py @@ -290,11 +290,6 @@ class DatabaseOperations(BaseDatabaseOperations): def pk_default_value(self): return "NULL" - def bulk_insert_sql(self, fields, placeholder_rows): - placeholder_rows_sql = (", ".join(row) for row in placeholder_rows) - values_sql = ", ".join("(%s)" % sql for sql in placeholder_rows_sql) - return "VALUES " + values_sql - def combine_expression(self, connector, sub_expressions): if connector == "^": return "POW(%s)" % ",".join(sub_expressions) diff --git a/django/db/backends/postgresql/operations.py b/django/db/backends/postgresql/operations.py index 4e444d5f2b..4b179ca83f 100644 --- a/django/db/backends/postgresql/operations.py +++ b/django/db/backends/postgresql/operations.py @@ -328,11 +328,6 @@ class DatabaseOperations(BaseDatabaseOperations): ] return "RETURNING %s" % ", ".join(columns), () - def bulk_insert_sql(self, fields, placeholder_rows): - placeholder_rows_sql = (", ".join(row) for row in placeholder_rows) - values_sql = ", ".join("(%s)" % sql for sql in placeholder_rows_sql) - return "VALUES " + values_sql - if is_psycopg3: def adapt_integerfield_value(self, value, internal_type): diff --git a/django/db/backends/sqlite3/operations.py b/django/db/backends/sqlite3/operations.py index 29a5c0391e..0078cc077a 100644 --- a/django/db/backends/sqlite3/operations.py +++ b/django/db/backends/sqlite3/operations.py @@ -353,11 +353,6 @@ class DatabaseOperations(BaseDatabaseOperations): def convert_booleanfield_value(self, value, expression, connection): return bool(value) if value in (1, 0) else value - def bulk_insert_sql(self, fields, placeholder_rows): - placeholder_rows_sql = (", ".join(row) for row in placeholder_rows) - values_sql = ", ".join(f"({sql})" for sql in placeholder_rows_sql) - return f"VALUES {values_sql}" - def combine_expression(self, connector, sub_expressions): # SQLite doesn't have a ^ operator, so use the user-defined POWER # function that's registered in connect().