Skip to content
Permalink
Browse files
fix: support INSERT from SELECT clause with args (#306)
* fix: support INSERT from SELECT clause with args

* the fix itself

* update docstrings
  • Loading branch information
Ilya Gurov committed Apr 15, 2021
1 parent 83adb1e commit 0dcda5e21f8fb30ee611fddf0829684d86ced0ef
Showing with 19 additions and 3 deletions.
  1. +3 −3 google/cloud/spanner_dbapi/parse_utils.py
  2. +16 −0 tests/unit/spanner_dbapi/test_parse_utils.py
@@ -224,11 +224,11 @@ def parse_insert(insert_sql, params):
}
Case b)
SQL: 'INSERT INTO T (s, c) SELECT st, zc FROM cus ORDER BY fn, ln',
SQL: 'INSERT INTO T (s, c) SELECT st, zc FROM cus WHERE col IN (%s, %s)',
it produces:
{
'sql_params_list': [
('INSERT INTO T (s, c) SELECT st, zc FROM cus ORDER BY fn, ln', None),
('INSERT INTO T (s, c) SELECT st, zc FROM cus ORDER BY fn, ln', ('a', 'b')),
]
}
@@ -276,7 +276,7 @@ def parse_insert(insert_sql, params):
if not after_values_sql:
# Case b)
insert_sql = sanitize_literals_for_upload(insert_sql)
return {"sql_params_list": [(insert_sql, None)]}
return {"sql_params_list": [(insert_sql, params)]}

if not params:
# Case a) perhaps?
@@ -425,3 +425,19 @@ def test_escape_name(self):
with self.subTest(name=name):
got = escape_name(name)
self.assertEqual(got, want)

def test_insert_from_select(self):
"""Check that INSERT from SELECT clause can be executed with arguments."""
from google.cloud.spanner_dbapi.parse_utils import parse_insert

SQL = """
INSERT INTO tab_name (id, data)
SELECT tab_name.id + %s AS anon_1, tab_name.data
FROM tab_name
WHERE tab_name.data IN (%s, %s)
"""
ARGS = [5, "data2", "data3"]

self.assertEqual(
parse_insert(SQL, ARGS), {"sql_params_list": [(SQL, ARGS)]},
)

0 comments on commit 0dcda5e

Please sign in to comment.