-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
50 lines (33 loc) · 1.47 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""Dialect class for SQLAlchemy that uses sqlean.py as the DBAPI."""
from __future__ import annotations
import typing as t
from importlib.metadata import version
from sqlalchemy.dialects.sqlite.pysqlite import SQLiteDialect_pysqlite
if t.TYPE_CHECKING:
from types import ModuleType
from sqlalchemy.engine.url import URL
__version__ = version(__package__)
class SQLeanDialect(SQLiteDialect_pysqlite):
"""A dialect for SQLite that uses sqlean.py as the DBAPI."""
driver = "sqlean"
supports_statement_cache = True
@classmethod
def dbapi(cls) -> ModuleType: # type: ignore[override]
"""Return the DBAPI module.
NOTE: This is a legacy method that will stop being used by SQLAlchemy at some point.
"""
return cls.import_dbapi()
@classmethod
def import_dbapi(cls) -> ModuleType:
"""Return the DBAPI module."""
import sqlean # noqa: PLC0415
return sqlean # type: ignore[no-any-return]
def on_connect_url(self, url: URL) -> t.Callable[[t.Any], t.Any] | None:
"""Return a callable that will be executed on connect."""
query = url.query.get("extensions", ())
extensions = query if isinstance(query, tuple) else query.split(",")
if "all" in extensions:
self.dbapi.extensions.enable_all() # type: ignore[attr-defined]
else:
self.dbapi.extensions.enable(*extensions) # type: ignore[attr-defined]
return super().on_connect_url(url)