|
20 | 20 |
|
21 | 21 | from robot.api import logger |
22 | 22 |
|
| 23 | +from .connection_manager import Connection |
23 | 24 | from .params_decorator import renamed_args |
24 | 25 |
|
25 | 26 |
|
@@ -87,9 +88,8 @@ def query( |
87 | 88 | if return_dict: |
88 | 89 | return [dict(zip(col_names, row)) for row in all_rows] |
89 | 90 | return all_rows |
90 | | - finally: |
91 | | - if cur and not no_transaction: |
92 | | - db_connection.client.rollback() |
| 91 | + except Exception as e: |
| 92 | + self._rollback_and_raise(db_connection, no_transaction, e) |
93 | 93 |
|
94 | 94 | @renamed_args(mapping={"selectStatement": "select_statement", "sansTran": "no_transaction"}) |
95 | 95 | def row_count( |
@@ -140,9 +140,8 @@ def row_count( |
140 | 140 | logger.info(f"Retrieved {current_row_count} rows") |
141 | 141 | self._log_query_results(col_names, data) |
142 | 142 | return current_row_count |
143 | | - finally: |
144 | | - if cur and not no_transaction: |
145 | | - db_connection.client.rollback() |
| 143 | + except Exception as e: |
| 144 | + self._rollback_and_raise(db_connection, no_transaction, e) |
146 | 145 |
|
147 | 146 | @renamed_args(mapping={"selectStatement": "select_statement", "sansTran": "no_transaction"}) |
148 | 147 | def description( |
@@ -189,9 +188,8 @@ def description( |
189 | 188 | for row in range(0, len(description)): |
190 | 189 | description[row] = (description[row][0].encode("utf-8"),) + description[row][1:] |
191 | 190 | return description |
192 | | - finally: |
193 | | - if cur and not no_transaction: |
194 | | - db_connection.client.rollback() |
| 191 | + except Exception as e: |
| 192 | + self._rollback_and_raise(db_connection, no_transaction, e) |
195 | 193 |
|
196 | 194 | @renamed_args(mapping={"tableName": "table_name", "sansTran": "no_transaction"}) |
197 | 195 | def delete_all_rows_from_table( |
@@ -229,15 +227,11 @@ def delete_all_rows_from_table( |
229 | 227 | try: |
230 | 228 | cur = db_connection.client.cursor() |
231 | 229 | result = self._execute_sql(cur, query) |
| 230 | + self._commit_if_needed(db_connection, no_transaction) |
232 | 231 | if result is not None: |
233 | | - if not no_transaction: |
234 | | - db_connection.client.commit() |
235 | 232 | return result |
236 | | - if not no_transaction: |
237 | | - db_connection.client.commit() |
238 | | - finally: |
239 | | - if cur and not no_transaction: |
240 | | - db_connection.client.rollback() |
| 233 | + except Exception as e: |
| 234 | + self._rollback_and_raise(db_connection, no_transaction, e) |
241 | 235 |
|
242 | 236 | @renamed_args(mapping={"sqlScriptFileName": "script_path", "sansTran": "no_transaction"}) |
243 | 237 | def execute_sql_script( |
@@ -348,11 +342,9 @@ def execute_sql_script( |
348 | 342 | line_ends_with_proc_end = re.compile(r"(\s|;)" + proc_end_pattern.pattern + "$") |
349 | 343 | omit_semicolon = not line_ends_with_proc_end.search(statement.lower()) |
350 | 344 | self._execute_sql(cur, statement, omit_semicolon) |
351 | | - if not no_transaction: |
352 | | - db_connection.client.commit() |
353 | | - finally: |
354 | | - if cur and not no_transaction: |
355 | | - db_connection.client.rollback() |
| 345 | + self._commit_if_needed(db_connection, no_transaction) |
| 346 | + except Exception as e: |
| 347 | + self._rollback_and_raise(db_connection, no_transaction, e) |
356 | 348 |
|
357 | 349 | @renamed_args( |
358 | 350 | mapping={ |
@@ -410,11 +402,9 @@ def execute_sql_string( |
410 | 402 | try: |
411 | 403 | cur = db_connection.client.cursor() |
412 | 404 | self._execute_sql(cur, sql_string, omit_trailing_semicolon=omit_trailing_semicolon, parameters=parameters) |
413 | | - if not no_transaction: |
414 | | - db_connection.client.commit() |
415 | | - finally: |
416 | | - if cur and not no_transaction: |
417 | | - db_connection.client.rollback() |
| 405 | + self._commit_if_needed(db_connection, no_transaction) |
| 406 | + except Exception as e: |
| 407 | + self._rollback_and_raise(db_connection, no_transaction, e) |
418 | 408 |
|
419 | 409 | @renamed_args(mapping={"spName": "procedure_name", "spParams": "procedure_params", "sansTran": "no_transaction"}) |
420 | 410 | def call_stored_procedure( |
@@ -717,13 +707,10 @@ def call_stored_procedure( |
717 | 707 | else: |
718 | 708 | result_sets_available = False |
719 | 709 |
|
720 | | - if not no_transaction: |
721 | | - db_connection.client.commit() |
722 | | - |
| 710 | + self._commit_if_needed(db_connection, no_transaction) |
723 | 711 | return param_values, result_sets |
724 | | - finally: |
725 | | - if cur and not no_transaction: |
726 | | - db_connection.client.rollback() |
| 712 | + except Exception as e: |
| 713 | + self._rollback_and_raise(db_connection, no_transaction, e) |
727 | 714 |
|
728 | 715 | def set_logging_query_results(self, enabled: Optional[bool] = None, log_head: Optional[int] = None): |
729 | 716 | """ |
@@ -771,6 +758,22 @@ def _execute_sql( |
771 | 758 | ) |
772 | 759 | return cur.execute(sql_statement, parameters) |
773 | 760 |
|
| 761 | + def _commit_if_needed(self, db_connection: Connection, no_transaction): |
| 762 | + if no_transaction: |
| 763 | + logger.info(f"Perform no commit, because 'no_transaction' set to {no_transaction}") |
| 764 | + else: |
| 765 | + logger.info("Commit the transaction") |
| 766 | + db_connection.client.commit() |
| 767 | + |
| 768 | + def _rollback_and_raise(self, db_connection: Connection, no_transaction, e): |
| 769 | + logger.info(f"Error occurred: {e}") |
| 770 | + if no_transaction: |
| 771 | + logger.info(f"Perform no rollback, because 'no_transaction' set to {no_transaction}") |
| 772 | + else: |
| 773 | + logger.info("Rollback the transaction") |
| 774 | + db_connection.client.rollback() |
| 775 | + raise e |
| 776 | + |
774 | 777 | def _log_query_results(self, col_names, result_rows, log_head: Optional[int] = None): |
775 | 778 | """ |
776 | 779 | Logs the `result_rows` of a query in RF log as a HTML table. |
|
0 commit comments