X-Git-Url: https://git.jsancho.org/?p=datasette-pytables.git;a=blobdiff_plain;f=datasette_pytables%2F__init__.py;h=3c71267e9f1144cff2d2c02c32c74264bc223a7d;hp=93be0f9a1206208f3667633526baca08b28ae335;hb=ecde481300b6a3d5c04febe4b5b770556c8d0708;hpb=25d0571fb4a85291df0f7cea4f1424588a7961fc diff --git a/datasette_pytables/__init__.py b/datasette_pytables/__init__.py index 93be0f9..3c71267 100644 --- a/datasette_pytables/__init__.py +++ b/datasette_pytables/__init__.py @@ -69,8 +69,9 @@ class Connection: else: table_rows = table.iterrows() + # Prepare rows if len(fields) == 1 and fields[0] == 'count(*)': - rows.append(Row({'count(*)': table.nrows})) + rows.append(Row({fields[0]: table.nrows})) else: for table_row in table_rows: row = Row() @@ -79,15 +80,24 @@ class Connection: row[field] = table_row.nrow elif field == '*': for col in table.colnames: - row[col] = table_row[col] + value = table_row[col] + if type(value) is bytes: + value = value.decode('utf-8') + row[col] = value else: row[field] = table_row[field] rows.append(row) - description = ((col,) for col in table.colnames) + # Prepare query description + for field in fields: + if field == '*': + for col in table.colnames: + description.append((col,)) + else: + description.append((field,)) if truncate: - return rows, truncated, description + return rows, truncated, tuple(description) else: return rows @@ -95,3 +105,8 @@ class Row(OrderedDict): def __getitem__(self, label): if type(label) is int: return super(OrderedDict, self).__getitem__(list(self.keys())[label]) + else: + return super(OrderedDict, self).__getitem__(label) + + def __iter__(self): + return self.values().__iter__()