X-Git-Url: https://git.jsancho.org/?p=datasette-pytables.git;a=blobdiff_plain;f=datasette_pytables%2F__init__.py;h=4c1cc0f9837aff45375c1f6c1654b8e71d359094;hp=889dccadbf9f4ad453022e9c7d6c5acdfa3bd430;hb=29e72871ce6657fd858dddb1689de8b64d7b6f54;hpb=8f981acbd074be2eec098253112f8ef479373a5e diff --git a/datasette_pytables/__init__.py b/datasette_pytables/__init__.py index 889dcca..4c1cc0f 100644 --- a/datasette_pytables/__init__.py +++ b/datasette_pytables/__init__.py @@ -26,30 +26,50 @@ class PyTablesConnector(dc.Connector): 'binary_or': '|', } + def _serialize_table_name(self, table_name): + return table_name.replace('/', '%') + + def _deserialize_table_name(self, table_name): + return table_name.replace('%', '/') + def table_names(self): return [ - node._v_pathname + self._serialize_table_name(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) + table = self.conn.h5file.get_node(self._deserialize_table_name(table_name)) return int(table.nrows) def table_info(self, table_name): - table = self.conn.h5file.get_node(table_name) - colnames = ['value'] + table = self.conn.h5file.get_node(self._deserialize_table_name(table_name)) + columns = [ + { + 'name': 'value', + 'type': table.dtype.name, + } + ] if isinstance(table, tables.table.Table): - colnames = table.colnames + columns = [ + { + 'name': colname, + 'type': table.coltypes[colname], + } + for colname in table.colnames + ] return [ { - 'idx': idx, - 'name': colname, - 'primary_key': False, + 'cid': cid, + 'name': column['name'], + 'type': column['type'], + 'notnull': True, + 'default_value': None, + 'is_pk': False, } - for idx, colname in enumerate(colnames) + for cid, column in enumerate(columns) ] def hidden_table_names(self): @@ -69,12 +89,13 @@ class PyTablesConnector(dc.Connector): def table_exists(self, table_name): try: - self.conn.h5file.get_node(table_name) + self.conn.h5file.get_node(self._deserialize_table_name(table_name)) return True except: return False def table_definition(self, table_type, table_name): + table_name = self._deserialize_table_name(table_name) table = self.conn.h5file.get_node(table_name) colnames = ['value'] if isinstance(table, tables.table.Table): @@ -101,13 +122,17 @@ class PyTablesConnector(dc.Connector): truncated = False description = () + # Some Datasette queries uses glob operand, not supported by Pytables + if ' glob ' in sql: + return results, truncated, description + parsed_sql = parse_sql(sql, params) while isinstance(parsed_sql['from'], dict): # Pytables does not support subqueries parsed_sql['from'] = parsed_sql['from']['value']['from'] - table = self.conn.h5file.get_node(parsed_sql['from']) + table = self.conn.h5file.get_node(self._deserialize_table_name(parsed_sql['from'])) table_rows = [] fields = parsed_sql['select'] colnames = ['value'] @@ -210,8 +235,7 @@ class PyTablesConnector(dc.Connector): # Execute query if query: - if not ' glob ' in query: - table_rows = table.where(query, params, start, end) + table_rows = table.where(query, params, start, end) elif orderby: table_rows = table.itersorted(orderby, start=start, stop=end) else: @@ -277,7 +301,7 @@ class PyTablesConnector(dc.Connector): row['count(*)'] = int(table.nrows) elif field_name.get('json_type'): field_name = field_name.get('json_type') - row['json_type(' + field_name + ')'] = _get_field_type(field) + row['json_type(' + field_name + ')'] = _get_field_type(field_name) else: raise Exception("Function not recognized") else: