Skip to content

Commit 36b12a7

Browse files
fix: fix execute insert for homogeneous statement (#233)
1 parent 539f145 commit 36b12a7

File tree

3 files changed

+42
-41
lines changed

3 files changed

+42
-41
lines changed

google/cloud/spanner_dbapi/parse_utils.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -306,19 +306,15 @@ def parse_insert(insert_sql, params):
306306
# Case c)
307307

308308
columns = [mi.strip(" `") for mi in match.group("columns").split(",")]
309-
sql_params_list = []
310-
insert_sql_preamble = "INSERT INTO %s (%s) VALUES %s" % (
311-
match.group("table_name"),
312-
match.group("columns"),
313-
values.argv[0],
314-
)
315309
values_pyformat = [str(arg) for arg in values.argv]
316310
rows_list = rows_for_insert_or_update(columns, params, values_pyformat)
317-
insert_sql_preamble = sanitize_literals_for_upload(insert_sql_preamble)
318-
for row in rows_list:
319-
sql_params_list.append((insert_sql_preamble, row))
320311

321-
return {"sql_params_list": sql_params_list}
312+
return {
313+
"homogenous": True,
314+
"table": match.group("table_name"),
315+
"columns": columns,
316+
"values": rows_list,
317+
}
322318

323319
# Case d)
324320
# insert_sql is of the form:

tests/unit/spanner_dbapi/test_connection.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,25 @@ def test_run_statement_dont_remember_retried_statements(self):
379379

380380
self.assertEqual(len(connection._statements), 0)
381381

382+
def test_run_statement_w_homogeneous_insert_statements(self):
383+
"""Check that Connection executed homogeneous insert statements."""
384+
from google.cloud.spanner_dbapi.checksum import ResultsChecksum
385+
from google.cloud.spanner_dbapi.cursor import Statement
386+
387+
sql = "INSERT INTO T (f1, f2) VALUES (%s, %s), (%s, %s)"
388+
params = ["a", "b", "c", "d"]
389+
param_types = {"f1": str, "f2": str}
390+
391+
connection = self._make_connection()
392+
393+
statement = Statement(sql, params, param_types, ResultsChecksum(), True)
394+
with mock.patch(
395+
"google.cloud.spanner_dbapi.connection.Connection.transaction_checkout"
396+
):
397+
connection.run_statement(statement, retried=True)
398+
399+
self.assertEqual(len(connection._statements), 0)
400+
382401
def test_clear_statements_on_commit(self):
383402
"""
384403
Check that all the saved statements are

tests/unit/spanner_dbapi/test_parse_utils.py

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -72,32 +72,20 @@ def test_parse_insert(self):
7272
"INSERT INTO django_migrations (app, name, applied) VALUES (%s, %s, %s)",
7373
[1, 2, 3, 4, 5, 6],
7474
{
75-
"sql_params_list": [
76-
(
77-
"INSERT INTO django_migrations (app, name, applied) VALUES (%s, %s, %s)",
78-
(1, 2, 3),
79-
),
80-
(
81-
"INSERT INTO django_migrations (app, name, applied) VALUES (%s, %s, %s)",
82-
(4, 5, 6),
83-
),
84-
]
75+
"homogenous": True,
76+
"table": "django_migrations",
77+
"columns": ["app", "name", "applied"],
78+
"values": [(1, 2, 3), (4, 5, 6)],
8579
},
8680
),
8781
(
8882
"INSERT INTO django_migrations(app, name, applied) VALUES (%s, %s, %s)",
8983
[1, 2, 3, 4, 5, 6],
9084
{
91-
"sql_params_list": [
92-
(
93-
"INSERT INTO django_migrations (app, name, applied) VALUES (%s, %s, %s)",
94-
(1, 2, 3),
95-
),
96-
(
97-
"INSERT INTO django_migrations (app, name, applied) VALUES (%s, %s, %s)",
98-
(4, 5, 6),
99-
),
100-
]
85+
"homogenous": True,
86+
"table": "django_migrations",
87+
"columns": ["app", "name", "applied"],
88+
"values": [(1, 2, 3), (4, 5, 6)],
10189
},
10290
),
10391
(
@@ -118,25 +106,23 @@ def test_parse_insert(self):
118106
),
119107
(
120108
"INSERT INTO ap (n, ct, cn) "
121-
"VALUES (%s, %s, %s), (%s, %s, %s), (%s, %s, %s),(%s, %s, %s)",
109+
"VALUES (%s, %s, %s), (%s, %s, %s), (%s, %s, %s),(%s,%s, %s)",
122110
(1, 2, 3, 4, 5, 6, 7, 8, 9),
123111
{
124-
"sql_params_list": [
125-
("INSERT INTO ap (n, ct, cn) VALUES (%s, %s, %s)", (1, 2, 3)),
126-
("INSERT INTO ap (n, ct, cn) VALUES (%s, %s, %s)", (4, 5, 6)),
127-
("INSERT INTO ap (n, ct, cn) VALUES (%s, %s, %s)", (7, 8, 9)),
128-
]
112+
"homogenous": True,
113+
"table": "ap",
114+
"columns": ["n", "ct", "cn"],
115+
"values": [(1, 2, 3), (4, 5, 6), (7, 8, 9)],
129116
},
130117
),
131118
(
132119
"INSERT INTO `no` (`yes`) VALUES (%s)",
133120
(1, 4, 5),
134121
{
135-
"sql_params_list": [
136-
("INSERT INTO `no` (`yes`) VALUES (%s)", (1,)),
137-
("INSERT INTO `no` (`yes`) VALUES (%s)", (4,)),
138-
("INSERT INTO `no` (`yes`) VALUES (%s)", (5,)),
139-
]
122+
"homogenous": True,
123+
"table": "`no`",
124+
"columns": ["yes"],
125+
"values": [(1,), (4,), (5,)],
140126
},
141127
),
142128
(

0 commit comments

Comments
 (0)