sqlspec 0.16.1__cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of sqlspec might be problematic. Click here for more details.
- 51ff5a9eadfdefd49f98__mypyc.cpython-310-aarch64-linux-gnu.so +0 -0
- sqlspec/__init__.py +92 -0
- sqlspec/__main__.py +12 -0
- sqlspec/__metadata__.py +14 -0
- sqlspec/_serialization.py +77 -0
- sqlspec/_sql.py +1780 -0
- sqlspec/_typing.py +680 -0
- sqlspec/adapters/__init__.py +0 -0
- sqlspec/adapters/adbc/__init__.py +5 -0
- sqlspec/adapters/adbc/_types.py +12 -0
- sqlspec/adapters/adbc/config.py +361 -0
- sqlspec/adapters/adbc/driver.py +512 -0
- sqlspec/adapters/aiosqlite/__init__.py +19 -0
- sqlspec/adapters/aiosqlite/_types.py +13 -0
- sqlspec/adapters/aiosqlite/config.py +253 -0
- sqlspec/adapters/aiosqlite/driver.py +248 -0
- sqlspec/adapters/asyncmy/__init__.py +19 -0
- sqlspec/adapters/asyncmy/_types.py +12 -0
- sqlspec/adapters/asyncmy/config.py +180 -0
- sqlspec/adapters/asyncmy/driver.py +274 -0
- sqlspec/adapters/asyncpg/__init__.py +21 -0
- sqlspec/adapters/asyncpg/_types.py +17 -0
- sqlspec/adapters/asyncpg/config.py +229 -0
- sqlspec/adapters/asyncpg/driver.py +344 -0
- sqlspec/adapters/bigquery/__init__.py +18 -0
- sqlspec/adapters/bigquery/_types.py +12 -0
- sqlspec/adapters/bigquery/config.py +298 -0
- sqlspec/adapters/bigquery/driver.py +558 -0
- sqlspec/adapters/duckdb/__init__.py +22 -0
- sqlspec/adapters/duckdb/_types.py +12 -0
- sqlspec/adapters/duckdb/config.py +504 -0
- sqlspec/adapters/duckdb/driver.py +368 -0
- sqlspec/adapters/oracledb/__init__.py +32 -0
- sqlspec/adapters/oracledb/_types.py +14 -0
- sqlspec/adapters/oracledb/config.py +317 -0
- sqlspec/adapters/oracledb/driver.py +538 -0
- sqlspec/adapters/psqlpy/__init__.py +16 -0
- sqlspec/adapters/psqlpy/_types.py +11 -0
- sqlspec/adapters/psqlpy/config.py +214 -0
- sqlspec/adapters/psqlpy/driver.py +530 -0
- sqlspec/adapters/psycopg/__init__.py +32 -0
- sqlspec/adapters/psycopg/_types.py +17 -0
- sqlspec/adapters/psycopg/config.py +426 -0
- sqlspec/adapters/psycopg/driver.py +796 -0
- sqlspec/adapters/sqlite/__init__.py +15 -0
- sqlspec/adapters/sqlite/_types.py +11 -0
- sqlspec/adapters/sqlite/config.py +240 -0
- sqlspec/adapters/sqlite/driver.py +294 -0
- sqlspec/base.py +571 -0
- sqlspec/builder/__init__.py +62 -0
- sqlspec/builder/_base.py +473 -0
- sqlspec/builder/_column.py +320 -0
- sqlspec/builder/_ddl.py +1346 -0
- sqlspec/builder/_ddl_utils.py +103 -0
- sqlspec/builder/_delete.py +76 -0
- sqlspec/builder/_insert.py +256 -0
- sqlspec/builder/_merge.py +71 -0
- sqlspec/builder/_parsing_utils.py +140 -0
- sqlspec/builder/_select.py +170 -0
- sqlspec/builder/_update.py +188 -0
- sqlspec/builder/mixins/__init__.py +55 -0
- sqlspec/builder/mixins/_cte_and_set_ops.py +222 -0
- sqlspec/builder/mixins/_delete_operations.py +41 -0
- sqlspec/builder/mixins/_insert_operations.py +244 -0
- sqlspec/builder/mixins/_join_operations.py +122 -0
- sqlspec/builder/mixins/_merge_operations.py +476 -0
- sqlspec/builder/mixins/_order_limit_operations.py +135 -0
- sqlspec/builder/mixins/_pivot_operations.py +153 -0
- sqlspec/builder/mixins/_select_operations.py +603 -0
- sqlspec/builder/mixins/_update_operations.py +187 -0
- sqlspec/builder/mixins/_where_clause.py +621 -0
- sqlspec/cli.py +247 -0
- sqlspec/config.py +395 -0
- sqlspec/core/__init__.py +63 -0
- sqlspec/core/cache.cpython-310-aarch64-linux-gnu.so +0 -0
- sqlspec/core/cache.py +871 -0
- sqlspec/core/compiler.cpython-310-aarch64-linux-gnu.so +0 -0
- sqlspec/core/compiler.py +417 -0
- sqlspec/core/filters.cpython-310-aarch64-linux-gnu.so +0 -0
- sqlspec/core/filters.py +830 -0
- sqlspec/core/hashing.cpython-310-aarch64-linux-gnu.so +0 -0
- sqlspec/core/hashing.py +310 -0
- sqlspec/core/parameters.cpython-310-aarch64-linux-gnu.so +0 -0
- sqlspec/core/parameters.py +1237 -0
- sqlspec/core/result.cpython-310-aarch64-linux-gnu.so +0 -0
- sqlspec/core/result.py +677 -0
- sqlspec/core/splitter.cpython-310-aarch64-linux-gnu.so +0 -0
- sqlspec/core/splitter.py +819 -0
- sqlspec/core/statement.cpython-310-aarch64-linux-gnu.so +0 -0
- sqlspec/core/statement.py +676 -0
- sqlspec/driver/__init__.py +19 -0
- sqlspec/driver/_async.py +502 -0
- sqlspec/driver/_common.py +631 -0
- sqlspec/driver/_sync.py +503 -0
- sqlspec/driver/mixins/__init__.py +6 -0
- sqlspec/driver/mixins/_result_tools.py +193 -0
- sqlspec/driver/mixins/_sql_translator.py +86 -0
- sqlspec/exceptions.py +193 -0
- sqlspec/extensions/__init__.py +0 -0
- sqlspec/extensions/aiosql/__init__.py +10 -0
- sqlspec/extensions/aiosql/adapter.py +461 -0
- sqlspec/extensions/litestar/__init__.py +6 -0
- sqlspec/extensions/litestar/_utils.py +52 -0
- sqlspec/extensions/litestar/cli.py +48 -0
- sqlspec/extensions/litestar/config.py +92 -0
- sqlspec/extensions/litestar/handlers.py +260 -0
- sqlspec/extensions/litestar/plugin.py +145 -0
- sqlspec/extensions/litestar/providers.py +454 -0
- sqlspec/loader.cpython-310-aarch64-linux-gnu.so +0 -0
- sqlspec/loader.py +760 -0
- sqlspec/migrations/__init__.py +35 -0
- sqlspec/migrations/base.py +414 -0
- sqlspec/migrations/commands.py +443 -0
- sqlspec/migrations/loaders.py +402 -0
- sqlspec/migrations/runner.py +213 -0
- sqlspec/migrations/tracker.py +140 -0
- sqlspec/migrations/utils.py +129 -0
- sqlspec/protocols.py +407 -0
- sqlspec/py.typed +0 -0
- sqlspec/storage/__init__.py +23 -0
- sqlspec/storage/backends/__init__.py +0 -0
- sqlspec/storage/backends/base.py +163 -0
- sqlspec/storage/backends/fsspec.py +386 -0
- sqlspec/storage/backends/obstore.py +459 -0
- sqlspec/storage/capabilities.py +102 -0
- sqlspec/storage/registry.py +239 -0
- sqlspec/typing.py +299 -0
- sqlspec/utils/__init__.py +3 -0
- sqlspec/utils/correlation.py +150 -0
- sqlspec/utils/deprecation.py +106 -0
- sqlspec/utils/fixtures.cpython-310-aarch64-linux-gnu.so +0 -0
- sqlspec/utils/fixtures.py +58 -0
- sqlspec/utils/logging.py +127 -0
- sqlspec/utils/module_loader.py +89 -0
- sqlspec/utils/serializers.py +4 -0
- sqlspec/utils/singleton.py +32 -0
- sqlspec/utils/sync_tools.cpython-310-aarch64-linux-gnu.so +0 -0
- sqlspec/utils/sync_tools.py +237 -0
- sqlspec/utils/text.cpython-310-aarch64-linux-gnu.so +0 -0
- sqlspec/utils/text.py +96 -0
- sqlspec/utils/type_guards.cpython-310-aarch64-linux-gnu.so +0 -0
- sqlspec/utils/type_guards.py +1139 -0
- sqlspec-0.16.1.dist-info/METADATA +365 -0
- sqlspec-0.16.1.dist-info/RECORD +148 -0
- sqlspec-0.16.1.dist-info/WHEEL +7 -0
- sqlspec-0.16.1.dist-info/entry_points.txt +2 -0
- sqlspec-0.16.1.dist-info/licenses/LICENSE +21 -0
- sqlspec-0.16.1.dist-info/licenses/NOTICE +29 -0
sqlspec/driver/_sync.py
ADDED
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
"""Synchronous driver protocol implementation.
|
|
2
|
+
|
|
3
|
+
This module provides the sync driver infrastructure for database adapters,
|
|
4
|
+
including connection management, transaction support, and result processing.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from abc import abstractmethod
|
|
8
|
+
from typing import TYPE_CHECKING, Any, Final, NoReturn, Optional, Union, cast, overload
|
|
9
|
+
|
|
10
|
+
from sqlspec.core import SQL
|
|
11
|
+
from sqlspec.driver._common import CommonDriverAttributesMixin, ExecutionResult
|
|
12
|
+
from sqlspec.driver.mixins import SQLTranslatorMixin, ToSchemaMixin
|
|
13
|
+
from sqlspec.exceptions import NotFoundError
|
|
14
|
+
from sqlspec.utils.logging import get_logger
|
|
15
|
+
from sqlspec.utils.type_guards import is_dict_row, is_indexable_row
|
|
16
|
+
|
|
17
|
+
if TYPE_CHECKING:
|
|
18
|
+
from collections.abc import Sequence
|
|
19
|
+
from contextlib import AbstractContextManager
|
|
20
|
+
|
|
21
|
+
from sqlspec.builder import QueryBuilder
|
|
22
|
+
from sqlspec.core import SQLResult, Statement, StatementConfig, StatementFilter
|
|
23
|
+
from sqlspec.typing import ModelDTOT, StatementParameters
|
|
24
|
+
|
|
25
|
+
_LOGGER_NAME: Final[str] = "sqlspec"
|
|
26
|
+
logger = get_logger(_LOGGER_NAME)
|
|
27
|
+
|
|
28
|
+
__all__ = ("SyncDriverAdapterBase",)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
EMPTY_FILTERS: Final["list[StatementFilter]"] = []
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class SyncDriverAdapterBase(CommonDriverAttributesMixin, SQLTranslatorMixin, ToSchemaMixin):
|
|
35
|
+
"""Base class for synchronous database drivers.
|
|
36
|
+
|
|
37
|
+
Provides the foundation for sync database adapters, including connection management,
|
|
38
|
+
transaction support, and SQL execution methods. All database operations are performed
|
|
39
|
+
synchronously and support context manager patterns for proper resource cleanup.
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
__slots__ = ()
|
|
43
|
+
|
|
44
|
+
def dispatch_statement_execution(self, statement: "SQL", connection: "Any") -> "SQLResult":
|
|
45
|
+
"""Central execution dispatcher using the Template Method Pattern.
|
|
46
|
+
|
|
47
|
+
Orchestrates the common execution flow, delegating database-specific steps
|
|
48
|
+
to abstract methods that concrete adapters must implement.
|
|
49
|
+
All database operations are wrapped in exception handling.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
statement: The SQL statement to execute
|
|
53
|
+
connection: The database connection to use
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
The result of the SQL execution
|
|
57
|
+
"""
|
|
58
|
+
with self.handle_database_exceptions(), self.with_cursor(connection) as cursor:
|
|
59
|
+
special_result = self._try_special_handling(cursor, statement)
|
|
60
|
+
if special_result is not None:
|
|
61
|
+
return special_result
|
|
62
|
+
|
|
63
|
+
if statement.is_script:
|
|
64
|
+
execution_result = self._execute_script(cursor, statement)
|
|
65
|
+
elif statement.is_many:
|
|
66
|
+
execution_result = self._execute_many(cursor, statement)
|
|
67
|
+
else:
|
|
68
|
+
execution_result = self._execute_statement(cursor, statement)
|
|
69
|
+
|
|
70
|
+
return self.build_statement_result(statement, execution_result)
|
|
71
|
+
|
|
72
|
+
@abstractmethod
|
|
73
|
+
def with_cursor(self, connection: Any) -> Any:
|
|
74
|
+
"""Create and return a context manager for cursor acquisition and cleanup.
|
|
75
|
+
|
|
76
|
+
Returns a context manager that yields a cursor for database operations.
|
|
77
|
+
Concrete implementations handle database-specific cursor creation and cleanup.
|
|
78
|
+
"""
|
|
79
|
+
|
|
80
|
+
@abstractmethod
|
|
81
|
+
def handle_database_exceptions(self) -> "AbstractContextManager[None]":
|
|
82
|
+
"""Handle database-specific exceptions and wrap them appropriately.
|
|
83
|
+
|
|
84
|
+
Returns:
|
|
85
|
+
ContextManager that can be used in with statements
|
|
86
|
+
"""
|
|
87
|
+
|
|
88
|
+
@abstractmethod
|
|
89
|
+
def begin(self) -> None:
|
|
90
|
+
"""Begin a database transaction on the current connection."""
|
|
91
|
+
|
|
92
|
+
@abstractmethod
|
|
93
|
+
def rollback(self) -> None:
|
|
94
|
+
"""Rollback the current transaction on the current connection."""
|
|
95
|
+
|
|
96
|
+
@abstractmethod
|
|
97
|
+
def commit(self) -> None:
|
|
98
|
+
"""Commit the current transaction on the current connection."""
|
|
99
|
+
|
|
100
|
+
@abstractmethod
|
|
101
|
+
def _try_special_handling(self, cursor: Any, statement: "SQL") -> "Optional[SQLResult]":
|
|
102
|
+
"""Hook for database-specific special operations (e.g., PostgreSQL COPY, bulk operations).
|
|
103
|
+
|
|
104
|
+
This method is called first in dispatch_statement_execution() to allow drivers to handle
|
|
105
|
+
special operations that don't follow the standard SQL execution pattern.
|
|
106
|
+
|
|
107
|
+
Args:
|
|
108
|
+
cursor: Database cursor/connection object
|
|
109
|
+
statement: SQL statement to analyze
|
|
110
|
+
|
|
111
|
+
Returns:
|
|
112
|
+
SQLResult if the special operation was handled and completed,
|
|
113
|
+
None if standard execution should proceed
|
|
114
|
+
"""
|
|
115
|
+
|
|
116
|
+
def _execute_script(self, cursor: Any, statement: "SQL") -> ExecutionResult:
|
|
117
|
+
"""Execute a SQL script containing multiple statements.
|
|
118
|
+
|
|
119
|
+
Default implementation splits the script and executes statements individually.
|
|
120
|
+
Drivers can override for database-specific script execution methods.
|
|
121
|
+
|
|
122
|
+
Args:
|
|
123
|
+
cursor: Database cursor/connection object
|
|
124
|
+
statement: SQL statement object with all necessary data and configuration
|
|
125
|
+
|
|
126
|
+
Returns:
|
|
127
|
+
ExecutionResult with script execution data including statement counts
|
|
128
|
+
"""
|
|
129
|
+
sql, prepared_parameters = self._get_compiled_sql(statement, self.statement_config)
|
|
130
|
+
statements = self.split_script_statements(sql, self.statement_config, strip_trailing_semicolon=True)
|
|
131
|
+
|
|
132
|
+
statement_count: int = len(statements)
|
|
133
|
+
successful_count: int = 0
|
|
134
|
+
|
|
135
|
+
for stmt in statements:
|
|
136
|
+
single_stmt = statement.copy(statement=stmt, parameters=prepared_parameters)
|
|
137
|
+
self._execute_statement(cursor, single_stmt)
|
|
138
|
+
successful_count += 1
|
|
139
|
+
|
|
140
|
+
return self.create_execution_result(
|
|
141
|
+
cursor, statement_count=statement_count, successful_statements=successful_count, is_script_result=True
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
@abstractmethod
|
|
145
|
+
def _execute_many(self, cursor: Any, statement: "SQL") -> ExecutionResult:
|
|
146
|
+
"""Execute SQL with multiple parameter sets (executemany).
|
|
147
|
+
|
|
148
|
+
Must be implemented by each driver for database-specific executemany logic.
|
|
149
|
+
|
|
150
|
+
Args:
|
|
151
|
+
cursor: Database cursor/connection object
|
|
152
|
+
statement: SQL statement object with all necessary data and configuration
|
|
153
|
+
|
|
154
|
+
Returns:
|
|
155
|
+
ExecutionResult with execution data for the many operation
|
|
156
|
+
"""
|
|
157
|
+
|
|
158
|
+
@abstractmethod
|
|
159
|
+
def _execute_statement(self, cursor: Any, statement: "SQL") -> ExecutionResult:
|
|
160
|
+
"""Execute a single SQL statement.
|
|
161
|
+
|
|
162
|
+
Must be implemented by each driver for database-specific execution logic.
|
|
163
|
+
|
|
164
|
+
Args:
|
|
165
|
+
cursor: Database cursor/connection object
|
|
166
|
+
statement: SQL statement object with all necessary data and configuration
|
|
167
|
+
|
|
168
|
+
Returns:
|
|
169
|
+
ExecutionResult with execution data
|
|
170
|
+
"""
|
|
171
|
+
|
|
172
|
+
def execute(
|
|
173
|
+
self,
|
|
174
|
+
statement: "Union[SQL, Statement, QueryBuilder]",
|
|
175
|
+
/,
|
|
176
|
+
*parameters: "Union[StatementParameters, StatementFilter]",
|
|
177
|
+
statement_config: "Optional[StatementConfig]" = None,
|
|
178
|
+
**kwargs: Any,
|
|
179
|
+
) -> "SQLResult":
|
|
180
|
+
"""Execute a statement with parameter handling."""
|
|
181
|
+
sql_statement = self.prepare_statement(
|
|
182
|
+
statement, parameters, statement_config=statement_config or self.statement_config, kwargs=kwargs
|
|
183
|
+
)
|
|
184
|
+
return self.dispatch_statement_execution(statement=sql_statement, connection=self.connection)
|
|
185
|
+
|
|
186
|
+
def execute_many(
|
|
187
|
+
self,
|
|
188
|
+
statement: "Union[SQL, Statement, QueryBuilder]",
|
|
189
|
+
/,
|
|
190
|
+
parameters: "Sequence[StatementParameters]",
|
|
191
|
+
*filters: "Union[StatementParameters, StatementFilter]",
|
|
192
|
+
statement_config: "Optional[StatementConfig]" = None,
|
|
193
|
+
**kwargs: Any,
|
|
194
|
+
) -> "SQLResult":
|
|
195
|
+
"""Execute statement multiple times with different parameters.
|
|
196
|
+
|
|
197
|
+
Parameters passed will be used as the batch execution sequence.
|
|
198
|
+
"""
|
|
199
|
+
config = statement_config or self.statement_config
|
|
200
|
+
|
|
201
|
+
if isinstance(statement, SQL):
|
|
202
|
+
sql_statement = SQL(statement._raw_sql, parameters, statement_config=config, is_many=True, **kwargs)
|
|
203
|
+
else:
|
|
204
|
+
base_statement = self.prepare_statement(statement, filters, statement_config=config, kwargs=kwargs)
|
|
205
|
+
sql_statement = SQL(base_statement._raw_sql, parameters, statement_config=config, is_many=True, **kwargs)
|
|
206
|
+
|
|
207
|
+
return self.dispatch_statement_execution(statement=sql_statement, connection=self.connection)
|
|
208
|
+
|
|
209
|
+
def execute_script(
|
|
210
|
+
self,
|
|
211
|
+
statement: "Union[str, SQL]",
|
|
212
|
+
/,
|
|
213
|
+
*parameters: "Union[StatementParameters, StatementFilter]",
|
|
214
|
+
statement_config: "Optional[StatementConfig]" = None,
|
|
215
|
+
**kwargs: Any,
|
|
216
|
+
) -> "SQLResult":
|
|
217
|
+
"""Execute a multi-statement script.
|
|
218
|
+
|
|
219
|
+
By default, validates each statement and logs warnings for dangerous
|
|
220
|
+
operations. Use suppress_warnings=True for migrations and admin scripts.
|
|
221
|
+
"""
|
|
222
|
+
config = statement_config or self.statement_config
|
|
223
|
+
sql_statement = self.prepare_statement(statement, parameters, statement_config=config, kwargs=kwargs)
|
|
224
|
+
|
|
225
|
+
return self.dispatch_statement_execution(statement=sql_statement.as_script(), connection=self.connection)
|
|
226
|
+
|
|
227
|
+
@overload
|
|
228
|
+
def select_one(
|
|
229
|
+
self,
|
|
230
|
+
statement: "Union[Statement, QueryBuilder]",
|
|
231
|
+
/,
|
|
232
|
+
*parameters: "Union[StatementParameters, StatementFilter]",
|
|
233
|
+
schema_type: "type[ModelDTOT]",
|
|
234
|
+
statement_config: "Optional[StatementConfig]" = None,
|
|
235
|
+
**kwargs: Any,
|
|
236
|
+
) -> "ModelDTOT": ...
|
|
237
|
+
|
|
238
|
+
@overload
|
|
239
|
+
def select_one(
|
|
240
|
+
self,
|
|
241
|
+
statement: "Union[Statement, QueryBuilder]",
|
|
242
|
+
/,
|
|
243
|
+
*parameters: "Union[StatementParameters, StatementFilter]",
|
|
244
|
+
schema_type: None = None,
|
|
245
|
+
statement_config: "Optional[StatementConfig]" = None,
|
|
246
|
+
**kwargs: Any,
|
|
247
|
+
) -> "dict[str, Any]": ...
|
|
248
|
+
|
|
249
|
+
def select_one(
|
|
250
|
+
self,
|
|
251
|
+
statement: "Union[Statement, QueryBuilder]",
|
|
252
|
+
/,
|
|
253
|
+
*parameters: "Union[StatementParameters, StatementFilter]",
|
|
254
|
+
schema_type: "Optional[type[ModelDTOT]]" = None,
|
|
255
|
+
statement_config: "Optional[StatementConfig]" = None,
|
|
256
|
+
**kwargs: Any,
|
|
257
|
+
) -> "Union[dict[str, Any], ModelDTOT]":
|
|
258
|
+
"""Execute a select statement and return exactly one row.
|
|
259
|
+
|
|
260
|
+
Raises an exception if no rows or more than one row is returned.
|
|
261
|
+
"""
|
|
262
|
+
result = self.execute(statement, *parameters, statement_config=statement_config, **kwargs)
|
|
263
|
+
data = result.get_data()
|
|
264
|
+
data_len: int = len(data)
|
|
265
|
+
if data_len == 0:
|
|
266
|
+
self._raise_no_rows_found()
|
|
267
|
+
if data_len > 1:
|
|
268
|
+
self._raise_expected_one_row(data_len)
|
|
269
|
+
first_row = data[0]
|
|
270
|
+
return self.to_schema(first_row, schema_type=schema_type) if schema_type else first_row
|
|
271
|
+
|
|
272
|
+
@overload
|
|
273
|
+
def select_one_or_none(
|
|
274
|
+
self,
|
|
275
|
+
statement: "Union[Statement, QueryBuilder]",
|
|
276
|
+
/,
|
|
277
|
+
*parameters: "Union[StatementParameters, StatementFilter]",
|
|
278
|
+
schema_type: "type[ModelDTOT]",
|
|
279
|
+
statement_config: "Optional[StatementConfig]" = None,
|
|
280
|
+
**kwargs: Any,
|
|
281
|
+
) -> "Optional[ModelDTOT]": ...
|
|
282
|
+
|
|
283
|
+
@overload
|
|
284
|
+
def select_one_or_none(
|
|
285
|
+
self,
|
|
286
|
+
statement: "Union[Statement, QueryBuilder]",
|
|
287
|
+
/,
|
|
288
|
+
*parameters: "Union[StatementParameters, StatementFilter]",
|
|
289
|
+
schema_type: None = None,
|
|
290
|
+
statement_config: "Optional[StatementConfig]" = None,
|
|
291
|
+
**kwargs: Any,
|
|
292
|
+
) -> "Optional[dict[str, Any]]": ...
|
|
293
|
+
|
|
294
|
+
def select_one_or_none(
|
|
295
|
+
self,
|
|
296
|
+
statement: "Union[Statement, QueryBuilder]",
|
|
297
|
+
/,
|
|
298
|
+
*parameters: "Union[StatementParameters, StatementFilter]",
|
|
299
|
+
schema_type: "Optional[type[ModelDTOT]]" = None,
|
|
300
|
+
statement_config: "Optional[StatementConfig]" = None,
|
|
301
|
+
**kwargs: Any,
|
|
302
|
+
) -> "Optional[Union[dict[str, Any], ModelDTOT]]":
|
|
303
|
+
"""Execute a select statement and return at most one row.
|
|
304
|
+
|
|
305
|
+
Returns None if no rows are found.
|
|
306
|
+
Raises an exception if more than one row is returned.
|
|
307
|
+
"""
|
|
308
|
+
result = self.execute(statement, *parameters, statement_config=statement_config, **kwargs)
|
|
309
|
+
data = result.get_data()
|
|
310
|
+
data_len: int = len(data)
|
|
311
|
+
if data_len == 0:
|
|
312
|
+
return None
|
|
313
|
+
if data_len > 1:
|
|
314
|
+
self._raise_expected_at_most_one_row(data_len)
|
|
315
|
+
first_row = data[0]
|
|
316
|
+
return cast(
|
|
317
|
+
"Optional[Union[dict[str, Any], ModelDTOT]]",
|
|
318
|
+
self.to_schema(first_row, schema_type=schema_type) if schema_type else first_row,
|
|
319
|
+
)
|
|
320
|
+
|
|
321
|
+
@overload
|
|
322
|
+
def select(
|
|
323
|
+
self,
|
|
324
|
+
statement: "Union[Statement, QueryBuilder]",
|
|
325
|
+
/,
|
|
326
|
+
*parameters: "Union[StatementParameters, StatementFilter]",
|
|
327
|
+
schema_type: "type[ModelDTOT]",
|
|
328
|
+
statement_config: "Optional[StatementConfig]" = None,
|
|
329
|
+
**kwargs: Any,
|
|
330
|
+
) -> "list[ModelDTOT]": ...
|
|
331
|
+
|
|
332
|
+
@overload
|
|
333
|
+
def select(
|
|
334
|
+
self,
|
|
335
|
+
statement: "Union[Statement, QueryBuilder]",
|
|
336
|
+
/,
|
|
337
|
+
*parameters: "Union[StatementParameters, StatementFilter]",
|
|
338
|
+
schema_type: None = None,
|
|
339
|
+
statement_config: "Optional[StatementConfig]" = None,
|
|
340
|
+
**kwargs: Any,
|
|
341
|
+
) -> "list[dict[str, Any]]": ...
|
|
342
|
+
|
|
343
|
+
def select(
|
|
344
|
+
self,
|
|
345
|
+
statement: "Union[Statement, QueryBuilder]",
|
|
346
|
+
/,
|
|
347
|
+
*parameters: "Union[StatementParameters, StatementFilter]",
|
|
348
|
+
schema_type: "Optional[type[ModelDTOT]]" = None,
|
|
349
|
+
statement_config: "Optional[StatementConfig]" = None,
|
|
350
|
+
**kwargs: Any,
|
|
351
|
+
) -> "Union[list[dict[str, Any]], list[ModelDTOT]]":
|
|
352
|
+
"""Execute a select statement and return all rows."""
|
|
353
|
+
result = self.execute(statement, *parameters, statement_config=statement_config, **kwargs)
|
|
354
|
+
return cast(
|
|
355
|
+
"Union[list[dict[str, Any]], list[ModelDTOT]]", self.to_schema(result.get_data(), schema_type=schema_type)
|
|
356
|
+
)
|
|
357
|
+
|
|
358
|
+
def select_value(
|
|
359
|
+
self,
|
|
360
|
+
statement: "Union[Statement, QueryBuilder]",
|
|
361
|
+
/,
|
|
362
|
+
*parameters: "Union[StatementParameters, StatementFilter]",
|
|
363
|
+
statement_config: "Optional[StatementConfig]" = None,
|
|
364
|
+
**kwargs: Any,
|
|
365
|
+
) -> Any:
|
|
366
|
+
"""Execute a select statement and return a single scalar value.
|
|
367
|
+
|
|
368
|
+
Expects exactly one row with one column.
|
|
369
|
+
Raises an exception if no rows or more than one row/column is returned.
|
|
370
|
+
"""
|
|
371
|
+
result = self.execute(statement, *parameters, statement_config=statement_config, **kwargs)
|
|
372
|
+
try:
|
|
373
|
+
row = result.one()
|
|
374
|
+
except ValueError as e:
|
|
375
|
+
self._raise_no_rows_found_from_exception(e)
|
|
376
|
+
if not row:
|
|
377
|
+
self._raise_no_rows_found()
|
|
378
|
+
if is_dict_row(row):
|
|
379
|
+
if not row:
|
|
380
|
+
self._raise_row_no_columns()
|
|
381
|
+
return next(iter(row.values()))
|
|
382
|
+
if is_indexable_row(row):
|
|
383
|
+
if not row:
|
|
384
|
+
self._raise_row_no_columns()
|
|
385
|
+
return row[0]
|
|
386
|
+
self._raise_unexpected_row_type(type(row))
|
|
387
|
+
return None
|
|
388
|
+
|
|
389
|
+
def select_value_or_none(
|
|
390
|
+
self,
|
|
391
|
+
statement: "Union[Statement, QueryBuilder]",
|
|
392
|
+
/,
|
|
393
|
+
*parameters: "Union[StatementParameters, StatementFilter]",
|
|
394
|
+
statement_config: "Optional[StatementConfig]" = None,
|
|
395
|
+
**kwargs: Any,
|
|
396
|
+
) -> Any:
|
|
397
|
+
"""Execute a select statement and return a single scalar value or None.
|
|
398
|
+
|
|
399
|
+
Returns None if no rows are found.
|
|
400
|
+
Expects at most one row with one column.
|
|
401
|
+
Raises an exception if more than one row is returned.
|
|
402
|
+
"""
|
|
403
|
+
result = self.execute(statement, *parameters, statement_config=statement_config, **kwargs)
|
|
404
|
+
data = result.get_data()
|
|
405
|
+
data_len: int = len(data)
|
|
406
|
+
if data_len == 0:
|
|
407
|
+
return None
|
|
408
|
+
if data_len > 1:
|
|
409
|
+
msg = f"Expected at most one row, found {data_len}"
|
|
410
|
+
raise ValueError(msg)
|
|
411
|
+
row = data[0]
|
|
412
|
+
if isinstance(row, dict):
|
|
413
|
+
if not row:
|
|
414
|
+
return None
|
|
415
|
+
return next(iter(row.values()))
|
|
416
|
+
if isinstance(row, (tuple, list)):
|
|
417
|
+
return row[0]
|
|
418
|
+
msg = f"Cannot extract value from row type {type(row).__name__}"
|
|
419
|
+
raise TypeError(msg)
|
|
420
|
+
|
|
421
|
+
@overload
|
|
422
|
+
def select_with_total(
|
|
423
|
+
self,
|
|
424
|
+
statement: "Union[Statement, QueryBuilder]",
|
|
425
|
+
/,
|
|
426
|
+
*parameters: "Union[StatementParameters, StatementFilter]",
|
|
427
|
+
schema_type: "type[ModelDTOT]",
|
|
428
|
+
statement_config: "Optional[StatementConfig]" = None,
|
|
429
|
+
**kwargs: Any,
|
|
430
|
+
) -> "tuple[list[ModelDTOT], int]": ...
|
|
431
|
+
|
|
432
|
+
@overload
|
|
433
|
+
def select_with_total(
|
|
434
|
+
self,
|
|
435
|
+
statement: "Union[Statement, QueryBuilder]",
|
|
436
|
+
/,
|
|
437
|
+
*parameters: "Union[StatementParameters, StatementFilter]",
|
|
438
|
+
schema_type: None = None,
|
|
439
|
+
statement_config: "Optional[StatementConfig]" = None,
|
|
440
|
+
**kwargs: Any,
|
|
441
|
+
) -> "tuple[list[dict[str, Any]], int]": ...
|
|
442
|
+
|
|
443
|
+
def select_with_total(
|
|
444
|
+
self,
|
|
445
|
+
statement: "Union[Statement, QueryBuilder]",
|
|
446
|
+
/,
|
|
447
|
+
*parameters: "Union[StatementParameters, StatementFilter]",
|
|
448
|
+
schema_type: "Optional[type[ModelDTOT]]" = None,
|
|
449
|
+
statement_config: "Optional[StatementConfig]" = None,
|
|
450
|
+
**kwargs: Any,
|
|
451
|
+
) -> "tuple[Union[list[dict[str, Any]], list[ModelDTOT]], int]":
|
|
452
|
+
"""Execute a select statement and return both the data and total count.
|
|
453
|
+
|
|
454
|
+
This method is designed for pagination scenarios where you need both
|
|
455
|
+
the current page of data and the total number of rows that match the query.
|
|
456
|
+
|
|
457
|
+
Args:
|
|
458
|
+
statement: The SQL statement, QueryBuilder, or raw SQL string
|
|
459
|
+
*parameters: Parameters for the SQL statement
|
|
460
|
+
schema_type: Optional schema type for data transformation
|
|
461
|
+
statement_config: Optional SQL configuration
|
|
462
|
+
**kwargs: Additional keyword arguments
|
|
463
|
+
|
|
464
|
+
Returns:
|
|
465
|
+
A tuple containing:
|
|
466
|
+
- List of data rows (transformed by schema_type if provided)
|
|
467
|
+
- Total count of rows matching the query (ignoring LIMIT/OFFSET)
|
|
468
|
+
"""
|
|
469
|
+
sql_statement = self.prepare_statement(
|
|
470
|
+
statement, parameters, statement_config=statement_config or self.statement_config, kwargs=kwargs
|
|
471
|
+
)
|
|
472
|
+
count_result = self.dispatch_statement_execution(self._create_count_query(sql_statement), self.connection)
|
|
473
|
+
select_result = self.execute(sql_statement)
|
|
474
|
+
|
|
475
|
+
return (self.to_schema(select_result.get_data(), schema_type=schema_type), count_result.scalar())
|
|
476
|
+
|
|
477
|
+
def _raise_no_rows_found(self) -> NoReturn:
|
|
478
|
+
msg = "No rows found"
|
|
479
|
+
raise NotFoundError(msg)
|
|
480
|
+
|
|
481
|
+
def _raise_no_rows_found_from_exception(self, e: ValueError) -> NoReturn:
|
|
482
|
+
msg = "No rows found"
|
|
483
|
+
raise NotFoundError(msg) from e
|
|
484
|
+
|
|
485
|
+
def _raise_expected_one_row(self, data_len: int) -> NoReturn:
|
|
486
|
+
msg = f"Expected exactly one row, found {data_len}"
|
|
487
|
+
raise ValueError(msg)
|
|
488
|
+
|
|
489
|
+
def _raise_expected_at_most_one_row(self, data_len: int) -> NoReturn:
|
|
490
|
+
msg = f"Expected at most one row, found {data_len}"
|
|
491
|
+
raise ValueError(msg)
|
|
492
|
+
|
|
493
|
+
def _raise_row_no_columns(self) -> NoReturn:
|
|
494
|
+
msg = "Row has no columns"
|
|
495
|
+
raise ValueError(msg)
|
|
496
|
+
|
|
497
|
+
def _raise_unexpected_row_type(self, row_type: type) -> NoReturn:
|
|
498
|
+
msg = f"Unexpected row type: {row_type}"
|
|
499
|
+
raise ValueError(msg)
|
|
500
|
+
|
|
501
|
+
def _raise_cannot_extract_value_from_row_type(self, type_name: str) -> NoReturn:
|
|
502
|
+
msg = f"Cannot extract value from row type {type_name}"
|
|
503
|
+
raise TypeError(msg)
|