sqlspec 0.9.0__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 +160 -21
- sqlspec/adapters/aiosqlite/__init__.py +2 -1
- sqlspec/adapters/aiosqlite/config.py +10 -12
- sqlspec/adapters/aiosqlite/driver.py +160 -22
- sqlspec/adapters/asyncmy/__init__.py +2 -1
- sqlspec/adapters/asyncmy/driver.py +158 -22
- sqlspec/adapters/asyncpg/config.py +1 -3
- sqlspec/adapters/asyncpg/driver.py +143 -5
- 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 +165 -27
- 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 +311 -42
- sqlspec/adapters/psqlpy/__init__.py +9 -0
- sqlspec/adapters/psqlpy/config.py +11 -19
- sqlspec/adapters/psqlpy/driver.py +171 -19
- 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 +344 -86
- sqlspec/adapters/sqlite/__init__.py +2 -1
- sqlspec/adapters/sqlite/config.py +12 -11
- sqlspec/adapters/sqlite/driver.py +160 -51
- sqlspec/base.py +402 -63
- sqlspec/exceptions.py +9 -0
- sqlspec/extensions/litestar/config.py +3 -11
- sqlspec/extensions/litestar/handlers.py +2 -1
- sqlspec/extensions/litestar/plugin.py +6 -2
- sqlspec/mixins.py +156 -0
- sqlspec/typing.py +19 -1
- {sqlspec-0.9.0.dist-info → sqlspec-0.10.0.dist-info}/METADATA +147 -3
- sqlspec-0.10.0.dist-info/RECORD +67 -0
- sqlspec-0.9.0.dist-info/RECORD +0 -61
- {sqlspec-0.9.0.dist-info → sqlspec-0.10.0.dist-info}/WHEEL +0 -0
- {sqlspec-0.9.0.dist-info → sqlspec-0.10.0.dist-info}/licenses/LICENSE +0 -0
- {sqlspec-0.9.0.dist-info → sqlspec-0.10.0.dist-info}/licenses/NOTICE +0 -0
|
@@ -3,22 +3,27 @@
|
|
|
3
3
|
|
|
4
4
|
import logging
|
|
5
5
|
import re
|
|
6
|
-
from typing import TYPE_CHECKING, Any, Optional, Union, cast
|
|
6
|
+
from typing import TYPE_CHECKING, Any, Optional, Union, cast, overload
|
|
7
7
|
|
|
8
|
+
from psqlpy import Connection, QueryResult
|
|
8
9
|
from psqlpy.exceptions import RustPSQLDriverPyBaseError
|
|
9
10
|
|
|
10
|
-
from sqlspec.base import AsyncDriverAdapterProtocol
|
|
11
|
+
from sqlspec.base import AsyncDriverAdapterProtocol
|
|
11
12
|
from sqlspec.exceptions import SQLParsingError
|
|
13
|
+
from sqlspec.mixins import SQLTranslatorMixin
|
|
12
14
|
from sqlspec.statement import PARAM_REGEX, SQLStatement
|
|
13
15
|
|
|
14
16
|
if TYPE_CHECKING:
|
|
15
|
-
from
|
|
17
|
+
from collections.abc import Sequence
|
|
16
18
|
|
|
17
|
-
from
|
|
19
|
+
from psqlpy import QueryResult
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
from sqlspec.typing import ModelDTOT, StatementParameterType, T
|
|
20
22
|
|
|
23
|
+
__all__ = ("PsqlpyConnection", "PsqlpyDriver")
|
|
21
24
|
|
|
25
|
+
|
|
26
|
+
PsqlpyConnection = Connection
|
|
22
27
|
# Regex to find '?' placeholders, skipping those inside quotes or SQL comments
|
|
23
28
|
QMARK_REGEX = re.compile(
|
|
24
29
|
r"""(?P<dquote>"[^"]*") | # Double-quoted strings
|
|
@@ -31,13 +36,16 @@ QMARK_REGEX = re.compile(
|
|
|
31
36
|
logger = logging.getLogger("sqlspec")
|
|
32
37
|
|
|
33
38
|
|
|
34
|
-
class PsqlpyDriver(
|
|
39
|
+
class PsqlpyDriver(
|
|
40
|
+
SQLTranslatorMixin["PsqlpyConnection"],
|
|
41
|
+
AsyncDriverAdapterProtocol["PsqlpyConnection"],
|
|
42
|
+
):
|
|
35
43
|
"""Psqlpy Postgres Driver Adapter."""
|
|
36
44
|
|
|
37
|
-
connection: "
|
|
45
|
+
connection: "PsqlpyConnection"
|
|
38
46
|
dialect: str = "postgres"
|
|
39
47
|
|
|
40
|
-
def __init__(self, connection: "
|
|
48
|
+
def __init__(self, connection: "PsqlpyConnection") -> None:
|
|
41
49
|
self.connection = connection
|
|
42
50
|
|
|
43
51
|
def _process_sql_params(
|
|
@@ -52,6 +60,17 @@ class PsqlpyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
52
60
|
psqlpy uses $1, $2 style parameters natively.
|
|
53
61
|
This method converts '?' (tuple/list) and ':name' (dict) styles to $n.
|
|
54
62
|
It relies on SQLStatement for initial parameter validation and merging.
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
sql: The SQL to process.
|
|
66
|
+
parameters: The parameters to process.
|
|
67
|
+
kwargs: Additional keyword arguments.
|
|
68
|
+
|
|
69
|
+
Raises:
|
|
70
|
+
SQLParsingError: If the SQL is invalid.
|
|
71
|
+
|
|
72
|
+
Returns:
|
|
73
|
+
A tuple of the processed SQL and parameters.
|
|
55
74
|
"""
|
|
56
75
|
stmt = SQLStatement(sql=sql, parameters=parameters, dialect=self.dialect, kwargs=kwargs or None)
|
|
57
76
|
sql, parameters = stmt.process()
|
|
@@ -154,16 +173,39 @@ class PsqlpyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
154
173
|
|
|
155
174
|
return sql, ()
|
|
156
175
|
|
|
176
|
+
# --- Public API Methods --- #
|
|
177
|
+
@overload
|
|
178
|
+
async def select(
|
|
179
|
+
self,
|
|
180
|
+
sql: str,
|
|
181
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
182
|
+
/,
|
|
183
|
+
*,
|
|
184
|
+
connection: "Optional[PsqlpyConnection]" = None,
|
|
185
|
+
schema_type: None = None,
|
|
186
|
+
**kwargs: Any,
|
|
187
|
+
) -> "Sequence[dict[str, Any]]": ...
|
|
188
|
+
@overload
|
|
189
|
+
async def select(
|
|
190
|
+
self,
|
|
191
|
+
sql: str,
|
|
192
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
193
|
+
/,
|
|
194
|
+
*,
|
|
195
|
+
connection: "Optional[PsqlpyConnection]" = None,
|
|
196
|
+
schema_type: "type[ModelDTOT]",
|
|
197
|
+
**kwargs: Any,
|
|
198
|
+
) -> "Sequence[ModelDTOT]": ...
|
|
157
199
|
async def select(
|
|
158
200
|
self,
|
|
159
201
|
sql: str,
|
|
160
202
|
parameters: Optional["StatementParameterType"] = None,
|
|
161
203
|
/,
|
|
162
204
|
*,
|
|
163
|
-
connection: Optional["
|
|
205
|
+
connection: Optional["PsqlpyConnection"] = None,
|
|
164
206
|
schema_type: "Optional[type[ModelDTOT]]" = None,
|
|
165
207
|
**kwargs: Any,
|
|
166
|
-
) -> "
|
|
208
|
+
) -> "Sequence[Union[ModelDTOT, dict[str, Any]]]":
|
|
167
209
|
connection = self._connection(connection)
|
|
168
210
|
sql, parameters = self._process_sql_params(sql, parameters, **kwargs)
|
|
169
211
|
parameters = parameters or [] # psqlpy expects a list/tuple
|
|
@@ -171,16 +213,38 @@ class PsqlpyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
171
213
|
results: QueryResult = await connection.fetch(sql, parameters=parameters)
|
|
172
214
|
|
|
173
215
|
if schema_type is None:
|
|
174
|
-
return cast("list[dict[str, Any]]", results.result())
|
|
216
|
+
return cast("list[dict[str, Any]]", results.result())
|
|
175
217
|
return results.as_class(as_class=schema_type)
|
|
176
218
|
|
|
219
|
+
@overload
|
|
220
|
+
async def select_one(
|
|
221
|
+
self,
|
|
222
|
+
sql: str,
|
|
223
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
224
|
+
/,
|
|
225
|
+
*,
|
|
226
|
+
connection: "Optional[PsqlpyConnection]" = None,
|
|
227
|
+
schema_type: None = None,
|
|
228
|
+
**kwargs: Any,
|
|
229
|
+
) -> "dict[str, Any]": ...
|
|
230
|
+
@overload
|
|
231
|
+
async def select_one(
|
|
232
|
+
self,
|
|
233
|
+
sql: str,
|
|
234
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
235
|
+
/,
|
|
236
|
+
*,
|
|
237
|
+
connection: "Optional[PsqlpyConnection]" = None,
|
|
238
|
+
schema_type: "type[ModelDTOT]",
|
|
239
|
+
**kwargs: Any,
|
|
240
|
+
) -> "ModelDTOT": ...
|
|
177
241
|
async def select_one(
|
|
178
242
|
self,
|
|
179
243
|
sql: str,
|
|
180
244
|
parameters: Optional["StatementParameterType"] = None,
|
|
181
245
|
/,
|
|
182
246
|
*,
|
|
183
|
-
connection: Optional["
|
|
247
|
+
connection: Optional["PsqlpyConnection"] = None,
|
|
184
248
|
schema_type: "Optional[type[ModelDTOT]]" = None,
|
|
185
249
|
**kwargs: Any,
|
|
186
250
|
) -> "Union[ModelDTOT, dict[str, Any]]":
|
|
@@ -195,13 +259,35 @@ class PsqlpyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
195
259
|
return cast("dict[str, Any]", result[0]) # type: ignore[index]
|
|
196
260
|
return result.as_class(as_class=schema_type)[0]
|
|
197
261
|
|
|
262
|
+
@overload
|
|
263
|
+
async def select_one_or_none(
|
|
264
|
+
self,
|
|
265
|
+
sql: str,
|
|
266
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
267
|
+
/,
|
|
268
|
+
*,
|
|
269
|
+
connection: "Optional[PsqlpyConnection]" = None,
|
|
270
|
+
schema_type: None = None,
|
|
271
|
+
**kwargs: Any,
|
|
272
|
+
) -> "Optional[dict[str, Any]]": ...
|
|
273
|
+
@overload
|
|
274
|
+
async def select_one_or_none(
|
|
275
|
+
self,
|
|
276
|
+
sql: str,
|
|
277
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
278
|
+
/,
|
|
279
|
+
*,
|
|
280
|
+
connection: "Optional[PsqlpyConnection]" = None,
|
|
281
|
+
schema_type: "type[ModelDTOT]",
|
|
282
|
+
**kwargs: Any,
|
|
283
|
+
) -> "Optional[ModelDTOT]": ...
|
|
198
284
|
async def select_one_or_none(
|
|
199
285
|
self,
|
|
200
286
|
sql: str,
|
|
201
287
|
parameters: Optional["StatementParameterType"] = None,
|
|
202
288
|
/,
|
|
203
289
|
*,
|
|
204
|
-
connection: Optional["
|
|
290
|
+
connection: Optional["PsqlpyConnection"] = None,
|
|
205
291
|
schema_type: "Optional[type[ModelDTOT]]" = None,
|
|
206
292
|
**kwargs: Any,
|
|
207
293
|
) -> "Optional[Union[ModelDTOT, dict[str, Any]]]":
|
|
@@ -220,13 +306,35 @@ class PsqlpyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
220
306
|
return None
|
|
221
307
|
return cast("ModelDTOT", result[0]) # type: ignore[index]
|
|
222
308
|
|
|
309
|
+
@overload
|
|
310
|
+
async def select_value(
|
|
311
|
+
self,
|
|
312
|
+
sql: str,
|
|
313
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
314
|
+
/,
|
|
315
|
+
*,
|
|
316
|
+
connection: "Optional[PsqlpyConnection]" = None,
|
|
317
|
+
schema_type: None = None,
|
|
318
|
+
**kwargs: Any,
|
|
319
|
+
) -> "Any": ...
|
|
320
|
+
@overload
|
|
223
321
|
async def select_value(
|
|
224
322
|
self,
|
|
225
323
|
sql: str,
|
|
226
324
|
parameters: "Optional[StatementParameterType]" = None,
|
|
227
325
|
/,
|
|
228
326
|
*,
|
|
229
|
-
connection: "Optional[
|
|
327
|
+
connection: "Optional[PsqlpyConnection]" = None,
|
|
328
|
+
schema_type: "type[T]",
|
|
329
|
+
**kwargs: Any,
|
|
330
|
+
) -> "T": ...
|
|
331
|
+
async def select_value(
|
|
332
|
+
self,
|
|
333
|
+
sql: str,
|
|
334
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
335
|
+
/,
|
|
336
|
+
*,
|
|
337
|
+
connection: "Optional[PsqlpyConnection]" = None,
|
|
230
338
|
schema_type: "Optional[type[T]]" = None,
|
|
231
339
|
**kwargs: Any,
|
|
232
340
|
) -> "Union[T, Any]":
|
|
@@ -240,13 +348,35 @@ class PsqlpyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
240
348
|
return value
|
|
241
349
|
return schema_type(value) # type: ignore[call-arg]
|
|
242
350
|
|
|
351
|
+
@overload
|
|
243
352
|
async def select_value_or_none(
|
|
244
353
|
self,
|
|
245
354
|
sql: str,
|
|
246
355
|
parameters: "Optional[StatementParameterType]" = None,
|
|
247
356
|
/,
|
|
248
357
|
*,
|
|
249
|
-
connection: "Optional[
|
|
358
|
+
connection: "Optional[PsqlpyConnection]" = None,
|
|
359
|
+
schema_type: None = None,
|
|
360
|
+
**kwargs: Any,
|
|
361
|
+
) -> "Optional[Any]": ...
|
|
362
|
+
@overload
|
|
363
|
+
async def select_value_or_none(
|
|
364
|
+
self,
|
|
365
|
+
sql: str,
|
|
366
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
367
|
+
/,
|
|
368
|
+
*,
|
|
369
|
+
connection: "Optional[PsqlpyConnection]" = None,
|
|
370
|
+
schema_type: "type[T]",
|
|
371
|
+
**kwargs: Any,
|
|
372
|
+
) -> "Optional[T]": ...
|
|
373
|
+
async def select_value_or_none(
|
|
374
|
+
self,
|
|
375
|
+
sql: str,
|
|
376
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
377
|
+
/,
|
|
378
|
+
*,
|
|
379
|
+
connection: "Optional[PsqlpyConnection]" = None,
|
|
250
380
|
schema_type: "Optional[type[T]]" = None,
|
|
251
381
|
**kwargs: Any,
|
|
252
382
|
) -> "Optional[Union[T, Any]]":
|
|
@@ -270,7 +400,7 @@ class PsqlpyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
270
400
|
parameters: Optional["StatementParameterType"] = None,
|
|
271
401
|
/,
|
|
272
402
|
*,
|
|
273
|
-
connection: Optional["
|
|
403
|
+
connection: Optional["PsqlpyConnection"] = None,
|
|
274
404
|
**kwargs: Any,
|
|
275
405
|
) -> int:
|
|
276
406
|
connection = self._connection(connection)
|
|
@@ -282,13 +412,35 @@ class PsqlpyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
282
412
|
# if no error was raised
|
|
283
413
|
return 1
|
|
284
414
|
|
|
415
|
+
@overload
|
|
416
|
+
async def insert_update_delete_returning(
|
|
417
|
+
self,
|
|
418
|
+
sql: str,
|
|
419
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
420
|
+
/,
|
|
421
|
+
*,
|
|
422
|
+
connection: "Optional[PsqlpyConnection]" = None,
|
|
423
|
+
schema_type: None = None,
|
|
424
|
+
**kwargs: Any,
|
|
425
|
+
) -> "dict[str, Any]": ...
|
|
426
|
+
@overload
|
|
427
|
+
async def insert_update_delete_returning(
|
|
428
|
+
self,
|
|
429
|
+
sql: str,
|
|
430
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
431
|
+
/,
|
|
432
|
+
*,
|
|
433
|
+
connection: "Optional[PsqlpyConnection]" = None,
|
|
434
|
+
schema_type: "type[ModelDTOT]",
|
|
435
|
+
**kwargs: Any,
|
|
436
|
+
) -> "ModelDTOT": ...
|
|
285
437
|
async def insert_update_delete_returning(
|
|
286
438
|
self,
|
|
287
439
|
sql: str,
|
|
288
440
|
parameters: Optional["StatementParameterType"] = None,
|
|
289
441
|
/,
|
|
290
442
|
*,
|
|
291
|
-
connection: Optional["
|
|
443
|
+
connection: Optional["PsqlpyConnection"] = None,
|
|
292
444
|
schema_type: "Optional[type[ModelDTOT]]" = None,
|
|
293
445
|
**kwargs: Any,
|
|
294
446
|
) -> "Optional[Union[dict[str, Any], ModelDTOT]]":
|
|
@@ -313,7 +465,7 @@ class PsqlpyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
313
465
|
parameters: Optional["StatementParameterType"] = None,
|
|
314
466
|
/,
|
|
315
467
|
*,
|
|
316
|
-
connection: Optional["
|
|
468
|
+
connection: Optional["PsqlpyConnection"] = None,
|
|
317
469
|
**kwargs: Any,
|
|
318
470
|
) -> str:
|
|
319
471
|
connection = self._connection(connection)
|
|
@@ -323,7 +475,7 @@ class PsqlpyDriver(AsyncDriverAdapterProtocol["Connection"]):
|
|
|
323
475
|
await connection.execute(sql, parameters=parameters)
|
|
324
476
|
return sql
|
|
325
477
|
|
|
326
|
-
def _connection(self, connection: Optional["
|
|
478
|
+
def _connection(self, connection: Optional["PsqlpyConnection"] = None) -> "PsqlpyConnection":
|
|
327
479
|
"""Get the connection to use.
|
|
328
480
|
|
|
329
481
|
Args:
|
|
@@ -4,13 +4,20 @@ from sqlspec.adapters.psycopg.config import (
|
|
|
4
4
|
PsycopgSyncConfig,
|
|
5
5
|
PsycopgSyncPoolConfig,
|
|
6
6
|
)
|
|
7
|
-
from sqlspec.adapters.psycopg.driver import
|
|
7
|
+
from sqlspec.adapters.psycopg.driver import (
|
|
8
|
+
PsycopgAsyncConnection,
|
|
9
|
+
PsycopgAsyncDriver,
|
|
10
|
+
PsycopgSyncConnection,
|
|
11
|
+
PsycopgSyncDriver,
|
|
12
|
+
)
|
|
8
13
|
|
|
9
14
|
__all__ = (
|
|
10
15
|
"PsycopgAsyncConfig",
|
|
16
|
+
"PsycopgAsyncConnection",
|
|
11
17
|
"PsycopgAsyncDriver",
|
|
12
18
|
"PsycopgAsyncPoolConfig",
|
|
13
19
|
"PsycopgSyncConfig",
|
|
20
|
+
"PsycopgSyncConnection",
|
|
14
21
|
"PsycopgSyncDriver",
|
|
15
22
|
"PsycopgSyncPoolConfig",
|
|
16
23
|
)
|
|
@@ -1,9 +1,19 @@
|
|
|
1
1
|
from sqlspec.adapters.psycopg.config._async import PsycopgAsyncConfig, PsycopgAsyncPoolConfig
|
|
2
2
|
from sqlspec.adapters.psycopg.config._sync import PsycopgSyncConfig, PsycopgSyncPoolConfig
|
|
3
|
+
from sqlspec.adapters.psycopg.driver import (
|
|
4
|
+
PsycopgAsyncConnection,
|
|
5
|
+
PsycopgAsyncDriver,
|
|
6
|
+
PsycopgSyncConnection,
|
|
7
|
+
PsycopgSyncDriver,
|
|
8
|
+
)
|
|
3
9
|
|
|
4
10
|
__all__ = (
|
|
5
11
|
"PsycopgAsyncConfig",
|
|
12
|
+
"PsycopgAsyncConnection",
|
|
13
|
+
"PsycopgAsyncDriver",
|
|
6
14
|
"PsycopgAsyncPoolConfig",
|
|
7
15
|
"PsycopgSyncConfig",
|
|
16
|
+
"PsycopgSyncConnection",
|
|
17
|
+
"PsycopgSyncDriver",
|
|
8
18
|
"PsycopgSyncPoolConfig",
|
|
9
19
|
)
|
|
@@ -2,11 +2,10 @@ from contextlib import asynccontextmanager
|
|
|
2
2
|
from dataclasses import dataclass, field
|
|
3
3
|
from typing import TYPE_CHECKING, Any, Optional
|
|
4
4
|
|
|
5
|
-
from psycopg import AsyncConnection
|
|
6
5
|
from psycopg_pool import AsyncConnectionPool
|
|
7
6
|
|
|
8
7
|
from sqlspec.adapters.psycopg.config._common import PsycopgGenericPoolConfig
|
|
9
|
-
from sqlspec.adapters.psycopg.driver import PsycopgAsyncDriver
|
|
8
|
+
from sqlspec.adapters.psycopg.driver import PsycopgAsyncConnection, PsycopgAsyncDriver
|
|
10
9
|
from sqlspec.base import AsyncDatabaseConfig
|
|
11
10
|
from sqlspec.exceptions import ImproperConfigurationError
|
|
12
11
|
from sqlspec.typing import dataclass_to_dict
|
|
@@ -22,12 +21,12 @@ __all__ = (
|
|
|
22
21
|
|
|
23
22
|
|
|
24
23
|
@dataclass
|
|
25
|
-
class PsycopgAsyncPoolConfig(PsycopgGenericPoolConfig[
|
|
24
|
+
class PsycopgAsyncPoolConfig(PsycopgGenericPoolConfig[PsycopgAsyncConnection, AsyncConnectionPool]):
|
|
26
25
|
"""Async Psycopg Pool Config"""
|
|
27
26
|
|
|
28
27
|
|
|
29
28
|
@dataclass
|
|
30
|
-
class PsycopgAsyncConfig(AsyncDatabaseConfig[
|
|
29
|
+
class PsycopgAsyncConfig(AsyncDatabaseConfig[PsycopgAsyncConnection, AsyncConnectionPool, PsycopgAsyncDriver]):
|
|
31
30
|
"""Async Psycopg database Configuration.
|
|
32
31
|
|
|
33
32
|
This class provides the base configuration for Psycopg database connections, extending
|
|
@@ -41,7 +40,7 @@ class PsycopgAsyncConfig(AsyncDatabaseConfig[AsyncConnection, AsyncConnectionPoo
|
|
|
41
40
|
"""Psycopg Pool configuration"""
|
|
42
41
|
pool_instance: "Optional[AsyncConnectionPool]" = None
|
|
43
42
|
"""Optional pool to use"""
|
|
44
|
-
connection_type: "type[
|
|
43
|
+
connection_type: "type[PsycopgAsyncConnection]" = field(init=False, default_factory=lambda: PsycopgAsyncConnection) # type: ignore[assignment]
|
|
45
44
|
"""Type of the connection object"""
|
|
46
45
|
driver_type: "type[PsycopgAsyncDriver]" = field(init=False, default_factory=lambda: PsycopgAsyncDriver) # type: ignore[type-abstract,unused-ignore]
|
|
47
46
|
"""Type of the driver object"""
|
|
@@ -93,7 +92,7 @@ class PsycopgAsyncConfig(AsyncDatabaseConfig[AsyncConnection, AsyncConnectionPoo
|
|
|
93
92
|
msg = "'pool_config' methods can not be used when a 'pool_instance' is provided."
|
|
94
93
|
raise ImproperConfigurationError(msg)
|
|
95
94
|
|
|
96
|
-
async def create_connection(self) -> "
|
|
95
|
+
async def create_connection(self) -> "PsycopgAsyncConnection":
|
|
97
96
|
"""Create and return a new psycopg async connection from the pool.
|
|
98
97
|
|
|
99
98
|
Returns:
|
|
@@ -143,7 +142,7 @@ class PsycopgAsyncConfig(AsyncDatabaseConfig[AsyncConnection, AsyncConnectionPoo
|
|
|
143
142
|
return self.create_pool()
|
|
144
143
|
|
|
145
144
|
@asynccontextmanager
|
|
146
|
-
async def provide_connection(self, *args: "Any", **kwargs: "Any") -> "AsyncGenerator[
|
|
145
|
+
async def provide_connection(self, *args: "Any", **kwargs: "Any") -> "AsyncGenerator[PsycopgAsyncConnection, None]":
|
|
147
146
|
"""Create and provide a database connection.
|
|
148
147
|
|
|
149
148
|
Yields:
|
|
@@ -2,11 +2,10 @@ from contextlib import contextmanager
|
|
|
2
2
|
from dataclasses import dataclass, field
|
|
3
3
|
from typing import TYPE_CHECKING, Any, Optional
|
|
4
4
|
|
|
5
|
-
from psycopg import Connection
|
|
6
5
|
from psycopg_pool import ConnectionPool
|
|
7
6
|
|
|
8
7
|
from sqlspec.adapters.psycopg.config._common import PsycopgGenericPoolConfig
|
|
9
|
-
from sqlspec.adapters.psycopg.driver import PsycopgSyncDriver
|
|
8
|
+
from sqlspec.adapters.psycopg.driver import PsycopgSyncConnection, PsycopgSyncDriver
|
|
10
9
|
from sqlspec.base import SyncDatabaseConfig
|
|
11
10
|
from sqlspec.exceptions import ImproperConfigurationError
|
|
12
11
|
from sqlspec.typing import dataclass_to_dict
|
|
@@ -22,12 +21,12 @@ __all__ = (
|
|
|
22
21
|
|
|
23
22
|
|
|
24
23
|
@dataclass
|
|
25
|
-
class PsycopgSyncPoolConfig(PsycopgGenericPoolConfig[
|
|
24
|
+
class PsycopgSyncPoolConfig(PsycopgGenericPoolConfig[PsycopgSyncConnection, ConnectionPool]):
|
|
26
25
|
"""Sync Psycopg Pool Config"""
|
|
27
26
|
|
|
28
27
|
|
|
29
28
|
@dataclass
|
|
30
|
-
class PsycopgSyncConfig(SyncDatabaseConfig[
|
|
29
|
+
class PsycopgSyncConfig(SyncDatabaseConfig[PsycopgSyncConnection, ConnectionPool, PsycopgSyncDriver]):
|
|
31
30
|
"""Sync Psycopg database Configuration.
|
|
32
31
|
This class provides the base configuration for Psycopg database connections, extending
|
|
33
32
|
the generic database configuration with Psycopg-specific settings.([1](https://www.psycopg.org/psycopg3/docs/api/connections.html))
|
|
@@ -40,7 +39,7 @@ class PsycopgSyncConfig(SyncDatabaseConfig[Connection, ConnectionPool, PsycopgSy
|
|
|
40
39
|
"""Psycopg Pool configuration"""
|
|
41
40
|
pool_instance: "Optional[ConnectionPool]" = None
|
|
42
41
|
"""Optional pool to use"""
|
|
43
|
-
connection_type: "type[
|
|
42
|
+
connection_type: "type[PsycopgSyncConnection]" = field(init=False, default_factory=lambda: PsycopgSyncConnection) # type: ignore[assignment]
|
|
44
43
|
"""Type of the connection object"""
|
|
45
44
|
driver_type: "type[PsycopgSyncDriver]" = field(init=False, default_factory=lambda: PsycopgSyncDriver) # type: ignore[type-abstract,unused-ignore]
|
|
46
45
|
"""Type of the driver object"""
|
|
@@ -92,7 +91,7 @@ class PsycopgSyncConfig(SyncDatabaseConfig[Connection, ConnectionPool, PsycopgSy
|
|
|
92
91
|
msg = "'pool_config' methods can not be used when a 'pool_instance' is provided."
|
|
93
92
|
raise ImproperConfigurationError(msg)
|
|
94
93
|
|
|
95
|
-
def create_connection(self) -> "
|
|
94
|
+
def create_connection(self) -> "PsycopgSyncConnection":
|
|
96
95
|
"""Create and return a new psycopg connection from the pool.
|
|
97
96
|
|
|
98
97
|
Returns:
|
|
@@ -142,11 +141,11 @@ class PsycopgSyncConfig(SyncDatabaseConfig[Connection, ConnectionPool, PsycopgSy
|
|
|
142
141
|
return self.create_pool()
|
|
143
142
|
|
|
144
143
|
@contextmanager
|
|
145
|
-
def provide_connection(self, *args: "Any", **kwargs: "Any") -> "Generator[
|
|
144
|
+
def provide_connection(self, *args: "Any", **kwargs: "Any") -> "Generator[PsycopgSyncConnection, None, None]":
|
|
146
145
|
"""Create and provide a database connection.
|
|
147
146
|
|
|
148
147
|
Yields:
|
|
149
|
-
|
|
148
|
+
PsycopgSyncConnection: A database connection from the pool.
|
|
150
149
|
"""
|
|
151
150
|
pool = self.provide_pool(*args, **kwargs)
|
|
152
151
|
with pool, pool.connection() as connection:
|