]> git.jsancho.org Git - datasette-pytables.git/blobdiff - datasette_pytables/__init__.py
Truncate query results according to page_size limit
[datasette-pytables.git] / datasette_pytables / __init__.py
index a1f11d91e3fb2e8b142068703fd4a8181e49ac33..74c72b1173fd7af371c414255c181c1c6bdaa3be 100644 (file)
@@ -41,12 +41,12 @@ def _parse_sql(sql, params):
         parsed = parse(sql)
     except:
         # Propably it's a PyTables expression
-        for token in ['group by', 'order by', 'limit']:
+        for token in ['group by', 'order by', 'limit', '']:
             res = re.search('(?i)where (.*)' + token, sql)
             if res:
                 modified_sql = re.sub('(?i)where (.*)(' + token + ')', '\g<2>', sql)
                 parsed = parse(modified_sql)
-                parsed['where'] = res.group(1)
+                parsed['where'] = res.group(1).strip()
                 break
 
     # Always a list of fields
@@ -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)
@@ -151,29 +157,51 @@ class Connection:
            fields[0]['value'].get('count') == '*':
             rows.append(Row({'count(*)': int(table.nrows)}))
         else:
-            for table_row in table_rows:
-                row = Row()
-                for field in fields:
-                    field_name = field['value']
-                    if type(field_name) is dict and 'distinct' in field_name:
-                        field_name = field_name['distinct']
-                    if field_name == 'rowid':
-                        row['rowid'] = int(table_row.nrow)
-                    elif field_name == '*':
-                        for col in table.colnames:
-                            value = table_row[col]
+            if type(table) is tables.table.Table:
+                for table_row in table_rows:
+                    row = Row()
+                    for field in fields:
+                        field_name = field['value']
+                        if type(field_name) is dict and 'distinct' in field_name:
+                            field_name = field_name['distinct']
+                        if field_name == 'rowid':
+                            row['rowid'] = int(table_row.nrow)
+                        elif field_name == '*':
+                            for col in table.colnames:
+                                value = table_row[col]
+                                if type(value) is bytes:
+                                    value = value.decode('utf-8')
+                                row[col] = value
+                        else:
+                            row[field_name] = table_row[field_name]
+                    rows.append(row)
+            else:
+                # Any kind of array
+                rowid = start - 1
+                for table_row in table_rows:
+                    row = Row()
+                    rowid += 1
+                    for field in fields:
+                        field_name = field['value']
+                        if type(field_name) is dict and 'distinct' in field_name:
+                            field_name = field_name['distinct']
+                        if field_name == 'rowid':
+                            row['rowid'] = rowid
+                        else:
+                            value = table_row
                             if type(value) is bytes:
                                 value = value.decode('utf-8')
-                            row[col] = value
-                    else:
-                        row[field_name] = table_row[field_name]
-                rows.append(row)
+                            row['value'] = value
+                    rows.append(row)
 
         # Prepare query description
         for field in [f['value'] for f in fields]:
             if field == '*':
-                for col in table.colnames:
-                    description.append((col,))
+                if type(table) is tables.table.Table:
+                    for col in table.colnames:
+                        description.append((col,))
+                else:
+                    description.append(('value',))
             else:
                 description.append((field,))