Skip to content

Commit e403ef4

Browse files
committed
Fix #237 - commit even after simple query
1 parent da5e893 commit e403ef4

File tree

6 files changed

+30
-20
lines changed

6 files changed

+30
-20
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,11 +401,10 @@ Library DatabaseLibrary log_query_results_head=0
401401
While creating a database connection, the library doesn't explicitly set the _autocommit_ behavior -
402402
so the default value of the Python DB module is used.
403403
According to Python DB API specification it should be disabled by default -
404-
which means each SQL transaction must contain a dedicated commit statement, if necessary.
404+
which means each SQL transaction (even a simple _SELECT_) must contain a dedicated commit statement, if necessary.
405405

406-
The library manages it for you:
407-
- Keywords like `Execute SQL String` perform automatically a commit after running the query - or a rollback in case of error
408-
- Keywords like `Query` don't perform a commit, but also do a rollback in case of error
406+
The library manages it for you - keywords like `Query` or `Execute SQL String`
407+
perform automatically a commit after running the query (or a rollback in case of error).
409408

410409
You can turn off this automatic commit/rollback behavior using the ``no_transaction`` parameter.
411410
See docs of a particular keyword.

src/DatabaseLibrary/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,10 @@ class DatabaseLibrary(ConnectionManager, Query, Assertion):
356356
While creating a database connection, the library doesn't explicitly set the _autocommit_ behavior -
357357
so the default value of the Python DB module is used.
358358
According to Python DB API specification it should be disabled by default -
359-
which means each SQL transaction must contain a dedicated commit statement, if necessary.
359+
which means each SQL transaction (even a simple _SELECT_) must contain a dedicated commit statement, if necessary.
360360
361-
The library manages it for you:
362-
- Keywords like `Execute SQL String` perform automatically a commit after running the query - or a rollback in case of error
363-
- Keywords like `Query` don't perform a commit, but also do a rollback in case of error
361+
The library manages it for you - keywords like `Query` or `Execute SQL String`
362+
perform automatically a commit after running the query (or a rollback in case of error).
364363
365364
You can turn off this automatic commit/rollback behavior using the ``no_transaction`` parameter.
366365
See docs of a particular keyword.

src/DatabaseLibrary/query.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def query(
5454
The type of row values depends on the database module -
5555
usually they are tuples or tuple-like objects.
5656
57-
Set ``no_transaction`` to _True_ to run command without explicit transaction rollback in case of error.
57+
Set ``no_transaction`` to _True_ to run command without explicit transaction commit or rollback in case of error.
5858
See `Commit behavior` for details.
5959
6060
Set ``return_dict`` to _True_ to explicitly convert the return values into list of dictionaries.
@@ -89,6 +89,7 @@ def query(
8989
omit_trailing_semicolon=db_connection.omit_trailing_semicolon,
9090
)
9191
all_rows = cur.fetchall()
92+
self._commit_if_needed(db_connection, no_transaction)
9293
col_names = [c[0] for c in cur.description]
9394
self._log_query_results(col_names, all_rows)
9495
if return_dict:
@@ -111,7 +112,7 @@ def row_count(
111112
"""
112113
Runs a query with the ``select_statement`` and returns the number of rows in the result.
113114
114-
Set ``no_transaction`` to _True_ to run command without explicit transaction rollback in case of error.
115+
Set ``no_transaction`` to _True_ to run command without explicit transaction commit or rollback in case of error.
115116
See `Commit behavior` for details.
116117
117118
Use ``alias`` to specify what connection should be used if `Handling multiple database connections`.
@@ -143,6 +144,7 @@ def row_count(
143144
omit_trailing_semicolon=db_connection.omit_trailing_semicolon,
144145
)
145146
data = cur.fetchall()
147+
self._commit_if_needed(db_connection, no_transaction)
146148
col_names = [c[0] for c in cur.description]
147149
if db_connection.module_name in ["sqlite3", "ibm_db", "ibm_db_dbi", "pyodbc", "jaydebeapi"]:
148150
current_row_count = len(data)
@@ -168,7 +170,7 @@ def description(
168170
"""
169171
Runs a query with the ``select_statement`` to determine the table description.
170172
171-
Set ``no_transaction`` to _True_ to run command without explicit transaction rollback in case of error.
173+
Set ``no_transaction`` to _True_ to run command without explicit transaction commit or rollback in case of error.
172174
See `Commit behavior` for details.
173175
174176
Use ``alias`` to specify what connection should be used if `Handling multiple database connections`.
@@ -199,6 +201,7 @@ def description(
199201
parameters=parameters,
200202
omit_trailing_semicolon=db_connection.omit_trailing_semicolon,
201203
)
204+
self._commit_if_needed(db_connection, no_transaction)
202205
description = list(cur.description)
203206
if sys.version_info[0] < 3:
204207
for row in range(0, len(description)):

test/tests/common_tests/basic_tests.robot

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,3 @@ Verify Query - Row Count foobar table 0 row
157157
[Setup] Create Foobar Table And Insert Data
158158
Delete All Rows From Table foobar
159159
Row Count Is 0 SELECT * FROM foobar
160-
161-
For Loop
162-
FOR ${i} IN RANGE 10
163-
${results}= Query SELECT LAST_NAME FROM person ORDER BY id
164-
Sleep 3s
165-
IF '${results}[0][0]' == 'Musk' BREAK
166-
END
167-
Should Be Equal As Strings ${results}[0][0] Musk

test/tests/common_tests/connection_params.robot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Test Teardown Disconnect From Database
1919
... invalid custom param=TypeError: connect() got an unexpected keyword argument 'blah'
2020
&{Errors pymysql}
2121
... missing basic params=OperationalError: (1045, "Access denied*
22-
... invalid custom param=TypeError: __init__() got an unexpected keyword argument 'blah'
22+
... invalid custom param=TypeError: Connection.__init__() got an unexpected keyword argument 'blah'
2323
&{Errors pyodbc}
2424
... missing basic params=REGEXP: InterfaceError.*Data source name not found and no default driver specified.*
2525

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
*** Settings ***
2+
Documentation Check if the SQL statement returns new results, if DB is being updated in the background -
3+
... this requires a commit after each query.
4+
... See https://github.com/MarketSquare/Robotframework-Database-Library/issues/237
5+
6+
Resource ../../resources/common.resource
7+
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+
*** Test Cases ***
15+
Use Auto retry
16+
[Documentation] Update the DB manually in the background and check if the query returns the new results
17+
Check Query Result SELECT LAST_NAME FROM person ORDER BY id == Musk retry_timeout=30s

0 commit comments

Comments
 (0)