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.
Merge pull request getredash#650 from alonho/mql
Feature: MQL query runner
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import json | ||
|
||
from . import BaseQueryRunner, register | ||
from .mongodb import TYPES_MAP, TYPE_STRING | ||
|
||
try: | ||
import pymongo | ||
from ognom import query_to_plan | ||
from website.server.utils import simplify | ||
enabled = True | ||
except ImportError: | ||
enabled = False | ||
|
||
def deduce_columns(rows): | ||
column_to_type = {} | ||
for row in rows: | ||
for column, value in row.iteritems(): | ||
column_to_type[column] = TYPES_MAP.get(value.__class__, TYPE_STRING) | ||
return [{'name': column, 'friendly_name': column, 'type': type} | ||
for column, type in column_to_type.iteritems()] | ||
|
||
class MQL(BaseQueryRunner): | ||
|
||
def __init__(self, configuration_json): | ||
super(MQL, self).__init__(configuration_json) | ||
self.syntax = 'sql' | ||
|
||
@classmethod | ||
def enabled(cls): | ||
return enabled | ||
|
||
@classmethod | ||
def annotate_query(cls): | ||
return False | ||
|
||
@classmethod | ||
def configuration_schema(cls): | ||
return { | ||
'type': 'object', | ||
'properties': { | ||
'uri': { | ||
'type': 'string', | ||
'title': 'Connection String' | ||
} | ||
}, | ||
'required': ['uri'] | ||
} | ||
|
||
def run_query(self, query): | ||
conn = pymongo.MongoClient(self.configuration['uri']) | ||
# execute() returns a generator (that wraps a cursor) | ||
gen = query_to_plan(query).execute(conn) | ||
# simplify converts special MongoDB data types (ObjectId, Date, etc') to strings | ||
result = simplify(list(gen)) | ||
return json.dumps({'columns': deduce_columns(result), 'rows': result}), None | ||
|
||
register(MQL) |
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