]> git.jsancho.org Git - datasette-pytables.git/blobdiff - datasette_pytables/__init__.py
Remove unnecessary import
[datasette-pytables.git] / datasette_pytables / __init__.py
index d9530ae9a7edb969a64628c018d2b2c83d29c051..751e2605e7d216a82c610d9fc3b7dc4df376b617 100644 (file)
@@ -1,4 +1,3 @@
-from collections import OrderedDict
 from moz_sql_parser import parse
 import re
 import tables
@@ -71,7 +70,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 +93,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 +109,7 @@ class Connection:
 
         def _translate_where(where):
             # Translate SQL to PyTables expression
+            nonlocal start, end
             expr = ''
             operator = list(where)[0]
 
@@ -118,9 +121,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 +149,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