]> git.jsancho.org Git - datasette-connectors.git/blob - datasette_connectors/connectors.py
0fa4b3d17eb60ae7a85061250c2d55eef725c840
[datasette-connectors.git] / datasette_connectors / connectors.py
1 from .connection import Connection
2
3
4 db_connectors = {}
5
6
7 class ConnectorList:
8     @staticmethod
9     def load():
10         for entry_point in pkg_resources.iter_entry_points('datasette.connectors'):
11             db_connectors[entry_point.name] = entry_point.load()
12
13     @staticmethod
14     def add_connector(name, connector):
15         db_connectors[name] = connector
16
17     class DatabaseNotSupported(Exception):
18         pass
19
20     @staticmethod
21     def connect(path):
22         for connector in db_connectors.values():
23             try:
24                 return connector.connect(path)
25             except:
26                 pass
27         else:
28             raise ConnectorList.DatabaseNotSupported
29
30
31 class Connector:
32     connector_type = None
33     connection_class = Connection
34
35     @classmethod
36     def connect(cls, path):
37         return cls.connection_class(path, cls)
38
39     def __init__(self, conn):
40         self.conn = conn
41
42     def table_names(self):
43         """
44         Return a list of table names
45         """
46         raise NotImplementedError
47
48     def hidden_table_names(self):
49         raise NotImplementedError
50
51     def detect_spatialite(self):
52         """
53         Return boolean indicating if geometry_columns exists
54         """
55         raise NotImplementedError
56
57     def view_names(self):
58         """
59         Return a list of view names
60         """
61         raise NotImplementedError
62
63     def table_count(self, table_name):
64         """
65         Return an integer with the rows count of the table
66         """
67         raise NotImplementedError
68
69     def table_info(self, table_name):
70         """
71         Return a list of dictionaries with columns description, with format:
72         [
73             {
74                 'idx': 0,
75                 'name': 'column1',
76                 'primary_key': False,
77             },
78             ...
79         ]
80         """
81         raise NotImplementedError
82
83     def detect_fts(self, table_name):
84         """
85         Return boolean indicating if table has a corresponding FTS virtual table
86         """
87         raise NotImplementedError
88
89     def foreign_keys(self, table_name):
90         """
91         Return a list of dictionaries with foreign keys description
92         id, seq, table_name, from_, to_, on_update, on_delete, match
93         """
94         raise NotImplementedError
95
96     def table_exists(self, table_name):
97         """
98         Return boolean indicating if table exists in the database
99         """
100         raise NotImplementedError
101
102     def table_definition(self, table_type, table_name):
103         """
104         Return string with a 'CREATE TABLE' sql definition
105         """
106         raise NotImplementedError
107
108     def indices_definition(self, table_name):
109         """
110         Return a list of strings with 'CREATE INDEX' sql definitions
111         """
112         raise NotImplementedError
113
114     def execute(
115         self,
116         sql,
117         params=None,
118         truncate=False,
119         custom_time_limit=None,
120         page_size=None,
121         log_sql_errors=True,
122     ):
123         raise NotImplementedError