Skip to content

Commit

Permalink
Make prepare_query accept a pre-compiled template
Browse files Browse the repository at this point in the history
  • Loading branch information
Photonios committed Aug 12, 2019
1 parent c81795b commit 23f88fa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion jinjasql/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ def _prepare_environment(self):
self.env.filters["inclause"] = bind_in_clause

def prepare_query(self, source, data):
template = self.env.from_string(source)
if isinstance(source, Template):
template = source
else:
template = self.env.from_string(source)

return self._prepare_query(template, data)

def _prepare_query(self, template, data):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_jinjasql.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ def test_include(self):
self.assertEquals(len(bind_params), 1)
self.assertEquals(list(bind_params)[0], 123)

def test_precompiled_template(self):
source = "select * from dummy where project_id = {{ request.project_id }}"

j = JinjaSql()
query, bind_params = j.prepare_query(j.env.from_string(source), _DATA)

expected_query = "select * from dummy where project_id = %s"
self.assertEquals(query.strip(), expected_query.strip())

def generate_yaml_tests():
file_path = join(YAML_TESTS_ROOT, "macros.yaml")
with open(file_path) as f:
Expand Down

0 comments on commit 23f88fa

Please sign in to comment.