forked from getredash/redash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove tree validations and introduce ParameterizedQuery (getredash#3230
- Loading branch information
Showing
10 changed files
with
120 additions
and
325 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import pystache | ||
from redash.utils import mustache_render | ||
from funcy import distinct | ||
|
||
|
||
def _collect_key_names(nodes): | ||
keys = [] | ||
for node in nodes._parse_tree: | ||
if isinstance(node, pystache.parser._EscapeNode): | ||
keys.append(node.key) | ||
elif isinstance(node, pystache.parser._SectionNode): | ||
keys.append(node.key) | ||
keys.extend(_collect_key_names(node.parsed)) | ||
|
||
return distinct(keys) | ||
|
||
|
||
def _collect_query_parameters(query): | ||
nodes = pystache.parse(query) | ||
keys = _collect_key_names(nodes) | ||
return keys | ||
|
||
|
||
def _parameter_names(parameter_values): | ||
names = [] | ||
for key, value in parameter_values.iteritems(): | ||
if isinstance(value, dict): | ||
for inner_key in value.keys(): | ||
names.append(u'{}.{}'.format(key, inner_key)) | ||
else: | ||
names.append(key) | ||
|
||
return names | ||
|
||
|
||
class ParameterizedQuery(object): | ||
def __init__(self, template): | ||
self.template = template | ||
self.query = template | ||
self.parameters = {} | ||
|
||
def apply(self, parameters): | ||
self.parameters.update(parameters) | ||
self.query = mustache_render(self.template, self.parameters) | ||
return self | ||
|
||
@property | ||
def missing_params(self): | ||
query_parameters = set(_collect_query_parameters(self.template)) | ||
return set(query_parameters) - set(_parameter_names(self.parameters)) | ||
|
||
@property | ||
def text(self): | ||
return self.query |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.