Skip to content

Commit 8292df2

Browse files
committed
Parameter 'omitTrailingSemicolon' for explicit instruction, if the trailing semicolon (;) at the SQL string end should be removed or not
1 parent 038d9f6 commit 8292df2

2 files changed

Lines changed: 43 additions & 5 deletions

File tree

src/DatabaseLibrary/query.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,13 +356,23 @@ def execute_sql_script(
356356
db_connection.client.rollback()
357357

358358
def execute_sql_string(
359-
self, sqlString: str, sansTran: bool = False, alias: Optional[str] = None, parameters: Optional[List] = None
359+
self,
360+
sqlString: str,
361+
sansTran: bool = False,
362+
omitTrailingSemicolon: Optional[bool] = None,
363+
alias: Optional[str] = None,
364+
parameters: Optional[List] = None,
360365
):
361366
"""
362-
Executes the sqlString as SQL commands. Useful to pass arguments to your sql.
363-
SQL commands are expected to be delimited by a semicolon (';').
367+
Executes the ``sqlString`` as a single SQL command.
364368
365-
Use optional `sansTran` to run command without an explicit transaction commit or rollback:
369+
Use optional ``sansTran`` to run command without an explicit transaction commit or rollback.
370+
371+
Use optional ``omitTrailingSemicolon`` parameter for explicit instruction,
372+
if the trailing semicolon (;) at the SQL string end should be removed or not:
373+
- Some database modules (e.g. Oracle) throw an exception, if you leave a semicolon at the string end
374+
- However, there are exceptional cases, when you need it even for Oracle - e.g. at the end of a PL/SQL block.
375+
- If not specified, it's decided based on the current database module in use. For Oracle, the semicolon is removed by default.
366376
367377
Use optional ``alias`` parameter to specify what connection should be used for the query if you have more
368378
than one connection open.
@@ -374,6 +384,7 @@ def execute_sql_string(
374384
| Execute Sql String | DELETE FROM person_employee_table; DELETE FROM person_table |
375385
| Execute Sql String | DELETE FROM person_employee_table; DELETE FROM person_table | alias=my_alias |
376386
| Execute Sql String | DELETE FROM person_employee_table; DELETE FROM person_table | sansTran=True |
387+
| Execute Sql String | CREATE PROCEDURE proc AS BEGIN DBMS_OUTPUT.PUT_LINE('Hello!'); END; | omitTrailingSemicolon=False |
377388
| @{parameters} | Create List | person_employee_table |
378389
| Execute Sql String | SELECT * FROM %s | parameters=${parameters} |
379390
"""
@@ -382,7 +393,7 @@ def execute_sql_string(
382393
try:
383394
cur = db_connection.client.cursor()
384395
logger.info(f"Executing : Execute SQL String | {sqlString}")
385-
self.__execute_sql(cur, sqlString, parameters=parameters)
396+
self.__execute_sql(cur, sqlString, omit_trailing_semicolon=omitTrailingSemicolon, parameters=parameters)
386397
if not sansTran:
387398
db_connection.client.commit()
388399
finally:
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
*** Settings ***
2+
Documentation Tests for the parameter _omitTrailingSemicolon_ in the keyword
3+
... _Execute SQL String_ - special for the issue #184:
4+
... https://github.com/MarketSquare/Robotframework-Database-Library/issues/184
5+
... The _PLSQL BLOCK_ is most likely valid for Oracle DB only.
6+
7+
Resource ../../resources/common.resource
8+
Suite Setup Connect To DB
9+
Suite Teardown Disconnect From Database
10+
Test Setup Create Person Table And Insert Data
11+
Test Teardown Drop Tables Person And Foobar
12+
13+
14+
*** Variables ***
15+
${NORMAL QUERY} SELECT * FROM person;
16+
${PLSQL BLOCK} DECLARE ERRCODE NUMBER; ERRMSG VARCHAR2(200); BEGIN DBMS_OUTPUT.PUT_LINE('Hello!'); END;
17+
18+
19+
*** Test Cases ***
20+
Explicitely Omit Semicolon
21+
[Documentation] Check if it works for Oracle - explicitely omitting the semicolon
22+
... is equal to the default behaviour, otherwise oracle_db throws an error
23+
Execute Sql String ${NORMAL QUERY} omitTrailingSemicolon=True
24+
25+
Explicitely Dont't Omit Semicolon
26+
[Documentation] Check if it works for Oracle - it throws an error without a semicolon
27+
Execute Sql String ${PLSQL BLOCK} omitTrailingSemicolon=False

0 commit comments

Comments
 (0)