sqlspec 0.7.0__py3-none-any.whl → 0.8.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 +15 -0
- sqlspec/_serialization.py +16 -2
- sqlspec/_typing.py +1 -1
- sqlspec/adapters/adbc/__init__.py +7 -0
- sqlspec/adapters/adbc/config.py +160 -17
- sqlspec/adapters/adbc/driver.py +333 -0
- sqlspec/adapters/aiosqlite/__init__.py +6 -2
- sqlspec/adapters/aiosqlite/config.py +25 -7
- sqlspec/adapters/aiosqlite/driver.py +275 -0
- sqlspec/adapters/asyncmy/__init__.py +7 -2
- sqlspec/adapters/asyncmy/config.py +75 -14
- sqlspec/adapters/asyncmy/driver.py +255 -0
- sqlspec/adapters/asyncpg/__init__.py +9 -0
- sqlspec/adapters/asyncpg/config.py +99 -20
- sqlspec/adapters/asyncpg/driver.py +288 -0
- sqlspec/adapters/duckdb/__init__.py +6 -2
- sqlspec/adapters/duckdb/config.py +197 -15
- sqlspec/adapters/duckdb/driver.py +225 -0
- sqlspec/adapters/oracledb/__init__.py +11 -8
- sqlspec/adapters/oracledb/config/__init__.py +6 -6
- sqlspec/adapters/oracledb/config/_asyncio.py +98 -13
- sqlspec/adapters/oracledb/config/_common.py +1 -1
- sqlspec/adapters/oracledb/config/_sync.py +99 -14
- sqlspec/adapters/oracledb/driver.py +498 -0
- sqlspec/adapters/psycopg/__init__.py +11 -0
- sqlspec/adapters/psycopg/config/__init__.py +6 -6
- sqlspec/adapters/psycopg/config/_async.py +105 -13
- sqlspec/adapters/psycopg/config/_common.py +2 -2
- sqlspec/adapters/psycopg/config/_sync.py +105 -13
- sqlspec/adapters/psycopg/driver.py +616 -0
- sqlspec/adapters/sqlite/__init__.py +7 -0
- sqlspec/adapters/sqlite/config.py +25 -7
- sqlspec/adapters/sqlite/driver.py +303 -0
- sqlspec/base.py +416 -36
- sqlspec/extensions/litestar/__init__.py +19 -0
- sqlspec/extensions/litestar/_utils.py +56 -0
- sqlspec/extensions/litestar/config.py +81 -0
- sqlspec/extensions/litestar/handlers.py +188 -0
- sqlspec/extensions/litestar/plugin.py +103 -11
- sqlspec/typing.py +72 -17
- sqlspec/utils/__init__.py +3 -0
- sqlspec/utils/deprecation.py +1 -1
- sqlspec/utils/fixtures.py +4 -5
- sqlspec/utils/sync_tools.py +335 -0
- {sqlspec-0.7.0.dist-info → sqlspec-0.8.0.dist-info}/METADATA +1 -1
- sqlspec-0.8.0.dist-info/RECORD +57 -0
- sqlspec-0.7.0.dist-info/RECORD +0 -46
- {sqlspec-0.7.0.dist-info → sqlspec-0.8.0.dist-info}/WHEEL +0 -0
- {sqlspec-0.7.0.dist-info → sqlspec-0.8.0.dist-info}/licenses/LICENSE +0 -0
- {sqlspec-0.7.0.dist-info → sqlspec-0.8.0.dist-info}/licenses/NOTICE +0 -0
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
from contextlib import asynccontextmanager, contextmanager
|
|
2
|
+
from typing import TYPE_CHECKING, Any, Optional, Union, cast
|
|
3
|
+
|
|
4
|
+
from sqlspec.base import AsyncDriverAdapterProtocol, SyncDriverAdapterProtocol, T
|
|
5
|
+
|
|
6
|
+
if TYPE_CHECKING:
|
|
7
|
+
from collections.abc import AsyncGenerator, Generator
|
|
8
|
+
|
|
9
|
+
from oracledb import AsyncConnection, AsyncCursor, Connection, Cursor
|
|
10
|
+
|
|
11
|
+
from sqlspec.typing import ModelDTOT, StatementParameterType
|
|
12
|
+
|
|
13
|
+
__all__ = ("OracleAsyncDriver", "OracleSyncDriver")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class OracleSyncDriver(SyncDriverAdapterProtocol["Connection"]):
|
|
17
|
+
"""Oracle Sync Driver Adapter."""
|
|
18
|
+
|
|
19
|
+
connection: "Connection"
|
|
20
|
+
|
|
21
|
+
def __init__(self, connection: "Connection") -> None:
|
|
22
|
+
self.connection = connection
|
|
23
|
+
|
|
24
|
+
@staticmethod
|
|
25
|
+
@contextmanager
|
|
26
|
+
def _with_cursor(connection: "Connection") -> "Generator[Cursor, None, None]":
|
|
27
|
+
cursor = connection.cursor()
|
|
28
|
+
try:
|
|
29
|
+
yield cursor
|
|
30
|
+
finally:
|
|
31
|
+
cursor.close()
|
|
32
|
+
|
|
33
|
+
def select(
|
|
34
|
+
self,
|
|
35
|
+
sql: str,
|
|
36
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
37
|
+
/,
|
|
38
|
+
connection: "Optional[Connection]" = None,
|
|
39
|
+
schema_type: "Optional[type[ModelDTOT]]" = None,
|
|
40
|
+
) -> "list[Union[ModelDTOT, dict[str, Any]]]":
|
|
41
|
+
"""Fetch data from the database.
|
|
42
|
+
|
|
43
|
+
Returns:
|
|
44
|
+
List of row data as either model instances or dictionaries.
|
|
45
|
+
"""
|
|
46
|
+
connection = self._connection(connection)
|
|
47
|
+
sql, parameters = self._process_sql_params(sql, parameters)
|
|
48
|
+
with self._with_cursor(connection) as cursor:
|
|
49
|
+
cursor.execute(sql, parameters) # pyright: ignore[reportUnknownMemberType]
|
|
50
|
+
results = cursor.fetchall() # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
|
|
51
|
+
if not results:
|
|
52
|
+
return []
|
|
53
|
+
# Get column names
|
|
54
|
+
column_names = [col[0] for col in cursor.description or []] # pyright: ignore[reportUnknownMemberType,reportUnknownVariableType]
|
|
55
|
+
|
|
56
|
+
if schema_type:
|
|
57
|
+
return [cast("ModelDTOT", schema_type(**dict(zip(column_names, row)))) for row in results] # pyright: ignore
|
|
58
|
+
|
|
59
|
+
return [dict(zip(column_names, row)) for row in results] # pyright: ignore
|
|
60
|
+
|
|
61
|
+
def select_one(
|
|
62
|
+
self,
|
|
63
|
+
sql: str,
|
|
64
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
65
|
+
/,
|
|
66
|
+
connection: "Optional[Connection]" = None,
|
|
67
|
+
schema_type: "Optional[type[ModelDTOT]]" = None,
|
|
68
|
+
) -> "Union[ModelDTOT, dict[str, Any]]":
|
|
69
|
+
"""Fetch one row from the database.
|
|
70
|
+
|
|
71
|
+
Returns:
|
|
72
|
+
The first row of the query results.
|
|
73
|
+
"""
|
|
74
|
+
connection = self._connection(connection)
|
|
75
|
+
sql, parameters = self._process_sql_params(sql, parameters)
|
|
76
|
+
|
|
77
|
+
with self._with_cursor(connection) as cursor:
|
|
78
|
+
cursor.execute(sql, parameters) # pyright: ignore[reportUnknownMemberType]
|
|
79
|
+
result = cursor.fetchone() # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
|
|
80
|
+
result = self.check_not_found(result) # pyright: ignore[reportUnknownArgumentType]
|
|
81
|
+
|
|
82
|
+
# Get column names
|
|
83
|
+
column_names = [col[0] for col in cursor.description or []] # pyright: ignore[reportUnknownMemberType,reportUnknownVariableType]
|
|
84
|
+
|
|
85
|
+
if schema_type is not None:
|
|
86
|
+
return cast("ModelDTOT", schema_type(**dict(zip(column_names, result)))) # pyright: ignore[reportUnknownArgumentType]
|
|
87
|
+
# Always return dictionaries
|
|
88
|
+
return dict(zip(column_names, result)) # pyright: ignore[reportUnknownArgumentType,reportUnknownVariableType]
|
|
89
|
+
|
|
90
|
+
def select_one_or_none(
|
|
91
|
+
self,
|
|
92
|
+
sql: str,
|
|
93
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
94
|
+
/,
|
|
95
|
+
connection: "Optional[Connection]" = None,
|
|
96
|
+
schema_type: "Optional[type[ModelDTOT]]" = None,
|
|
97
|
+
) -> "Optional[Union[ModelDTOT, dict[str, Any]]]":
|
|
98
|
+
"""Fetch one row from the database.
|
|
99
|
+
|
|
100
|
+
Returns:
|
|
101
|
+
The first row of the query results.
|
|
102
|
+
"""
|
|
103
|
+
connection = self._connection(connection)
|
|
104
|
+
sql, parameters = self._process_sql_params(sql, parameters)
|
|
105
|
+
|
|
106
|
+
with self._with_cursor(connection) as cursor:
|
|
107
|
+
cursor.execute(sql, parameters) # pyright: ignore[reportUnknownMemberType]
|
|
108
|
+
result = cursor.fetchone() # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
|
|
109
|
+
|
|
110
|
+
if result is None:
|
|
111
|
+
return None
|
|
112
|
+
|
|
113
|
+
# Get column names
|
|
114
|
+
column_names = [col[0] for col in cursor.description or []] # pyright: ignore[reportUnknownMemberType,reportUnknownVariableType]
|
|
115
|
+
|
|
116
|
+
if schema_type is not None:
|
|
117
|
+
return cast("ModelDTOT", schema_type(**dict(zip(column_names, result)))) # pyright: ignore[reportUnknownArgumentType]
|
|
118
|
+
# Always return dictionaries
|
|
119
|
+
return dict(zip(column_names, result)) # pyright: ignore[reportUnknownArgumentType,reportUnknownVariableType]
|
|
120
|
+
|
|
121
|
+
def select_value(
|
|
122
|
+
self,
|
|
123
|
+
sql: str,
|
|
124
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
125
|
+
/,
|
|
126
|
+
connection: "Optional[Connection]" = None,
|
|
127
|
+
schema_type: "Optional[type[T]]" = None,
|
|
128
|
+
) -> "Union[T, Any]":
|
|
129
|
+
"""Fetch a single value from the database.
|
|
130
|
+
|
|
131
|
+
Returns:
|
|
132
|
+
The first value from the first row of results, or None if no results.
|
|
133
|
+
"""
|
|
134
|
+
connection = self._connection(connection)
|
|
135
|
+
sql, parameters = self._process_sql_params(sql, parameters)
|
|
136
|
+
|
|
137
|
+
with self._with_cursor(connection) as cursor:
|
|
138
|
+
cursor.execute(sql, parameters) # pyright: ignore[reportUnknownMemberType]
|
|
139
|
+
result = cursor.fetchone() # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
|
|
140
|
+
result = self.check_not_found(result) # pyright: ignore[reportUnknownArgumentType]
|
|
141
|
+
|
|
142
|
+
if schema_type is None:
|
|
143
|
+
return result[0] # pyright: ignore[reportUnknownArgumentType]
|
|
144
|
+
return schema_type(result[0]) # type: ignore[call-arg]
|
|
145
|
+
|
|
146
|
+
def select_value_or_none(
|
|
147
|
+
self,
|
|
148
|
+
sql: str,
|
|
149
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
150
|
+
/,
|
|
151
|
+
connection: "Optional[Connection]" = None,
|
|
152
|
+
schema_type: "Optional[type[T]]" = None,
|
|
153
|
+
) -> "Optional[Union[T, Any]]":
|
|
154
|
+
"""Fetch a single value from the database.
|
|
155
|
+
|
|
156
|
+
Returns:
|
|
157
|
+
The first value from the first row of results, or None if no results.
|
|
158
|
+
"""
|
|
159
|
+
connection = self._connection(connection)
|
|
160
|
+
sql, parameters = self._process_sql_params(sql, parameters)
|
|
161
|
+
|
|
162
|
+
with self._with_cursor(connection) as cursor:
|
|
163
|
+
cursor.execute(sql, parameters) # pyright: ignore[reportUnknownMemberType]
|
|
164
|
+
result = cursor.fetchone() # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
|
|
165
|
+
|
|
166
|
+
if result is None:
|
|
167
|
+
return None
|
|
168
|
+
|
|
169
|
+
if schema_type is None:
|
|
170
|
+
return result[0] # pyright: ignore[reportUnknownArgumentType]
|
|
171
|
+
return schema_type(result[0]) # type: ignore[call-arg]
|
|
172
|
+
|
|
173
|
+
def insert_update_delete(
|
|
174
|
+
self,
|
|
175
|
+
sql: str,
|
|
176
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
177
|
+
/,
|
|
178
|
+
connection: "Optional[Connection]" = None,
|
|
179
|
+
) -> int:
|
|
180
|
+
"""Insert, update, or delete data from the database.
|
|
181
|
+
|
|
182
|
+
Returns:
|
|
183
|
+
Row count affected by the operation.
|
|
184
|
+
"""
|
|
185
|
+
connection = self._connection(connection)
|
|
186
|
+
sql, parameters = self._process_sql_params(sql, parameters)
|
|
187
|
+
|
|
188
|
+
with self._with_cursor(connection) as cursor:
|
|
189
|
+
cursor.execute(sql, parameters) # pyright: ignore[reportUnknownMemberType]
|
|
190
|
+
return cursor.rowcount # pyright: ignore[reportUnknownMemberType]
|
|
191
|
+
|
|
192
|
+
def insert_update_delete_returning(
|
|
193
|
+
self,
|
|
194
|
+
sql: str,
|
|
195
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
196
|
+
/,
|
|
197
|
+
connection: "Optional[Connection]" = None,
|
|
198
|
+
schema_type: "Optional[type[ModelDTOT]]" = None,
|
|
199
|
+
) -> "Optional[Union[dict[str, Any], ModelDTOT]]":
|
|
200
|
+
"""Insert, update, or delete data from the database and return result.
|
|
201
|
+
|
|
202
|
+
Returns:
|
|
203
|
+
The first row of results.
|
|
204
|
+
"""
|
|
205
|
+
connection = self._connection(connection)
|
|
206
|
+
sql, parameters = self._process_sql_params(sql, parameters)
|
|
207
|
+
|
|
208
|
+
with self._with_cursor(connection) as cursor:
|
|
209
|
+
cursor.execute(sql, parameters) # pyright: ignore[reportUnknownMemberType]
|
|
210
|
+
result = cursor.fetchone() # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
|
|
211
|
+
|
|
212
|
+
if result is None:
|
|
213
|
+
return None
|
|
214
|
+
|
|
215
|
+
# Get column names
|
|
216
|
+
column_names = [col[0] for col in cursor.description or []] # pyright: ignore[reportUnknownMemberType,reportUnknownVariableType]
|
|
217
|
+
|
|
218
|
+
if schema_type is not None:
|
|
219
|
+
return cast("ModelDTOT", schema_type(**dict(zip(column_names, result)))) # pyright: ignore[reportUnknownArgumentType]
|
|
220
|
+
# Always return dictionaries
|
|
221
|
+
return dict(zip(column_names, result)) # pyright: ignore[reportUnknownArgumentType,reportUnknownVariableType]
|
|
222
|
+
|
|
223
|
+
def execute_script(
|
|
224
|
+
self,
|
|
225
|
+
sql: str,
|
|
226
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
227
|
+
/,
|
|
228
|
+
connection: "Optional[Connection]" = None,
|
|
229
|
+
) -> str:
|
|
230
|
+
"""Execute a script.
|
|
231
|
+
|
|
232
|
+
Returns:
|
|
233
|
+
Status message for the operation.
|
|
234
|
+
"""
|
|
235
|
+
connection = self._connection(connection)
|
|
236
|
+
sql, parameters = self._process_sql_params(sql, parameters)
|
|
237
|
+
|
|
238
|
+
with self._with_cursor(connection) as cursor:
|
|
239
|
+
cursor.execute(sql, parameters) # pyright: ignore[reportUnknownMemberType]
|
|
240
|
+
return str(cursor.rowcount) # pyright: ignore[reportUnknownMemberType]
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
class OracleAsyncDriver(AsyncDriverAdapterProtocol["AsyncConnection"]):
|
|
244
|
+
"""Oracle Async Driver Adapter."""
|
|
245
|
+
|
|
246
|
+
connection: "AsyncConnection"
|
|
247
|
+
|
|
248
|
+
def __init__(self, connection: "AsyncConnection") -> None:
|
|
249
|
+
self.connection = connection
|
|
250
|
+
|
|
251
|
+
@staticmethod
|
|
252
|
+
@asynccontextmanager
|
|
253
|
+
async def _with_cursor(connection: "AsyncConnection") -> "AsyncGenerator[AsyncCursor, None]":
|
|
254
|
+
cursor = connection.cursor()
|
|
255
|
+
try:
|
|
256
|
+
yield cursor
|
|
257
|
+
finally:
|
|
258
|
+
cursor.close()
|
|
259
|
+
|
|
260
|
+
async def select(
|
|
261
|
+
self,
|
|
262
|
+
sql: str,
|
|
263
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
264
|
+
/,
|
|
265
|
+
connection: "Optional[AsyncConnection]" = None,
|
|
266
|
+
schema_type: "Optional[type[ModelDTOT]]" = None,
|
|
267
|
+
) -> "list[Union[ModelDTOT, dict[str, Any]]]":
|
|
268
|
+
"""Fetch data from the database.
|
|
269
|
+
|
|
270
|
+
Returns:
|
|
271
|
+
List of row data as either model instances or dictionaries.
|
|
272
|
+
"""
|
|
273
|
+
connection = self._connection(connection)
|
|
274
|
+
sql, parameters = self._process_sql_params(sql, parameters)
|
|
275
|
+
|
|
276
|
+
async with self._with_cursor(connection) as cursor:
|
|
277
|
+
await cursor.execute(sql, parameters) # pyright: ignore[reportUnknownMemberType]
|
|
278
|
+
results = await cursor.fetchall() # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
|
|
279
|
+
if not results:
|
|
280
|
+
return []
|
|
281
|
+
# Get column names
|
|
282
|
+
column_names = [col[0] for col in cursor.description or []] # pyright: ignore[reportUnknownMemberType,reportUnknownVariableType]
|
|
283
|
+
|
|
284
|
+
if schema_type:
|
|
285
|
+
return [cast("ModelDTOT", schema_type(**dict(zip(column_names, row)))) for row in results] # pyright: ignore
|
|
286
|
+
|
|
287
|
+
return [dict(zip(column_names, row)) for row in results] # pyright: ignore
|
|
288
|
+
|
|
289
|
+
async def select_one(
|
|
290
|
+
self,
|
|
291
|
+
sql: str,
|
|
292
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
293
|
+
/,
|
|
294
|
+
connection: "Optional[AsyncConnection]" = None,
|
|
295
|
+
schema_type: "Optional[type[ModelDTOT]]" = None,
|
|
296
|
+
) -> "Union[ModelDTOT, dict[str, Any]]":
|
|
297
|
+
"""Fetch one row from the database.
|
|
298
|
+
|
|
299
|
+
Returns:
|
|
300
|
+
The first row of the query results.
|
|
301
|
+
"""
|
|
302
|
+
connection = self._connection(connection)
|
|
303
|
+
sql, parameters = self._process_sql_params(sql, parameters)
|
|
304
|
+
|
|
305
|
+
async with self._with_cursor(connection) as cursor:
|
|
306
|
+
await cursor.execute(sql, parameters) # pyright: ignore[reportUnknownMemberType]
|
|
307
|
+
result = await cursor.fetchone() # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
|
|
308
|
+
result = self.check_not_found(result) # pyright: ignore[reportUnknownArgumentType]
|
|
309
|
+
# Get column names
|
|
310
|
+
column_names = [col[0] for col in cursor.description or []] # pyright: ignore[reportUnknownMemberType,reportUnknownVariableType]
|
|
311
|
+
|
|
312
|
+
if schema_type is not None:
|
|
313
|
+
return cast("ModelDTOT", schema_type(**dict(zip(column_names, result)))) # pyright: ignore[reportUnknownArgumentType]
|
|
314
|
+
# Always return dictionaries
|
|
315
|
+
return dict(zip(column_names, result)) # pyright: ignore[reportUnknownArgumentType,reportUnknownVariableType]
|
|
316
|
+
|
|
317
|
+
async def select_one_or_none(
|
|
318
|
+
self,
|
|
319
|
+
sql: str,
|
|
320
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
321
|
+
/,
|
|
322
|
+
connection: "Optional[AsyncConnection]" = None,
|
|
323
|
+
schema_type: "Optional[type[ModelDTOT]]" = None,
|
|
324
|
+
) -> "Optional[Union[ModelDTOT, dict[str, Any]]]":
|
|
325
|
+
"""Fetch one row from the database.
|
|
326
|
+
|
|
327
|
+
Returns:
|
|
328
|
+
The first row of the query results.
|
|
329
|
+
"""
|
|
330
|
+
connection = self._connection(connection)
|
|
331
|
+
sql, parameters = self._process_sql_params(sql, parameters)
|
|
332
|
+
|
|
333
|
+
async with self._with_cursor(connection) as cursor:
|
|
334
|
+
await cursor.execute(sql, parameters) # pyright: ignore[reportUnknownMemberType]
|
|
335
|
+
result = await cursor.fetchone() # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
|
|
336
|
+
|
|
337
|
+
if result is None:
|
|
338
|
+
return None
|
|
339
|
+
|
|
340
|
+
# Get column names
|
|
341
|
+
column_names = [col[0] for col in cursor.description or []] # pyright: ignore[reportUnknownMemberType,reportUnknownVariableType]
|
|
342
|
+
|
|
343
|
+
if schema_type is not None:
|
|
344
|
+
return cast("ModelDTOT", schema_type(**dict(zip(column_names, result)))) # pyright: ignore[reportUnknownArgumentType]
|
|
345
|
+
# Always return dictionaries
|
|
346
|
+
return dict(zip(column_names, result)) # pyright: ignore[reportUnknownArgumentType,reportUnknownVariableType]
|
|
347
|
+
|
|
348
|
+
async def select_value(
|
|
349
|
+
self,
|
|
350
|
+
sql: str,
|
|
351
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
352
|
+
/,
|
|
353
|
+
connection: "Optional[AsyncConnection]" = None,
|
|
354
|
+
schema_type: "Optional[type[T]]" = None,
|
|
355
|
+
) -> "Union[T, Any]":
|
|
356
|
+
"""Fetch a single value from the database.
|
|
357
|
+
|
|
358
|
+
Returns:
|
|
359
|
+
The first value from the first row of results, or None if no results.
|
|
360
|
+
"""
|
|
361
|
+
connection = self._connection(connection)
|
|
362
|
+
sql, parameters = self._process_sql_params(sql, parameters)
|
|
363
|
+
|
|
364
|
+
async with self._with_cursor(connection) as cursor:
|
|
365
|
+
await cursor.execute(sql, parameters) # pyright: ignore[reportUnknownMemberType]
|
|
366
|
+
result = await cursor.fetchone() # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
|
|
367
|
+
result = self.check_not_found(result) # pyright: ignore[reportUnknownArgumentType]
|
|
368
|
+
|
|
369
|
+
if schema_type is None:
|
|
370
|
+
return result[0] # pyright: ignore[reportUnknownArgumentType]
|
|
371
|
+
return schema_type(result[0]) # type: ignore[call-arg]
|
|
372
|
+
|
|
373
|
+
async def select_value_or_none(
|
|
374
|
+
self,
|
|
375
|
+
sql: str,
|
|
376
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
377
|
+
/,
|
|
378
|
+
connection: "Optional[AsyncConnection]" = None,
|
|
379
|
+
schema_type: "Optional[type[T]]" = None,
|
|
380
|
+
) -> "Optional[Union[T, Any]]":
|
|
381
|
+
"""Fetch a single value from the database.
|
|
382
|
+
|
|
383
|
+
Returns:
|
|
384
|
+
The first value from the first row of results, or None if no results.
|
|
385
|
+
"""
|
|
386
|
+
connection = self._connection(connection)
|
|
387
|
+
sql, parameters = self._process_sql_params(sql, parameters)
|
|
388
|
+
|
|
389
|
+
async with self._with_cursor(connection) as cursor:
|
|
390
|
+
await cursor.execute(sql, parameters) # pyright: ignore[reportUnknownMemberType]
|
|
391
|
+
result = await cursor.fetchone() # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
|
|
392
|
+
|
|
393
|
+
if result is None:
|
|
394
|
+
return None
|
|
395
|
+
|
|
396
|
+
if schema_type is None:
|
|
397
|
+
return result[0] # pyright: ignore[reportUnknownArgumentType]
|
|
398
|
+
return schema_type(result[0]) # type: ignore[call-arg]
|
|
399
|
+
|
|
400
|
+
async def insert_update_delete(
|
|
401
|
+
self,
|
|
402
|
+
sql: str,
|
|
403
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
404
|
+
/,
|
|
405
|
+
connection: "Optional[AsyncConnection]" = None,
|
|
406
|
+
) -> int:
|
|
407
|
+
"""Insert, update, or delete data from the database.
|
|
408
|
+
|
|
409
|
+
Returns:
|
|
410
|
+
Row count affected by the operation.
|
|
411
|
+
"""
|
|
412
|
+
connection = self._connection(connection)
|
|
413
|
+
sql, parameters = self._process_sql_params(sql, parameters)
|
|
414
|
+
|
|
415
|
+
async with self._with_cursor(connection) as cursor:
|
|
416
|
+
await cursor.execute(sql, parameters) # pyright: ignore[reportUnknownMemberType]
|
|
417
|
+
return cursor.rowcount # pyright: ignore[reportUnknownMemberType]
|
|
418
|
+
|
|
419
|
+
async def insert_update_delete_returning(
|
|
420
|
+
self,
|
|
421
|
+
sql: str,
|
|
422
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
423
|
+
/,
|
|
424
|
+
connection: "Optional[AsyncConnection]" = None,
|
|
425
|
+
schema_type: "Optional[type[ModelDTOT]]" = None,
|
|
426
|
+
) -> "Optional[Union[dict[str, Any], ModelDTOT]]":
|
|
427
|
+
"""Insert, update, or delete data from the database and return result.
|
|
428
|
+
|
|
429
|
+
Returns:
|
|
430
|
+
The first row of results.
|
|
431
|
+
"""
|
|
432
|
+
connection = self._connection(connection)
|
|
433
|
+
sql, parameters = self._process_sql_params(sql, parameters)
|
|
434
|
+
|
|
435
|
+
async with self._with_cursor(connection) as cursor:
|
|
436
|
+
await cursor.execute(sql, parameters) # pyright: ignore[reportUnknownMemberType]
|
|
437
|
+
result = await cursor.fetchone() # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
|
|
438
|
+
|
|
439
|
+
if result is None:
|
|
440
|
+
return None
|
|
441
|
+
|
|
442
|
+
# Get column names
|
|
443
|
+
column_names = [col[0] for col in cursor.description or []] # pyright: ignore[reportUnknownMemberType,reportUnknownVariableType]
|
|
444
|
+
|
|
445
|
+
if schema_type is not None:
|
|
446
|
+
return cast("ModelDTOT", schema_type(**dict(zip(column_names, result)))) # pyright: ignore[reportUnknownArgumentType]
|
|
447
|
+
# Always return dictionaries
|
|
448
|
+
return dict(zip(column_names, result)) # pyright: ignore[reportUnknownArgumentType,reportUnknownVariableType]
|
|
449
|
+
|
|
450
|
+
async def execute_script(
|
|
451
|
+
self,
|
|
452
|
+
sql: str,
|
|
453
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
454
|
+
/,
|
|
455
|
+
connection: "Optional[AsyncConnection]" = None,
|
|
456
|
+
) -> str:
|
|
457
|
+
"""Execute a script.
|
|
458
|
+
|
|
459
|
+
Returns:
|
|
460
|
+
Status message for the operation.
|
|
461
|
+
"""
|
|
462
|
+
connection = self._connection(connection)
|
|
463
|
+
sql, parameters = self._process_sql_params(sql, parameters)
|
|
464
|
+
|
|
465
|
+
async with self._with_cursor(connection) as cursor:
|
|
466
|
+
await cursor.execute(sql, parameters) # pyright: ignore[reportUnknownMemberType]
|
|
467
|
+
return str(cursor.rowcount) # pyright: ignore[reportUnknownMemberType]
|
|
468
|
+
|
|
469
|
+
async def execute_script_returning(
|
|
470
|
+
self,
|
|
471
|
+
sql: str,
|
|
472
|
+
parameters: "Optional[StatementParameterType]" = None,
|
|
473
|
+
/,
|
|
474
|
+
connection: "Optional[AsyncConnection]" = None,
|
|
475
|
+
schema_type: "Optional[type[ModelDTOT]]" = None,
|
|
476
|
+
) -> "Optional[Union[dict[str, Any], ModelDTOT]]":
|
|
477
|
+
"""Execute a script and return result.
|
|
478
|
+
|
|
479
|
+
Returns:
|
|
480
|
+
The first row of results.
|
|
481
|
+
"""
|
|
482
|
+
connection = self._connection(connection)
|
|
483
|
+
sql, parameters = self._process_sql_params(sql, parameters)
|
|
484
|
+
|
|
485
|
+
async with self._with_cursor(connection) as cursor:
|
|
486
|
+
await cursor.execute(sql, parameters) # pyright: ignore[reportUnknownMemberType]
|
|
487
|
+
result = await cursor.fetchone() # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
|
|
488
|
+
|
|
489
|
+
if result is None:
|
|
490
|
+
return None
|
|
491
|
+
|
|
492
|
+
# Get column names
|
|
493
|
+
column_names = [col[0] for col in cursor.description or []] # pyright: ignore[reportUnknownMemberType,reportUnknownVariableType]
|
|
494
|
+
|
|
495
|
+
if schema_type is not None:
|
|
496
|
+
return cast("ModelDTOT", schema_type(**dict(zip(column_names, result)))) # pyright: ignore[reportUnknownArgumentType]
|
|
497
|
+
# Always return dictionaries
|
|
498
|
+
return dict(zip(column_names, result)) # pyright: ignore[reportUnknownArgumentType,reportUnknownVariableType]
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from sqlspec.adapters.psycopg.config import PsycopgAsync, PsycopgAsyncPool, PsycopgSync, PsycopgSyncPool
|
|
2
|
+
from sqlspec.adapters.psycopg.driver import PsycopgAsyncDriver, PsycopgSyncDriver
|
|
3
|
+
|
|
4
|
+
__all__ = (
|
|
5
|
+
"PsycopgAsync",
|
|
6
|
+
"PsycopgAsyncDriver",
|
|
7
|
+
"PsycopgAsyncPool",
|
|
8
|
+
"PsycopgSync",
|
|
9
|
+
"PsycopgSyncDriver",
|
|
10
|
+
"PsycopgSyncPool",
|
|
11
|
+
)
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
from sqlspec.adapters.psycopg.config._async import
|
|
2
|
-
from sqlspec.adapters.psycopg.config._sync import
|
|
1
|
+
from sqlspec.adapters.psycopg.config._async import PsycopgAsync, PsycopgAsyncPool
|
|
2
|
+
from sqlspec.adapters.psycopg.config._sync import PsycopgSync, PsycopgSyncPool
|
|
3
3
|
|
|
4
4
|
__all__ = (
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
5
|
+
"PsycopgAsync",
|
|
6
|
+
"PsycopgAsyncPool",
|
|
7
|
+
"PsycopgSync",
|
|
8
|
+
"PsycopgSyncPool",
|
|
9
9
|
)
|