X-Git-Url: https://git.jsancho.org/?p=datasette-pytables.git;a=blobdiff_plain;f=datasette_pytables%2F__init__.py;h=52979829a08a3709629d6e5ffe9c7a3750bbefd7;hp=93f9b89cfa0577b9d4b10215cec6b191c595c3d3;hb=af831c7abf27458e94524ba768764aa232a6bb13;hpb=8d58881ab27b2acfc1f08f4108f8b4ac443b0204 diff --git a/datasette_pytables/__init__.py b/datasette_pytables/__init__.py index 93f9b89..5297982 100644 --- a/datasette_pytables/__init__.py +++ b/datasette_pytables/__init__.py @@ -81,7 +81,7 @@ class Connection: if parsed_sql['from'] == 'sqlite_master': rows = self._execute_datasette_query(sql, params) - description = (('value',)) + description = (('value',),) return rows, truncated, description table = self.h5file.get_node(parsed_sql['from']) @@ -146,6 +146,16 @@ class Connection: 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: @@ -159,6 +169,8 @@ class Connection: # 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) @@ -233,23 +245,22 @@ class Connection: def _execute_datasette_query(self, sql, params): "Datasette special queries for getting tables info" - if sql == "SELECT count(*) from sqlite_master WHERE type = 'view' and name=:n": - row = Row() - row['count(*)'] = 0 - return [row] - elif sql == 'select sql from sqlite_master where name = :n and type="table"': - 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: + 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") + raise Exception("SQLite queries cannot be executed with this connector: %s, %s" % (sql, params)) class Row(list):