Skip to content

Commit

Permalink
Support RF dotted dictionary access syntax when returning query resul…
Browse files Browse the repository at this point in the history
…ts as a dictionary (#239)
  • Loading branch information
amochin authored Feb 15, 2025
1 parent b077395 commit ff1645d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/DatabaseLibrary/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from typing import List, Optional, Tuple

from robot.api import logger
from robot.utils.dotdict import DotDict

from .connection_manager import Connection
from .params_decorator import renamed_args
Expand Down Expand Up @@ -91,7 +92,7 @@ def query(
col_names = [c[0] for c in cur.description]
self._log_query_results(col_names, all_rows)
if return_dict:
return [dict(zip(col_names, row)) for row in all_rows]
return [DotDict(zip(col_names, row)) for row in all_rows]
return all_rows
except Exception as e:
self._rollback_and_raise(db_connection, no_transaction, e)
Expand Down
13 changes: 13 additions & 0 deletions test/tests/common_tests/basic_tests.robot
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ Verify Query - Get results as a list of dictionaries
Should Be Equal As Strings ${value 1} Franz Allan
Should Be Equal As Strings ${value 2} Jerry

Return As Dictionary - Dotted Syntax
${output}= Query SELECT * FROM person return_dict=True
${field_names}= Get Dictionary Keys ${output}[0]
IF "FIRST_NAME" in $field_names
VAR ${field_name}= FIRST_NAME
ELSE IF "first_name" in $field_names
VAR ${field_name}= first_name
ELSE
FAIL Unexpected field name in dictionary
END
Should Be Equal As Strings ${output[0].${field_name}} Franz Allan
Should Be Equal As Strings ${output[1].${field_name}} Jerry

Verify Execute SQL String - Row Count person table
${output}= Execute SQL String SELECT COUNT(*) FROM person
Log ${output}
Expand Down

0 comments on commit ff1645d

Please sign in to comment.