X-Git-Url: https://git.jsancho.org/?p=datasette-connectors.git;a=blobdiff_plain;f=datasette_connectors%2Fconnectors.py;fp=datasette_connectors%2Fconnectors.py;h=aa8f824cf4d0a51234107c7801e0fd2d3fa8d7a4;hp=e3d10ba4dacd0fd5da758558377a38c6b9924c13;hb=52416a749fac092a032a8b5239e477dd68180dfa;hpb=1a34f766bbcada99da81fabdc93b802e4ff8fb2a diff --git a/datasette_connectors/connectors.py b/datasette_connectors/connectors.py index e3d10ba..aa8f824 100644 --- a/datasette_connectors/connectors.py +++ b/datasette_connectors/connectors.py @@ -1,22 +1,101 @@ import pkg_resources +import functools db_connectors = {} -def load(): - for entry_point in pkg_resources.iter_entry_points('datasette.connectors'): - db_connectors[entry_point.name] = entry_point.load() - -def inspect(path): - for connector in db_connectors.values(): - try: - return connector.inspect(path) - except: - pass - else: - raise Exception("No database connector found for %s" % path) - -def connect(path, dbtype): - try: - return db_connectors[dbtype].Connection(path) - except: - raise Exception("No database connector found for %s" % path) +def for_each_connector(func): + @functools.wraps(func) + def wrapper_for_each_connector(*args, **kwargs): + for connector in db_connectors.values(): + try: + return func(connector, *args, **kwargs) + except: + pass + else: + raise Exception("No database connector found!!") + return wrapper_for_each_connector + + +class ConnectorList: + @staticmethod + def load(): + for entry_point in pkg_resources.iter_entry_points('datasette.connectors'): + db_connectors[entry_point.name] = entry_point.load() + + @staticmethod + def add_connector(name, connector): + db_connectors[name] = connector + + @staticmethod + @for_each_connector + def table_names(connector, path): + return connector.table_names(path) + + @staticmethod + @for_each_connector + def hidden_table_names(connector, path): + return connector.hidden_table_names(path) + + @staticmethod + @for_each_connector + def view_names(connector, path): + return connector.view_names(path) + + @staticmethod + @for_each_connector + def table_columns(connector, path, table): + return connector.table_columns(path, table) + + @staticmethod + @for_each_connector + def primary_keys(connector, path, table): + return connector.primary_keys(path, table) + + @staticmethod + @for_each_connector + def fts_table(connector, path, table): + return connector.fts_table(path, table) + + @staticmethod + @for_each_connector + def get_all_foreign_keys(connector, path): + return connector.get_all_foreign_keys(path) + + @staticmethod + @for_each_connector + def table_counts(connector, path, *args, **kwargs): + return connector.table_counts(path, *args, **kwargs) + + +class Connector: + @staticmethod + def table_names(path): + return [] + + @staticmethod + def hidden_table_names(path): + return [] + + @staticmethod + def view_names(path): + return [] + + @staticmethod + def table_columns(path, table): + return [] + + @staticmethod + def primary_keys(path, table): + return [] + + @staticmethod + def fts_table(path, table): + return None + + @staticmethod + def get_all_foreign_keys(path): + return {} + + @staticmethod + def table_counts(path, *args, **kwargs): + return {}