sqlspec 0.32.0__py3-none-any.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.
- sqlspec/__init__.py +104 -0
- sqlspec/__main__.py +12 -0
- sqlspec/__metadata__.py +14 -0
- sqlspec/_serialization.py +312 -0
- sqlspec/_typing.py +784 -0
- sqlspec/adapters/__init__.py +0 -0
- sqlspec/adapters/adbc/__init__.py +5 -0
- sqlspec/adapters/adbc/_types.py +12 -0
- sqlspec/adapters/adbc/adk/__init__.py +5 -0
- sqlspec/adapters/adbc/adk/store.py +880 -0
- sqlspec/adapters/adbc/config.py +436 -0
- sqlspec/adapters/adbc/data_dictionary.py +537 -0
- sqlspec/adapters/adbc/driver.py +841 -0
- sqlspec/adapters/adbc/litestar/__init__.py +5 -0
- sqlspec/adapters/adbc/litestar/store.py +504 -0
- sqlspec/adapters/adbc/type_converter.py +153 -0
- sqlspec/adapters/aiosqlite/__init__.py +29 -0
- sqlspec/adapters/aiosqlite/_types.py +13 -0
- sqlspec/adapters/aiosqlite/adk/__init__.py +5 -0
- sqlspec/adapters/aiosqlite/adk/store.py +536 -0
- sqlspec/adapters/aiosqlite/config.py +310 -0
- sqlspec/adapters/aiosqlite/data_dictionary.py +260 -0
- sqlspec/adapters/aiosqlite/driver.py +463 -0
- sqlspec/adapters/aiosqlite/litestar/__init__.py +5 -0
- sqlspec/adapters/aiosqlite/litestar/store.py +281 -0
- sqlspec/adapters/aiosqlite/pool.py +500 -0
- sqlspec/adapters/asyncmy/__init__.py +25 -0
- sqlspec/adapters/asyncmy/_types.py +12 -0
- sqlspec/adapters/asyncmy/adk/__init__.py +5 -0
- sqlspec/adapters/asyncmy/adk/store.py +503 -0
- sqlspec/adapters/asyncmy/config.py +246 -0
- sqlspec/adapters/asyncmy/data_dictionary.py +241 -0
- sqlspec/adapters/asyncmy/driver.py +632 -0
- sqlspec/adapters/asyncmy/litestar/__init__.py +5 -0
- sqlspec/adapters/asyncmy/litestar/store.py +296 -0
- sqlspec/adapters/asyncpg/__init__.py +23 -0
- sqlspec/adapters/asyncpg/_type_handlers.py +76 -0
- sqlspec/adapters/asyncpg/_types.py +23 -0
- sqlspec/adapters/asyncpg/adk/__init__.py +5 -0
- sqlspec/adapters/asyncpg/adk/store.py +460 -0
- sqlspec/adapters/asyncpg/config.py +464 -0
- sqlspec/adapters/asyncpg/data_dictionary.py +321 -0
- sqlspec/adapters/asyncpg/driver.py +720 -0
- sqlspec/adapters/asyncpg/litestar/__init__.py +5 -0
- sqlspec/adapters/asyncpg/litestar/store.py +253 -0
- sqlspec/adapters/bigquery/__init__.py +18 -0
- sqlspec/adapters/bigquery/_types.py +12 -0
- sqlspec/adapters/bigquery/adk/__init__.py +5 -0
- sqlspec/adapters/bigquery/adk/store.py +585 -0
- sqlspec/adapters/bigquery/config.py +298 -0
- sqlspec/adapters/bigquery/data_dictionary.py +256 -0
- sqlspec/adapters/bigquery/driver.py +1073 -0
- sqlspec/adapters/bigquery/litestar/__init__.py +5 -0
- sqlspec/adapters/bigquery/litestar/store.py +327 -0
- sqlspec/adapters/bigquery/type_converter.py +125 -0
- sqlspec/adapters/duckdb/__init__.py +24 -0
- sqlspec/adapters/duckdb/_types.py +12 -0
- sqlspec/adapters/duckdb/adk/__init__.py +14 -0
- sqlspec/adapters/duckdb/adk/store.py +563 -0
- sqlspec/adapters/duckdb/config.py +396 -0
- sqlspec/adapters/duckdb/data_dictionary.py +264 -0
- sqlspec/adapters/duckdb/driver.py +604 -0
- sqlspec/adapters/duckdb/litestar/__init__.py +5 -0
- sqlspec/adapters/duckdb/litestar/store.py +332 -0
- sqlspec/adapters/duckdb/pool.py +273 -0
- sqlspec/adapters/duckdb/type_converter.py +133 -0
- sqlspec/adapters/oracledb/__init__.py +32 -0
- sqlspec/adapters/oracledb/_numpy_handlers.py +133 -0
- sqlspec/adapters/oracledb/_types.py +39 -0
- sqlspec/adapters/oracledb/_uuid_handlers.py +130 -0
- sqlspec/adapters/oracledb/adk/__init__.py +5 -0
- sqlspec/adapters/oracledb/adk/store.py +1632 -0
- sqlspec/adapters/oracledb/config.py +469 -0
- sqlspec/adapters/oracledb/data_dictionary.py +717 -0
- sqlspec/adapters/oracledb/driver.py +1493 -0
- sqlspec/adapters/oracledb/litestar/__init__.py +5 -0
- sqlspec/adapters/oracledb/litestar/store.py +765 -0
- sqlspec/adapters/oracledb/migrations.py +532 -0
- sqlspec/adapters/oracledb/type_converter.py +207 -0
- sqlspec/adapters/psqlpy/__init__.py +16 -0
- sqlspec/adapters/psqlpy/_type_handlers.py +44 -0
- sqlspec/adapters/psqlpy/_types.py +12 -0
- sqlspec/adapters/psqlpy/adk/__init__.py +5 -0
- sqlspec/adapters/psqlpy/adk/store.py +483 -0
- sqlspec/adapters/psqlpy/config.py +271 -0
- sqlspec/adapters/psqlpy/data_dictionary.py +179 -0
- sqlspec/adapters/psqlpy/driver.py +892 -0
- sqlspec/adapters/psqlpy/litestar/__init__.py +5 -0
- sqlspec/adapters/psqlpy/litestar/store.py +272 -0
- sqlspec/adapters/psqlpy/type_converter.py +102 -0
- sqlspec/adapters/psycopg/__init__.py +32 -0
- sqlspec/adapters/psycopg/_type_handlers.py +90 -0
- sqlspec/adapters/psycopg/_types.py +18 -0
- sqlspec/adapters/psycopg/adk/__init__.py +5 -0
- sqlspec/adapters/psycopg/adk/store.py +962 -0
- sqlspec/adapters/psycopg/config.py +487 -0
- sqlspec/adapters/psycopg/data_dictionary.py +630 -0
- sqlspec/adapters/psycopg/driver.py +1336 -0
- sqlspec/adapters/psycopg/litestar/__init__.py +5 -0
- sqlspec/adapters/psycopg/litestar/store.py +554 -0
- sqlspec/adapters/spanner/__init__.py +38 -0
- sqlspec/adapters/spanner/_type_handlers.py +186 -0
- sqlspec/adapters/spanner/_types.py +12 -0
- sqlspec/adapters/spanner/adk/__init__.py +5 -0
- sqlspec/adapters/spanner/adk/store.py +435 -0
- sqlspec/adapters/spanner/config.py +241 -0
- sqlspec/adapters/spanner/data_dictionary.py +95 -0
- sqlspec/adapters/spanner/dialect/__init__.py +6 -0
- sqlspec/adapters/spanner/dialect/_spangres.py +52 -0
- sqlspec/adapters/spanner/dialect/_spanner.py +123 -0
- sqlspec/adapters/spanner/driver.py +366 -0
- sqlspec/adapters/spanner/litestar/__init__.py +5 -0
- sqlspec/adapters/spanner/litestar/store.py +266 -0
- sqlspec/adapters/spanner/type_converter.py +46 -0
- sqlspec/adapters/sqlite/__init__.py +18 -0
- sqlspec/adapters/sqlite/_type_handlers.py +86 -0
- sqlspec/adapters/sqlite/_types.py +11 -0
- sqlspec/adapters/sqlite/adk/__init__.py +5 -0
- sqlspec/adapters/sqlite/adk/store.py +582 -0
- sqlspec/adapters/sqlite/config.py +221 -0
- sqlspec/adapters/sqlite/data_dictionary.py +256 -0
- sqlspec/adapters/sqlite/driver.py +527 -0
- sqlspec/adapters/sqlite/litestar/__init__.py +5 -0
- sqlspec/adapters/sqlite/litestar/store.py +318 -0
- sqlspec/adapters/sqlite/pool.py +140 -0
- sqlspec/base.py +811 -0
- sqlspec/builder/__init__.py +146 -0
- sqlspec/builder/_base.py +900 -0
- sqlspec/builder/_column.py +517 -0
- sqlspec/builder/_ddl.py +1642 -0
- sqlspec/builder/_delete.py +84 -0
- sqlspec/builder/_dml.py +381 -0
- sqlspec/builder/_expression_wrappers.py +46 -0
- sqlspec/builder/_factory.py +1537 -0
- sqlspec/builder/_insert.py +315 -0
- sqlspec/builder/_join.py +375 -0
- sqlspec/builder/_merge.py +848 -0
- sqlspec/builder/_parsing_utils.py +297 -0
- sqlspec/builder/_select.py +1615 -0
- sqlspec/builder/_update.py +161 -0
- sqlspec/builder/_vector_expressions.py +259 -0
- sqlspec/cli.py +764 -0
- sqlspec/config.py +1540 -0
- sqlspec/core/__init__.py +305 -0
- sqlspec/core/cache.py +785 -0
- sqlspec/core/compiler.py +603 -0
- sqlspec/core/filters.py +872 -0
- sqlspec/core/hashing.py +274 -0
- sqlspec/core/metrics.py +83 -0
- sqlspec/core/parameters/__init__.py +64 -0
- sqlspec/core/parameters/_alignment.py +266 -0
- sqlspec/core/parameters/_converter.py +413 -0
- sqlspec/core/parameters/_processor.py +341 -0
- sqlspec/core/parameters/_registry.py +201 -0
- sqlspec/core/parameters/_transformers.py +226 -0
- sqlspec/core/parameters/_types.py +430 -0
- sqlspec/core/parameters/_validator.py +123 -0
- sqlspec/core/pipeline.py +187 -0
- sqlspec/core/result.py +1124 -0
- sqlspec/core/splitter.py +940 -0
- sqlspec/core/stack.py +163 -0
- sqlspec/core/statement.py +835 -0
- sqlspec/core/type_conversion.py +235 -0
- sqlspec/driver/__init__.py +36 -0
- sqlspec/driver/_async.py +1027 -0
- sqlspec/driver/_common.py +1236 -0
- sqlspec/driver/_sync.py +1025 -0
- sqlspec/driver/mixins/__init__.py +7 -0
- sqlspec/driver/mixins/_result_tools.py +61 -0
- sqlspec/driver/mixins/_sql_translator.py +122 -0
- sqlspec/driver/mixins/_storage.py +311 -0
- sqlspec/exceptions.py +321 -0
- sqlspec/extensions/__init__.py +0 -0
- sqlspec/extensions/adk/__init__.py +53 -0
- sqlspec/extensions/adk/_types.py +51 -0
- sqlspec/extensions/adk/converters.py +172 -0
- sqlspec/extensions/adk/migrations/0001_create_adk_tables.py +144 -0
- sqlspec/extensions/adk/migrations/__init__.py +0 -0
- sqlspec/extensions/adk/service.py +181 -0
- sqlspec/extensions/adk/store.py +536 -0
- sqlspec/extensions/aiosql/__init__.py +10 -0
- sqlspec/extensions/aiosql/adapter.py +471 -0
- sqlspec/extensions/fastapi/__init__.py +19 -0
- sqlspec/extensions/fastapi/extension.py +341 -0
- sqlspec/extensions/fastapi/providers.py +543 -0
- sqlspec/extensions/flask/__init__.py +36 -0
- sqlspec/extensions/flask/_state.py +72 -0
- sqlspec/extensions/flask/_utils.py +40 -0
- sqlspec/extensions/flask/extension.py +402 -0
- sqlspec/extensions/litestar/__init__.py +23 -0
- sqlspec/extensions/litestar/_utils.py +52 -0
- sqlspec/extensions/litestar/cli.py +92 -0
- sqlspec/extensions/litestar/config.py +90 -0
- sqlspec/extensions/litestar/handlers.py +316 -0
- sqlspec/extensions/litestar/migrations/0001_create_session_table.py +137 -0
- sqlspec/extensions/litestar/migrations/__init__.py +3 -0
- sqlspec/extensions/litestar/plugin.py +638 -0
- sqlspec/extensions/litestar/providers.py +454 -0
- sqlspec/extensions/litestar/store.py +265 -0
- sqlspec/extensions/otel/__init__.py +58 -0
- sqlspec/extensions/prometheus/__init__.py +107 -0
- sqlspec/extensions/starlette/__init__.py +10 -0
- sqlspec/extensions/starlette/_state.py +26 -0
- sqlspec/extensions/starlette/_utils.py +52 -0
- sqlspec/extensions/starlette/extension.py +257 -0
- sqlspec/extensions/starlette/middleware.py +154 -0
- sqlspec/loader.py +716 -0
- sqlspec/migrations/__init__.py +36 -0
- sqlspec/migrations/base.py +728 -0
- sqlspec/migrations/commands.py +1140 -0
- sqlspec/migrations/context.py +142 -0
- sqlspec/migrations/fix.py +203 -0
- sqlspec/migrations/loaders.py +450 -0
- sqlspec/migrations/runner.py +1024 -0
- sqlspec/migrations/templates.py +234 -0
- sqlspec/migrations/tracker.py +403 -0
- sqlspec/migrations/utils.py +256 -0
- sqlspec/migrations/validation.py +203 -0
- sqlspec/observability/__init__.py +22 -0
- sqlspec/observability/_config.py +228 -0
- sqlspec/observability/_diagnostics.py +67 -0
- sqlspec/observability/_dispatcher.py +151 -0
- sqlspec/observability/_observer.py +180 -0
- sqlspec/observability/_runtime.py +381 -0
- sqlspec/observability/_spans.py +158 -0
- sqlspec/protocols.py +530 -0
- sqlspec/py.typed +0 -0
- sqlspec/storage/__init__.py +46 -0
- sqlspec/storage/_utils.py +104 -0
- sqlspec/storage/backends/__init__.py +1 -0
- sqlspec/storage/backends/base.py +163 -0
- sqlspec/storage/backends/fsspec.py +398 -0
- sqlspec/storage/backends/local.py +377 -0
- sqlspec/storage/backends/obstore.py +580 -0
- sqlspec/storage/errors.py +104 -0
- sqlspec/storage/pipeline.py +604 -0
- sqlspec/storage/registry.py +289 -0
- sqlspec/typing.py +219 -0
- sqlspec/utils/__init__.py +31 -0
- sqlspec/utils/arrow_helpers.py +95 -0
- sqlspec/utils/config_resolver.py +153 -0
- sqlspec/utils/correlation.py +132 -0
- sqlspec/utils/data_transformation.py +114 -0
- sqlspec/utils/dependencies.py +79 -0
- sqlspec/utils/deprecation.py +113 -0
- sqlspec/utils/fixtures.py +250 -0
- sqlspec/utils/logging.py +172 -0
- sqlspec/utils/module_loader.py +273 -0
- sqlspec/utils/portal.py +325 -0
- sqlspec/utils/schema.py +288 -0
- sqlspec/utils/serializers.py +396 -0
- sqlspec/utils/singleton.py +41 -0
- sqlspec/utils/sync_tools.py +277 -0
- sqlspec/utils/text.py +108 -0
- sqlspec/utils/type_converters.py +99 -0
- sqlspec/utils/type_guards.py +1324 -0
- sqlspec/utils/version.py +444 -0
- sqlspec-0.32.0.dist-info/METADATA +202 -0
- sqlspec-0.32.0.dist-info/RECORD +262 -0
- sqlspec-0.32.0.dist-info/WHEEL +4 -0
- sqlspec-0.32.0.dist-info/entry_points.txt +2 -0
- sqlspec-0.32.0.dist-info/licenses/LICENSE +21 -0
sqlspec/base.py
ADDED
|
@@ -0,0 +1,811 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import atexit
|
|
3
|
+
from collections.abc import AsyncIterator, Awaitable, Coroutine, Iterator
|
|
4
|
+
from contextlib import AbstractAsyncContextManager, AbstractContextManager, asynccontextmanager, contextmanager
|
|
5
|
+
from typing import TYPE_CHECKING, Any, TypeGuard, Union, cast, overload
|
|
6
|
+
|
|
7
|
+
from sqlspec.config import (
|
|
8
|
+
AsyncConfigT,
|
|
9
|
+
AsyncDatabaseConfig,
|
|
10
|
+
DatabaseConfigProtocol,
|
|
11
|
+
DriverT,
|
|
12
|
+
NoPoolAsyncConfig,
|
|
13
|
+
NoPoolSyncConfig,
|
|
14
|
+
SyncConfigT,
|
|
15
|
+
SyncDatabaseConfig,
|
|
16
|
+
)
|
|
17
|
+
from sqlspec.core import (
|
|
18
|
+
CacheConfig,
|
|
19
|
+
get_cache_config,
|
|
20
|
+
get_cache_statistics,
|
|
21
|
+
log_cache_stats,
|
|
22
|
+
reset_cache_stats,
|
|
23
|
+
update_cache_config,
|
|
24
|
+
)
|
|
25
|
+
from sqlspec.loader import SQLFileLoader
|
|
26
|
+
from sqlspec.observability import ObservabilityConfig, ObservabilityRuntime, TelemetryDiagnostics
|
|
27
|
+
from sqlspec.typing import ConnectionT
|
|
28
|
+
from sqlspec.utils.logging import get_logger
|
|
29
|
+
|
|
30
|
+
if TYPE_CHECKING:
|
|
31
|
+
from pathlib import Path
|
|
32
|
+
|
|
33
|
+
from sqlspec.core import SQL
|
|
34
|
+
from sqlspec.typing import PoolT
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
__all__ = ("SQLSpec",)
|
|
38
|
+
|
|
39
|
+
logger = get_logger()
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _is_async_context_manager(obj: Any) -> TypeGuard[AbstractAsyncContextManager[Any]]:
|
|
43
|
+
return hasattr(obj, "__aenter__")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _is_sync_context_manager(obj: Any) -> TypeGuard[AbstractContextManager[Any]]:
|
|
47
|
+
return hasattr(obj, "__enter__")
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class SQLSpec:
|
|
51
|
+
"""Configuration manager and registry for database connections and pools."""
|
|
52
|
+
|
|
53
|
+
__slots__ = ("_configs", "_instance_cache_config", "_loader_runtime", "_observability_config", "_sql_loader")
|
|
54
|
+
|
|
55
|
+
def __init__(
|
|
56
|
+
self, *, loader: "SQLFileLoader | None" = None, observability_config: "ObservabilityConfig | None" = None
|
|
57
|
+
) -> None:
|
|
58
|
+
self._configs: dict[Any, DatabaseConfigProtocol[Any, Any, Any]] = {}
|
|
59
|
+
atexit.register(self._cleanup_sync_pools)
|
|
60
|
+
self._instance_cache_config: CacheConfig | None = None
|
|
61
|
+
self._sql_loader: SQLFileLoader | None = loader
|
|
62
|
+
self._observability_config = observability_config
|
|
63
|
+
self._loader_runtime = ObservabilityRuntime(observability_config, config_name="SQLFileLoader")
|
|
64
|
+
if self._sql_loader is not None:
|
|
65
|
+
self._sql_loader.set_observability_runtime(self._loader_runtime)
|
|
66
|
+
|
|
67
|
+
@staticmethod
|
|
68
|
+
def _get_config_name(obj: Any) -> str:
|
|
69
|
+
"""Get display name for configuration object."""
|
|
70
|
+
return getattr(obj, "__name__", str(obj))
|
|
71
|
+
|
|
72
|
+
def _cleanup_sync_pools(self) -> None:
|
|
73
|
+
"""Clean up only synchronous connection pools at exit."""
|
|
74
|
+
cleaned_count = 0
|
|
75
|
+
|
|
76
|
+
for config_type, config in self._configs.items():
|
|
77
|
+
if config.supports_connection_pooling and not config.is_async:
|
|
78
|
+
try:
|
|
79
|
+
config.close_pool()
|
|
80
|
+
cleaned_count += 1
|
|
81
|
+
except Exception as e:
|
|
82
|
+
logger.debug("Failed to clean up sync pool for config %s: %s", config_type.__name__, e)
|
|
83
|
+
|
|
84
|
+
if cleaned_count > 0:
|
|
85
|
+
logger.debug("Sync pool cleanup completed. Cleaned %d pools.", cleaned_count)
|
|
86
|
+
|
|
87
|
+
async def close_all_pools(self) -> None:
|
|
88
|
+
"""Explicitly close all connection pools (async and sync).
|
|
89
|
+
|
|
90
|
+
This method should be called before application shutdown for proper cleanup.
|
|
91
|
+
"""
|
|
92
|
+
cleanup_tasks = []
|
|
93
|
+
sync_configs = []
|
|
94
|
+
|
|
95
|
+
for config_type, config in self._configs.items():
|
|
96
|
+
if config.supports_connection_pooling:
|
|
97
|
+
try:
|
|
98
|
+
if config.is_async:
|
|
99
|
+
close_pool_awaitable = config.close_pool()
|
|
100
|
+
if close_pool_awaitable is not None:
|
|
101
|
+
cleanup_tasks.append(cast("Coroutine[Any, Any, None]", close_pool_awaitable))
|
|
102
|
+
else:
|
|
103
|
+
sync_configs.append((config_type, config))
|
|
104
|
+
except Exception as e:
|
|
105
|
+
logger.debug("Failed to prepare cleanup for config %s: %s", config_type.__name__, e)
|
|
106
|
+
|
|
107
|
+
if cleanup_tasks:
|
|
108
|
+
try:
|
|
109
|
+
await asyncio.gather(*cleanup_tasks, return_exceptions=True)
|
|
110
|
+
logger.debug("Async pool cleanup completed. Cleaned %d pools.", len(cleanup_tasks))
|
|
111
|
+
except Exception as e:
|
|
112
|
+
logger.debug("Failed to complete async pool cleanup: %s", e)
|
|
113
|
+
|
|
114
|
+
for _config_type, config in sync_configs:
|
|
115
|
+
config.close_pool()
|
|
116
|
+
|
|
117
|
+
if sync_configs:
|
|
118
|
+
logger.debug("Sync pool cleanup completed. Cleaned %d pools.", len(sync_configs))
|
|
119
|
+
|
|
120
|
+
async def __aenter__(self) -> "SQLSpec":
|
|
121
|
+
"""Async context manager entry."""
|
|
122
|
+
return self
|
|
123
|
+
|
|
124
|
+
async def __aexit__(self, _exc_type: Any, _exc_val: Any, _exc_tb: Any) -> None:
|
|
125
|
+
"""Async context manager exit with automatic cleanup."""
|
|
126
|
+
await self.close_all_pools()
|
|
127
|
+
|
|
128
|
+
@overload
|
|
129
|
+
def add_config(self, config: "SyncConfigT") -> "type[SyncConfigT]": # pyright: ignore[reportInvalidTypeVarUse]
|
|
130
|
+
...
|
|
131
|
+
|
|
132
|
+
@overload
|
|
133
|
+
def add_config(self, config: "AsyncConfigT") -> "type[AsyncConfigT]": # pyright: ignore[reportInvalidTypeVarUse]
|
|
134
|
+
...
|
|
135
|
+
|
|
136
|
+
def add_config(self, config: "SyncConfigT | AsyncConfigT") -> "type[SyncConfigT | AsyncConfigT]": # pyright: ignore[reportInvalidTypeVarUse]
|
|
137
|
+
"""Add a configuration instance to the registry.
|
|
138
|
+
|
|
139
|
+
Args:
|
|
140
|
+
config: The configuration instance to add.
|
|
141
|
+
|
|
142
|
+
Returns:
|
|
143
|
+
The type of the added configuration for use as a registry key.
|
|
144
|
+
"""
|
|
145
|
+
config_type = type(config)
|
|
146
|
+
if config_type in self._configs:
|
|
147
|
+
logger.debug("Configuration for %s already exists. Overwriting.", config_type.__name__)
|
|
148
|
+
if hasattr(config, "attach_observability"):
|
|
149
|
+
config.attach_observability(self._observability_config)
|
|
150
|
+
self._configs[config_type] = config
|
|
151
|
+
return config_type
|
|
152
|
+
|
|
153
|
+
@overload
|
|
154
|
+
def get_config(self, name: "type[SyncConfigT]") -> "SyncConfigT": ...
|
|
155
|
+
|
|
156
|
+
@overload
|
|
157
|
+
def get_config(self, name: "type[AsyncConfigT]") -> "AsyncConfigT": ...
|
|
158
|
+
|
|
159
|
+
def get_config(
|
|
160
|
+
self, name: "type[DatabaseConfigProtocol[ConnectionT, PoolT, DriverT]] | Any"
|
|
161
|
+
) -> "DatabaseConfigProtocol[ConnectionT, PoolT, DriverT]":
|
|
162
|
+
"""Retrieve a configuration instance by its type or a key.
|
|
163
|
+
|
|
164
|
+
Args:
|
|
165
|
+
name: The type of the configuration or a key associated with it.
|
|
166
|
+
|
|
167
|
+
Returns:
|
|
168
|
+
The configuration instance.
|
|
169
|
+
|
|
170
|
+
Raises:
|
|
171
|
+
KeyError: If the configuration is not found.
|
|
172
|
+
"""
|
|
173
|
+
config = self._configs.get(name)
|
|
174
|
+
if not config:
|
|
175
|
+
logger.error("No configuration found for %s", name)
|
|
176
|
+
msg = f"No configuration found for {name}"
|
|
177
|
+
raise KeyError(msg)
|
|
178
|
+
|
|
179
|
+
logger.debug("Retrieved configuration: %s", self._get_config_name(name))
|
|
180
|
+
return config
|
|
181
|
+
|
|
182
|
+
@property
|
|
183
|
+
def configs(self) -> "dict[type, DatabaseConfigProtocol[Any, Any, Any]]":
|
|
184
|
+
"""Access the registry of database configurations.
|
|
185
|
+
|
|
186
|
+
Returns:
|
|
187
|
+
Dictionary mapping config types to config instances.
|
|
188
|
+
"""
|
|
189
|
+
return self._configs
|
|
190
|
+
|
|
191
|
+
def telemetry_snapshot(self) -> "dict[str, Any]":
|
|
192
|
+
"""Return aggregated diagnostics across all registered configurations."""
|
|
193
|
+
|
|
194
|
+
diagnostics = TelemetryDiagnostics()
|
|
195
|
+
loader_metrics = self._loader_runtime.metrics_snapshot()
|
|
196
|
+
if loader_metrics:
|
|
197
|
+
diagnostics.add_metric_snapshot(loader_metrics)
|
|
198
|
+
for config in self._configs.values():
|
|
199
|
+
runtime = config.get_observability_runtime()
|
|
200
|
+
diagnostics.add_lifecycle_snapshot(runtime.diagnostics_key, runtime.lifecycle_snapshot())
|
|
201
|
+
metrics_snapshot = runtime.metrics_snapshot()
|
|
202
|
+
if metrics_snapshot:
|
|
203
|
+
diagnostics.add_metric_snapshot(metrics_snapshot)
|
|
204
|
+
return diagnostics.snapshot()
|
|
205
|
+
|
|
206
|
+
def _ensure_sql_loader(self) -> SQLFileLoader:
|
|
207
|
+
"""Return a SQLFileLoader instance configured with observability runtime."""
|
|
208
|
+
|
|
209
|
+
if self._sql_loader is None:
|
|
210
|
+
self._sql_loader = SQLFileLoader(runtime=self._loader_runtime)
|
|
211
|
+
else:
|
|
212
|
+
self._sql_loader.set_observability_runtime(self._loader_runtime)
|
|
213
|
+
return self._sql_loader
|
|
214
|
+
|
|
215
|
+
@overload
|
|
216
|
+
def get_connection(
|
|
217
|
+
self,
|
|
218
|
+
name: Union[
|
|
219
|
+
"type[NoPoolSyncConfig[ConnectionT, DriverT]]",
|
|
220
|
+
"type[SyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
|
|
221
|
+
"NoPoolSyncConfig[ConnectionT, DriverT]",
|
|
222
|
+
"SyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
223
|
+
],
|
|
224
|
+
) -> "ConnectionT": ...
|
|
225
|
+
|
|
226
|
+
@overload
|
|
227
|
+
def get_connection(
|
|
228
|
+
self,
|
|
229
|
+
name: Union[
|
|
230
|
+
"type[NoPoolAsyncConfig[ConnectionT, DriverT]]",
|
|
231
|
+
"type[AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
|
|
232
|
+
"NoPoolAsyncConfig[ConnectionT, DriverT]",
|
|
233
|
+
"AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
234
|
+
],
|
|
235
|
+
) -> "Awaitable[ConnectionT]": ...
|
|
236
|
+
|
|
237
|
+
def get_connection(
|
|
238
|
+
self,
|
|
239
|
+
name: Union[
|
|
240
|
+
"type[NoPoolSyncConfig[ConnectionT, DriverT]]",
|
|
241
|
+
"type[NoPoolAsyncConfig[ConnectionT, DriverT]]",
|
|
242
|
+
"type[SyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
|
|
243
|
+
"type[AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
|
|
244
|
+
"NoPoolSyncConfig[ConnectionT, DriverT]",
|
|
245
|
+
"SyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
246
|
+
"NoPoolAsyncConfig[ConnectionT, DriverT]",
|
|
247
|
+
"AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
248
|
+
],
|
|
249
|
+
) -> "ConnectionT | Awaitable[ConnectionT]":
|
|
250
|
+
"""Get a database connection for the specified configuration.
|
|
251
|
+
|
|
252
|
+
Args:
|
|
253
|
+
name: The configuration name or instance.
|
|
254
|
+
|
|
255
|
+
Returns:
|
|
256
|
+
A database connection or an awaitable yielding a connection.
|
|
257
|
+
"""
|
|
258
|
+
if isinstance(name, (NoPoolSyncConfig, NoPoolAsyncConfig, SyncDatabaseConfig, AsyncDatabaseConfig)):
|
|
259
|
+
config = name
|
|
260
|
+
config_name = config.__class__.__name__
|
|
261
|
+
else:
|
|
262
|
+
config = self.get_config(name)
|
|
263
|
+
config_name = self._get_config_name(name)
|
|
264
|
+
|
|
265
|
+
logger.debug("Getting connection for config: %s", config_name, extra={"config_type": config_name})
|
|
266
|
+
return config.create_connection()
|
|
267
|
+
|
|
268
|
+
@overload
|
|
269
|
+
def get_session(
|
|
270
|
+
self,
|
|
271
|
+
name: Union[
|
|
272
|
+
"type[NoPoolSyncConfig[ConnectionT, DriverT]]",
|
|
273
|
+
"type[SyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
|
|
274
|
+
"NoPoolSyncConfig[ConnectionT, DriverT]",
|
|
275
|
+
"SyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
276
|
+
],
|
|
277
|
+
) -> "DriverT": ...
|
|
278
|
+
|
|
279
|
+
@overload
|
|
280
|
+
def get_session(
|
|
281
|
+
self,
|
|
282
|
+
name: Union[
|
|
283
|
+
"type[NoPoolAsyncConfig[ConnectionT, DriverT]]",
|
|
284
|
+
"type[AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
|
|
285
|
+
"NoPoolAsyncConfig[ConnectionT, DriverT]",
|
|
286
|
+
"AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
287
|
+
],
|
|
288
|
+
) -> "Awaitable[DriverT]": ...
|
|
289
|
+
|
|
290
|
+
def get_session(
|
|
291
|
+
self,
|
|
292
|
+
name: Union[
|
|
293
|
+
"type[NoPoolSyncConfig[ConnectionT, DriverT]]",
|
|
294
|
+
"type[NoPoolAsyncConfig[ConnectionT, DriverT]]",
|
|
295
|
+
"type[SyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
|
|
296
|
+
"type[AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
|
|
297
|
+
"NoPoolSyncConfig[ConnectionT, DriverT]",
|
|
298
|
+
"NoPoolAsyncConfig[ConnectionT, DriverT]",
|
|
299
|
+
"SyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
300
|
+
"AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
301
|
+
],
|
|
302
|
+
) -> "DriverT | Awaitable[DriverT]":
|
|
303
|
+
"""Get a database session (driver adapter) for the specified configuration.
|
|
304
|
+
|
|
305
|
+
Args:
|
|
306
|
+
name: The configuration name or instance.
|
|
307
|
+
|
|
308
|
+
Returns:
|
|
309
|
+
A driver adapter instance or an awaitable yielding one.
|
|
310
|
+
"""
|
|
311
|
+
if isinstance(name, (NoPoolSyncConfig, NoPoolAsyncConfig, SyncDatabaseConfig, AsyncDatabaseConfig)):
|
|
312
|
+
config = name
|
|
313
|
+
config_name = config.__class__.__name__
|
|
314
|
+
else:
|
|
315
|
+
config = self.get_config(name)
|
|
316
|
+
config_name = self._get_config_name(name)
|
|
317
|
+
|
|
318
|
+
logger.debug("Getting session for config: %s", config_name, extra={"config_type": config_name})
|
|
319
|
+
|
|
320
|
+
connection_obj = self.get_connection(name)
|
|
321
|
+
|
|
322
|
+
if isinstance(connection_obj, Awaitable):
|
|
323
|
+
|
|
324
|
+
async def _create_driver_async() -> "DriverT":
|
|
325
|
+
resolved_connection = await connection_obj # pyright: ignore
|
|
326
|
+
driver = config.driver_type( # pyright: ignore
|
|
327
|
+
connection=resolved_connection,
|
|
328
|
+
statement_config=config.statement_config,
|
|
329
|
+
driver_features=config.driver_features,
|
|
330
|
+
)
|
|
331
|
+
return config._prepare_driver(driver) # pyright: ignore
|
|
332
|
+
|
|
333
|
+
return _create_driver_async()
|
|
334
|
+
|
|
335
|
+
driver = config.driver_type( # pyright: ignore
|
|
336
|
+
connection=connection_obj, statement_config=config.statement_config, driver_features=config.driver_features
|
|
337
|
+
)
|
|
338
|
+
return config._prepare_driver(driver) # pyright: ignore
|
|
339
|
+
|
|
340
|
+
@overload
|
|
341
|
+
def provide_connection(
|
|
342
|
+
self,
|
|
343
|
+
name: Union[
|
|
344
|
+
"type[NoPoolSyncConfig[ConnectionT, DriverT]]",
|
|
345
|
+
"type[SyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
|
|
346
|
+
"NoPoolSyncConfig[ConnectionT, DriverT]",
|
|
347
|
+
"SyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
348
|
+
],
|
|
349
|
+
*args: Any,
|
|
350
|
+
**kwargs: Any,
|
|
351
|
+
) -> "AbstractContextManager[ConnectionT]": ...
|
|
352
|
+
|
|
353
|
+
@overload
|
|
354
|
+
def provide_connection(
|
|
355
|
+
self,
|
|
356
|
+
name: Union[
|
|
357
|
+
"type[NoPoolAsyncConfig[ConnectionT, DriverT]]",
|
|
358
|
+
"type[AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
|
|
359
|
+
"NoPoolAsyncConfig[ConnectionT, DriverT]",
|
|
360
|
+
"AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
361
|
+
],
|
|
362
|
+
*args: Any,
|
|
363
|
+
**kwargs: Any,
|
|
364
|
+
) -> "AbstractAsyncContextManager[ConnectionT]": ...
|
|
365
|
+
|
|
366
|
+
def provide_connection(
|
|
367
|
+
self,
|
|
368
|
+
name: Union[
|
|
369
|
+
"type[NoPoolSyncConfig[ConnectionT, DriverT]]",
|
|
370
|
+
"type[NoPoolAsyncConfig[ConnectionT, DriverT]]",
|
|
371
|
+
"type[SyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
|
|
372
|
+
"type[AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
|
|
373
|
+
"NoPoolSyncConfig[ConnectionT, DriverT]",
|
|
374
|
+
"NoPoolAsyncConfig[ConnectionT, DriverT]",
|
|
375
|
+
"SyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
376
|
+
"AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
377
|
+
],
|
|
378
|
+
*args: Any,
|
|
379
|
+
**kwargs: Any,
|
|
380
|
+
) -> "AbstractContextManager[ConnectionT] | AbstractAsyncContextManager[ConnectionT]":
|
|
381
|
+
"""Create and provide a database connection from the specified configuration.
|
|
382
|
+
|
|
383
|
+
Args:
|
|
384
|
+
name: The configuration name or instance.
|
|
385
|
+
*args: Positional arguments to pass to the config's provide_connection.
|
|
386
|
+
**kwargs: Keyword arguments to pass to the config's provide_connection.
|
|
387
|
+
|
|
388
|
+
Returns:
|
|
389
|
+
A sync or async context manager yielding a connection.
|
|
390
|
+
"""
|
|
391
|
+
if isinstance(name, (NoPoolSyncConfig, NoPoolAsyncConfig, SyncDatabaseConfig, AsyncDatabaseConfig)):
|
|
392
|
+
config = name
|
|
393
|
+
config_name = config.__class__.__name__
|
|
394
|
+
else:
|
|
395
|
+
config = self.get_config(name)
|
|
396
|
+
config_name = self._get_config_name(name)
|
|
397
|
+
|
|
398
|
+
logger.debug("Providing connection context for config: %s", config_name, extra={"config_type": config_name})
|
|
399
|
+
connection_context = config.provide_connection(*args, **kwargs)
|
|
400
|
+
runtime = config.get_observability_runtime()
|
|
401
|
+
|
|
402
|
+
if _is_async_context_manager(connection_context):
|
|
403
|
+
async_context = cast("AbstractAsyncContextManager[ConnectionT]", connection_context)
|
|
404
|
+
|
|
405
|
+
@asynccontextmanager
|
|
406
|
+
async def _async_wrapper() -> AsyncIterator[ConnectionT]:
|
|
407
|
+
connection: ConnectionT | None = None
|
|
408
|
+
try:
|
|
409
|
+
async with async_context as conn:
|
|
410
|
+
connection = conn
|
|
411
|
+
runtime.emit_connection_create(conn)
|
|
412
|
+
yield conn
|
|
413
|
+
finally:
|
|
414
|
+
if connection is not None:
|
|
415
|
+
runtime.emit_connection_destroy(connection)
|
|
416
|
+
|
|
417
|
+
return _async_wrapper()
|
|
418
|
+
|
|
419
|
+
sync_context = cast("AbstractContextManager[ConnectionT]", connection_context)
|
|
420
|
+
|
|
421
|
+
@contextmanager
|
|
422
|
+
def _sync_wrapper() -> Iterator[ConnectionT]:
|
|
423
|
+
connection: ConnectionT | None = None
|
|
424
|
+
try:
|
|
425
|
+
with sync_context as conn:
|
|
426
|
+
connection = conn
|
|
427
|
+
runtime.emit_connection_create(conn)
|
|
428
|
+
yield conn
|
|
429
|
+
finally:
|
|
430
|
+
if connection is not None:
|
|
431
|
+
runtime.emit_connection_destroy(connection)
|
|
432
|
+
|
|
433
|
+
return _sync_wrapper()
|
|
434
|
+
|
|
435
|
+
@overload
|
|
436
|
+
def provide_session(
|
|
437
|
+
self,
|
|
438
|
+
name: Union[
|
|
439
|
+
"type[NoPoolSyncConfig[ConnectionT, DriverT]]",
|
|
440
|
+
"type[SyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
|
|
441
|
+
"NoPoolSyncConfig[ConnectionT, DriverT]",
|
|
442
|
+
"SyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
443
|
+
],
|
|
444
|
+
*args: Any,
|
|
445
|
+
**kwargs: Any,
|
|
446
|
+
) -> "AbstractContextManager[DriverT]": ...
|
|
447
|
+
|
|
448
|
+
@overload
|
|
449
|
+
def provide_session(
|
|
450
|
+
self,
|
|
451
|
+
name: Union[
|
|
452
|
+
"type[NoPoolAsyncConfig[ConnectionT, DriverT]]",
|
|
453
|
+
"type[AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
|
|
454
|
+
"NoPoolAsyncConfig[ConnectionT, DriverT]",
|
|
455
|
+
"AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
456
|
+
],
|
|
457
|
+
*args: Any,
|
|
458
|
+
**kwargs: Any,
|
|
459
|
+
) -> "AbstractAsyncContextManager[DriverT]": ...
|
|
460
|
+
|
|
461
|
+
def provide_session(
|
|
462
|
+
self,
|
|
463
|
+
name: Union[
|
|
464
|
+
"type[NoPoolSyncConfig[ConnectionT, DriverT]]",
|
|
465
|
+
"type[NoPoolAsyncConfig[ConnectionT, DriverT]]",
|
|
466
|
+
"type[SyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
|
|
467
|
+
"type[AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
|
|
468
|
+
"NoPoolSyncConfig[ConnectionT, DriverT]",
|
|
469
|
+
"NoPoolAsyncConfig[ConnectionT, DriverT]",
|
|
470
|
+
"SyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
471
|
+
"AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
472
|
+
],
|
|
473
|
+
*args: Any,
|
|
474
|
+
**kwargs: Any,
|
|
475
|
+
) -> "AbstractContextManager[DriverT] | AbstractAsyncContextManager[DriverT]":
|
|
476
|
+
"""Create and provide a database session from the specified configuration.
|
|
477
|
+
|
|
478
|
+
Args:
|
|
479
|
+
name: The configuration name or instance.
|
|
480
|
+
*args: Positional arguments to pass to the config's provide_session.
|
|
481
|
+
**kwargs: Keyword arguments to pass to the config's provide_session.
|
|
482
|
+
|
|
483
|
+
Returns:
|
|
484
|
+
A sync or async context manager yielding a driver adapter instance.
|
|
485
|
+
"""
|
|
486
|
+
if isinstance(name, (NoPoolSyncConfig, NoPoolAsyncConfig, SyncDatabaseConfig, AsyncDatabaseConfig)):
|
|
487
|
+
config = name
|
|
488
|
+
config_name = config.__class__.__name__
|
|
489
|
+
else:
|
|
490
|
+
config = self.get_config(name)
|
|
491
|
+
config_name = self._get_config_name(name)
|
|
492
|
+
|
|
493
|
+
logger.debug("Providing session context for config: %s", config_name, extra={"config_type": config_name})
|
|
494
|
+
session_context = config.provide_session(*args, **kwargs)
|
|
495
|
+
runtime = config.get_observability_runtime()
|
|
496
|
+
|
|
497
|
+
if _is_async_context_manager(session_context):
|
|
498
|
+
async_session = cast("AbstractAsyncContextManager[DriverT]", session_context)
|
|
499
|
+
|
|
500
|
+
@asynccontextmanager
|
|
501
|
+
async def _async_session_wrapper() -> AsyncIterator[DriverT]:
|
|
502
|
+
driver: DriverT | None = None
|
|
503
|
+
try:
|
|
504
|
+
async with async_session as session:
|
|
505
|
+
driver = config._prepare_driver(session) # pyright: ignore
|
|
506
|
+
connection = getattr(driver, "connection", None)
|
|
507
|
+
if connection is not None:
|
|
508
|
+
runtime.emit_connection_create(connection)
|
|
509
|
+
runtime.emit_session_start(driver)
|
|
510
|
+
yield driver
|
|
511
|
+
finally:
|
|
512
|
+
if driver is not None:
|
|
513
|
+
runtime.emit_session_end(driver)
|
|
514
|
+
connection = getattr(driver, "connection", None)
|
|
515
|
+
if connection is not None:
|
|
516
|
+
runtime.emit_connection_destroy(connection)
|
|
517
|
+
|
|
518
|
+
return _async_session_wrapper()
|
|
519
|
+
|
|
520
|
+
sync_session = cast("AbstractContextManager[DriverT]", session_context)
|
|
521
|
+
|
|
522
|
+
@contextmanager
|
|
523
|
+
def _sync_session_wrapper() -> Iterator[DriverT]:
|
|
524
|
+
driver: DriverT | None = None
|
|
525
|
+
try:
|
|
526
|
+
with sync_session as session:
|
|
527
|
+
driver = config._prepare_driver(session) # pyright: ignore
|
|
528
|
+
connection = getattr(driver, "connection", None)
|
|
529
|
+
if connection is not None:
|
|
530
|
+
runtime.emit_connection_create(connection)
|
|
531
|
+
runtime.emit_session_start(driver)
|
|
532
|
+
yield driver
|
|
533
|
+
finally:
|
|
534
|
+
if driver is not None:
|
|
535
|
+
runtime.emit_session_end(driver)
|
|
536
|
+
connection = getattr(driver, "connection", None)
|
|
537
|
+
if connection is not None:
|
|
538
|
+
runtime.emit_connection_destroy(connection)
|
|
539
|
+
|
|
540
|
+
return _sync_session_wrapper()
|
|
541
|
+
|
|
542
|
+
@overload
|
|
543
|
+
def get_pool(
|
|
544
|
+
self,
|
|
545
|
+
name: "type[NoPoolSyncConfig[ConnectionT, DriverT] | NoPoolAsyncConfig[ConnectionT, DriverT]] | NoPoolSyncConfig[ConnectionT, DriverT] | NoPoolAsyncConfig[ConnectionT, DriverT]",
|
|
546
|
+
) -> "None": ...
|
|
547
|
+
@overload
|
|
548
|
+
def get_pool(
|
|
549
|
+
self,
|
|
550
|
+
name: "type[SyncDatabaseConfig[ConnectionT, PoolT, DriverT]] | SyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
551
|
+
) -> "type[PoolT]": ...
|
|
552
|
+
@overload
|
|
553
|
+
def get_pool(
|
|
554
|
+
self,
|
|
555
|
+
name: "type[AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]] | AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
556
|
+
) -> "Awaitable[type[PoolT]]": ...
|
|
557
|
+
|
|
558
|
+
def get_pool(
|
|
559
|
+
self,
|
|
560
|
+
name: Union[
|
|
561
|
+
"type[NoPoolSyncConfig[ConnectionT, DriverT]]",
|
|
562
|
+
"type[NoPoolAsyncConfig[ConnectionT, DriverT]]",
|
|
563
|
+
"type[SyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
|
|
564
|
+
"type[AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
|
|
565
|
+
"NoPoolSyncConfig[ConnectionT, DriverT]",
|
|
566
|
+
"NoPoolAsyncConfig[ConnectionT, DriverT]",
|
|
567
|
+
"SyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
568
|
+
"AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
569
|
+
],
|
|
570
|
+
) -> "type[PoolT] | Awaitable[type[PoolT]] | None":
|
|
571
|
+
"""Get the connection pool for the specified configuration.
|
|
572
|
+
|
|
573
|
+
Args:
|
|
574
|
+
name: The configuration name or instance.
|
|
575
|
+
|
|
576
|
+
Returns:
|
|
577
|
+
The connection pool, an awaitable yielding the pool, or None if not supported.
|
|
578
|
+
"""
|
|
579
|
+
config = (
|
|
580
|
+
name
|
|
581
|
+
if isinstance(name, (NoPoolSyncConfig, NoPoolAsyncConfig, SyncDatabaseConfig, AsyncDatabaseConfig))
|
|
582
|
+
else self.get_config(name)
|
|
583
|
+
)
|
|
584
|
+
config_name = config.__class__.__name__
|
|
585
|
+
|
|
586
|
+
if config.supports_connection_pooling:
|
|
587
|
+
logger.debug("Getting pool for config: %s", config_name, extra={"config_type": config_name})
|
|
588
|
+
return cast("type[PoolT] | Awaitable[type[PoolT]]", config.create_pool())
|
|
589
|
+
|
|
590
|
+
logger.debug("Config %s does not support connection pooling", config_name)
|
|
591
|
+
return None
|
|
592
|
+
|
|
593
|
+
@overload
|
|
594
|
+
def close_pool(
|
|
595
|
+
self,
|
|
596
|
+
name: Union[
|
|
597
|
+
"type[NoPoolSyncConfig[ConnectionT, DriverT]]",
|
|
598
|
+
"type[SyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
|
|
599
|
+
"NoPoolSyncConfig[ConnectionT, DriverT]",
|
|
600
|
+
"SyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
601
|
+
],
|
|
602
|
+
) -> "None": ...
|
|
603
|
+
|
|
604
|
+
@overload
|
|
605
|
+
def close_pool(
|
|
606
|
+
self,
|
|
607
|
+
name: Union[
|
|
608
|
+
"type[NoPoolAsyncConfig[ConnectionT, DriverT]]",
|
|
609
|
+
"type[AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
|
|
610
|
+
"NoPoolAsyncConfig[ConnectionT, DriverT]",
|
|
611
|
+
"AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
612
|
+
],
|
|
613
|
+
) -> "Awaitable[None]": ...
|
|
614
|
+
|
|
615
|
+
def close_pool(
|
|
616
|
+
self,
|
|
617
|
+
name: Union[
|
|
618
|
+
"type[NoPoolSyncConfig[ConnectionT, DriverT]]",
|
|
619
|
+
"type[NoPoolAsyncConfig[ConnectionT, DriverT]]",
|
|
620
|
+
"type[SyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
|
|
621
|
+
"type[AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]]",
|
|
622
|
+
"NoPoolSyncConfig[ConnectionT, DriverT]",
|
|
623
|
+
"SyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
624
|
+
"NoPoolAsyncConfig[ConnectionT, DriverT]",
|
|
625
|
+
"AsyncDatabaseConfig[ConnectionT, PoolT, DriverT]",
|
|
626
|
+
],
|
|
627
|
+
) -> "Awaitable[None] | None":
|
|
628
|
+
"""Close the connection pool for the specified configuration.
|
|
629
|
+
|
|
630
|
+
Args:
|
|
631
|
+
name: The configuration name or instance.
|
|
632
|
+
|
|
633
|
+
Returns:
|
|
634
|
+
None, or an awaitable if closing an async pool.
|
|
635
|
+
"""
|
|
636
|
+
if isinstance(name, (NoPoolSyncConfig, NoPoolAsyncConfig, SyncDatabaseConfig, AsyncDatabaseConfig)):
|
|
637
|
+
config = name
|
|
638
|
+
config_name = config.__class__.__name__
|
|
639
|
+
else:
|
|
640
|
+
config = self.get_config(name)
|
|
641
|
+
config_name = self._get_config_name(name)
|
|
642
|
+
|
|
643
|
+
if config.supports_connection_pooling:
|
|
644
|
+
logger.debug("Closing pool for config: %s", config_name, extra={"config_type": config_name})
|
|
645
|
+
return config.close_pool()
|
|
646
|
+
|
|
647
|
+
logger.debug("Config %s does not support connection pooling - nothing to close", config_name)
|
|
648
|
+
return None
|
|
649
|
+
|
|
650
|
+
@staticmethod
|
|
651
|
+
def get_cache_config() -> CacheConfig:
|
|
652
|
+
"""Get the current global cache configuration.
|
|
653
|
+
|
|
654
|
+
Returns:
|
|
655
|
+
The current cache configuration.
|
|
656
|
+
"""
|
|
657
|
+
return get_cache_config()
|
|
658
|
+
|
|
659
|
+
@staticmethod
|
|
660
|
+
def update_cache_config(config: CacheConfig) -> None:
|
|
661
|
+
"""Update the global cache configuration.
|
|
662
|
+
|
|
663
|
+
Args:
|
|
664
|
+
config: The new cache configuration to apply.
|
|
665
|
+
"""
|
|
666
|
+
update_cache_config(config)
|
|
667
|
+
|
|
668
|
+
@staticmethod
|
|
669
|
+
def get_cache_stats() -> "dict[str, Any]":
|
|
670
|
+
"""Get current cache statistics.
|
|
671
|
+
|
|
672
|
+
Returns:
|
|
673
|
+
Cache statistics object with detailed metrics.
|
|
674
|
+
"""
|
|
675
|
+
return get_cache_statistics()
|
|
676
|
+
|
|
677
|
+
@staticmethod
|
|
678
|
+
def reset_cache_stats() -> None:
|
|
679
|
+
"""Reset all cache statistics to zero."""
|
|
680
|
+
reset_cache_stats()
|
|
681
|
+
|
|
682
|
+
@staticmethod
|
|
683
|
+
def log_cache_stats() -> None:
|
|
684
|
+
"""Log current cache statistics using the configured logger."""
|
|
685
|
+
log_cache_stats()
|
|
686
|
+
|
|
687
|
+
@staticmethod
|
|
688
|
+
def configure_cache(
|
|
689
|
+
*,
|
|
690
|
+
sql_cache_size: int | None = None,
|
|
691
|
+
fragment_cache_size: int | None = None,
|
|
692
|
+
optimized_cache_size: int | None = None,
|
|
693
|
+
sql_cache_enabled: bool | None = None,
|
|
694
|
+
fragment_cache_enabled: bool | None = None,
|
|
695
|
+
optimized_cache_enabled: bool | None = None,
|
|
696
|
+
) -> None:
|
|
697
|
+
"""Update cache configuration with partial values.
|
|
698
|
+
|
|
699
|
+
Args:
|
|
700
|
+
sql_cache_size: Size of the SQL statement cache.
|
|
701
|
+
fragment_cache_size: Size of the AST fragment cache.
|
|
702
|
+
optimized_cache_size: Size of the optimized expression cache.
|
|
703
|
+
sql_cache_enabled: Enable/disable SQL cache.
|
|
704
|
+
fragment_cache_enabled: Enable/disable fragment cache.
|
|
705
|
+
optimized_cache_enabled: Enable/disable optimized cache.
|
|
706
|
+
"""
|
|
707
|
+
current_config = get_cache_config()
|
|
708
|
+
update_cache_config(
|
|
709
|
+
CacheConfig(
|
|
710
|
+
sql_cache_size=sql_cache_size if sql_cache_size is not None else current_config.sql_cache_size,
|
|
711
|
+
fragment_cache_size=fragment_cache_size
|
|
712
|
+
if fragment_cache_size is not None
|
|
713
|
+
else current_config.fragment_cache_size,
|
|
714
|
+
optimized_cache_size=optimized_cache_size
|
|
715
|
+
if optimized_cache_size is not None
|
|
716
|
+
else current_config.optimized_cache_size,
|
|
717
|
+
sql_cache_enabled=sql_cache_enabled
|
|
718
|
+
if sql_cache_enabled is not None
|
|
719
|
+
else current_config.sql_cache_enabled,
|
|
720
|
+
fragment_cache_enabled=fragment_cache_enabled
|
|
721
|
+
if fragment_cache_enabled is not None
|
|
722
|
+
else current_config.fragment_cache_enabled,
|
|
723
|
+
optimized_cache_enabled=optimized_cache_enabled
|
|
724
|
+
if optimized_cache_enabled is not None
|
|
725
|
+
else current_config.optimized_cache_enabled,
|
|
726
|
+
)
|
|
727
|
+
)
|
|
728
|
+
|
|
729
|
+
def load_sql_files(self, *paths: "str | Path") -> None:
|
|
730
|
+
"""Load SQL files from paths or directories.
|
|
731
|
+
|
|
732
|
+
Args:
|
|
733
|
+
*paths: One or more file paths or directory paths to load.
|
|
734
|
+
"""
|
|
735
|
+
loader = self._ensure_sql_loader()
|
|
736
|
+
loader.load_sql(*paths)
|
|
737
|
+
logger.debug("Loaded SQL files: %s", paths)
|
|
738
|
+
|
|
739
|
+
def add_named_sql(self, name: str, sql: str, dialect: "str | None" = None) -> None:
|
|
740
|
+
"""Add a named SQL query directly.
|
|
741
|
+
|
|
742
|
+
Args:
|
|
743
|
+
name: Name for the SQL query.
|
|
744
|
+
sql: Raw SQL content.
|
|
745
|
+
dialect: Optional dialect for the SQL statement.
|
|
746
|
+
"""
|
|
747
|
+
loader = self._ensure_sql_loader()
|
|
748
|
+
loader.add_named_sql(name, sql, dialect)
|
|
749
|
+
logger.debug("Added named SQL: %s", name)
|
|
750
|
+
|
|
751
|
+
def get_sql(self, name: str) -> "SQL":
|
|
752
|
+
"""Get a SQL object by name.
|
|
753
|
+
|
|
754
|
+
Args:
|
|
755
|
+
name: Name of the statement from SQL file comments.
|
|
756
|
+
Hyphens in names are converted to underscores.
|
|
757
|
+
|
|
758
|
+
Returns:
|
|
759
|
+
SQL object ready for execution.
|
|
760
|
+
"""
|
|
761
|
+
loader = self._ensure_sql_loader()
|
|
762
|
+
return loader.get_sql(name)
|
|
763
|
+
|
|
764
|
+
def list_sql_queries(self) -> "list[str]":
|
|
765
|
+
"""List all available query names.
|
|
766
|
+
|
|
767
|
+
Returns:
|
|
768
|
+
Sorted list of query names.
|
|
769
|
+
"""
|
|
770
|
+
if self._sql_loader is None:
|
|
771
|
+
return []
|
|
772
|
+
return self._sql_loader.list_queries()
|
|
773
|
+
|
|
774
|
+
def has_sql_query(self, name: str) -> bool:
|
|
775
|
+
"""Check if a SQL query exists.
|
|
776
|
+
|
|
777
|
+
Args:
|
|
778
|
+
name: Query name to check.
|
|
779
|
+
|
|
780
|
+
Returns:
|
|
781
|
+
True if the query exists in the loader.
|
|
782
|
+
"""
|
|
783
|
+
if self._sql_loader is None:
|
|
784
|
+
return False
|
|
785
|
+
return self._sql_loader.has_query(name)
|
|
786
|
+
|
|
787
|
+
def clear_sql_cache(self) -> None:
|
|
788
|
+
"""Clear the SQL file cache."""
|
|
789
|
+
if self._sql_loader is not None:
|
|
790
|
+
self._sql_loader.clear_cache()
|
|
791
|
+
logger.debug("Cleared SQL cache")
|
|
792
|
+
|
|
793
|
+
def reload_sql_files(self) -> None:
|
|
794
|
+
"""Reload all SQL files.
|
|
795
|
+
|
|
796
|
+
Note:
|
|
797
|
+
This clears the cache and requires calling load_sql_files again.
|
|
798
|
+
"""
|
|
799
|
+
if self._sql_loader is not None:
|
|
800
|
+
self._sql_loader.clear_cache()
|
|
801
|
+
logger.debug("Cleared SQL cache for reload")
|
|
802
|
+
|
|
803
|
+
def get_sql_files(self) -> "list[str]":
|
|
804
|
+
"""Get list of loaded SQL files.
|
|
805
|
+
|
|
806
|
+
Returns:
|
|
807
|
+
Sorted list of file paths.
|
|
808
|
+
"""
|
|
809
|
+
if self._sql_loader is None:
|
|
810
|
+
return []
|
|
811
|
+
return self._sql_loader.list_files()
|