Skip to content

Commit 801ddc8

Browse files
author
Ilya Gurov
authored
feat(db_api): support executing several DDLs separated by semicolon (#277)
* feat(db_api): support executing several DDLs separated by semicolon * add a unit test * add a line with a "newline" symbol into test
1 parent 1b0ce1d commit 801ddc8

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

google/cloud/spanner_dbapi/cursor.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ def execute(self, sql, args=None):
174174
try:
175175
classification = parse_utils.classify_stmt(sql)
176176
if classification == parse_utils.STMT_DDL:
177-
self.connection._ddl_statements.append(sql)
177+
for ddl in sql.split(";"):
178+
ddl = ddl.strip()
179+
if ddl:
180+
self.connection._ddl_statements.append(ddl)
178181
return
179182

180183
# For every other operation, we've got to ensure that

tests/unit/spanner_dbapi/test_cursor.py

+30
Original file line numberDiff line numberDiff line change
@@ -862,3 +862,33 @@ def test_fetchmany_retry_aborted_statements_checksums_mismatch(self):
862862
cursor.fetchmany(len(row))
863863

864864
run_mock.assert_called_with(statement, retried=True)
865+
866+
def test_ddls_with_semicolon(self):
867+
"""
868+
Check that one script with several DDL statements separated
869+
with semicolons is splitted into several DDLs.
870+
"""
871+
from google.cloud.spanner_dbapi.connection import connect
872+
873+
EXP_DDLS = [
874+
"CREATE TABLE table_name (row_id INT64) PRIMARY KEY ()",
875+
"DROP INDEX index_name",
876+
"DROP TABLE table_name",
877+
]
878+
879+
with mock.patch(
880+
"google.cloud.spanner_v1.instance.Instance.exists", return_value=True,
881+
):
882+
with mock.patch(
883+
"google.cloud.spanner_v1.database.Database.exists", return_value=True,
884+
):
885+
connection = connect("test-instance", "test-database")
886+
887+
cursor = connection.cursor()
888+
cursor.execute(
889+
"CREATE TABLE table_name (row_id INT64) PRIMARY KEY ();"
890+
"DROP INDEX index_name;\n"
891+
"DROP TABLE table_name;"
892+
)
893+
894+
self.assertEqual(connection._ddl_statements, EXP_DDLS)

0 commit comments

Comments
 (0)