Skip to content

Commit

Permalink
Merge pull request #409 from erans/master
Browse files Browse the repository at this point in the history
Fix: minor fixes for MongoDB, script and Python query runners
  • Loading branch information
arikfr committed Apr 26, 2015
2 parents 513ef50 + 4ae4cff commit 5e8d0d3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
13 changes: 8 additions & 5 deletions redash/query_runner/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ def configuration_schema(cls):
'type': 'string',
'title': 'Replica Set Name'
},
'required': ['connectionString']
}
},
'required': ['connectionString']
}

@classmethod
Expand Down Expand Up @@ -132,8 +132,8 @@ def run_query(self, query):
s = None
if "sort" in query_data and query_data["sort"]:
s = []
for field_name in query_data["sort"]:
s.append((field_name, query_data["sort"][field_name]))
for field_data in query_data["sort"]:
s.append((field_data["name"], field_data["direction"]))

columns = []
rows = []
Expand All @@ -146,6 +146,9 @@ def run_query(self, query):
else:
cursor = db[collection].find(q, f)

if "limit" in query_data and query_data["limit"]:
cursor = cursor.limit(query_data["limit"])

for r in cursor:
for k in r:
if _get_column_by_name(columns, k) is None:
Expand Down Expand Up @@ -177,4 +180,4 @@ def run_query(self, query):

return json_data, error

register(MongoDB)
register(MongoDB)
3 changes: 3 additions & 0 deletions redash/query_runner/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def custom_import(name, globals=None, locals=None, fromlist=(), level=0):

raise Exception("'{0}' is not configured as a supported import module".format(name))

def custom_get_item(obj, key):
return obj[key]

def get_query_result(query_id):
try:
Expand Down Expand Up @@ -140,6 +142,7 @@ def run_query(self, query):
safe_builtins["getattr"] = getattr
safe_builtins["_setattr_"] = setattr
safe_builtins["setattr"] = setattr
safe_builtins["_getitem_"] = custom_get_item

script_locals = { "result" : { "rows" : [], "columns" : [] } }

Expand Down
10 changes: 6 additions & 4 deletions redash/query_runner/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def annotate_query(cls):
def __init__(self, configuration_json):
super(Script, self).__init__(configuration_json)

# Poor man's protection against running scripts from output the scripts directory
# Poor man's protection against running scripts from outside the scripts directory
if self.configuration["path"].find("../") > -1:
raise ValidationError("Scripts can only be run from the configured scripts directory")

Expand All @@ -41,11 +41,13 @@ def run_query(self, query):

query = query.strip()

script = os.path.join(self.configuration["path"], query)
script = os.path.join(self.configuration["path"], query.split(" ")[0])
if not os.path.exists(script):
return None, "Script '%s' not found in script directory" % query

output = subprocess.check_output(script, shell=False)
script = os.path.join(self.configuration["path"], query)

output = subprocess.check_output(script.split(" "), shell=False)
if output is not None:
output = output.strip()
if output != "":
Expand All @@ -62,4 +64,4 @@ def run_query(self, query):

return json_data, error

register(Script)
register(Script)

0 comments on commit 5e8d0d3

Please sign in to comment.