xinbinhuang commented on a change in pull request #12677:
URL: https://github.com/apache/airflow/pull/12677#discussion_r559123773



##########
File path: airflow/operators/sql.py
##########
@@ -18,27 +18,59 @@
 from distutils.util import strtobool
 from typing import Any, Dict, Iterable, List, Mapping, Optional, SupportsAbs, 
Union
 
+from cached_property import cached_property
+
 from airflow.exceptions import AirflowException
 from airflow.hooks.base import BaseHook
+from airflow.hooks.dbapi import DbApiHook
 from airflow.models import BaseOperator, SkipMixin
 from airflow.utils.decorators import apply_defaults
 
-ALLOWED_CONN_TYPE = {
-    "google_cloud_platform",
-    "jdbc",
-    "mssql",
-    "mysql",
-    "odbc",
-    "oracle",
-    "postgres",
-    "presto",
-    "snowflake",
-    "sqlite",
-    "vertica",
-}
-
-
-class SQLCheckOperator(BaseOperator):
+
+class BaseSQLOperator(BaseOperator):
+    """
+    This is a base class for generic SQL Operator to get a DB Hook
+
+    The provided method is .get_db_hook(). The default behavior will try to
+    retrieve the DB hook based on connection type.
+    You can custom the behavior by overriding the .get_db_hook() method.
+    """
+
+    @apply_defaults
+    def __init__(self, *, conn_id: Optional[str] = None, database: 
Optional[str] = None, **kwargs):
+        super().__init__(**kwargs)
+        self.conn_id = conn_id
+        self.database = database
+
+    @cached_property
+    def _hook(self):
+        """Get DB Hook based on connection type"""
+        self.log.debug("Get connection for %s", self.conn_id)
+        conn = BaseHook.get_connection(self.conn_id)
+
+        hook = conn.get_hook()
+        if not isinstance(hook, DbApiHook):
+            raise AirflowException(
+                f'The connection type is not supported by 
{self.__class__.__name__}. '
+                f'The associated hook should be a subclass of `DbApiHook`. Got 
{hook.__class__.__name__}'
+            )
+
+        if self.database:
+            hook.schema = self.database

Review comment:
       I totally agree!  But this is how the current mapping of Airflow works. 
Currently, the `Connection` object uses the field `schema` to specify the 
database (not schema) for database type connections, i.e. Postgres, MySQL, etc. 
And as a result, all `DbApiHook`s use the field `schema` to specify the 
database to connect to using the underlying DB API (i.e. `psycopg2`). `extra` 
is used to specify the schema if applicable. So the relationship looks like 
this:
   
   ```
   Connection(schema) -> DbApiHook (schema) -> DB API (dbname/database) 
   ```
   
   About the field `database`, it was already used for the `SQLBranchOperator` 
and also other SQL like operators. For this PR, I simply refactor it out to 
reduce duplicated code and add them to the `check*` operators for consistency.
   
   Though I think it will be nice to consolidate between `schema` and 
`database`, I think they should be addressed in a separate PR or worth a 
discussion in the `dev` list. I can send an email in the dev list if you don't 
object.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to