dstandish commented on a change in pull request #6850: [AIRFLOW-6296] add ODBC 
hook & deprecation warning for pymssql
URL: https://github.com/apache/airflow/pull/6850#discussion_r367556670
 
 

 ##########
 File path: airflow/providers/odbc/hooks/odbc.py
 ##########
 @@ -0,0 +1,220 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""
+This module contains ODBC hook.
+"""
+from typing import Optional
+from urllib.parse import quote_plus
+
+import pyodbc
+
+from airflow.hooks.dbapi_hook import DbApiHook
+from airflow.utils.helpers import merge_dicts
+
+
+class OdbcHook(DbApiHook):
+    """
+    Interact with odbc data sources using pyodbc.
+
+    See :ref:`howto/connection/odbc` for full documentation.
+    """
+
+    DEFAULT_SQLALCHEMY_SCHEME = 'mssql+pyodbc'
+    conn_name_attr = 'odbc_conn_id'
+    default_conn_name = 'odbc_default'
+    supports_autocommit = True
+
+    def __init__(
+        self,
+        *args,
+        database: Optional[str] = None,
+        driver: Optional[str] = None,
+        dsn: Optional[str] = None,
+        connect_kwargs: Optional[dict] = None,
+        sqlalchemy_scheme: Optional[str] = None,
+        **kwargs,
+    ):
+        """
+        :param args: passed to DbApiHook
+        :param database: database to use -- overrides connection ``schema``
+        :param driver: name of driver or path to driver. overrides driver 
supplied in connection ``extra``
+        :param dsn: name of DSN to use.  overrides DSN supplied in connection 
``extra``
+        :param connect_kwargs: keyword arguments passed to ``pyodbc.connect``
+        :param sqlalchemy_scheme: Scheme sqlalchemy connection.  Default is 
``mssql+pyodbc`` Only used for
+          ``get_sqlalchemy_engine`` and ``get_sqlalchemy_connection`` methods.
+        :param kwargs: passed to DbApiHook
+        """
+        super().__init__(*args, **kwargs)
+        self._database = database
+        self._driver = driver
+        self._dsn = dsn
+        self._conn_str = None
+        self._sqlalchemy_scheme = sqlalchemy_scheme
+        self._connection = None
+        self._connect_kwargs = connect_kwargs
+
+    @property
+    def connection(self):
+        """
+        ``airflow.Connection`` object with connection id ``odbc_conn_id``
+        """
+        if not self._connection:
+            self._connection = self.get_connection(getattr(self, 
self.conn_name_attr))
+        return self._connection
+
+    @property
+    def database(self):
+        """
+        Database provided in init if exists; otherwise, ``schema`` from 
``Connection`` object.
+        """
+        return self._database or self.connection.schema
+
+    @property
+    def sqlalchemy_scheme(self):
+        """
+        Database provided in init if exists; otherwise, ``schema`` from 
``Connection`` object.
+        """
+        return (
+            self._sqlalchemy_scheme or
+            self.connection_extra_lower.get('sqlalchemy_scheme') or
+            self.DEFAULT_SQLALCHEMY_SCHEME
+        )
+
+    @property
+    def connection_extra_lower(self):
+        """
+        ``connection.extra_dejson`` but where keys are converted to lower case.
+
+        This is used internally for case-insensitive access of odbc params.
+        """
+        return {k.lower(): v for k, v in self.connection.extra_dejson.items()}
+
+    @property
+    def driver(self):
+        """
+        Driver from init param if given; else try to find one in connection 
extra.
+        """
+        if not self._driver:
+            driver = self.connection_extra_lower.get('driver')
+            if driver:
+                self._driver = driver
+        return self._driver and 
self._driver.strip().lstrip('{').rstrip('}').strip()
 
 Review comment:
   i am stripping it so that user could do `{ODBC Driver 17 for SQL Server}` or 
`ODBC Driver 17 for SQL Server` and either way will work.
   
   this way, whether user has provided them or not, when i build the connection 
string, i add safely add the curly braces.

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to