sqlspec 0.16.0__cp310-cp310-macosx_14_0_arm64.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.

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