]> git.jsancho.org Git - datasette-pytables.git/blobdiff - datasette_pytables/__init__.py
Merge branch 'master' of https://github.com/jsancho-gpl/datasette-pytables
[datasette-pytables.git] / datasette_pytables / __init__.py
index 7bbc7c14063b5a562cb080c72609ccdf062fc9b2..2e6920eceab3f96c3f28e17a566962023534fb44 100644 (file)
@@ -146,6 +146,16 @@ class Connection:
             else:
                 query = parsed_sql['where']
 
+        # Sort by column
+        orderby = ''
+        if 'orderby' in parsed_sql:
+            orderby = parsed_sql['orderby']
+            if type(orderby) is list:
+                orderby = orderby[0]
+            orderby = orderby['value']
+            if orderby == 'rowid':
+                orderby = ''
+
         # Limit number of rows
         limit = None
         if 'limit' in parsed_sql:
@@ -159,6 +169,8 @@ class Connection:
         # Execute query
         if query:
             table_rows = table.where(query, params, start, end)
+        elif orderby:
+            table_rows = table.itersorted(orderby, start=start, stop=end)
         else:
             table_rows = table.iterrows(start, end)
 
@@ -240,14 +252,18 @@ class Connection:
         elif sql == 'select sql from sqlite_master where name = :n and type="table"':
             try:
                 table = self.h5file.get_node(params['n'])
+                colnames = ['value']
+                if type(table) is tables.table.Table:
+                    colnames = table.colnames
                 row = Row()
-                row['sql'] = 'CREATE TABLE {} ()'.format(params['n'])
+                row['sql'] = 'CREATE TABLE {} ({})'.format(params['n'], ", ".join(colnames))
                 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 = []