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: classify batched DDL statements #360

Merged
merged 3 commits into from Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion google/cloud/spanner_dbapi/cursor.py
Expand Up @@ -176,11 +176,16 @@ def execute(self, sql, args=None):
try:
classification = parse_utils.classify_stmt(sql)
if classification == parse_utils.STMT_DDL:
ddl_statements = []
for ddl in sqlparse.split(sql):
if ddl:
if ddl[-1] == ";":
ddl = ddl[:-1]
self.connection._ddl_statements.append(ddl)
if parse_utils.classify_stmt(ddl) != parse_utils.STMT_DDL:
raise ValueError("Only DDL statements may be batched.")
ddl_statements.append(ddl)
# Only queue DDL statements if they are all correctly classified.
self.connection._ddl_statements.extend(ddl_statements)
IlyaFaer marked this conversation as resolved.
Show resolved Hide resolved
if self.connection.autocommit:
self.connection.run_prior_DDL_statements()
return
Expand Down
11 changes: 11 additions & 0 deletions test.py
@@ -0,0 +1,11 @@
from google.cloud import spanner
from gooogle.cloud.spanner_v1 import RequestOptions

client = spanner.Client()
instance = client.instance('test-instance')
database = instance.database('test-db')

with database.snapshot() as snapshot:
results = snapshot.execute_sql("SELECT * in all_types LIMIT %s", )

database.drop()
14 changes: 13 additions & 1 deletion tests/unit/spanner_dbapi/test_cursor.py
Expand Up @@ -171,13 +171,25 @@ def test_execute_statement(self):
connection = self._make_connection(self.INSTANCE, mock.MagicMock())
cursor = self._make_one(connection)

with mock.patch(
"google.cloud.spanner_dbapi.parse_utils.classify_stmt",
side_effect=[parse_utils.STMT_DDL, parse_utils.STMT_INSERT],
) as mock_classify_stmt:
sql = "sql"
with self.assertRaises(ValueError):
cursor.execute(sql=sql)
mock_classify_stmt.assert_called_with(sql)
self.assertEqual(mock_classify_stmt.call_count, 2)
self.assertEqual(cursor.connection._ddl_statements, [])

with mock.patch(
"google.cloud.spanner_dbapi.parse_utils.classify_stmt",
return_value=parse_utils.STMT_DDL,
) as mock_classify_stmt:
sql = "sql"
cursor.execute(sql=sql)
mock_classify_stmt.assert_called_once_with(sql)
mock_classify_stmt.assert_called_with(sql)
self.assertEqual(mock_classify_stmt.call_count, 2)
self.assertEqual(cursor.connection._ddl_statements, [sql])

with mock.patch(
Expand Down