Skip to content

Commit

Permalink
var expansion save
Browse files Browse the repository at this point in the history
with

revert test change

alias, close

function

doc

doc fix

doc fix

doc fix

doc

fix

plot expansion

Lint

doc fix

profile

profile tests

Explore

columns tables

snippets

snippets test

minor

fix tests

Test

removed function

import

print removed
  • Loading branch information
neelasha23 committed Dec 31, 2023
1 parent 5f961d1 commit 854138d
Show file tree
Hide file tree
Showing 16 changed files with 476 additions and 14 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 0.10.8dev

* [Feature] Variable expansion outside queries (#699)

## 0.10.7 (2023-12-23)

* [Feature] Add Spark Connection as a dialect for Jupysql ([#965](https://github.com/ploomber/jupysql/issues/965)) (by [@gilandose](https://github.com/gilandose))
Expand Down Expand Up @@ -485,4 +487,4 @@ Converted from an IPython Plugin to an Extension for 1.0 compatibility

*Release date: 21-Mar-2013*

* Initial release
* Initial release
20 changes: 19 additions & 1 deletion doc/api/magic-sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jupytext:
extension: .md
format_name: myst
format_version: 0.13
jupytext_version: 1.15.1
jupytext_version: 1.16.0
kernelspec:
display_name: Python 3 (ipykernel)
language: python
Expand Down Expand Up @@ -359,3 +359,21 @@ LIMIT 3
```{code-cell} ipython3
%sql --file my-query.sql
```

# Parameterizing arguments

JupySQL supports variable expansion of arguments in the form of `{{variable}}`. This allows the user to specify arguments with placeholders that can be replaced by variables dynamically. The arguments supported are `section`, `close`, `alias`, `save` and `with`.

Let's see an example of creating a connection using an alias and closing the same through variable substitution.

```{code-cell} ipython3
alias = "db-four"
```

```{code-cell} ipython3
%sql sqlite:///db_four.db --alias {{alias}}
```

```{code-cell} ipython3
%sql --close {{alias}}
```
7 changes: 5 additions & 2 deletions src/sql/cmd/columns.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from sql import inspect
from sql.util import sanitize_identifier
from sql.util import sanitize_identifier, expand_args
from sql.cmd.cmd_utils import CmdParser


def columns(others):
def columns(others, user_ns):
"""
Implementation of `%sqlcmd columns`
This function takes in a string containing command line arguments,
Expand All @@ -26,4 +26,7 @@ def columns(others):
parser.add_argument("-s", "--schema", type=str, help="Schema name", required=False)

args = parser.parse_args(others)

expand_args(args, user_ns, ["table", "schema"])

return inspect.get_columns(name=sanitize_identifier(args.table), schema=args.schema)
6 changes: 5 additions & 1 deletion src/sql/cmd/explore.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from sql.widgets import TableWidget
from sql.cmd.cmd_utils import CmdParser
from sql.util import expand_args


def explore(others):
def explore(others, user_ns):
"""
Implementation of `%sqlcmd explore`
This function takes in a string containing command line arguments,
Expand All @@ -14,10 +15,13 @@ def explore(others):
others : str,
A string containing the command line arguments.
user_ns :
"""
parser = CmdParser()
parser.add_argument("-t", "--table", type=str, help="Table name", required=True)
parser.add_argument("-s", "--schema", type=str, help="Schema name", required=False)
args = parser.parse_args(others)
expand_args(args, user_ns, ["table", "schema"])
table_widget = TableWidget(args.table, args.schema)
return table_widget
7 changes: 6 additions & 1 deletion src/sql/cmd/profile.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from sql import inspect
from sql.cmd.cmd_utils import CmdParser
from sql.util import expand_args


def profile(others):
def profile(others, user_ns):
"""
Implementation of `%sqlcmd profile`
This function takes in a string containing command line arguments,
Expand All @@ -16,6 +17,8 @@ def profile(others):
others : str,
A string containing the command line arguments.
user_ns :
Returns
-------
report: PrettyTable
Expand All @@ -32,6 +35,8 @@ def profile(others):

args = parser.parse_args(others)

expand_args(args, user_ns, ["table", "schema", "output"])

report = inspect.get_table_statistics(schema=args.schema, name=args.table)

if args.output:
Expand Down
11 changes: 10 additions & 1 deletion src/sql/cmd/snippets.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from jinja2 import Template

from sql import util
from sql import store
from sql.exceptions import UsageError
Expand Down Expand Up @@ -32,7 +34,7 @@ def _modify_display_msg(key, remaining_keys, dependent_keys=None):
return msg


def snippets(others):
def snippets(others, user_ns):
"""
Implementation of `%sqlcmd snippets`
This function handles all the arguments related to %sqlcmd snippets, namely
Expand All @@ -45,6 +47,8 @@ def snippets(others):
others : str,
A string containing the command line arguments.
user_ns :
"""
parser = CmdParser()
parser.add_argument(
Expand All @@ -66,6 +70,8 @@ def snippets(others):
)
all_snippets = store.get_all_keys()
if len(others) == 1:
others[0] = Template(others[0]).render(user_ns)

if others[0] in all_snippets:
return str(store.store[others[0]])

Expand All @@ -79,6 +85,9 @@ def snippets(others):
raise UsageError(err_msg)

args = parser.parse_args(others)

util.expand_args(args, user_ns, ["delete", "delete_force_all", "delete_force"])

SNIPPET_ARGS = [args.delete, args.delete_force, args.delete_force_all]
if SNIPPET_ARGS.count(None) == len(SNIPPET_ARGS):
if len(all_snippets) == 0:
Expand Down
5 changes: 4 additions & 1 deletion src/sql/cmd/tables.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from sql import inspect
from sql.util import expand_args
from sql.cmd.cmd_utils import CmdParser


def tables(others):
def tables(others, user_ns):
"""
Implementation of `%sqlcmd tables`
Expand All @@ -27,4 +28,6 @@ def tables(others):

args = parser.parse_args(others)

expand_args(args, user_ns, ["schema"])

return inspect.get_table_names(schema=args.schema)
5 changes: 4 additions & 1 deletion src/sql/cmd/test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from sql import exceptions
import sql.connection
from sql.util import expand_args
from sqlglot import select, condition
from prettytable import PrettyTable
from sql.cmd.cmd_utils import CmdParser
Expand Down Expand Up @@ -77,7 +78,7 @@ def run_each_individually(args, conn):
return storage


def test(others):
def test(others, user_ns):
"""
Implementation of `%sqlcmd test`
Expand Down Expand Up @@ -142,6 +143,8 @@ def test(others):

args = parser.parse_args(others)

expand_args(args, user_ns, ["table", "schema", "column"])

COMPARATOR_ARGS = [
args.greater,
args.greater_or_equal,
Expand Down
3 changes: 3 additions & 0 deletions src/sql/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ def __init__(self, magic, user_ns, line, cell) -> None:
self.parsed["connection"] = self.args.line[0]

if self.args.with_:
self.args.with_ = [
Template(item).render(user_ns) for item in self.args.with_
]
final = store.render(self.parsed["sql"], with_=self.args.with_)
self.parsed["sql"] = str(final)

Expand Down
5 changes: 3 additions & 2 deletions src/sql/magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
from sql.magic_cmd import SqlCmdMagic
from sql._patch import patch_ipython_usage_error
from sql import util
from sql.util import pretty_print
from sql.error_handler import handle_exception
from sql._current import _set_sql_magic

Expand Down Expand Up @@ -409,6 +408,8 @@ def interactive_execute_wrapper(**kwargs):

args = command.args

util.expand_args(args, user_ns, ["section", "close", "alias", "save"])

if args.section and args.alias:
raise exceptions.UsageError(
"Cannot use --section with --alias since the section name "
Expand All @@ -435,7 +436,7 @@ def interactive_execute_wrapper(**kwargs):
command.set_sql_with(with_)
display.message(
f"Generating CTE with stored snippets: \
{pretty_print(with_)}"
{util.pretty_print(with_)}"
)
else:
with_ = None
Expand Down
9 changes: 7 additions & 2 deletions src/sql/magic_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import argparse
import shlex

from IPython.core.magic import Magics, line_magic, magics_class
from IPython.core.magic import Magics, line_magic, magics_class, no_var_expand
from IPython.core.magic_arguments import argument, magic_arguments
from sql.inspect import support_only_sql_alchemy_connection
from sql.cmd.tables import tables
Expand Down Expand Up @@ -35,6 +35,7 @@ def error(self, message):
class SqlCmdMagic(Magics, Configurable):
"""%sqlcmd magic"""

@no_var_expand
@line_magic("sqlcmd")
@magic_arguments()
@argument("line", type=str, help="Command name")
Expand Down Expand Up @@ -127,6 +128,10 @@ def execute(self, cmd_name="", others="", cell="", local_ns=None):
"connect": connect,
}

user_ns = self.shell.user_ns.copy()

cmd = router.get(cmd_name)
if cmd:
if cmd_name == "connect":
return cmd(others)
else:
return cmd(others, user_ns)
7 changes: 6 additions & 1 deletion src/sql/magic_plot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from IPython.core.magic import Magics, line_magic, magics_class
from IPython.core.magic import Magics, line_magic, magics_class, no_var_expand
from IPython.core.magic_arguments import argument, magic_arguments
from ploomber_core.exceptions import modify_exceptions

Expand All @@ -21,6 +21,7 @@
class SqlPlotMagic(Magics, Configurable):
"""%sqlplot magic"""

@no_var_expand
@line_magic("sqlplot")
@magic_arguments()
@argument(
Expand Down Expand Up @@ -81,8 +82,12 @@ def execute(self, line="", cell="", local_ns=None):
Plot magic
"""

user_ns = self.shell.user_ns.copy()

cmd = SQLPlotCommand(self, line)

util.expand_args(cmd.args, user_ns, ["table", "column"])

if len(cmd.args.column) == 1:
column = cmd.args.column[0]
else:
Expand Down
17 changes: 17 additions & 0 deletions src/sql/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from os.path import isfile
import re

from jinja2 import Template


try:
import toml
Expand Down Expand Up @@ -574,3 +576,18 @@ def enclose_table_with_double_quotations(table, conn):
_table = _table.replace('"', "`")

return _table


def expand_args(args, user_ns, attributes_to_expand):
"""Doc string"""
for attribute in attributes_to_expand:
value = getattr(args, attribute)
if value:
if isinstance(value, list):
setattr(
args,
attribute,
[Template(item).render(user_ns) for item in value],
)
else:
setattr(args, attribute, Template(value).render(user_ns))
Loading

0 comments on commit 854138d

Please sign in to comment.