From 3c2e826f2fd5c6a05ada7dceb09d9e9e1b8cf5e6 Mon Sep 17 00:00:00 2001 From: Javier Sancho Date: Tue, 22 May 2018 13:19:28 +0200 Subject: [PATCH] Little support for arrays --- datasette_pytables/__init__.py | 56 +++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/datasette_pytables/__init__.py b/datasette_pytables/__init__.py index a1f11d9..4a7c5c8 100644 --- a/datasette_pytables/__init__.py +++ b/datasette_pytables/__init__.py @@ -151,29 +151,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,)) -- 2.39.5