Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Return query result from hook #43

Merged
merged 7 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 1 addition & 32 deletions firebolt_provider/hooks/firebolt.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class FireboltHook(DbApiHook):
default_conn_name = "firebolt_default"
conn_type = "firebolt"
hook_name = "Firebolt"
supports_autocommit = False

ConnectionParameters = namedtuple(
"ConnectionParameters",
Expand Down Expand Up @@ -201,38 +202,6 @@ def engine_action(self, engine_name: Optional[str], action: str) -> None:
"""
self._run_action(self._get_engine(engine_name), action)

def run(
self,
sql: Union[str, List],
autocommit: bool = False,
parameters: Optional[Sequence] = None,
handler: Optional[Callable] = None,
) -> None:
"""
Runs a command or a list of commands. Pass a list of sql
statements to the sql parameter to get them to execute
sequentially
:param sql: the sql statement to be executed (str) or a list of
sql statements to execute
:type sql: str or list
:param autocommit: What to set the connection's autocommit setting to
before executing the query.
:type autocommit: bool
:param parameters: The parameters to render the SQL query with.
:type parameters: dict or iterable
"""
scalar = isinstance(sql, str)
if scalar:
sql = [sql]
with self.get_conn() as conn:
with conn.cursor() as cursor:
for sql_statement in sql:
if parameters:
cursor.execute(sql_statement, parameters)
else:
cursor.execute(sql_statement)
self.log.info(f"Rows returned: {cursor.rowcount}")

def test_connection(self) -> Tuple[bool, str]:
"""Test the Firebolt connection by running a simple query."""
try:
Expand Down
6 changes: 3 additions & 3 deletions tests/hooks/test_firebolt.py
stepansergeevitch marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,20 @@ def test_run_with_parameters(self):
sql = "SQL"
parameters = ("param1", "param2")
self.db_hook.run(sql=sql, parameters=parameters)
self.conn.__enter__().cursor().__enter__().execute.assert_called_once_with(
self.conn.cursor().execute.assert_called_once_with(
sql, parameters
)

def test_run_with_single_query(self):
sql = "SQL"
self.db_hook.run(sql)
self.conn.__enter__().cursor().__enter__().execute.assert_called_once_with(sql)
self.conn.cursor().execute.assert_called_once_with(sql)

def test_run_multi_queries(self):
sql = ["SQL1", "SQL2"]
self.db_hook.run(sql, autocommit=True)
for query in sql:
self.conn.__enter__().cursor().__enter__().execute.assert_any_call(query)
self.conn.cursor().execute.assert_any_call(query)

def test_get_ui_field_behaviour(self):
widget = {
Expand Down
Loading