]> git.jsancho.org Git - datasette-connectors.git/blob - datasette_connectors/connectors.py
Clean code
[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     def connect(self, path):
36         return self.connection_class(path, self)
37
38     def table_names(self):
39         """
40         Return a list of table names
41         """
42         raise NotImplementedError
43
44     def hidden_table_names(self):
45         raise NotImplementedError
46
47     def detect_spatialite(self):
48         """
49         Return boolean indicating if geometry_columns exists
50         """
51         raise NotImplementedError
52
53     def view_names(self):
54         """
55         Return a list of view names
56         """
57         raise NotImplementedError
58
59     def table_count(self, table_name):
60         """
61         Return an integer with the rows count of the table
62         """
63         raise NotImplementedError
64
65     def table_info(self, table_name):
66         """
67         Return a list of dictionaries with columns description, with format:
68         [
69             {
70                 'idx': 0,
71                 'name': 'column1',
72                 'primary_key': False,
73             },
74             ...
75         ]
76         """
77         raise NotImplementedError
78
79     def detect_fts(self, table_name):
80         """
81         Return boolean indicating if table has a corresponding FTS virtual table
82         """
83         raise NotImplementedError
84
85     def foreign_keys(self, table_name):
86         """
87         Return a list of dictionaries with foreign keys description
88         id, seq, table_name, from_, to_, on_update, on_delete, match
89         """
90         raise NotImplementedError
91
92     def table_exists(self, table_name):
93         """
94         Return boolean indicating if table exists in the database
95         """
96         raise NotImplementedError
97
98     def table_definition(self, table_type, table_name):
99         """
100         Return string with a 'CREATE TABLE' sql definition
101         """
102         raise NotImplementedError
103
104     def indices_definition(self, table_name):
105         """
106         Return a list of strings with 'CREATE INDEX' sql definitions
107         """
108         raise NotImplementedError
109
110     def execute(
111         self,
112         sql,
113         params=None,
114         truncate=False,
115         custom_time_limit=None,
116         page_size=None,
117         log_sql_errors=True,
118     ):
119         raise NotImplementedError