@@ -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