RehanAhmad25 commented on code in PR #77423:
URL: https://github.com/apache/airflow/pull/77423#discussion_r4655768804
##########
providers/microsoft/mssql/src/airflow/providers/microsoft/mssql/hooks/mssql.py:
##########
@@ -37,6 +37,162 @@
from airflow.providers.common.sql.dialects.dialect import Dialect
from airflow.providers.openlineage.sqlparser import DatabaseInfo
+# Connection extras that are handled by the hook itself and must never reach
the DBAPI driver.
+_AIRFLOW_ONLY_EXTRAS = frozenset({"sqlalchemy_scheme", "dbapi_driver"})
+
+
+class _PymssqlDriver:
+ """What ``MsSqlHook`` does differently when ``dbapi_driver`` is
``pymssql`` (the default)."""
+
+ name = "pymssql"
+ default_sqlalchemy_scheme = "mssql+pymssql"
+ # None means the placeholder of ``DbApiHook``.
+ placeholder: str | None = None
+
+ def connect(self, hook: MsSqlHook) -> PymssqlConnection:
+ conn = hook.connection
+ extra_conn_args = {
+ key: val for key, val in conn.extra_dejson.items() if key.lower()
not in _AIRFLOW_ONLY_EXTRAS
+ }
+ return pymssql.connect(
+ server=conn.host or "",
+ user=conn.login,
+ password=conn.password,
+ database=hook.schema or conn.schema or "",
+ port=str(conn.port),
+ **extra_conn_args,
+ )
+
+ def sqlalchemy_url(self, hook: MsSqlHook) -> URL | None:
+ # None means the default of ``DbApiHook``, so the engine is built from
``get_uri()``.
+ return None
+
+ def get_uri(self, hook: MsSqlHook) -> str:
+ from urllib.parse import parse_qs, urlencode, urlsplit, urlunsplit
+
+ r = list(urlsplit(DbApiHook.get_uri(hook)))
Review Comment:
Done, thanks. There is now a small base class with the plain DB-API
behaviour and each driver only overrides what differs:
```python
class _DbApiDriver(ABC):
placeholder = "%s"
def set_autocommit(self, conn, autocommit):
conn.autocommit = autocommit
def get_autocommit(self, conn):
return bool(getattr(conn, "autocommit", False))
def make_common_data_structure(self, result):
return result
class _PymssqlDriver(_DbApiDriver):
def set_autocommit(self, conn, autocommit):
conn.autocommit(autocommit)
```
So the unbound DbApiHook calls are gone, and "default" is no longer spelled
three ways. The hook keeps super().get_uri() and passes it to the driver's
rewrite_uri. connect and the others take the connection and the database
instead of the hook. get_autocommit uses bool(getattr(...)), same result for
MsSqlHook since supports_autocommit is True.
I also ran SLF001, ARG002 and PLR6301 on it. The private access and the
unused hook arguments are gone. What is left is PLR6301 on the stateless base
methods and an unused scheme in the default rewrite_uri, neither rule is
enabled in the repo.
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]