sqlspec 0.9.1__py3-none-any.whl → 0.10.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.
Potentially problematic release.
This version of sqlspec might be problematic. Click here for more details.
- sqlspec/__init__.py +2 -1
- sqlspec/adapters/adbc/__init__.py +2 -1
- sqlspec/adapters/adbc/config.py +7 -13
- sqlspec/adapters/adbc/driver.py +37 -30
- sqlspec/adapters/aiosqlite/__init__.py +2 -1
- sqlspec/adapters/aiosqlite/config.py +10 -12
- sqlspec/adapters/aiosqlite/driver.py +36 -31
- sqlspec/adapters/asyncmy/__init__.py +2 -1
- sqlspec/adapters/asyncmy/driver.py +34 -31
- sqlspec/adapters/asyncpg/config.py +1 -3
- sqlspec/adapters/asyncpg/driver.py +7 -3
- sqlspec/adapters/bigquery/__init__.py +4 -0
- sqlspec/adapters/bigquery/config/__init__.py +3 -0
- sqlspec/adapters/bigquery/config/_common.py +40 -0
- sqlspec/adapters/bigquery/config/_sync.py +87 -0
- sqlspec/adapters/bigquery/driver.py +701 -0
- sqlspec/adapters/duckdb/__init__.py +2 -1
- sqlspec/adapters/duckdb/config.py +17 -18
- sqlspec/adapters/duckdb/driver.py +38 -30
- sqlspec/adapters/oracledb/__init__.py +8 -1
- sqlspec/adapters/oracledb/config/_asyncio.py +7 -8
- sqlspec/adapters/oracledb/config/_sync.py +6 -7
- sqlspec/adapters/oracledb/driver.py +65 -62
- sqlspec/adapters/psqlpy/__init__.py +9 -0
- sqlspec/adapters/psqlpy/config.py +5 -5
- sqlspec/adapters/psqlpy/driver.py +34 -28
- sqlspec/adapters/psycopg/__init__.py +8 -1
- sqlspec/adapters/psycopg/config/__init__.py +10 -0
- sqlspec/adapters/psycopg/config/_async.py +6 -7
- sqlspec/adapters/psycopg/config/_sync.py +7 -8
- sqlspec/adapters/psycopg/driver.py +63 -53
- sqlspec/adapters/sqlite/__init__.py +2 -1
- sqlspec/adapters/sqlite/config.py +12 -11
- sqlspec/adapters/sqlite/driver.py +36 -29
- sqlspec/base.py +1 -66
- sqlspec/exceptions.py +9 -0
- sqlspec/extensions/litestar/config.py +3 -11
- sqlspec/extensions/litestar/handlers.py +2 -1
- sqlspec/extensions/litestar/plugin.py +4 -2
- sqlspec/mixins.py +156 -0
- sqlspec/typing.py +19 -1
- {sqlspec-0.9.1.dist-info → sqlspec-0.10.0.dist-info}/METADATA +8 -3
- sqlspec-0.10.0.dist-info/RECORD +67 -0
- sqlspec-0.9.1.dist-info/RECORD +0 -61
- {sqlspec-0.9.1.dist-info → sqlspec-0.10.0.dist-info}/WHEEL +0 -0
- {sqlspec-0.9.1.dist-info → sqlspec-0.10.0.dist-info}/licenses/LICENSE +0 -0
- {sqlspec-0.9.1.dist-info → sqlspec-0.10.0.dist-info}/licenses/NOTICE +0 -0
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
from sqlspec.adapters.asyncmy.config import AsyncmyConfig, AsyncmyPoolConfig
|
|
2
|
-
from sqlspec.adapters.asyncmy.driver import AsyncmyDriver # type: ignore[attr-defined]
|
|
2
|
+
from sqlspec.adapters.asyncmy.driver import AsyncmyConnection, AsyncmyDriver # type: ignore[attr-defined]
|
|
3
3
|
|
|
4
4
|
__all__ = (
|
|
5
5
|
"AsyncmyConfig",
|
|
6
|
+
"AsyncmyConnection",
|
|
6
7
|
"AsyncmyDriver",
|
|
7
8
|
"AsyncmyPoolConfig",
|
|
8
9
|
)
|
|
@@ -3,33 +3,36 @@ from collections.abc import AsyncGenerator, Sequence
|
|
|
3
3
|
from contextlib import asynccontextmanager
|
|
4
4
|
from typing import TYPE_CHECKING, Any, Optional, Union, cast, overload
|
|
5
5
|
|
|
6
|
-
from
|
|
6
|
+
from asyncmy import Connection
|
|
7
|
+
|
|
8
|
+
from sqlspec.base import AsyncDriverAdapterProtocol
|
|
9
|
+
from sqlspec.mixins import SQLTranslatorMixin
|
|
7
10
|
|
|
8
11
|
if TYPE_CHECKING:
|
|
9
|
-
from asyncmy import Connection
|
|
10
12
|
from asyncmy.cursors import Cursor
|
|
11
13
|
|
|
12
|
-
from sqlspec.typing import ModelDTOT, StatementParameterType
|
|
14
|
+
from sqlspec.typing import ModelDTOT, StatementParameterType, T
|
|
13
15
|
|
|
14
16
|
__all__ = ("AsyncmyDriver",)
|
|
15
17
|
|
|
18
|
+
AsyncmyConnection = Connection
|
|
19
|
+
|
|
16
20
|
|
|
17
|
-
class AsyncmyDriver(
|
|
21
|
+
class AsyncmyDriver(
|
|
22
|
+
SQLTranslatorMixin["AsyncmyConnection"],
|
|
23
|
+
AsyncDriverAdapterProtocol["AsyncmyConnection"],
|
|
24
|
+
):
|
|
18
25
|
"""Asyncmy MySQL/MariaDB Driver Adapter."""
|
|
19
26
|
|
|
20
|
-
connection: "
|
|
27
|
+
connection: "AsyncmyConnection"
|
|
21
28
|
dialect: str = "mysql"
|
|
22
29
|
|
|
23
|
-
def __init__(self, connection: "
|
|
30
|
+
def __init__(self, connection: "AsyncmyConnection") -> None:
|
|
24
31
|
self.connection = connection
|
|
25
32
|
|
|
26
|
-
@staticmethod
|
|
27
|
-
async def _cursor(connection: "Connection") -> "Cursor":
|
|
28
|
-
return await connection.cursor()
|
|
29
|
-
|
|
30
33
|
@staticmethod
|
|
31
34
|
@asynccontextmanager
|
|
32
|
-
async def _with_cursor(connection: "
|
|
35
|
+
async def _with_cursor(connection: "AsyncmyConnection") -> AsyncGenerator["Cursor", None]:
|
|
33
36
|
cursor = connection.cursor()
|
|
34
37
|
try:
|
|
35
38
|
yield cursor
|
|
@@ -44,7 +47,7 @@ class AsyncmyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
44
47
|
parameters: "Optional[StatementParameterType]" = None,
|
|
45
48
|
/,
|
|
46
49
|
*,
|
|
47
|
-
connection: "Optional[
|
|
50
|
+
connection: "Optional[AsyncmyConnection]" = None,
|
|
48
51
|
schema_type: None = None,
|
|
49
52
|
**kwargs: Any,
|
|
50
53
|
) -> "Sequence[dict[str, Any]]": ...
|
|
@@ -55,7 +58,7 @@ class AsyncmyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
55
58
|
parameters: "Optional[StatementParameterType]" = None,
|
|
56
59
|
/,
|
|
57
60
|
*,
|
|
58
|
-
connection: "Optional[
|
|
61
|
+
connection: "Optional[AsyncmyConnection]" = None,
|
|
59
62
|
schema_type: "type[ModelDTOT]",
|
|
60
63
|
**kwargs: Any,
|
|
61
64
|
) -> "Sequence[ModelDTOT]": ...
|
|
@@ -65,7 +68,7 @@ class AsyncmyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
65
68
|
parameters: Optional["StatementParameterType"] = None,
|
|
66
69
|
/,
|
|
67
70
|
*,
|
|
68
|
-
connection: Optional["
|
|
71
|
+
connection: Optional["AsyncmyConnection"] = None,
|
|
69
72
|
schema_type: "Optional[type[ModelDTOT]]" = None,
|
|
70
73
|
**kwargs: Any,
|
|
71
74
|
) -> "Sequence[Union[ModelDTOT, dict[str, Any]]]":
|
|
@@ -93,7 +96,7 @@ class AsyncmyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
93
96
|
parameters: "Optional[StatementParameterType]" = None,
|
|
94
97
|
/,
|
|
95
98
|
*,
|
|
96
|
-
connection: "Optional[
|
|
99
|
+
connection: "Optional[AsyncmyConnection]" = None,
|
|
97
100
|
schema_type: None = None,
|
|
98
101
|
**kwargs: Any,
|
|
99
102
|
) -> "dict[str, Any]": ...
|
|
@@ -104,7 +107,7 @@ class AsyncmyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
104
107
|
parameters: "Optional[StatementParameterType]" = None,
|
|
105
108
|
/,
|
|
106
109
|
*,
|
|
107
|
-
connection: "Optional[
|
|
110
|
+
connection: "Optional[AsyncmyConnection]" = None,
|
|
108
111
|
schema_type: "type[ModelDTOT]",
|
|
109
112
|
**kwargs: Any,
|
|
110
113
|
) -> "ModelDTOT": ...
|
|
@@ -114,7 +117,7 @@ class AsyncmyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
114
117
|
parameters: Optional["StatementParameterType"] = None,
|
|
115
118
|
/,
|
|
116
119
|
*,
|
|
117
|
-
connection: Optional["
|
|
120
|
+
connection: Optional["AsyncmyConnection"] = None,
|
|
118
121
|
schema_type: "Optional[type[ModelDTOT]]" = None,
|
|
119
122
|
**kwargs: Any,
|
|
120
123
|
) -> "Union[ModelDTOT, dict[str, Any]]":
|
|
@@ -141,7 +144,7 @@ class AsyncmyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
141
144
|
parameters: "Optional[StatementParameterType]" = None,
|
|
142
145
|
/,
|
|
143
146
|
*,
|
|
144
|
-
connection: "Optional[
|
|
147
|
+
connection: "Optional[AsyncmyConnection]" = None,
|
|
145
148
|
schema_type: None = None,
|
|
146
149
|
**kwargs: Any,
|
|
147
150
|
) -> "Optional[dict[str, Any]]": ...
|
|
@@ -152,7 +155,7 @@ class AsyncmyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
152
155
|
parameters: "Optional[StatementParameterType]" = None,
|
|
153
156
|
/,
|
|
154
157
|
*,
|
|
155
|
-
connection: "Optional[
|
|
158
|
+
connection: "Optional[AsyncmyConnection]" = None,
|
|
156
159
|
schema_type: "type[ModelDTOT]",
|
|
157
160
|
**kwargs: Any,
|
|
158
161
|
) -> "Optional[ModelDTOT]": ...
|
|
@@ -162,7 +165,7 @@ class AsyncmyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
162
165
|
parameters: Optional["StatementParameterType"] = None,
|
|
163
166
|
/,
|
|
164
167
|
*,
|
|
165
|
-
connection: Optional["
|
|
168
|
+
connection: Optional["AsyncmyConnection"] = None,
|
|
166
169
|
schema_type: "Optional[type[ModelDTOT]]" = None,
|
|
167
170
|
**kwargs: Any,
|
|
168
171
|
) -> "Optional[Union[ModelDTOT, dict[str, Any]]]":
|
|
@@ -190,7 +193,7 @@ class AsyncmyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
190
193
|
parameters: "Optional[StatementParameterType]" = None,
|
|
191
194
|
/,
|
|
192
195
|
*,
|
|
193
|
-
connection: "Optional[
|
|
196
|
+
connection: "Optional[AsyncmyConnection]" = None,
|
|
194
197
|
schema_type: None = None,
|
|
195
198
|
**kwargs: Any,
|
|
196
199
|
) -> "Any": ...
|
|
@@ -201,7 +204,7 @@ class AsyncmyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
201
204
|
parameters: "Optional[StatementParameterType]" = None,
|
|
202
205
|
/,
|
|
203
206
|
*,
|
|
204
|
-
connection: "Optional[
|
|
207
|
+
connection: "Optional[AsyncmyConnection]" = None,
|
|
205
208
|
schema_type: "type[T]",
|
|
206
209
|
**kwargs: Any,
|
|
207
210
|
) -> "T": ...
|
|
@@ -211,7 +214,7 @@ class AsyncmyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
211
214
|
parameters: "Optional[StatementParameterType]" = None,
|
|
212
215
|
/,
|
|
213
216
|
*,
|
|
214
|
-
connection: "Optional[
|
|
217
|
+
connection: "Optional[AsyncmyConnection]" = None,
|
|
215
218
|
schema_type: "Optional[type[T]]" = None,
|
|
216
219
|
**kwargs: Any,
|
|
217
220
|
) -> "Union[T, Any]":
|
|
@@ -240,7 +243,7 @@ class AsyncmyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
240
243
|
parameters: "Optional[StatementParameterType]" = None,
|
|
241
244
|
/,
|
|
242
245
|
*,
|
|
243
|
-
connection: "Optional[
|
|
246
|
+
connection: "Optional[AsyncmyConnection]" = None,
|
|
244
247
|
schema_type: None = None,
|
|
245
248
|
**kwargs: Any,
|
|
246
249
|
) -> "Optional[Any]": ...
|
|
@@ -251,7 +254,7 @@ class AsyncmyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
251
254
|
parameters: "Optional[StatementParameterType]" = None,
|
|
252
255
|
/,
|
|
253
256
|
*,
|
|
254
|
-
connection: "Optional[
|
|
257
|
+
connection: "Optional[AsyncmyConnection]" = None,
|
|
255
258
|
schema_type: "type[T]",
|
|
256
259
|
**kwargs: Any,
|
|
257
260
|
) -> "Optional[T]": ...
|
|
@@ -261,7 +264,7 @@ class AsyncmyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
261
264
|
parameters: "Optional[StatementParameterType]" = None,
|
|
262
265
|
/,
|
|
263
266
|
*,
|
|
264
|
-
connection: "Optional[
|
|
267
|
+
connection: "Optional[AsyncmyConnection]" = None,
|
|
265
268
|
schema_type: "Optional[type[T]]" = None,
|
|
266
269
|
**kwargs: Any,
|
|
267
270
|
) -> "Optional[Union[T, Any]]":
|
|
@@ -291,7 +294,7 @@ class AsyncmyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
291
294
|
parameters: Optional["StatementParameterType"] = None,
|
|
292
295
|
/,
|
|
293
296
|
*,
|
|
294
|
-
connection: Optional["
|
|
297
|
+
connection: Optional["AsyncmyConnection"] = None,
|
|
295
298
|
**kwargs: Any,
|
|
296
299
|
) -> int:
|
|
297
300
|
"""Insert, update, or delete data from the database.
|
|
@@ -313,7 +316,7 @@ class AsyncmyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
313
316
|
parameters: "Optional[StatementParameterType]" = None,
|
|
314
317
|
/,
|
|
315
318
|
*,
|
|
316
|
-
connection: "Optional[
|
|
319
|
+
connection: "Optional[AsyncmyConnection]" = None,
|
|
317
320
|
schema_type: None = None,
|
|
318
321
|
**kwargs: Any,
|
|
319
322
|
) -> "dict[str, Any]": ...
|
|
@@ -324,7 +327,7 @@ class AsyncmyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
324
327
|
parameters: "Optional[StatementParameterType]" = None,
|
|
325
328
|
/,
|
|
326
329
|
*,
|
|
327
|
-
connection: "Optional[
|
|
330
|
+
connection: "Optional[AsyncmyConnection]" = None,
|
|
328
331
|
schema_type: "type[ModelDTOT]",
|
|
329
332
|
**kwargs: Any,
|
|
330
333
|
) -> "ModelDTOT": ...
|
|
@@ -334,7 +337,7 @@ class AsyncmyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
334
337
|
parameters: Optional["StatementParameterType"] = None,
|
|
335
338
|
/,
|
|
336
339
|
*,
|
|
337
|
-
connection: Optional["
|
|
340
|
+
connection: Optional["AsyncmyConnection"] = None,
|
|
338
341
|
schema_type: "Optional[type[ModelDTOT]]" = None,
|
|
339
342
|
**kwargs: Any,
|
|
340
343
|
) -> "Optional[Union[dict[str, Any], ModelDTOT]]":
|
|
@@ -363,7 +366,7 @@ class AsyncmyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
363
366
|
parameters: Optional["StatementParameterType"] = None,
|
|
364
367
|
/,
|
|
365
368
|
*,
|
|
366
|
-
connection: Optional["
|
|
369
|
+
connection: Optional["AsyncmyConnection"] = None,
|
|
367
370
|
**kwargs: Any,
|
|
368
371
|
) -> str:
|
|
369
372
|
"""Execute a script.
|
|
@@ -190,9 +190,7 @@ class AsyncpgConfig(AsyncDatabaseConfig["AsyncpgConnection", "Pool", "AsyncpgDri
|
|
|
190
190
|
raise ImproperConfigurationError(msg) from e
|
|
191
191
|
|
|
192
192
|
@asynccontextmanager
|
|
193
|
-
async def provide_connection(
|
|
194
|
-
self, *args: "Any", **kwargs: "Any"
|
|
195
|
-
) -> "AsyncGenerator[PoolConnectionProxy[Any], None]": # pyright: ignore[reportMissingTypeArgument,reportUnknownParameterType]
|
|
193
|
+
async def provide_connection(self, *args: "Any", **kwargs: "Any") -> "AsyncGenerator[AsyncpgConnection, None]": # pyright: ignore[reportMissingTypeArgument,reportUnknownParameterType]
|
|
196
194
|
"""Create a connection instance.
|
|
197
195
|
|
|
198
196
|
Yields:
|
|
@@ -5,8 +5,9 @@ from typing import TYPE_CHECKING, Any, Optional, Union, cast, overload
|
|
|
5
5
|
from asyncpg import Connection
|
|
6
6
|
from typing_extensions import TypeAlias
|
|
7
7
|
|
|
8
|
-
from sqlspec.base import AsyncDriverAdapterProtocol
|
|
8
|
+
from sqlspec.base import AsyncDriverAdapterProtocol
|
|
9
9
|
from sqlspec.exceptions import SQLParsingError
|
|
10
|
+
from sqlspec.mixins import SQLTranslatorMixin
|
|
10
11
|
from sqlspec.statement import PARAM_REGEX, SQLStatement
|
|
11
12
|
|
|
12
13
|
if TYPE_CHECKING:
|
|
@@ -15,7 +16,7 @@ if TYPE_CHECKING:
|
|
|
15
16
|
from asyncpg.connection import Connection
|
|
16
17
|
from asyncpg.pool import PoolConnectionProxy
|
|
17
18
|
|
|
18
|
-
from sqlspec.typing import ModelDTOT, StatementParameterType
|
|
19
|
+
from sqlspec.typing import ModelDTOT, StatementParameterType, T
|
|
19
20
|
|
|
20
21
|
__all__ = ("AsyncpgConnection", "AsyncpgDriver")
|
|
21
22
|
|
|
@@ -35,7 +36,10 @@ QMARK_REGEX = re.compile(
|
|
|
35
36
|
AsyncpgConnection: TypeAlias = "Union[Connection[Any], PoolConnectionProxy[Any]]" # pyright: ignore[reportMissingTypeArgument]
|
|
36
37
|
|
|
37
38
|
|
|
38
|
-
class AsyncpgDriver(
|
|
39
|
+
class AsyncpgDriver(
|
|
40
|
+
SQLTranslatorMixin["AsyncpgConnection"],
|
|
41
|
+
AsyncDriverAdapterProtocol["AsyncpgConnection"],
|
|
42
|
+
):
|
|
39
43
|
"""AsyncPG Postgres Driver Adapter."""
|
|
40
44
|
|
|
41
45
|
connection: "AsyncpgConnection"
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
from typing import TYPE_CHECKING, Optional
|
|
3
|
+
|
|
4
|
+
from google.cloud.bigquery import LoadJobConfig, QueryJobConfig
|
|
5
|
+
|
|
6
|
+
if TYPE_CHECKING:
|
|
7
|
+
from google.api_core.client_info import ClientInfo
|
|
8
|
+
from google.api_core.client_options import ClientOptions
|
|
9
|
+
from google.auth.credentials import Credentials
|
|
10
|
+
|
|
11
|
+
__all__ = ("BigQueryConnectionConfigCommon",)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass
|
|
15
|
+
class BigQueryConnectionConfigCommon:
|
|
16
|
+
"""Common configuration options for BigQuery."""
|
|
17
|
+
|
|
18
|
+
project: "Optional[str]" = field(default=None)
|
|
19
|
+
"""Google Cloud project ID."""
|
|
20
|
+
location: "Optional[str]" = field(default=None)
|
|
21
|
+
"""Default geographic location for jobs and datasets."""
|
|
22
|
+
credentials: "Optional[Credentials]" = field(default=None, hash=False)
|
|
23
|
+
"""Credentials to use for authentication."""
|
|
24
|
+
dataset_id: "Optional[str]" = field(default=None)
|
|
25
|
+
"""Default dataset ID to use if not specified in queries."""
|
|
26
|
+
credentials_path: "Optional[str]" = field(default=None)
|
|
27
|
+
"""Path to Google Cloud service account key file (JSON). If None, attempts default authentication."""
|
|
28
|
+
client_options: "Optional[ClientOptions]" = field(default=None, hash=False)
|
|
29
|
+
"""Client options used to set user options on the client (e.g., api_endpoint)."""
|
|
30
|
+
default_query_job_config: "Optional[QueryJobConfig]" = field(default=None, hash=False)
|
|
31
|
+
"""Default QueryJobConfig settings."""
|
|
32
|
+
default_load_job_config: "Optional[LoadJobConfig]" = field(default=None, hash=False)
|
|
33
|
+
"""Default LoadJobConfig settings."""
|
|
34
|
+
client_info: "Optional[ClientInfo]" = field(default=None, hash=False)
|
|
35
|
+
"""Client info used to send a user-agent string along with API requests."""
|
|
36
|
+
|
|
37
|
+
def __post_init__(self) -> None:
|
|
38
|
+
"""Post-initialization hook."""
|
|
39
|
+
if self.default_query_job_config is None:
|
|
40
|
+
self.default_query_job_config = QueryJobConfig(default_dataset=self.dataset_id)
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import contextlib
|
|
2
|
+
from dataclasses import dataclass, field
|
|
3
|
+
from typing import TYPE_CHECKING, Any, Optional
|
|
4
|
+
|
|
5
|
+
from sqlspec.adapters.bigquery.config._common import BigQueryConnectionConfigCommon
|
|
6
|
+
from sqlspec.adapters.bigquery.driver import BigQueryConnection, BigQueryDriver
|
|
7
|
+
from sqlspec.base import NoPoolSyncConfig
|
|
8
|
+
from sqlspec.typing import dataclass_to_dict
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from collections.abc import Iterator
|
|
12
|
+
|
|
13
|
+
__all__ = ("BigQueryConfig", "BigQueryConnectionConfig")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class BigQueryConnectionConfig(BigQueryConnectionConfigCommon):
|
|
17
|
+
"""BigQuery Connection Configuration."""
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@dataclass
|
|
21
|
+
class BigQueryConfig(NoPoolSyncConfig["BigQueryConnection", "BigQueryDriver"]):
|
|
22
|
+
"""BigQuery Synchronous Driver Configuration."""
|
|
23
|
+
|
|
24
|
+
connection_config: "BigQueryConnectionConfig" = field(default_factory=BigQueryConnectionConfig)
|
|
25
|
+
"""BigQuery Connection Configuration."""
|
|
26
|
+
driver_type: "type[BigQueryDriver]" = field(init=False, repr=False, default=BigQueryDriver)
|
|
27
|
+
"""BigQuery Driver Type."""
|
|
28
|
+
connection_type: "type[BigQueryConnection]" = field(init=False, repr=False, default=BigQueryConnection)
|
|
29
|
+
"""BigQuery Connection Type."""
|
|
30
|
+
pool_instance: "None" = field(init=False, repr=False, default=None, hash=False)
|
|
31
|
+
"""This is set to have a init=False since BigQuery does not support pooling."""
|
|
32
|
+
connection_instance: "Optional[BigQueryConnection]" = field(init=False, repr=False, default=None, hash=False)
|
|
33
|
+
"""BigQuery Connection Instance."""
|
|
34
|
+
|
|
35
|
+
@property
|
|
36
|
+
def connection_config_dict(self) -> "dict[str, Any]":
|
|
37
|
+
"""Return the connection configuration as a dict.
|
|
38
|
+
|
|
39
|
+
Returns:
|
|
40
|
+
A string keyed dict of config kwargs for the BigQueryConnection constructor.
|
|
41
|
+
"""
|
|
42
|
+
return dataclass_to_dict(
|
|
43
|
+
self.connection_config,
|
|
44
|
+
exclude_empty=True,
|
|
45
|
+
exclude_none=True,
|
|
46
|
+
exclude={"dataset_id", "credentials_path"},
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
def create_connection(self) -> "BigQueryConnection":
|
|
50
|
+
"""Create a BigQuery Client instance.
|
|
51
|
+
|
|
52
|
+
Returns:
|
|
53
|
+
A BigQuery Client instance.
|
|
54
|
+
"""
|
|
55
|
+
if self.connection_instance is not None:
|
|
56
|
+
return self.connection_instance
|
|
57
|
+
|
|
58
|
+
self.connection_instance = self.connection_type(**self.connection_config_dict)
|
|
59
|
+
return self.connection_instance
|
|
60
|
+
|
|
61
|
+
@contextlib.contextmanager
|
|
62
|
+
def provide_connection(self, *args: Any, **kwargs: Any) -> "Iterator[BigQueryConnection]":
|
|
63
|
+
"""Provide a BigQuery client within a context manager.
|
|
64
|
+
|
|
65
|
+
Args:
|
|
66
|
+
*args: Additional arguments to pass to the connection.
|
|
67
|
+
**kwargs: Additional keyword arguments to pass to the connection.
|
|
68
|
+
|
|
69
|
+
Yields:
|
|
70
|
+
An iterator of BigQuery Client instances.
|
|
71
|
+
"""
|
|
72
|
+
conn = self.create_connection()
|
|
73
|
+
yield conn
|
|
74
|
+
|
|
75
|
+
@contextlib.contextmanager
|
|
76
|
+
def provide_session(self, *args: Any, **kwargs: Any) -> "Iterator[BigQueryDriver]":
|
|
77
|
+
"""Provide a BigQuery driver session within a context manager.
|
|
78
|
+
|
|
79
|
+
Args:
|
|
80
|
+
*args: Additional arguments to pass to the driver.
|
|
81
|
+
**kwargs: Additional keyword arguments to pass to the driver.
|
|
82
|
+
|
|
83
|
+
Yields:
|
|
84
|
+
An iterator of BigQueryDriver instances.
|
|
85
|
+
"""
|
|
86
|
+
conn = self.create_connection()
|
|
87
|
+
yield self.driver_type(connection=conn)
|