X-Git-Url: https://git.jsancho.org/?p=datasette-pytables.git;a=blobdiff_plain;f=datasette_pytables%2F__init__.py;h=396cd0dbb93a09dbcd41da70b1d505e6a72d822c;hp=e8d0a6ccb4519f72d2e0680e0c3b4b8f44dc2574;hb=0cd020af033fcb2a15fe3326f3a49830b816379c;hpb=2cf7538022a4290be1cdbe6f8cf13e44f8190bee diff --git a/datasette_pytables/__init__.py b/datasette_pytables/__init__.py index e8d0a6c..396cd0d 100644 --- a/datasette_pytables/__init__.py +++ b/datasette_pytables/__init__.py @@ -1,9 +1,58 @@ -from collections import OrderedDict from moz_sql_parser import parse import re + import tables +import datasette_connectors as dc + + +class PyTablesConnection(dc.Connection): + def __init__(self, path, connector): + super().__init__(path, connector) + self.h5file = tables.open_file(path) + + +class PyTablesConnector(dc.Connector): + connector_type = 'pytables' + connection_class = PyTablesConnection + + def table_names(self): + return [ + node._v_pathname + for node in self.conn.h5file + if not(isinstance(node, tables.group.Group)) + ] + + def table_count(self, table_name): + table = self.conn.h5file.get_node(table_name) + return int(table.nrows) + + def table_info(self, table_name): + table = self.conn.h5file.get_node(table_name) + colnames = ['value'] + if isinstance(table, tables.table.Table): + colnames = table.colnames + + return [ + { + 'idx': idx, + 'name': colname, + 'primary_key': False, + } + for idx, colname in enumerate(colnames) + ] + + def hidden_table_names(self): + return [] + + def detect_spatialite(self): + return False + + def view_names(self): + return [] + + def detect_fts(self, table_name): + return False -_connector_type = 'pytables' def inspect(path): "Open file and return tables info" @@ -12,7 +61,7 @@ def inspect(path): h5file = tables.open_file(path) for table in filter(lambda node: not(isinstance(node, tables.group.Group)), h5file): - colnames = [] + colnames = ['value'] if isinstance(table, tables.table.Table): colnames = table.colnames @@ -32,12 +81,24 @@ def inspect(path): def _parse_sql(sql, params): # Table name - sql = re.sub('(?i)from \[(.*)]', 'from "\g<1>"', sql) + sql = re.sub(r'(?i)from \[(.*)]', r'from "\g<1>"', sql) # Params for param in params: sql = sql.replace(":" + param, param) - parsed = parse(sql) + try: + parsed = parse(sql) + except: + # Propably it's a PyTables expression + for token in ['group by', 'order by', 'limit', '']: + res = re.search('(?i)where (.*)' + token, sql) + if res: + modified_sql = re.sub('(?i)where (.*)(' + token + ')', r'\g<2>', sql) + parsed = parse(modified_sql) + parsed['where'] = res.group(1).strip() + break + + # Always a list of fields if type(parsed['select']) is not list: parsed['select'] = [parsed['select']] @@ -59,7 +120,7 @@ class Connection: self.path = path self.h5file = tables.open_file(path) - def execute(self, sql, params=None, truncate=False): + def execute(self, sql, params=None, truncate=False, page_size=None, max_returned_rows=None): if params is None: params = {} rows = [] @@ -67,9 +128,18 @@ class Connection: description = [] parsed_sql = _parse_sql(sql, params) + + if parsed_sql['from'] == 'sqlite_master': + rows = self._execute_datasette_query(sql, params) + description = (('value',),) + return rows, truncated, description + table = self.h5file.get_node(parsed_sql['from']) table_rows = [] fields = parsed_sql['select'] + colnames = ['value'] + if type(table) is tables.table.Table: + colnames = table.colnames query = '' start = 0 @@ -78,7 +148,9 @@ class Connection: # Use 'where' statement or get all the rows def _cast_param(field, pname): # Cast value to the column type - coltype = table.coltypes[field] + coltype = table.dtype.name + if type(table) is tables.table.Table: + coltype = table.coltypes[field] fcast = None if coltype == 'string': fcast = str @@ -91,16 +163,22 @@ class Connection: def _translate_where(where): # Translate SQL to PyTables expression + nonlocal start, end expr = '' operator = list(where)[0] if operator in ['and', 'or']: - subexpr = ["({})".format(_translate_where(q)) for q in where[operator]] + subexpr = [_translate_where(e) for e in where[operator]] + subexpr = filter(lambda e: e, subexpr) + subexpr = ["({})".format(e) for e in subexpr] expr = " {} ".format(_operators[operator]).join(subexpr) + elif operator == 'exists': + pass elif where == {'eq': ['rowid', 'p0']}: - nonlocal start, end start = int(params['p0']) end = start + 1 + elif where == {'gt': ['rowid', 'p0']}: + start = int(params['p0']) + 1 else: left, right = where[operator] if left in params: @@ -113,64 +191,151 @@ class Connection: return expr if 'where' in parsed_sql: - try: + if type(parsed_sql['where']) is dict: query = _translate_where(parsed_sql['where']) - except: - # Probably it's a PyTables query - query = str(parsed_sql['where'])[6:] # without where keyword + else: + query = parsed_sql['where'] + + # Sort by column + orderby = '' + if 'orderby' in parsed_sql: + orderby = parsed_sql['orderby'] + if type(orderby) is list: + orderby = orderby[0] + orderby = orderby['value'] + if orderby == 'rowid': + orderby = '' # Limit number of rows + limit = None if 'limit' in parsed_sql: - max_rows = int(parsed_sql['limit']) - if end - start > max_rows: - end = start + max_rows + limit = int(parsed_sql['limit']) + + # Truncate if needed + if page_size and max_returned_rows and truncate: + if max_returned_rows == page_size: + max_returned_rows += 1 # Execute query if query: table_rows = table.where(query, params, start, end) + elif orderby: + table_rows = table.itersorted(orderby, start=start, stop=end) else: table_rows = table.iterrows(start, end) # Prepare rows + def normalize_field_value(value): + if type(value) is bytes: + return value.decode('utf-8') + elif not type(value) in (int, float, complex): + return str(value) + else: + return value + + def make_get_rowid(): + if type(table) is tables.table.Table: + def get_rowid(row): + return int(row.nrow) + else: + rowid = start - 1 + def get_rowid(row): + nonlocal rowid + rowid += 1 + return rowid + return get_rowid + + def make_get_row_value(): + if type(table) is tables.table.Table: + def get_row_value(row, field): + return row[field] + else: + def get_row_value(row, field): + return row + return get_row_value + if len(fields) == 1 and type(fields[0]['value']) is dict and \ fields[0]['value'].get('count') == '*': rows.append(Row({'count(*)': int(table.nrows)})) else: + get_rowid = make_get_rowid() + get_row_value = make_get_row_value() + count = 0 for table_row in table_rows: + count += 1 + if limit and count > limit: + break + if truncate and max_returned_rows and count > max_returned_rows: + truncated = True + break row = Row() for field in fields: - if field['value'] == 'rowid': - row['rowid'] = int(table_row.nrow) - elif field['value'] == '*': - for col in table.colnames: - value = table_row[col] - if type(value) is bytes: - value = value.decode('utf-8') - row[col] = value + field_name = field['value'] + if type(field_name) is dict and 'distinct' in field_name: + field_name = field_name['distinct'] + if field_name == 'rowid': + row['rowid'] = get_rowid(table_row) + elif field_name == '*': + for col in colnames: + row[col] = normalize_field_value(get_row_value(table_row, col)) else: - row[field['value']] = table_row[field['value']] + row[field_name] = normalize_field_value(get_row_value(table_row, field_name)) rows.append(row) # Prepare query description for field in [f['value'] for f in fields]: if field == '*': - for col in table.colnames: + for col in colnames: description.append((col,)) else: description.append((field,)) # Return the rows - if truncate: - return rows, truncated, tuple(description) + return rows, truncated, tuple(description) + + def _execute_datasette_query(self, sql, params): + "Datasette special queries for getting tables info" + if sql == 'select sql from sqlite_master where name = :n and type=:t': + if params['t'] == 'view': + return [] + else: + try: + table = self.h5file.get_node(params['n']) + colnames = ['value'] + if type(table) is tables.table.Table: + colnames = table.colnames + row = Row() + row['sql'] = 'CREATE TABLE {} ({})'.format(params['n'], ", ".join(colnames)) + return [row] + except: + return [] + else: + raise Exception("SQLite queries cannot be executed with this connector: %s, %s" % (sql, params)) + + +class Row(list): + def __init__(self, values=None): + self.labels = [] + self.values = [] + if values: + for idx in values: + self.__setitem__(idx, values[idx]) + + def __setitem__(self, idx, value): + if type(idx) is str: + if idx in self.labels: + self.values[self.labels.index(idx)] = value + else: + self.labels.append(idx) + self.values.append(value) else: - return rows + self.values[idx] = value -class Row(OrderedDict): - def __getitem__(self, label): - if type(label) is int: - return super(OrderedDict, self).__getitem__(list(self.keys())[label]) + def __getitem__(self, idx): + if type(idx) is str: + return self.values[self.labels.index(idx)] else: - return super(OrderedDict, self).__getitem__(label) + return self.values[idx] def __iter__(self): - return self.values().__iter__() + return self.values.__iter__()