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