]> 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 a1f11d91e3fb2e8b142068703fd4a8181e49ac33..80349a6f918cb5eb58728785bca258a69a42a9ce 100644 (file)
@@ -1,4 +1,3 @@
-from collections import OrderedDict
 from moz_sql_parser import parse
 import re
 import tables
@@ -41,12 +40,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 +70,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, max_returned_rows=None):
         if params is None:
             params = {}
         rows = []
@@ -79,6 +78,12 @@ class Connection:
         description = []
 
         parsed_sql = _parse_sql(sql, params)
+
+        if parsed_sql['from'] == 'sqlite_master':
+            rows = self._execute_datasette_query(sql, params)
+            description = (('value',))
+            return rows, truncated, description
+
         table = self.h5file.get_node(parsed_sql['from'])
         table_rows = []
         fields = parsed_sql['select']
@@ -90,7 +95,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
@@ -103,6 +111,7 @@ class Connection:
 
         def _translate_where(where):
             # Translate SQL to PyTables expression
+            nonlocal start, end
             expr = ''
             operator = list(where)[0]
 
@@ -114,9 +123,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:
@@ -135,10 +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
 
         # Execute query
         if query:
@@ -151,44 +165,113 @@ 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:
+                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']
+                        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
+                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:
+                        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)
+                            elif not type(value) in (int, float, complex):
+                                value = str(value)
+                            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,))
 
         # Return the rows
-        if truncate:
-            return rows, truncated, tuple(description)
+        return rows, truncated, tuple(description)
+
+    def _execute_datasette_query(self, sql, params):
+        "Datasette special queries for getting tables info"
+        if sql == "SELECT count(*) from sqlite_master WHERE type = 'view' and name=:n":
+            row = Row()
+            row['count(*)'] = 0
+            return [row]
+        elif sql == 'select sql from sqlite_master where name = :n and type="table"':
+            try:
+                table = self.h5file.get_node(params['n'])
+                row = Row()
+                row['sql'] = 'CREATE TABLE {} ()'.format(params['n'])
+                return [row]
+            except:
+                return []
+        else:
+            raise Exception("SQLite queries cannot be executed with this connector")
+
+class Row(list):
+    def __init__(self, values=None):
+        self.labels = []
+        self.values = []
+        if values:
+            for idx in values:
+                self.__setitem__(idx, values[idx])
+
+    def __setitem__(self, idx, value):
+        if type(idx) is str:
+            if idx in self.labels:
+                self.values[self.labels.index(idx)] = value
+            else:
+                self.labels.append(idx)
+                self.values.append(value)
         else:
-            return rows
+            self.values[idx] = value
 
-class Row(OrderedDict):
-    def __getitem__(self, label):
-        if type(label) is int:
-            return super(OrderedDict, self).__getitem__(list(self.keys())[label])
+    def __getitem__(self, idx):
+        if type(idx) is str:
+            return self.values[self.labels.index(idx)]
         else:
-            return super(OrderedDict, self).__getitem__(label)
+            return self.values[idx]
 
     def __iter__(self):
-        return self.values().__iter__()
+        return self.values.__iter__()