]> git.jsancho.org Git - datasette-pytables.git/blobdiff - datasette_pytables/__init__.py
Process some sqlite standard queries for obtaining tables info
[datasette-pytables.git] / datasette_pytables / __init__.py
index 6e34774bc24e83688571fcac6645741a9c98ba28..5d9fb22f78a88601900a08396b40541267f967c1 100644 (file)
@@ -71,7 +71,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):
         if params is None:
             params = {}
         rows = []
@@ -79,6 +79,10 @@ class Connection:
         description = []
 
         parsed_sql = _parse_sql(sql, params)
+
+        if parsed_sql['from'] == 'sqlite_master':
+            return self._execute_datasette_query(sql, params)
+
         table = self.h5file.get_node(parsed_sql['from'])
         table_rows = []
         fields = parsed_sql['select']
@@ -140,6 +144,12 @@ class Connection:
             if end - start > max_rows:
                 end = start + max_rows
 
+        # Truncate if needed
+        if page_size and truncate:
+            if end - start > page_size:
+                end = start + page_size
+                truncated = True
+
         # Execute query
         if query:
             table_rows = table.where(query, params, start, end)
@@ -205,6 +215,23 @@ class Connection:
         else:
             return rows
 
+    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(OrderedDict):
     def __getitem__(self, label):
         if type(label) is int: