From 0a7ca00e04f7c1cfdbb8bdfe7da5f62fc9b40930 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 16 Aug 2019 22:38:51 -0400 Subject: Revise psycopg2 execute_values approach Revised the approach for the just added support for the psycopg2 "execute_values()" feature added in 1.3.7 for :ticket:`4623`. The approach relied upon a regular expression that would fail to match for a more complex INSERT statement such as one which had subqueries involved. The new approach matches exactly the string that was rendered as the VALUES clause. Fixes: #4623 Change-Id: Icaae0f7b6bcf87a2cf5c6290a839c8429dd5fac3 --- lib/sqlalchemy/sql/compiler.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'lib/sqlalchemy/sql/compiler.py') diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 8bd2249e2..fa7eeaecf 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -498,6 +498,15 @@ class SQLCompiler(Compiled): """ + insert_single_values_expr = None + """When an INSERT is compiled with a single set of parameters inside + a VALUES expression, the string is assigned here, where it can be + used for insert batching schemes to rewrite the VALUES expression. + + .. versionadded:: 1.3.8 + + """ + insert_prefetch = update_prefetch = () def __init__( @@ -2512,7 +2521,10 @@ class SQLCompiler(Compiled): ) ) else: - text += " VALUES (%s)" % ", ".join([c[1] for c in crud_params]) + insert_single_values_expr = ", ".join([c[1] for c in crud_params]) + text += " VALUES (%s)" % insert_single_values_expr + if toplevel: + self.insert_single_values_expr = insert_single_values_expr if insert_stmt._post_values_clause is not None: post_values_clause = self.process( -- cgit v1.2.1