Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support INSERT from SELECT clause with args #306

Merged
merged 3 commits into from Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion google/cloud/spanner_dbapi/parse_utils.py
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)]}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the previous author of the DB API didn't notice that INSERT from SELECT will be a case b), and it can include some args. Thus, passing None instead of args is not correct.


if not params:
# Case a) perhaps?
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/spanner_dbapi/test_parse_utils.py
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)]},
)