|
| 1 | +try: |
| 2 | + from databend_sqlalchemy import connector |
| 3 | + import re |
| 4 | + |
| 5 | + enabled = True |
| 6 | +except ImportError: |
| 7 | + enabled = False |
| 8 | + |
| 9 | +from redash.query_runner import BaseQueryRunner, register |
| 10 | +from redash.query_runner import TYPE_STRING, TYPE_INTEGER, TYPE_BOOLEAN, TYPE_FLOAT, TYPE_DATETIME, TYPE_DATE |
| 11 | +from redash.utils import json_dumps, json_loads |
| 12 | + |
| 13 | + |
| 14 | +class Databend(BaseQueryRunner): |
| 15 | + noop_query = "SELECT 1" |
| 16 | + |
| 17 | + @classmethod |
| 18 | + def configuration_schema(cls): |
| 19 | + return { |
| 20 | + "type": "object", |
| 21 | + "properties": { |
| 22 | + "host": {"type": "string", "default": "localhost"}, |
| 23 | + "port": {"type": "int", "default": 8000}, |
| 24 | + "username": {"type": "string"}, |
| 25 | + "password": {"type": "string", "default": ""}, |
| 26 | + "database": {"type": "string"}, |
| 27 | + "secure": {"type": "string", "default": False}, |
| 28 | + }, |
| 29 | + "order": ["username", "password", "host", "port", "database"], |
| 30 | + "required": ["username", "database"], |
| 31 | + "secret": ["password"], |
| 32 | + } |
| 33 | + |
| 34 | + @classmethod |
| 35 | + def name(cls): |
| 36 | + return "Databend" |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def type(cls): |
| 40 | + return "Databend" |
| 41 | + |
| 42 | + @classmethod |
| 43 | + def enabled(cls): |
| 44 | + return enabled |
| 45 | + |
| 46 | + @staticmethod |
| 47 | + def _define_column_type(column_type): |
| 48 | + c = column_type.lower() |
| 49 | + f = re.search(r"^nullable\((.*)\)$", c) |
| 50 | + if f is not None: |
| 51 | + c = f.group(1) |
| 52 | + if c.startswith("int") or c.startswith("uint"): |
| 53 | + return TYPE_INTEGER |
| 54 | + elif c.startswith("float"): |
| 55 | + return TYPE_FLOAT |
| 56 | + elif c == "datetime": |
| 57 | + return TYPE_DATETIME |
| 58 | + elif c == "date": |
| 59 | + return TYPE_DATE |
| 60 | + else: |
| 61 | + return TYPE_STRING |
| 62 | + |
| 63 | + def run_query(self, query, user): |
| 64 | + host = (self.configuration.get("host") or "localhost"), |
| 65 | + port = (self.configuration.get("port") or 8000), |
| 66 | + username = (self.configuration.get("username") or None), |
| 67 | + password = (self.configuration.get("password") or None), |
| 68 | + database = (self.configuration.get("database") or None), |
| 69 | + secure = (self.configuration.get("secure") or False), |
| 70 | + connection = connector.connect(f"databend://{username}:{password}@{host}:{port}/{database}?secure={secure}") |
| 71 | + cursor = connection.cursor() |
| 72 | + |
| 73 | + try: |
| 74 | + cursor.execute(query) |
| 75 | + columns = self.fetch_columns( |
| 76 | + [(i[0], self._define_column_type(i[1])) for i in cursor.description] |
| 77 | + ) |
| 78 | + rows = [ |
| 79 | + dict(zip((column["name"] for column in columns), row)) for row in cursor |
| 80 | + ] |
| 81 | + |
| 82 | + data = {"columns": columns, "rows": rows} |
| 83 | + error = None |
| 84 | + json_data = json_dumps(data) |
| 85 | + finally: |
| 86 | + connection.close() |
| 87 | + |
| 88 | + return json_data, error |
| 89 | + |
| 90 | + def _get_tables(self): |
| 91 | + query = """ |
| 92 | + SELECT TABLE_SCHEMA, |
| 93 | + TABLE_NAME, |
| 94 | + COLUMN_NAME |
| 95 | + FROM INFORMATION_SCHEMA.COLUMNS |
| 96 | + WHERE TABLE_SCHEMA NOT IN ('information_schema', 'system') |
| 97 | + """ |
| 98 | + |
| 99 | + results, error = self.run_query(query, None) |
| 100 | + |
| 101 | + if error is not None: |
| 102 | + self._handle_run_query_error(error) |
| 103 | + |
| 104 | + schema = {} |
| 105 | + results = json_loads(results) |
| 106 | + |
| 107 | + for row in results["rows"]: |
| 108 | + table_name = "{}.{}".format(row["table_schema"], row["table_name"]) |
| 109 | + |
| 110 | + if table_name not in schema: |
| 111 | + schema[table_name] = {"name": table_name, "columns": []} |
| 112 | + |
| 113 | + schema[table_name]["columns"].append(row["column_name"]) |
| 114 | + |
| 115 | + return list(schema.values()) |
| 116 | + |
| 117 | + |
| 118 | +register(Databend) |
0 commit comments