|
| 1 | +import logging |
| 2 | +from contextlib import contextmanager |
| 3 | + |
| 4 | +from Orange.util import Registry |
| 5 | + |
| 6 | +log = logging.getLogger(__name__) |
| 7 | + |
| 8 | + |
| 9 | +class BackendError(Exception): |
| 10 | + pass |
| 11 | + |
| 12 | + |
| 13 | +class Backend(metaclass=Registry): |
| 14 | + """Base class for SqlTable backends. Implementations should define |
| 15 | + all of the methods defined below. |
| 16 | +
|
| 17 | + Parameters |
| 18 | + ---------- |
| 19 | + connection_params: dict |
| 20 | + connection params |
| 21 | + """ |
| 22 | + |
| 23 | + display_name = "" |
| 24 | + |
| 25 | + def __init__(self, connection_params): |
| 26 | + self.connection_params = connection_params |
| 27 | + |
| 28 | + @classmethod |
| 29 | + def available_backends(cls): |
| 30 | + """Return a list of all available backends""" |
| 31 | + return cls.registry.values() |
| 32 | + |
| 33 | + # "meta" methods |
| 34 | + |
| 35 | + def list_tables_query(self, schema=None): |
| 36 | + """Return a list of tuples (schema, table_name) |
| 37 | +
|
| 38 | + Parameters |
| 39 | + ---------- |
| 40 | + schema : Optional[str] |
| 41 | + If set, only tables from schema should be listed |
| 42 | +
|
| 43 | + Returns |
| 44 | + ------- |
| 45 | + A list of tuples |
| 46 | + """ |
| 47 | + raise NotImplementedError |
| 48 | + |
| 49 | + def list_tables(self, schema=None): |
| 50 | + """Return a list of tables in database |
| 51 | +
|
| 52 | + Parameters |
| 53 | + ---------- |
| 54 | + schema : Optional[str] |
| 55 | + If set, only tables from given schema will be listed |
| 56 | +
|
| 57 | + Returns |
| 58 | + ------- |
| 59 | + A list of TableDesc objects, describing the tables in the database |
| 60 | + """ |
| 61 | + query = self.list_tables_query(schema) |
| 62 | + with self.execute_sql_query(query) as cur: |
| 63 | + tables = [] |
| 64 | + for schema, name in cur.fetchall(): |
| 65 | + sql = "{}.{}".format( |
| 66 | + self.quote_identifier(schema), |
| 67 | + self.quote_identifier(name)) if schema else self.quote_identifier(name) |
| 68 | + tables.append(TableDesc(name, schema, sql)) |
| 69 | + return tables |
| 70 | + |
| 71 | + def get_fields(self, table_name): |
| 72 | + """Return a list of field names and metadata in the given table |
| 73 | +
|
| 74 | + Parameters |
| 75 | + ---------- |
| 76 | + table_name: str |
| 77 | +
|
| 78 | + Returns |
| 79 | + ------- |
| 80 | + a list of tuples (field_name, *field_metadata) |
| 81 | + both will be passed to create_variable |
| 82 | + """ |
| 83 | + query = self.create_sql_query(table_name, ["*"], limit=0) |
| 84 | + with self.execute_sql_query(query) as cur: |
| 85 | + return cur.description |
| 86 | + |
| 87 | + def get_distinct_values(self, field_name, table_name): |
| 88 | + """Return a list of distinct values of field |
| 89 | +
|
| 90 | + Parameters |
| 91 | + ---------- |
| 92 | + field_name : name of the field |
| 93 | + table_name : name of the table or query to search |
| 94 | +
|
| 95 | + Returns |
| 96 | + ------- |
| 97 | + List[str] of values |
| 98 | + """ |
| 99 | + fields = [self.quote_identifier(field_name)] |
| 100 | + |
| 101 | + query = self.create_sql_query(table_name, fields, |
| 102 | + group_by=fields, order_by=fields, |
| 103 | + limit=21) |
| 104 | + with self.execute_sql_query(query) as cur: |
| 105 | + values = cur.fetchall() |
| 106 | + if len(values) > 20: |
| 107 | + return () |
| 108 | + else: |
| 109 | + return tuple(str(x[0]) for x in values) |
| 110 | + |
| 111 | + def create_variable(self, field_name, field_metadata, |
| 112 | + type_hints, inspect_table=None): |
| 113 | + """Create variable based on field information |
| 114 | +
|
| 115 | + Parameters |
| 116 | + ---------- |
| 117 | + field_name : str |
| 118 | + name do the field |
| 119 | + field_metadata : tuple |
| 120 | + data to guess field type from |
| 121 | + type_hints : Domain |
| 122 | + domain with variable templates |
| 123 | + inspect_table : Option[str] |
| 124 | + name of the table to expect the field values or None |
| 125 | + if no inspection is to be performed |
| 126 | +
|
| 127 | + Returns |
| 128 | + ------- |
| 129 | + Variable representing the field |
| 130 | + """ |
| 131 | + raise NotImplementedError |
| 132 | + |
| 133 | + def count_approx(self, query): |
| 134 | + """Return estimated number of rows returned by query. |
| 135 | +
|
| 136 | + Parameters |
| 137 | + ---------- |
| 138 | + query : str |
| 139 | +
|
| 140 | + Returns |
| 141 | + ------- |
| 142 | + Approximate number of rows |
| 143 | + """ |
| 144 | + raise NotImplementedError |
| 145 | + |
| 146 | + # query related methods |
| 147 | + |
| 148 | + def create_sql_query( |
| 149 | + self, table_name, fields, filters=(), |
| 150 | + group_by=None, order_by=None, offset=None, limit=None, |
| 151 | + use_time_sample=None): |
| 152 | + """Construct an sql query using the provided elements. |
| 153 | +
|
| 154 | + Parameters |
| 155 | + ---------- |
| 156 | + table_name : str |
| 157 | + fields : List[str] |
| 158 | + filters : List[str] |
| 159 | + group_by: List[str] |
| 160 | + order_by: List[str] |
| 161 | + offset: int |
| 162 | + limit: int |
| 163 | + use_time_sample: int |
| 164 | +
|
| 165 | + Returns |
| 166 | + ------- |
| 167 | + string containing sql query |
| 168 | + """ |
| 169 | + raise NotImplementedError |
| 170 | + |
| 171 | + @contextmanager |
| 172 | + def execute_sql_query(self, query, params=None): |
| 173 | + """Context manager for execution of sql queries |
| 174 | +
|
| 175 | + Usage: |
| 176 | + ``` |
| 177 | + with backend.execute_sql_query("SELECT * FROM foo") as cur: |
| 178 | + cur.fetch_all() |
| 179 | + ``` |
| 180 | +
|
| 181 | + Parameters |
| 182 | + ---------- |
| 183 | + query : string |
| 184 | + query to be executed |
| 185 | + params: tuple |
| 186 | + parameters to be passed to the query |
| 187 | +
|
| 188 | + Returns |
| 189 | + ------- |
| 190 | + yields a cursor that can be used to access the data |
| 191 | + """ |
| 192 | + raise NotImplementedError |
| 193 | + |
| 194 | + def quote_identifier(self, name): |
| 195 | + """Quote identifier name so it can be safely used in queries |
| 196 | +
|
| 197 | + Parameters |
| 198 | + ---------- |
| 199 | + name: str |
| 200 | + name of the parameter |
| 201 | +
|
| 202 | + Returns |
| 203 | + ------- |
| 204 | + quoted parameter that can be used in sql queries |
| 205 | + """ |
| 206 | + raise NotImplementedError |
| 207 | + |
| 208 | + def unquote_identifier(self, quoted_name): |
| 209 | + """Remove quotes from identifier name |
| 210 | + Used when sql table name is used in where parameter to |
| 211 | + query special tables |
| 212 | +
|
| 213 | + Parameters |
| 214 | + ---------- |
| 215 | + quoted_name : str |
| 216 | +
|
| 217 | + Returns |
| 218 | + ------- |
| 219 | + unquoted name |
| 220 | + """ |
| 221 | + raise NotImplementedError |
| 222 | + |
| 223 | + |
| 224 | +class TableDesc: |
| 225 | + def __init__(self, name, schema, sql): |
| 226 | + self.name = name |
| 227 | + self.schema = schema |
| 228 | + self.sql = sql |
| 229 | + |
| 230 | + def __str__(self): |
| 231 | + return self.name |
| 232 | + |
| 233 | +class ToSql: |
| 234 | + def __init__(self, sql): |
| 235 | + self.sql = sql |
| 236 | + |
| 237 | + def __call__(self): |
| 238 | + return self.sql |
0 commit comments