Skip to content

Commit 9a8389b

Browse files
committed
New keyword 'Set Omit Trailing Semicolon'
1 parent c4e1872 commit 9a8389b

5 files changed

Lines changed: 93 additions & 14 deletions

File tree

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ The retry mechanism is disabled by default - ``retry_timeout`` is set to ``0``.
367367
${sql}= Catenate SELECT first_name FROM person
368368
Check Row Count ${sql} == 2 retry_timeout=10 seconds
369369
Check Query Result ${sql} contains Allan retry_timeout=5s retry_pause=1s
370-
````
370+
```
371371

372372
# Logging query results
373373
Keywords, that fetch results of a SQL query, print the result rows as a table in RF log.
@@ -389,7 +389,7 @@ Library DatabaseLibrary log_query_results_head=10
389389
390390
# Logging of query results is enabled (default), log head limit is disabled (log all rows).
391391
Library DatabaseLibrary log_query_results_head=0
392-
````
392+
```
393393

394394
# Commit behavior
395395
While creating a database connection, the library doesn't explicitly set the _autocommit_ behavior -
@@ -408,6 +408,19 @@ It's also possible to explicitly set the _autocommit_ behavior on the Python DB
408408
using the `Set Auto Commit` keyword.
409409
This has no impact on the automatic commit/rollback behavior in library keywords (described above).
410410

411+
# Omitting trailing semicolon behavior
412+
Some databases (e.g. Oracle) throw an exception, if you leave a semicolon (;) at the SQL string end.
413+
However, there are exceptional cases, when you need it even for Oracle - e.g. at the end of a PL/SQL block.
414+
415+
The library can handle it for you and remove the semicolon at the end of the SQL string.
416+
By default, it's decided based on the current database module in use:
417+
- For `oracle_db` and `cx_Oracle`, the trailing semicolon is removed
418+
- For other modules, the trailing semicolon is left as it is
419+
420+
You can also set this behavior explicitly:
421+
- Using the `Set Omit Trailing Semicolon` keyword
422+
- Using the `omit_trailing_semicolon` parameter in the `Execute SQL String` keyword.
423+
411424
# Database modules compatibility
412425
> Looking for [Connection examples for different DB modules](#connection-examples-for-different-db-modules)?
413426

doc/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/DatabaseLibrary/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,19 @@ class DatabaseLibrary(ConnectionManager, Query, Assertion):
363363
using the `Set Auto Commit` keyword.
364364
This has no impact on the automatic commit/rollback behavior in library keywords (described above).
365365
366+
= Omitting trailing semicolon behavior =
367+
Some databases (e.g. Oracle) throw an exception, if you leave a semicolon (;) at the SQL string end.
368+
However, there are exceptional cases, when you need it even for Oracle - e.g. at the end of a PL/SQL block.
369+
370+
The library can handle it for you and remove the semicolon at the end of the SQL string.
371+
By default, it's decided based on the current database module in use:
372+
- For `oracle_db` and `cx_Oracle`, the trailing semicolon is removed
373+
- For other modules, the trailing semicolon is left as it is
374+
375+
You can also set this behavior explicitly:
376+
- Using the `Set Omit Trailing Semicolon` keyword
377+
- Using the `omit_trailing_semicolon` parameter in the `Execute SQL String` keyword.
378+
366379
= Database modules compatibility =
367380
The library is basically compatible with any [https://peps.python.org/pep-0249|Python Database API Specification 2.0] module.
368381

src/DatabaseLibrary/connection_manager.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,3 +655,16 @@ def switch_database(self, alias: str):
655655
| Switch Database | alias=my_alias |
656656
"""
657657
self.connection_store.switch(alias)
658+
659+
def set_omit_trailing_semicolon(self, omit=True, alias: Optional[str] = None):
660+
"""
661+
Set the ``omit_trailing_semicolon`` to control the `Omitting trailing semicolon behavior` for the connection.
662+
663+
Use ``alias`` to specify what connection should be used if `Handling multiple database connections`.
664+
665+
Examples:
666+
| Set Omit Trailing Semicolon | True |
667+
| Set Omit Trailing Semicolon | False | alias=my_alias |
668+
"""
669+
db_connection = self.connection_store.get_connection(alias)
670+
db_connection.omit_trailing_semicolon = omit
Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,67 @@
11
*** 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.
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.
66
77
Resource ../../resources/common.resource
8+
89
Suite Setup Connect To DB
910
Suite Teardown Disconnect From Database
1011
Test Setup Create Person Table And Insert Data
1112
Test Teardown Drop Tables Person And Foobar
1213

1314

1415
*** Variables ***
15-
${NORMAL QUERY} SELECT * FROM person;
16-
${PLSQL BLOCK} DECLARE ERRCODE NUMBER; ERRMSG VARCHAR2(200); BEGIN DBMS_OUTPUT.PUT_LINE('Hello!'); END;
16+
${NORMAL QUERY} SELECT * FROM person;
17+
${PLSQL BLOCK} DECLARE ERRCODE NUMBER; ERRMSG VARCHAR2(200); BEGIN DBMS_OUTPUT.PUT_LINE('Hello!'); END;
18+
19+
${ERROR SIMPLE QUERY} *ORA-03048: SQL reserved word ';' is not syntactically valid following*
20+
${ERROR PLSQL} *PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following*
1721

1822

1923
*** 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
24+
Explicitely Omit Semicolon - Simple Query
25+
[Documentation] Check if it works for Oracle - explicitly omitting the semicolon
26+
... is equal to the default behavior
2327
Execute Sql String ${NORMAL QUERY} omit_trailing_semicolon=True
2428

25-
Explicitely Dont't Omit Semicolon
26-
[Documentation] Check if it works for Oracle - it throws an error without a semicolon
29+
Explicitely Don't Omit Semicolon - Simple Query
30+
[Documentation] Check if Oracle throws an error
31+
32+
Run Keyword And Expect Error ${ERROR SIMPLE QUERY}
33+
... Execute Sql String ${NORMAL QUERY} omit_trailing_semicolon=False
34+
35+
Explicitely Omit Semicolon - PLSQL Block
36+
[Documentation] Check if Oracle throws an error
37+
Run Keyword And Expect Error ${ERROR PLSQL}
38+
... Execute Sql String ${PLSQL BLOCK} omit_trailing_semicolon=True
39+
40+
Explicitely Don't Omit Semicolon - PLSQL Block
41+
[Documentation] Should run without errors, because the semicolon is needed
42+
... at the end of the PLSQL block even with Oracle
2743
Execute Sql String ${PLSQL BLOCK} omit_trailing_semicolon=False
44+
45+
Explicitely Omit Semicolon With Keyword - Simple Query
46+
[Documentation] Check if it works for Oracle - explicitly omitting the semicolon
47+
... is equal to the default behavior
48+
Set Omit Trailing Semicolon True
49+
Execute Sql String ${NORMAL QUERY}
50+
51+
Explicitely Don't Omit Semicolon With Keyword - Simple Query
52+
[Documentation] Check if Oracle throws an error
53+
Set Omit Trailing Semicolon False
54+
Run Keyword And Expect Error ${ERROR SIMPLE QUERY}
55+
... Execute Sql String ${NORMAL QUERY}
56+
57+
Explicitely Omit Semicolon With Keyword - PLSQL Block
58+
[Documentation] Check if Oracle throws an error
59+
Set Omit Trailing Semicolon True
60+
Run Keyword And Expect Error ${ERROR PLSQL}
61+
... Execute Sql String ${PLSQL BLOCK}
62+
63+
Explicitely Don't Omit Semicolon With Keyword - PLSQL Block
64+
[Documentation] Should run without errors, because the semicolon is needed
65+
... at the end of the PLSQL block even with Oracle
66+
Set Omit Trailing Semicolon False
67+
Execute Sql String ${PLSQL BLOCK}

0 commit comments

Comments
 (0)