sqlspec 0.16.1__cp39-cp39-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of sqlspec might be problematic. Click here for more details.
- 51ff5a9eadfdefd49f98__mypyc.cpython-39-aarch64-linux-gnu.so +0 -0
- sqlspec/__init__.py +92 -0
- sqlspec/__main__.py +12 -0
- sqlspec/__metadata__.py +14 -0
- sqlspec/_serialization.py +77 -0
- sqlspec/_sql.py +1780 -0
- sqlspec/_typing.py +680 -0
- sqlspec/adapters/__init__.py +0 -0
- sqlspec/adapters/adbc/__init__.py +5 -0
- sqlspec/adapters/adbc/_types.py +12 -0
- sqlspec/adapters/adbc/config.py +361 -0
- sqlspec/adapters/adbc/driver.py +512 -0
- sqlspec/adapters/aiosqlite/__init__.py +19 -0
- sqlspec/adapters/aiosqlite/_types.py +13 -0
- sqlspec/adapters/aiosqlite/config.py +253 -0
- sqlspec/adapters/aiosqlite/driver.py +248 -0
- sqlspec/adapters/asyncmy/__init__.py +19 -0
- sqlspec/adapters/asyncmy/_types.py +12 -0
- sqlspec/adapters/asyncmy/config.py +180 -0
- sqlspec/adapters/asyncmy/driver.py +274 -0
- sqlspec/adapters/asyncpg/__init__.py +21 -0
- sqlspec/adapters/asyncpg/_types.py +17 -0
- sqlspec/adapters/asyncpg/config.py +229 -0
- sqlspec/adapters/asyncpg/driver.py +344 -0
- sqlspec/adapters/bigquery/__init__.py +18 -0
- sqlspec/adapters/bigquery/_types.py +12 -0
- sqlspec/adapters/bigquery/config.py +298 -0
- sqlspec/adapters/bigquery/driver.py +558 -0
- sqlspec/adapters/duckdb/__init__.py +22 -0
- sqlspec/adapters/duckdb/_types.py +12 -0
- sqlspec/adapters/duckdb/config.py +504 -0
- sqlspec/adapters/duckdb/driver.py +368 -0
- sqlspec/adapters/oracledb/__init__.py +32 -0
- sqlspec/adapters/oracledb/_types.py +14 -0
- sqlspec/adapters/oracledb/config.py +317 -0
- sqlspec/adapters/oracledb/driver.py +538 -0
- sqlspec/adapters/psqlpy/__init__.py +16 -0
- sqlspec/adapters/psqlpy/_types.py +11 -0
- sqlspec/adapters/psqlpy/config.py +214 -0
- sqlspec/adapters/psqlpy/driver.py +530 -0
- sqlspec/adapters/psycopg/__init__.py +32 -0
- sqlspec/adapters/psycopg/_types.py +17 -0
- sqlspec/adapters/psycopg/config.py +426 -0
- sqlspec/adapters/psycopg/driver.py +796 -0
- sqlspec/adapters/sqlite/__init__.py +15 -0
- sqlspec/adapters/sqlite/_types.py +11 -0
- sqlspec/adapters/sqlite/config.py +240 -0
- sqlspec/adapters/sqlite/driver.py +294 -0
- sqlspec/base.py +571 -0
- sqlspec/builder/__init__.py +62 -0
- sqlspec/builder/_base.py +473 -0
- sqlspec/builder/_column.py +320 -0
- sqlspec/builder/_ddl.py +1346 -0
- sqlspec/builder/_ddl_utils.py +103 -0
- sqlspec/builder/_delete.py +76 -0
- sqlspec/builder/_insert.py +256 -0
- sqlspec/builder/_merge.py +71 -0
- sqlspec/builder/_parsing_utils.py +140 -0
- sqlspec/builder/_select.py +170 -0
- sqlspec/builder/_update.py +188 -0
- sqlspec/builder/mixins/__init__.py +55 -0
- sqlspec/builder/mixins/_cte_and_set_ops.py +222 -0
- sqlspec/builder/mixins/_delete_operations.py +41 -0
- sqlspec/builder/mixins/_insert_operations.py +244 -0
- sqlspec/builder/mixins/_join_operations.py +122 -0
- sqlspec/builder/mixins/_merge_operations.py +476 -0
- sqlspec/builder/mixins/_order_limit_operations.py +135 -0
- sqlspec/builder/mixins/_pivot_operations.py +153 -0
- sqlspec/builder/mixins/_select_operations.py +603 -0
- sqlspec/builder/mixins/_update_operations.py +187 -0
- sqlspec/builder/mixins/_where_clause.py +621 -0
- sqlspec/cli.py +247 -0
- sqlspec/config.py +395 -0
- sqlspec/core/__init__.py +63 -0
- sqlspec/core/cache.cpython-39-aarch64-linux-gnu.so +0 -0
- sqlspec/core/cache.py +871 -0
- sqlspec/core/compiler.cpython-39-aarch64-linux-gnu.so +0 -0
- sqlspec/core/compiler.py +417 -0
- sqlspec/core/filters.cpython-39-aarch64-linux-gnu.so +0 -0
- sqlspec/core/filters.py +830 -0
- sqlspec/core/hashing.cpython-39-aarch64-linux-gnu.so +0 -0
- sqlspec/core/hashing.py +310 -0
- sqlspec/core/parameters.cpython-39-aarch64-linux-gnu.so +0 -0
- sqlspec/core/parameters.py +1237 -0
- sqlspec/core/result.cpython-39-aarch64-linux-gnu.so +0 -0
- sqlspec/core/result.py +677 -0
- sqlspec/core/splitter.cpython-39-aarch64-linux-gnu.so +0 -0
- sqlspec/core/splitter.py +819 -0
- sqlspec/core/statement.cpython-39-aarch64-linux-gnu.so +0 -0
- sqlspec/core/statement.py +676 -0
- sqlspec/driver/__init__.py +19 -0
- sqlspec/driver/_async.py +502 -0
- sqlspec/driver/_common.py +631 -0
- sqlspec/driver/_sync.py +503 -0
- sqlspec/driver/mixins/__init__.py +6 -0
- sqlspec/driver/mixins/_result_tools.py +193 -0
- sqlspec/driver/mixins/_sql_translator.py +86 -0
- sqlspec/exceptions.py +193 -0
- sqlspec/extensions/__init__.py +0 -0
- sqlspec/extensions/aiosql/__init__.py +10 -0
- sqlspec/extensions/aiosql/adapter.py +461 -0
- sqlspec/extensions/litestar/__init__.py +6 -0
- sqlspec/extensions/litestar/_utils.py +52 -0
- sqlspec/extensions/litestar/cli.py +48 -0
- sqlspec/extensions/litestar/config.py +92 -0
- sqlspec/extensions/litestar/handlers.py +260 -0
- sqlspec/extensions/litestar/plugin.py +145 -0
- sqlspec/extensions/litestar/providers.py +454 -0
- sqlspec/loader.cpython-39-aarch64-linux-gnu.so +0 -0
- sqlspec/loader.py +760 -0
- sqlspec/migrations/__init__.py +35 -0
- sqlspec/migrations/base.py +414 -0
- sqlspec/migrations/commands.py +443 -0
- sqlspec/migrations/loaders.py +402 -0
- sqlspec/migrations/runner.py +213 -0
- sqlspec/migrations/tracker.py +140 -0
- sqlspec/migrations/utils.py +129 -0
- sqlspec/protocols.py +407 -0
- sqlspec/py.typed +0 -0
- sqlspec/storage/__init__.py +23 -0
- sqlspec/storage/backends/__init__.py +0 -0
- sqlspec/storage/backends/base.py +163 -0
- sqlspec/storage/backends/fsspec.py +386 -0
- sqlspec/storage/backends/obstore.py +459 -0
- sqlspec/storage/capabilities.py +102 -0
- sqlspec/storage/registry.py +239 -0
- sqlspec/typing.py +299 -0
- sqlspec/utils/__init__.py +3 -0
- sqlspec/utils/correlation.py +150 -0
- sqlspec/utils/deprecation.py +106 -0
- sqlspec/utils/fixtures.cpython-39-aarch64-linux-gnu.so +0 -0
- sqlspec/utils/fixtures.py +58 -0
- sqlspec/utils/logging.py +127 -0
- sqlspec/utils/module_loader.py +89 -0
- sqlspec/utils/serializers.py +4 -0
- sqlspec/utils/singleton.py +32 -0
- sqlspec/utils/sync_tools.cpython-39-aarch64-linux-gnu.so +0 -0
- sqlspec/utils/sync_tools.py +237 -0
- sqlspec/utils/text.cpython-39-aarch64-linux-gnu.so +0 -0
- sqlspec/utils/text.py +96 -0
- sqlspec/utils/type_guards.cpython-39-aarch64-linux-gnu.so +0 -0
- sqlspec/utils/type_guards.py +1139 -0
- sqlspec-0.16.1.dist-info/METADATA +365 -0
- sqlspec-0.16.1.dist-info/RECORD +148 -0
- sqlspec-0.16.1.dist-info/WHEEL +7 -0
- sqlspec-0.16.1.dist-info/entry_points.txt +2 -0
- sqlspec-0.16.1.dist-info/licenses/LICENSE +21 -0
- sqlspec-0.16.1.dist-info/licenses/NOTICE +29 -0
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
"""ADBC database configuration using TypedDict for better maintainability."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
from contextlib import contextmanager
|
|
5
|
+
from typing import TYPE_CHECKING, Any, Callable, ClassVar, Optional, TypedDict, Union
|
|
6
|
+
|
|
7
|
+
from typing_extensions import NotRequired
|
|
8
|
+
|
|
9
|
+
from sqlspec.adapters.adbc._types import AdbcConnection
|
|
10
|
+
from sqlspec.adapters.adbc.driver import AdbcCursor, AdbcDriver, get_adbc_statement_config
|
|
11
|
+
from sqlspec.config import NoPoolSyncConfig
|
|
12
|
+
from sqlspec.core.statement import StatementConfig
|
|
13
|
+
from sqlspec.exceptions import ImproperConfigurationError
|
|
14
|
+
from sqlspec.utils.module_loader import import_string
|
|
15
|
+
|
|
16
|
+
if TYPE_CHECKING:
|
|
17
|
+
from collections.abc import Generator
|
|
18
|
+
from contextlib import AbstractContextManager
|
|
19
|
+
|
|
20
|
+
from sqlglot.dialects.dialect import DialectType
|
|
21
|
+
|
|
22
|
+
logger = logging.getLogger("sqlspec.adapters.adbc")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class AdbcConnectionParams(TypedDict, total=False):
|
|
26
|
+
"""ADBC connection parameters."""
|
|
27
|
+
|
|
28
|
+
uri: NotRequired[str]
|
|
29
|
+
driver_name: NotRequired[str]
|
|
30
|
+
db_kwargs: NotRequired[dict[str, Any]]
|
|
31
|
+
conn_kwargs: NotRequired[dict[str, Any]]
|
|
32
|
+
adbc_driver_manager_entrypoint: NotRequired[str]
|
|
33
|
+
autocommit: NotRequired[bool]
|
|
34
|
+
isolation_level: NotRequired[str]
|
|
35
|
+
batch_size: NotRequired[int]
|
|
36
|
+
query_timeout: NotRequired[float]
|
|
37
|
+
connection_timeout: NotRequired[float]
|
|
38
|
+
ssl_mode: NotRequired[str]
|
|
39
|
+
ssl_cert: NotRequired[str]
|
|
40
|
+
ssl_key: NotRequired[str]
|
|
41
|
+
ssl_ca: NotRequired[str]
|
|
42
|
+
username: NotRequired[str]
|
|
43
|
+
password: NotRequired[str]
|
|
44
|
+
token: NotRequired[str]
|
|
45
|
+
project_id: NotRequired[str]
|
|
46
|
+
dataset_id: NotRequired[str]
|
|
47
|
+
account: NotRequired[str]
|
|
48
|
+
warehouse: NotRequired[str]
|
|
49
|
+
database: NotRequired[str]
|
|
50
|
+
schema: NotRequired[str]
|
|
51
|
+
role: NotRequired[str]
|
|
52
|
+
authorization_header: NotRequired[str]
|
|
53
|
+
grpc_options: NotRequired[dict[str, Any]]
|
|
54
|
+
extra: NotRequired[dict[str, Any]]
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
__all__ = ("AdbcConfig", "AdbcConnectionParams")
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class AdbcConfig(NoPoolSyncConfig[AdbcConnection, AdbcDriver]):
|
|
61
|
+
"""ADBC configuration for Arrow Database Connectivity.
|
|
62
|
+
|
|
63
|
+
ADBC (Arrow Database Connectivity) provides a unified interface for connecting
|
|
64
|
+
to multiple database systems with Arrow-native data transfer.
|
|
65
|
+
|
|
66
|
+
This configuration supports:
|
|
67
|
+
- Universal driver detection and loading
|
|
68
|
+
- Arrow data streaming
|
|
69
|
+
- Bulk ingestion operations
|
|
70
|
+
- Multiple database backends (PostgreSQL, SQLite, DuckDB, BigQuery, Snowflake, etc.)
|
|
71
|
+
- Driver path resolution
|
|
72
|
+
- Cloud database integrations
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
driver_type: ClassVar[type[AdbcDriver]] = AdbcDriver
|
|
76
|
+
connection_type: "ClassVar[type[AdbcConnection]]" = AdbcConnection
|
|
77
|
+
|
|
78
|
+
def __init__(
|
|
79
|
+
self,
|
|
80
|
+
*,
|
|
81
|
+
connection_config: Optional[Union[AdbcConnectionParams, dict[str, Any]]] = None,
|
|
82
|
+
statement_config: Optional[StatementConfig] = None,
|
|
83
|
+
migration_config: Optional[dict[str, Any]] = None,
|
|
84
|
+
) -> None:
|
|
85
|
+
"""Initialize ADBC configuration.
|
|
86
|
+
|
|
87
|
+
Args:
|
|
88
|
+
connection_config: Connection configuration parameters
|
|
89
|
+
statement_config: Default SQL statement configuration
|
|
90
|
+
migration_config: Migration configuration
|
|
91
|
+
"""
|
|
92
|
+
if connection_config is None:
|
|
93
|
+
connection_config = {}
|
|
94
|
+
extras = connection_config.pop("extra", {})
|
|
95
|
+
if not isinstance(extras, dict):
|
|
96
|
+
msg = "The 'extra' field in connection_config must be a dictionary."
|
|
97
|
+
raise ImproperConfigurationError(msg)
|
|
98
|
+
self.connection_config: dict[str, Any] = dict(connection_config)
|
|
99
|
+
self.connection_config.update(extras)
|
|
100
|
+
|
|
101
|
+
if statement_config is None:
|
|
102
|
+
detected_dialect = str(self._get_dialect() or "sqlite")
|
|
103
|
+
statement_config = get_adbc_statement_config(detected_dialect)
|
|
104
|
+
|
|
105
|
+
super().__init__(
|
|
106
|
+
connection_config=self.connection_config,
|
|
107
|
+
migration_config=migration_config,
|
|
108
|
+
statement_config=statement_config,
|
|
109
|
+
driver_features={},
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
def _resolve_driver_name(self) -> str:
|
|
113
|
+
"""Resolve and normalize the ADBC driver name.
|
|
114
|
+
|
|
115
|
+
Returns:
|
|
116
|
+
The normalized driver connect function path.
|
|
117
|
+
"""
|
|
118
|
+
driver_name = self.connection_config.get("driver_name")
|
|
119
|
+
uri = self.connection_config.get("uri")
|
|
120
|
+
|
|
121
|
+
if isinstance(driver_name, str):
|
|
122
|
+
driver_aliases = {
|
|
123
|
+
"sqlite": "adbc_driver_sqlite.dbapi.connect",
|
|
124
|
+
"sqlite3": "adbc_driver_sqlite.dbapi.connect",
|
|
125
|
+
"adbc_driver_sqlite": "adbc_driver_sqlite.dbapi.connect",
|
|
126
|
+
"duckdb": "adbc_driver_duckdb.dbapi.connect",
|
|
127
|
+
"adbc_driver_duckdb": "adbc_driver_duckdb.dbapi.connect",
|
|
128
|
+
"postgres": "adbc_driver_postgresql.dbapi.connect",
|
|
129
|
+
"postgresql": "adbc_driver_postgresql.dbapi.connect",
|
|
130
|
+
"pg": "adbc_driver_postgresql.dbapi.connect",
|
|
131
|
+
"adbc_driver_postgresql": "adbc_driver_postgresql.dbapi.connect",
|
|
132
|
+
"snowflake": "adbc_driver_snowflake.dbapi.connect",
|
|
133
|
+
"sf": "adbc_driver_snowflake.dbapi.connect",
|
|
134
|
+
"adbc_driver_snowflake": "adbc_driver_snowflake.dbapi.connect",
|
|
135
|
+
"bigquery": "adbc_driver_bigquery.dbapi.connect",
|
|
136
|
+
"bq": "adbc_driver_bigquery.dbapi.connect",
|
|
137
|
+
"adbc_driver_bigquery": "adbc_driver_bigquery.dbapi.connect",
|
|
138
|
+
"flightsql": "adbc_driver_flightsql.dbapi.connect",
|
|
139
|
+
"adbc_driver_flightsql": "adbc_driver_flightsql.dbapi.connect",
|
|
140
|
+
"grpc": "adbc_driver_flightsql.dbapi.connect",
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
resolved_driver = driver_aliases.get(driver_name, driver_name)
|
|
144
|
+
|
|
145
|
+
if not resolved_driver.endswith(".dbapi.connect"):
|
|
146
|
+
resolved_driver = f"{resolved_driver}.dbapi.connect"
|
|
147
|
+
|
|
148
|
+
return resolved_driver
|
|
149
|
+
|
|
150
|
+
if isinstance(uri, str):
|
|
151
|
+
if uri.startswith(("postgresql://", "postgres://")):
|
|
152
|
+
return "adbc_driver_postgresql.dbapi.connect"
|
|
153
|
+
if uri.startswith("sqlite://"):
|
|
154
|
+
return "adbc_driver_sqlite.dbapi.connect"
|
|
155
|
+
if uri.startswith("duckdb://"):
|
|
156
|
+
return "adbc_driver_duckdb.dbapi.connect"
|
|
157
|
+
if uri.startswith("grpc://"):
|
|
158
|
+
return "adbc_driver_flightsql.dbapi.connect"
|
|
159
|
+
if uri.startswith("snowflake://"):
|
|
160
|
+
return "adbc_driver_snowflake.dbapi.connect"
|
|
161
|
+
if uri.startswith("bigquery://"):
|
|
162
|
+
return "adbc_driver_bigquery.dbapi.connect"
|
|
163
|
+
|
|
164
|
+
return "adbc_driver_sqlite.dbapi.connect"
|
|
165
|
+
|
|
166
|
+
def _get_connect_func(self) -> Callable[..., AdbcConnection]:
|
|
167
|
+
"""Get the ADBC driver connect function.
|
|
168
|
+
|
|
169
|
+
Returns:
|
|
170
|
+
The driver connect function.
|
|
171
|
+
|
|
172
|
+
Raises:
|
|
173
|
+
ImproperConfigurationError: If driver cannot be loaded.
|
|
174
|
+
"""
|
|
175
|
+
driver_path = self._resolve_driver_name()
|
|
176
|
+
|
|
177
|
+
try:
|
|
178
|
+
connect_func = import_string(driver_path)
|
|
179
|
+
except ImportError as e:
|
|
180
|
+
driver_path_with_suffix = f"{driver_path}.dbapi.connect"
|
|
181
|
+
try:
|
|
182
|
+
connect_func = import_string(driver_path_with_suffix)
|
|
183
|
+
except ImportError as e2:
|
|
184
|
+
msg = (
|
|
185
|
+
f"Failed to import ADBC connect function from '{driver_path}' or "
|
|
186
|
+
f"'{driver_path_with_suffix}'. Is the driver installed? "
|
|
187
|
+
f"Original errors: {e} / {e2}"
|
|
188
|
+
)
|
|
189
|
+
raise ImproperConfigurationError(msg) from e2
|
|
190
|
+
|
|
191
|
+
if not callable(connect_func):
|
|
192
|
+
msg = f"The path '{driver_path}' did not resolve to a callable function."
|
|
193
|
+
raise ImproperConfigurationError(msg)
|
|
194
|
+
|
|
195
|
+
return connect_func # type: ignore[no-any-return]
|
|
196
|
+
|
|
197
|
+
def _get_dialect(self) -> "DialectType":
|
|
198
|
+
"""Get the SQL dialect type based on the ADBC driver.
|
|
199
|
+
|
|
200
|
+
Returns:
|
|
201
|
+
The SQL dialect type for the ADBC driver.
|
|
202
|
+
"""
|
|
203
|
+
try:
|
|
204
|
+
driver_path = self._resolve_driver_name()
|
|
205
|
+
except ImproperConfigurationError:
|
|
206
|
+
return None
|
|
207
|
+
|
|
208
|
+
dialect_map = {
|
|
209
|
+
"postgres": "postgres",
|
|
210
|
+
"sqlite": "sqlite",
|
|
211
|
+
"duckdb": "duckdb",
|
|
212
|
+
"bigquery": "bigquery",
|
|
213
|
+
"snowflake": "snowflake",
|
|
214
|
+
"flightsql": "sqlite",
|
|
215
|
+
"grpc": "sqlite",
|
|
216
|
+
}
|
|
217
|
+
for keyword, dialect in dialect_map.items():
|
|
218
|
+
if keyword in driver_path:
|
|
219
|
+
return dialect
|
|
220
|
+
return None
|
|
221
|
+
|
|
222
|
+
def _get_parameter_styles(self) -> tuple[tuple[str, ...], str]:
|
|
223
|
+
"""Get parameter styles based on the underlying driver.
|
|
224
|
+
|
|
225
|
+
Returns:
|
|
226
|
+
Tuple of (supported_parameter_styles, default_parameter_style)
|
|
227
|
+
"""
|
|
228
|
+
try:
|
|
229
|
+
driver_path = self._resolve_driver_name()
|
|
230
|
+
if "postgresql" in driver_path:
|
|
231
|
+
return (("numeric",), "numeric")
|
|
232
|
+
if "sqlite" in driver_path:
|
|
233
|
+
return (("qmark", "named_colon"), "qmark")
|
|
234
|
+
if "duckdb" in driver_path:
|
|
235
|
+
return (("qmark", "numeric"), "qmark")
|
|
236
|
+
if "bigquery" in driver_path:
|
|
237
|
+
return (("named_at",), "named_at")
|
|
238
|
+
if "snowflake" in driver_path:
|
|
239
|
+
return (("qmark", "numeric"), "qmark")
|
|
240
|
+
|
|
241
|
+
except Exception:
|
|
242
|
+
logger.debug("Error resolving parameter styles for ADBC driver, using defaults")
|
|
243
|
+
return (("qmark",), "qmark")
|
|
244
|
+
|
|
245
|
+
def create_connection(self) -> AdbcConnection:
|
|
246
|
+
"""Create and return a new ADBC connection using the specified driver.
|
|
247
|
+
|
|
248
|
+
Returns:
|
|
249
|
+
A new ADBC connection instance.
|
|
250
|
+
|
|
251
|
+
Raises:
|
|
252
|
+
ImproperConfigurationError: If the connection could not be established.
|
|
253
|
+
"""
|
|
254
|
+
|
|
255
|
+
try:
|
|
256
|
+
connect_func = self._get_connect_func()
|
|
257
|
+
connection_config_dict = self._get_connection_config_dict()
|
|
258
|
+
connection = connect_func(**connection_config_dict)
|
|
259
|
+
except Exception as e:
|
|
260
|
+
driver_name = self.connection_config.get("driver_name", "Unknown")
|
|
261
|
+
msg = f"Could not configure ADBC connection using driver '{driver_name}'. Error: {e}"
|
|
262
|
+
raise ImproperConfigurationError(msg) from e
|
|
263
|
+
return connection
|
|
264
|
+
|
|
265
|
+
@contextmanager
|
|
266
|
+
def provide_connection(self, *args: Any, **kwargs: Any) -> "Generator[AdbcConnection, None, None]":
|
|
267
|
+
"""Provide an ADBC connection context manager.
|
|
268
|
+
|
|
269
|
+
Args:
|
|
270
|
+
*args: Additional arguments.
|
|
271
|
+
**kwargs: Additional keyword arguments.
|
|
272
|
+
|
|
273
|
+
Yields:
|
|
274
|
+
An ADBC connection instance.
|
|
275
|
+
"""
|
|
276
|
+
connection = self.create_connection()
|
|
277
|
+
try:
|
|
278
|
+
yield connection
|
|
279
|
+
finally:
|
|
280
|
+
connection.close()
|
|
281
|
+
|
|
282
|
+
def provide_session(
|
|
283
|
+
self, *args: Any, statement_config: "Optional[StatementConfig]" = None, **kwargs: Any
|
|
284
|
+
) -> "AbstractContextManager[AdbcDriver]":
|
|
285
|
+
"""Provide an ADBC driver session context manager.
|
|
286
|
+
|
|
287
|
+
Args:
|
|
288
|
+
*args: Additional arguments.
|
|
289
|
+
statement_config: Optional statement configuration override.
|
|
290
|
+
**kwargs: Additional keyword arguments.
|
|
291
|
+
|
|
292
|
+
Returns:
|
|
293
|
+
A context manager that yields an AdbcDriver instance.
|
|
294
|
+
"""
|
|
295
|
+
|
|
296
|
+
@contextmanager
|
|
297
|
+
def session_manager() -> "Generator[AdbcDriver, None, None]":
|
|
298
|
+
with self.provide_connection(*args, **kwargs) as connection:
|
|
299
|
+
final_statement_config = (
|
|
300
|
+
statement_config
|
|
301
|
+
or self.statement_config
|
|
302
|
+
or get_adbc_statement_config(str(self._get_dialect() or "sqlite"))
|
|
303
|
+
)
|
|
304
|
+
yield self.driver_type(connection=connection, statement_config=final_statement_config)
|
|
305
|
+
|
|
306
|
+
return session_manager()
|
|
307
|
+
|
|
308
|
+
def _get_connection_config_dict(self) -> dict[str, Any]:
|
|
309
|
+
"""Get the connection configuration dictionary.
|
|
310
|
+
|
|
311
|
+
Returns:
|
|
312
|
+
The connection configuration dictionary.
|
|
313
|
+
"""
|
|
314
|
+
config = dict(self.connection_config)
|
|
315
|
+
|
|
316
|
+
if "driver_name" in config:
|
|
317
|
+
driver_name = config["driver_name"]
|
|
318
|
+
|
|
319
|
+
if "uri" in config:
|
|
320
|
+
uri = config["uri"]
|
|
321
|
+
|
|
322
|
+
if driver_name in {"sqlite", "sqlite3", "adbc_driver_sqlite"} and uri.startswith("sqlite://"): # pyright: ignore
|
|
323
|
+
config["uri"] = uri[9:] # pyright: ignore
|
|
324
|
+
|
|
325
|
+
elif driver_name in {"duckdb", "adbc_driver_duckdb"} and uri.startswith("duckdb://"): # pyright: ignore
|
|
326
|
+
config["path"] = uri[9:] # pyright: ignore
|
|
327
|
+
config.pop("uri", None)
|
|
328
|
+
|
|
329
|
+
if driver_name in {"bigquery", "bq", "adbc_driver_bigquery"}:
|
|
330
|
+
bigquery_parameters = ["project_id", "dataset_id", "token"]
|
|
331
|
+
db_kwargs = config.get("db_kwargs", {})
|
|
332
|
+
|
|
333
|
+
for param in bigquery_parameters:
|
|
334
|
+
if param in config and param != "db_kwargs":
|
|
335
|
+
db_kwargs[param] = config.pop(param) # pyright: ignore
|
|
336
|
+
|
|
337
|
+
if db_kwargs:
|
|
338
|
+
config["db_kwargs"] = db_kwargs
|
|
339
|
+
|
|
340
|
+
elif "db_kwargs" in config and driver_name not in {"bigquery", "bq", "adbc_driver_bigquery"}:
|
|
341
|
+
db_kwargs = config.pop("db_kwargs")
|
|
342
|
+
if isinstance(db_kwargs, dict):
|
|
343
|
+
config.update(db_kwargs)
|
|
344
|
+
|
|
345
|
+
config.pop("driver_name", None)
|
|
346
|
+
|
|
347
|
+
return config
|
|
348
|
+
|
|
349
|
+
def get_signature_namespace(self) -> "dict[str, type[Any]]":
|
|
350
|
+
"""Get the signature namespace for ADBC types.
|
|
351
|
+
|
|
352
|
+
This provides all ADBC-specific types that Litestar needs to recognize
|
|
353
|
+
to avoid serialization attempts.
|
|
354
|
+
|
|
355
|
+
Returns:
|
|
356
|
+
Dictionary mapping type names to types.
|
|
357
|
+
"""
|
|
358
|
+
|
|
359
|
+
namespace = super().get_signature_namespace()
|
|
360
|
+
namespace.update({"AdbcConnection": AdbcConnection, "AdbcCursor": AdbcCursor})
|
|
361
|
+
return namespace
|