Skip to content

Commit

Permalink
subquery and more detailed error message added
Browse files Browse the repository at this point in the history
  • Loading branch information
bbeat2782 committed Jun 28, 2023
1 parent da925d3 commit 97dd662
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
29 changes: 20 additions & 9 deletions doc/api/magic-snippets.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,36 @@ Arguments:
`-A`/`--delete-force-all` Force delete a snippet and all dependent snippets.

```{code-cell} ipython3
gentoo_snippet = %sqlcmd snippets gentoo
print(gentoo_snippet)
chinstrap_snippet = %sqlcmd snippets chinstrap
print(chinstrap_snippet)
```

This returns the stored snippet `gentoo`.
This returns the stored snippet `chinstrap`.

Calling `%sqlcmd snippets {snippet_name}` also works on a snippet that is dependent on others. To demonstrate it, let's create a snippet dependent on `chinstrap` snippet.

```{code-cell} ipython3
%%sql --save chinstrap_sub
SELECT * FROM chinstrap where island == 'Dream'
```

```{code-cell} ipython3
chinstrap_sub_snippet = %sqlcmd snippets chinstrap_sub
print(chinstrap_sub_snippet)
```

This returns the stored snippet `chinstrap_sub`.

```{code-cell} ipython3
%sqlcmd snippets -d gentoo
```

This deletes the stored snippet `gentoo`.

To demonstrate `force-delete` let's create a snippet dependent on `chinstrap` snippet.
To demonstrate `force-delete` let's first confirm there is a snippet dependent on `chinstrap` snippet.

```{code-cell} ipython3
:tags: [hide-output]
%%sql --save chinstrap_sub
SELECT * FROM chinstrap where island == 'Dream'
%sqlcmd snippets
```

Trying to delete the `chinstrap` snippet will display an error message:
Expand All @@ -110,7 +121,7 @@ Trying to delete the `chinstrap` snippet will display an error message:
%sqlcmd snippets -d chinstrap
```

If you still wish to delete this snippet, you can run the below command:
If you still wish to delete this snippet, you should use `force-delete` by running the below command:

```{code-cell} ipython3
%sqlcmd snippets -D chinstrap
Expand Down
21 changes: 16 additions & 5 deletions src/sql/cmd/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,24 @@ def snippets(others):
if others[0] in all_snippets:
return str(store[others[0]])

if len(all_snippets) < 3:
snippets_format = " and ".join(all_snippets)
base_err_msg = f"'{others[0]}' is not a snippet. "
if len(all_snippets) == 0:
err_msg = "%sThere is no available snippet."
elif len(all_snippets) < 3:
err_msg = (
"%sAvailable snippets are "
f"{' and '.join(all_snippets)}."
)
else:
snippets_format = f"{', '.join(all_snippets[:-1])}, and {all_snippets[-1]}"
err_msg = (
"%sAvailable snippets are "
f"{', '.join(all_snippets[:-1])}, and "
f"{all_snippets[-1]}."
)
err_msg = err_msg % (base_err_msg)

raise UsageError(
f"'{others[0]}' is not a snippet. "
f"Available snippets are {snippets_format}"
err_msg
)

args = parser.parse_args(others)
Expand Down
9 changes: 7 additions & 2 deletions src/tests/test_magic_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,17 +429,22 @@ def test_snippet(ip_snippets):
"%sqlcmd snippets invalid",
(
"'invalid' is not a snippet. Available snippets are high_price, "
"high_price_a, and high_price_b"
"high_price_a, and high_price_b."
),
),
(
"%sqlcmd snippets -d high_price_b",
"%sqlcmd snippets invalid",
(
"'invalid' is not a snippet. Available snippets are high_price "
"and high_price_a"
"and high_price_a."
),
),
(
"%sqlcmd snippets -A high_price",
"%sqlcmd snippets invalid",
"'invalid' is not a snippet. There is no available snippet."
)
],
)
def test_invalid_snippet(ip_snippets, precmd, cmd, err_msg):
Expand Down

0 comments on commit 97dd662

Please sign in to comment.