]> git.jsancho.org Git - datasette-pytables.git/blobdiff - datasette_pytables/__init__.py
Show multidimensional arrays as string arrays
[datasette-pytables.git] / datasette_pytables / __init__.py
index d16109b541d106447ab22964d3a20beffd74d60e..80349a6f918cb5eb58728785bca258a69a42a9ce 100644 (file)
@@ -1,4 +1,3 @@
-from collections import OrderedDict
 from moz_sql_parser import parse
 import re
 import tables
@@ -81,7 +80,9 @@ class Connection:
         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 = []
@@ -144,18 +145,14 @@ class Connection:
                 query = parsed_sql['where']
 
         # Limit number of rows
+        limit = None
         if 'limit' in parsed_sql:
-            max_rows = int(parsed_sql['limit'])
-            if end - start > max_rows:
-                end = start + max_rows
+            limit = int(parsed_sql['limit'])
 
         # Truncate if needed
         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
         if query:
@@ -169,7 +166,14 @@ class Connection:
             rows.append(Row({'count(*)': int(table.nrows)}))
         else:
             if type(table) is tables.table.Table:
+                count = 0
                 for table_row in table_rows:
+                    count += 1
+                    if limit and count > limit:
+                        break
+                    if truncate and max_returned_rows and count > max_returned_rows:
+                        truncated = True
+                        break
                     row = Row()
                     for field in fields:
                         field_name = field['value']
@@ -189,7 +193,14 @@ class Connection:
             else:
                 # Any kind of array
                 rowid = start - 1
+                count = 0
                 for table_row in table_rows:
+                    count += 1
+                    if limit and count > limit:
+                        break
+                    if truncate and max_returned_rows and count > max_returned_rows:
+                        truncated = True
+                        break
                     row = Row()
                     rowid += 1
                     for field in fields:
@@ -202,6 +213,8 @@ class Connection:
                             value = table_row
                             if type(value) is bytes:
                                 value = value.decode('utf-8')
+                            elif not type(value) in (int, float, complex):
+                                value = str(value)
                             row['value'] = value
                     rows.append(row)
 
@@ -217,10 +230,7 @@ class Connection:
                 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"