forked from getredash/redash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapd.py
More file actions
115 lines (100 loc) · 3.31 KB
/
mapd.py
File metadata and controls
115 lines (100 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from __future__ import absolute_import
try:
import pymapd
enabled = True
except ImportError:
enabled = False
from redash.query_runner import BaseSQLQueryRunner, register
from redash.query_runner import TYPE_STRING, TYPE_DATE, TYPE_DATETIME, TYPE_INTEGER, TYPE_FLOAT, TYPE_BOOLEAN
from redash.utils import json_dumps
TYPES_MAP = {
0: TYPE_INTEGER,
1: TYPE_INTEGER,
2: TYPE_INTEGER,
3: TYPE_FLOAT,
4: TYPE_FLOAT,
5: TYPE_FLOAT,
6: TYPE_STRING,
7: TYPE_DATE,
8: TYPE_DATETIME,
9: TYPE_DATE,
10: TYPE_BOOLEAN,
11: TYPE_DATE,
12: TYPE_DATE
}
class Mapd(BaseSQLQueryRunner):
@classmethod
def configuration_schema(cls):
return {
"type": "object",
"properties": {
"host": {
"type": "string",
"default": "localhost"
},
"port": {
"type": "number",
"default": 9091
},
"user": {
"type": "string",
"default": "mapd",
"title": "username"
},
"password": {
"type": "string",
"default": "HyperInteractive"
},
"database": {
"type": "string",
"default": "mapd"
}
},
"order": ["user", "password", "host", "port", "database"],
"required": ["host", "port", "user", "password", "database"],
"secret": ["password"]
}
@classmethod
def enabled(cls):
return enabled
def connect_database(self):
connection = pymapd.connect(
user=self.configuration['user'],
password=self.configuration['password'],
host=self.configuration['host'],
port=self.configuration['port'],
dbname=self.configuration['database']
)
return connection
def run_query(self, query, user):
connection = self.connect_database()
cursor = connection.cursor()
try:
cursor.execute(query)
columns = self.fetch_columns([(i[0], TYPES_MAP.get(i[1], None)) for i in cursor.description])
rows = [dict(zip((c['name'] for c in columns), row)) for row in cursor]
data = {'columns': columns, 'rows': rows}
error = None
json_data = json_dumps(data)
finally:
cursor.close()
connection.close()
return json_data, error
def _get_tables(self, schema):
connection = self.connect_database()
try:
for table_name in connection.get_tables():
schema[table_name] = {'name': table_name, 'columns': []}
for row_column in connection.get_table_details(table_name):
schema[table_name]['columns'].append(row_column[0])
finally:
connection.close
return schema.values()
def test_connection(self):
connection = self.connect_database()
try:
tables = connection.get_tables()
num_tables = tables.count(tables)
finally:
connection.close
register(Mapd)