-from datasette_connectors.row import Row
+import datasette_connectors as dc
-_connector_type = 'dummy'
+class DummyConnector(dc.Connector):
+ connector_type = 'dummy'
-def inspect(path):
- tables = {}
- views = []
+ def table_names(self):
+ return ['table1', 'table2']
- for table in ['table1', 'table2']:
- tables[table] = {
- 'name': table,
- 'columns': ['c1', 'c2', 'c3'],
- 'primary_keys': [],
- 'count': 2,
- 'label_column': None,
- 'hidden': False,
- 'fts_table': None,
- 'foreign_keys': {'incoming': [], 'outgoing': []},
- }
+ def hidden_table_names(self):
+ return []
- return tables, views, _connector_type
+ def detect_spatialite(self):
+ return False
+ def view_names(self):
+ return []
-class Connection:
- def __init__(self, path):
- self.path = path
+ def table_count(self, table_name):
+ return 2
- def execute(self, sql, params=None, truncate=False, page_size=None, max_returned_rows=None):
- sql = sql.strip()
+ def table_info(self, table_name):
+ return [
+ {
+ 'cid': 0,
+ 'name': 'c1',
+ 'type': 'integer',
+ 'notnull': False,
+ 'default_value': None,
+ 'is_pk': False,
+ },
+ {
+ 'cid': 1,
+ 'name': 'c2',
+ 'type': 'integer',
+ 'notnull': False,
+ 'default_value': None,
+ 'is_pk': False,
+ },
+ {
+ 'cid': 2,
+ 'name': 'c3',
+ 'type': 'integer',
+ 'notnull': False,
+ 'default_value': None,
+ 'is_pk': False,
+ },
+ ]
- rows = []
+ def detect_fts(self, table_name):
+ return False
+
+ def foreign_keys(self, table_name):
+ return []
+
+ def table_exists(self, table_name):
+ return table_name in ['table1', 'table2']
+
+ def table_definition(self, table_type, table_name):
+ return 'CREATE TABLE ' + table_name + ' (c1, c2, c3)'
+
+ def indices_definition(self, table_name):
+ return []
+
+ def execute(
+ self,
+ sql,
+ params=None,
+ truncate=False,
+ custom_time_limit=None,
+ page_size=None,
+ log_sql_errors=True,
+ ):
+ results = []
truncated = False
- description = []
+ description = ()
if sql == 'select c1 from table1':
- rows = [
- Row({'c1': 10}),
- Row({'c1': 20})
+ results = [
+ {'c1': 10},
+ {'c1': 20},
]
description = (('c1',),)
- elif sql == 'select rowid, * from table2 order by rowid limit 51':
- rows = [
- Row({'rowid': 1, 'c1': 100, 'c2': 120, 'c3': 130}),
- Row({'rowid': 2, 'c1': 200, 'c2': 220, 'c3': 230})
- ]
- description = (('rowid',), ('c1',), ('c2',), ('c3',))
- elif sql == 'select count(*) from table2':
- rows = [Row({'count(*)': 2})]
- description = (('count(*)',),)
- elif sql == """select distinct rowid from table2
- where rowid is not null
- limit 31""":
- rows = [
- Row({'rowid': 1}),
- Row({'rowid': 2})
+ elif sql == 'select c1, c2, c3 from table2 limit 51':
+ results = [
+ {'c1': 100, 'c2': 120, 'c3': 130},
+ {'c1': 200, 'c2': 220, 'c3': 230},
]
- description = (('rowid',),)
- elif sql == """select distinct c1 from table2
- where c1 is not null
- limit 31""":
- rows = [
- Row({'c1': 100}),
- Row({'c1': 200})
+ description = (('c1',), ('c2',), ('c3',))
+ elif sql == "select * from (select c1, c2, c3 from table2 ) limit 0":
+ pass
+ elif sql == "select c1, count(*) as n from ( select c1, c2, c3 from table2 ) where c1 is not null group by c1 limit 31":
+ results = [
+ {'c1': 100, 'n': 1},
+ {'c1': 200, 'n': 1},
]
- description = (('c1',),)
- elif sql == """select distinct c2 from table2
- where c2 is not null
- limit 31""":
- rows = [
- Row({'c2': 120}),
- Row({'c2': 220})
+ description = (('c1',), ('n',))
+ elif sql == "select c2, count(*) as n from ( select c1, c2, c3 from table2 ) where c2 is not null group by c2 limit 31":
+ results = [
+ {'c2': 120, 'n': 1},
+ {'c2': 220, 'n': 1},
]
- description = (('c2',),)
- elif sql == """select distinct c3 from table2
- where c3 is not null
- limit 31""":
- rows = [
- Row({'c3': 130}),
- Row({'c3': 230})
+ description = (('c2',), ('n',))
+ elif sql == "select c3, count(*) as n from ( select c1, c2, c3 from table2 ) where c3 is not null group by c3 limit 31":
+ results = [
+ {'c3': 130, 'n': 1},
+ {'c3': 230, 'n': 1},
]
- description = (('c3',),)
- elif sql == 'select sql from sqlite_master where name = :n and type=:t':
- if params['t'] != 'view':
- rows = [Row({'sql': 'CREATE TABLE ' + params['n'] + ' (c1, c2, c3)'})]
- description = (('sql',),)
+ description = (('c3',), ('n',))
+ elif sql == 'select date(c1) from ( select c1, c2, c3 from table2 ) where c1 glob "????-??-*" limit 100;':
+ pass
+ elif sql == "select c1, c2, c3 from blah limit 51":
+ raise dc.OperationalError("no such table: blah")
else:
- raise Exception("Unexpected query: %s" % sql)
+ raise Exception("Unexpected query:", sql)
- return rows, truncated, description
+ return results, truncated, description