-from collections import OrderedDict
from moz_sql_parser import parse
import re
import tables
self.path = path
self.h5file = tables.open_file(path)
- def execute(self, sql, params=None, truncate=False, page_size=None):
+ def execute(self, sql, params=None, truncate=False, page_size=None, max_returned_rows=None):
if params is None:
params = {}
rows = []
parsed_sql = _parse_sql(sql, params)
if parsed_sql['from'] == 'sqlite_master':
- return self._execute_datasette_query(sql, params)
+ rows = self._execute_datasette_query(sql, params)
+ description = (('value',))
+ return rows, truncated, description
table = self.h5file.get_node(parsed_sql['from'])
table_rows = []
# Use 'where' statement or get all the rows
def _cast_param(field, pname):
# Cast value to the column type
- coltype = table.coltypes[field]
+ if type(table) is tables.table.Table:
+ coltype = table.coltypes[field]
+ else:
+ coltype = table.dtype.name
fcast = None
if coltype == 'string':
fcast = str
def _translate_where(where):
# Translate SQL to PyTables expression
+ nonlocal start, end
expr = ''
operator = list(where)[0]
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:
end = start + max_rows
# Truncate if needed
- if page_size and truncate:
- if end - start > page_size:
- end = start + page_size
+ if page_size and max_returned_rows and truncate:
+ if max_returned_rows == page_size:
+ max_returned_rows += 1
+ if end - start > max_returned_rows:
+ end = start + max_returned_rows
truncated = True
# Execute query
description.append((field,))
# Return the rows
- if truncate:
- return rows, truncated, tuple(description)
- else:
- return rows
+ return rows, truncated, tuple(description)
def _execute_datasette_query(self, sql, params):
"Datasette special queries for getting tables info"