X-Git-Url: https://git.jsancho.org/?p=datasette-pytables.git;a=blobdiff_plain;f=datasette_pytables%2F__init__.py;h=5d9fb22f78a88601900a08396b40541267f967c1;hp=6e34774bc24e83688571fcac6645741a9c98ba28;hb=a0ba326e2827753049cf411967bea0503c30154a;hpb=31f57a6c3e8cf26430cebdb091ca52c1fe6d66ac diff --git a/datasette_pytables/__init__.py b/datasette_pytables/__init__.py index 6e34774..5d9fb22 100644 --- a/datasette_pytables/__init__.py +++ b/datasette_pytables/__init__.py @@ -71,7 +71,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): if params is None: params = {} rows = [] @@ -79,6 +79,10 @@ class Connection: description = [] parsed_sql = _parse_sql(sql, params) + + if parsed_sql['from'] == 'sqlite_master': + return self._execute_datasette_query(sql, params) + table = self.h5file.get_node(parsed_sql['from']) table_rows = [] fields = parsed_sql['select'] @@ -140,6 +144,12 @@ class Connection: if end - start > max_rows: end = start + max_rows + # Truncate if needed + if page_size and truncate: + if end - start > page_size: + end = start + page_size + truncated = True + # Execute query if query: table_rows = table.where(query, params, start, end) @@ -205,6 +215,23 @@ class Connection: else: return rows + 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']) + row = Row() + row['sql'] = 'CREATE TABLE {} ()'.format(params['n']) + return [row] + except: + return [] + else: + raise Exception("SQLite queries cannot be executed with this connector") + class Row(OrderedDict): def __getitem__(self, label): if type(label) is int: