]> git.jsancho.org Git - datasette-connectors.git/blobdiff - datasette_connectors/connectors.py
Fix missed import
[datasette-connectors.git] / datasette_connectors / connectors.py
index aa8f824cf4d0a51234107c7801e0fd2d3fa8d7a4..005b77a26cfd8ba13052d4a27e13559fccc0f25f 100644 (file)
@@ -1,19 +1,8 @@
 import pkg_resources
-import functools
+from .connection import Connection
 
-db_connectors = {}
 
-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
+db_connectors = {}
 
 
 class ConnectorList:
@@ -26,76 +15,110 @@ class ConnectorList:
     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)
+    class DatabaseNotSupported(Exception):
+        pass
 
     @staticmethod
-    @for_each_connector
-    def table_counts(connector, path, *args, **kwargs):
-        return connector.table_counts(path, *args, **kwargs)
+    def connect(path):
+        for connector in db_connectors.values():
+            try:
+                return connector.connect(path)
+            except:
+                pass
+        else:
+            raise ConnectorList.DatabaseNotSupported
 
 
 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 {}
+    connector_type = None
+    connection_class = Connection
+
+    @classmethod
+    def connect(cls, path):
+        return cls.connection_class(path, cls)
+
+    def __init__(self, conn):
+        self.conn = conn
+
+    def table_names(self):
+        """
+        Return a list of table names
+        """
+        raise NotImplementedError
+
+    def hidden_table_names(self):
+        raise NotImplementedError
+
+    def detect_spatialite(self):
+        """
+        Return boolean indicating if geometry_columns exists
+        """
+        raise NotImplementedError
+
+    def view_names(self):
+        """
+        Return a list of view names
+        """
+        raise NotImplementedError
+
+    def table_count(self, table_name):
+        """
+        Return an integer with the rows count of the table
+        """
+        raise NotImplementedError
+
+    def table_info(self, table_name):
+        """
+        Return a list of dictionaries with columns description, with format:
+        [
+            {
+                'idx': 0,
+                'name': 'column1',
+                'primary_key': False,
+            },
+            ...
+        ]
+        """
+        raise NotImplementedError
+
+    def detect_fts(self, table_name):
+        """
+        Return boolean indicating if table has a corresponding FTS virtual table
+        """
+        raise NotImplementedError
+
+    def foreign_keys(self, table_name):
+        """
+        Return a list of dictionaries with foreign keys description
+        id, seq, table_name, from_, to_, on_update, on_delete, match
+        """
+        raise NotImplementedError
+
+    def table_exists(self, table_name):
+        """
+        Return boolean indicating if table exists in the database
+        """
+        raise NotImplementedError
+
+    def table_definition(self, table_type, table_name):
+        """
+        Return string with a 'CREATE TABLE' sql definition
+        """
+        raise NotImplementedError
+
+    def indices_definition(self, table_name):
+        """
+        Return a list of strings with 'CREATE INDEX' sql definitions
+        """
+        raise NotImplementedError
+
+    def execute(
+        self,
+        sql,
+        params=None,
+        truncate=False,
+        custom_time_limit=None,
+        page_size=None,
+        log_sql_errors=True,
+    ):
+        raise NotImplementedError