]> git.jsancho.org Git - datasette-connectors.git/blob - datasette_connectors/connectors.py
Adapting project to last datasette version (WIP)
[datasette-connectors.git] / datasette_connectors / connectors.py
1 import pkg_resources
2 import functools
3
4 db_connectors = {}
5
6 def for_each_connector(func):
7     @functools.wraps(func)
8     def wrapper_for_each_connector(*args, **kwargs):
9         for connector in db_connectors.values():
10             try:
11                 return func(connector, *args, **kwargs)
12             except:
13                 pass
14         else:
15             raise Exception("No database connector found!!")
16     return wrapper_for_each_connector
17
18
19 class ConnectorList:
20     @staticmethod
21     def load():
22         for entry_point in pkg_resources.iter_entry_points('datasette.connectors'):
23             db_connectors[entry_point.name] = entry_point.load()
24
25     @staticmethod
26     def add_connector(name, connector):
27         db_connectors[name] = connector
28
29     @staticmethod
30     @for_each_connector
31     def table_names(connector, path):
32         return connector.table_names(path)
33
34     @staticmethod
35     @for_each_connector
36     def hidden_table_names(connector, path):
37         return connector.hidden_table_names(path)
38
39     @staticmethod
40     @for_each_connector
41     def view_names(connector, path):
42         return connector.view_names(path)
43
44     @staticmethod
45     @for_each_connector
46     def table_columns(connector, path, table):
47         return connector.table_columns(path, table)
48
49     @staticmethod
50     @for_each_connector
51     def primary_keys(connector, path, table):
52         return connector.primary_keys(path, table)
53
54     @staticmethod
55     @for_each_connector
56     def fts_table(connector, path, table):
57         return connector.fts_table(path, table)
58
59     @staticmethod
60     @for_each_connector
61     def get_all_foreign_keys(connector, path):
62         return connector.get_all_foreign_keys(path)
63
64     @staticmethod
65     @for_each_connector
66     def table_counts(connector, path, *args, **kwargs):
67         return connector.table_counts(path, *args, **kwargs)
68
69
70 class Connector:
71     @staticmethod
72     def table_names(path):
73         return []
74
75     @staticmethod
76     def hidden_table_names(path):
77         return []
78
79     @staticmethod
80     def view_names(path):
81         return []
82
83     @staticmethod
84     def table_columns(path, table):
85         return []
86
87     @staticmethod
88     def primary_keys(path, table):
89         return []
90
91     @staticmethod
92     def fts_table(path, table):
93         return None
94
95     @staticmethod
96     def get_all_foreign_keys(path):
97         return {}
98
99     @staticmethod
100     def table_counts(path, *args, **kwargs):
101         return {}