]> git.jsancho.org Git - datasette-pytables.git/blobdiff - datasette_pytables/__init__.py
Fix problems with arrays
[datasette-pytables.git] / datasette_pytables / __init__.py
index d9530ae9a7edb969a64628c018d2b2c83d29c051..d16109b541d106447ab22964d3a20beffd74d60e 100644 (file)
@@ -71,7 +71,7 @@ class Connection:
         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 = []
@@ -94,7 +94,10 @@ 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]
+            if type(table) is tables.table.Table:
+                coltype = table.coltypes[field]
+            else:
+                coltype = table.dtype.name
             fcast = None
             if coltype == 'string':
                 fcast = str
@@ -107,6 +110,7 @@ class Connection:
 
         def _translate_where(where):
             # Translate SQL to PyTables expression
+            nonlocal start, end
             expr = ''
             operator = list(where)[0]
 
@@ -118,9 +122,10 @@ class Connection:
             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:
@@ -145,9 +150,11 @@ class Connection:
                 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