Skip to content

Commit

Permalink
fix: support INSERT from SELECT clause with args (#306)
Browse files Browse the repository at this point in the history
* 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 0dcda5e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
6 changes: 3 additions & 3 deletions google/cloud/spanner_dbapi/parse_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
]
}
Expand Down Expand Up @@ -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?
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/spanner_dbapi/test_parse_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.