Skip to content

Commit

Permalink
var expansion save
Browse files Browse the repository at this point in the history
  • Loading branch information
neelasha23 committed Nov 16, 2023
1 parent 5d36832 commit 34c4cad
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

## 0.10.4dev
* [Fix] Fix bug causing empty result on SQL with trailing semicolon and comment (#907)

* [Fix] Fixed bug that returns empty results when exception is raised from DB driver
* [Feature] Variable expansion outside queries (#699)

## 0.10.3 (2023-11-06)

Expand Down
6 changes: 5 additions & 1 deletion src/sql/magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import warnings
import shlex
from jinja2 import Template
import sql.connection
import sql.parse
from sql.run.run import run_statements
Expand Down Expand Up @@ -554,7 +555,10 @@ def interactive_execute_wrapper(**kwargs):
+ " instead for the save argument.",
FutureWarning,
)
self._store.store(args.save, command.sql_original, with_=with_)

self._store.store(
Template(args.save).render(user_ns), command.sql_original, with_=with_
)

if args.no_execute:
display.message("Skipping execution...")
Expand Down
12 changes: 12 additions & 0 deletions src/tests/test_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2332,3 +2332,15 @@ def test_get_query_type(query, query_type):
def test_query_comment_after_semicolon(ip, query, expected):
result = ip.run_cell(query).result
assert list(result.dict().values())[-1][0] == expected


def test_bracket_var_substitution_save(ip):
ip.user_global_ns["col"] = "first_name"
ip.user_global_ns["snippet"] = "mysnippet"
ip.run_cell("%sql --save mysnippet SELECT * FROM author WHERE {{col}} = 'William' ")
out = ip.run_cell("%sql SELECT * FROM mysnippet").result
assert out[0] == (
"William",
"Shakespeare",
1616,
)

0 comments on commit 34c4cad

Please sign in to comment.