Skip to content

Commit 4373b96

Browse files
authored
Merge pull request #1 from singlestore-labs/users/asaini/allow-connection-override
Allow overriding `connection` while executing the sql magic.
2 parents 23b496e + 9e647b6 commit 4373b96

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/sql/command.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class SQLCommand:
2222
2323
"""
2424

25-
def __init__(self, magic, user_ns, line, cell) -> None:
25+
def __init__(self, magic, user_ns, line, cell, connection=None) -> None:
2626
self._line = line
2727
self._cell = cell
2828

@@ -89,6 +89,9 @@ def __init__(self, magic, user_ns, line, cell) -> None:
8989
if add_alias:
9090
self.parsed["connection"] = self.args.line[0]
9191

92+
if connection is not None:
93+
self.parsed["connection"] = connection
94+
9295
if self.args.with_:
9396
self.args.with_ = [
9497
Template(item).render(user_ns) for item in self.args.with_

src/sql/magic.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ def check_random_arguments(self, line="", cell=""):
335335
action="append",
336336
help="Interactive mode",
337337
)
338-
def execute(self, line="", cell="", local_ns=None):
338+
def execute(self, line="", cell="", local_ns=None, connection=None):
339339
"""
340340
Runs SQL statement against a database, specified by
341341
SQLAlchemy connect string.
@@ -363,17 +363,26 @@ def execute(self, line="", cell="", local_ns=None):
363363
364364
"""
365365
return self._execute(
366-
line=line, cell=cell, local_ns=local_ns, is_interactive_mode=False
366+
line=line,
367+
cell=cell,
368+
local_ns=local_ns,
369+
is_interactive_mode=False,
370+
connection=connection,
367371
)
368372

369373
@modify_exceptions
370-
def _execute(self, line, cell, local_ns, is_interactive_mode=False):
374+
def _execute(
375+
self, line, cell, local_ns, is_interactive_mode=False, connection=None
376+
):
371377
"""
372378
This function implements the cell logic; we create this private
373379
method so we can control how the function is called. Otherwise,
374380
decorating ``SqlMagic.execute`` will break when adding the ``@log_call``
375381
decorator with ``payload=True``
376382
383+
``connection`` is any [DBAPI v2](https://peps.python.org/pep-0249/) compatible connection object.
384+
If provided, it will override any other connection configuration.
385+
377386
NOTE: telemetry has been removed, we can remove this function
378387
"""
379388

@@ -401,7 +410,7 @@ def interactive_execute_wrapper(**kwargs):
401410
user_ns = self.shell.user_ns.copy()
402411
user_ns.update(local_ns)
403412

404-
command = SQLCommand(self, user_ns, line, cell)
413+
command = SQLCommand(self, user_ns, line, cell, connection)
405414
# args.line: contains the line after the magic with all options removed
406415

407416
args = command.args

0 commit comments

Comments
 (0)