Truncate query results according to page_size limit
authorJavier Sancho <jsf@jsancho.org>
Wed, 23 May 2018 11:03:56 +0000 (13:03 +0200)
committerJavier Sancho <jsf@jsancho.org>
Wed, 23 May 2018 11:03:56 +0000 (13:03 +0200)
datasette_pytables/__init__.py
tests/test_api.py

index 6e34774..74c72b1 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):
+    def execute(self, sql, params=None, truncate=False, page_size=None):
         if params is None:
             params = {}
         rows = []
@@ -140,6 +140,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)
index 834a65d..85b4bb1 100644 (file)
@@ -63,6 +63,7 @@ def test_custom_sql(app_client):
         'sql': 'select identity from [/group1/table1]',
         'params': {}
     } == data['query']
+    assert 50 == len(data['rows'])
     assert [
         {'identity': 'This is particle:  0'},
         {'identity': 'This is particle:  1'},