]> git.jsancho.org Git - datasette-pytables.git/blobdiff - datasette_pytables/__init__.py
Little support for arrays
[datasette-pytables.git] / datasette_pytables / __init__.py
index 094710b517ee27d50dbd2341952b4144fa2d082e..4a7c5c8c3e83af5c4ef5a1f35f97f64b84b2bb63 100644 (file)
@@ -107,8 +107,12 @@ class Connection:
             operator = list(where)[0]
 
             if operator in ['and', 'or']:
             operator = list(where)[0]
 
             if operator in ['and', 'or']:
-                subexpr = ["({})".format(_translate_where(q)) for q in where[operator]]
+                subexpr = [_translate_where(e) for e in where[operator]]
+                subexpr = filter(lambda e: e, subexpr)
+                subexpr = ["({})".format(e) for e in subexpr]
                 expr = " {} ".format(_operators[operator]).join(subexpr)
                 expr = " {} ".format(_operators[operator]).join(subexpr)
+            elif operator == 'exists':
+                pass
             elif where == {'eq': ['rowid', 'p0']}:
                 nonlocal start, end
                 start = int(params['p0'])
             elif where == {'eq': ['rowid', 'p0']}:
                 nonlocal start, end
                 start = int(params['p0'])
@@ -147,26 +151,51 @@ class Connection:
            fields[0]['value'].get('count') == '*':
             rows.append(Row({'count(*)': int(table.nrows)}))
         else:
            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:
-                    if field['value'] == 'rowid':
-                        row['rowid'] = int(table_row.nrow)
-                    elif field['value'] == '*':
-                        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')
                             if type(value) is bytes:
                                 value = value.decode('utf-8')
-                            row[col] = value
-                    else:
-                        row[field['value']] = table_row[field['value']]
-                rows.append(row)
+                            row['value'] = value
+                    rows.append(row)
 
         # Prepare query description
         for field in [f['value'] for f in fields]:
             if field == '*':
 
         # 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,))
 
             else:
                 description.append((field,))