Skip to content

Commit 5a16ea8

Browse files
committed
Split SQL script - remove comments when using external parser to avoid DB errors executing empty statements
1 parent 5cf6a52 commit 5a16ea8

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/DatabaseLibrary/query.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,11 @@ def split_sql_script(
352352
logger.info("Splitting script file into statements...")
353353
statements_to_execute = []
354354
if external_parser:
355-
statements_to_execute = sqlparse.split(sql_file.read())
355+
split_statements = sqlparse.split(sql_file.read())
356+
for statement in split_statements:
357+
statement_without_comments = sqlparse.format(statement, strip_comments=True)
358+
if statement_without_comments:
359+
statements_to_execute.append(statement_without_comments)
356360
else:
357361
current_statement = ""
358362
inside_statements_group = False
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- here is a comment
2+
SELECT * FROM person;
3+
-- here is some another comment
4+
-- on more lines
5+
SELECT * FROM person WHERE id=1;
6+
--

test/tests/common_tests/script_files.robot

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ Split Script Into Statements - External Parser
5757
${results}= Query ${command}
5858
END
5959

60+
Split Script Into Statements - External Parser - Comments Are Removed
61+
Insert Data In Person Table Using SQL Script
62+
@{Expected commands}= Create List
63+
... SELECT * FROM person;
64+
... SELECT * FROM person WHERE id=1;
65+
${extracted commands}= Split Sql Script ${Script files dir}/split_commands_comments.sql external_parser=True
66+
Lists Should Be Equal ${Expected commands} ${extracted commands}
67+
FOR ${command} IN @{extracted commands}
68+
${results}= Query ${command}
69+
END
70+
6071

6172
*** Keywords ***
6273
Run SQL Script File

0 commit comments

Comments
 (0)