-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathsqlalchemy.py
More file actions
586 lines (461 loc) Β· 17.6 KB
/
sqlalchemy.py
File metadata and controls
586 lines (461 loc) Β· 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
from __future__ import annotations
__all__ = [
"Selectable",
"apaginate",
"create_count_query",
"create_count_query_from_text",
"create_paginate_query",
"create_paginate_query_from_text",
"paginate",
]
import warnings
from collections.abc import Sequence
from contextlib import suppress
from functools import partial
from typing import TYPE_CHECKING, Any, Literal, TypeAlias, TypeVar, cast, overload
from sqlalchemy import func, select, text
from sqlalchemy.engine import Connection
from sqlalchemy.exc import InvalidRequestError
from sqlalchemy.orm import Query, Session, noload, scoped_session
from sqlalchemy.sql import CompoundSelect, Select
from sqlalchemy.sql.elements import TextClause
from typing_extensions import deprecated
from fastapi_pagination.api import create_page
from fastapi_pagination.bases import AbstractParams, CursorRawParams, RawParams
from fastapi_pagination.config import Config
from fastapi_pagination.flow import flow, run_async_flow, run_sync_flow
from fastapi_pagination.flows import CursorFlow, LimitOffsetFlow, TotalFlow, generic_flow
from fastapi_pagination.types import AdditionalData, AsyncItemsTransformer, ItemsTransformer, SyncItemsTransformer
from .raw_sql import create_count_query_from_text as _create_count_query_from_text
from .raw_sql import create_paginate_query_from_text as _create_paginate_query_from_text
from .utils import generic_query_apply_params, unwrap_scalars
if TYPE_CHECKING:
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncSession
try:
from typing import TypeVarTuple, Unpack # ty:ignore[unresolved-import]
except ImportError:
from typing_extensions import TypeVarTuple, Unpack
try:
from sqlalchemy.orm import FromStatement
except ImportError: # pragma: no cover
class FromStatement:
element: Any
def __init__(self, *args: Any, **kwargs: Any) -> None:
raise ImportError("sqlalchemy.orm.FromStatement is not available")
try:
from sqlalchemy.util import await_only, greenlet_spawn
except ImportError: # pragma: no cover
async def greenlet_spawn(*_: Any, **__: Any) -> Any:
raise ImportError("sqlalchemy.util.greenlet_spawn is not available")
def await_only(*_: Any, **__: Any) -> Any:
raise ImportError("sqlalchemy.util.await_only is not available")
try:
from sqlalchemy.ext.asyncio import async_scoped_session
except ImportError: # pragma: no cover
class async_scoped_session: # noqa: N801
def __init__(self, *_: Any, **__: Any) -> None:
raise ImportError("sqlalchemy.ext.asyncio is not available")
try:
from sqlakeyset import asyncio as apaging
from sqlakeyset import paging
except ImportError: # pragma: no cover
paging = None # type: ignore[ty:invalid-assignment]
apaging = None # type: ignore[ty:invalid-assignment]
AsyncConn: TypeAlias = "AsyncSession | AsyncConnection | async_scoped_session[Any]"
SyncConn: TypeAlias = "Session | Connection | scoped_session[Any]"
AnyConn: TypeAlias = "AsyncConn | SyncConn"
UnwrapMode: TypeAlias = Literal[
"auto", # default, unwrap only if select is select(model)
"legacy", # legacy mode, unwrap only when there is one column in select
"no-unwrap", # never unwrap
"unwrap", # always unwrap
]
TupleAny = TypeVarTuple("TupleAny")
Selectable: TypeAlias = (
"Select[Unpack[TupleAny]] | TextClause | FromStatement[Unpack[TupleAny]] | CompoundSelect[Unpack[TupleAny]]"
)
SelectableOrQuery: TypeAlias = "Selectable | Query[Any]"
@overload
def _prepare_query(query: Select[Unpack[TupleAny]]) -> Select[Unpack[TupleAny]]:
pass
@overload
def _prepare_query(query: Select[Unpack[TupleAny]] | None) -> Select[Unpack[TupleAny]] | None:
pass
def _prepare_query(query: Select[Unpack[TupleAny]] | None) -> Select[Unpack[TupleAny]] | None:
if query is None:
return None
with suppress(AttributeError):
query = query._statement_20() # type: ignore[ty:unresolved-attribute]
return query
def _prepare_query_for_cursor(query: Selectable) -> Selectable:
if isinstance(query, CompoundSelect):
ordering = query._order_by_clauses or ()
query = query.order_by(None) # reset ordering, it will be applied later
subquery = query.subquery("__cursor_subquery__")
return select(subquery).order_by(*ordering)
return query
_selectable_classes = (Select, TextClause, FromStatement, CompoundSelect)
def _should_unwrap_scalars_for_query(query: Selectable) -> bool:
cols_desc = query.column_descriptions # type: ignore[ty:unresolved-attribute]
all_cols = [*query._all_selected_columns]
# we have select(a, b, c) no need to unwrap
if len(cols_desc) != 1:
return False
# select one thing and it has more than one column, unwrap
if len(all_cols) > 1:
return True
# select one thing and it has only one column, check if it actually is a select(model)
if len(all_cols) == 1:
(desc,) = cols_desc
expr, entity = [desc.get(key) for key in ("expr", "entity")]
return expr is not None and expr is entity
return False
def _should_unwrap_scalars(query: Selectable) -> bool:
if not isinstance(query, _selectable_classes):
return False
if isinstance(query, CompoundSelect):
return False
try:
return _should_unwrap_scalars_for_query(query)
except (AttributeError, NotImplementedError):
return True
return False
AnyParams: TypeAlias = AbstractParams | RawParams
def _unwrap_params(params: AnyParams) -> RawParams:
if isinstance(params, RawParams):
return params
return params.to_raw_params().as_limit_offset()
@deprecated("Use fastapi_pagination.ext.raw_sql.create_paginate_query_from_text instead.")
def create_paginate_query_from_text(query: str, params: AnyParams) -> str:
return _create_paginate_query_from_text(query, _unwrap_params(params))
@deprecated("Use fastapi_pagination.ext.raw_sql.create_count_query_from_text instead.")
def create_count_query_from_text(query: str) -> str:
return _create_count_query_from_text(query)
def _paginate_from_statement(
query: FromStatement[Unpack[TupleAny]], params: AnyParams
) -> FromStatement[Unpack[TupleAny]]:
query = query._generate()
query.element = create_paginate_query(query.element, params)
return query
def create_paginate_query(query: Selectable, params: AnyParams) -> Selectable:
if isinstance(query, TextClause):
return text(_create_paginate_query_from_text(query.text, params))
if isinstance(query, FromStatement):
return _paginate_from_statement(query, params)
return generic_query_apply_params(query, _unwrap_params(params))
def create_count_query(query: Selectable, *, use_subquery: bool = True) -> Selectable:
if isinstance(query, TextClause):
return text(_create_count_query_from_text(query.text))
if isinstance(query, FromStatement):
return create_count_query(query.element)
query = query.order_by(None).options(noload("*"))
if use_subquery:
return select(func.count()).select_from(query.subquery())
return query.with_only_columns( # type: ignore[ty:unresolved-attribute]
func.count(),
maintain_column_froms=True,
)
class NonHashableRowsException(Exception):
pass
def _maybe_unique(result: Any, unique: bool) -> Any:
try:
return (result.unique() if unique else result).all()
except InvalidRequestError as e: # pragma: no cover
if "non-hashable" in str(e):
raise NonHashableRowsException("The rows are not hashable, please use `unique=False`") from e
raise
_TSeq = TypeVar("_TSeq", bound=Sequence[Any])
def _unwrap_items(
items: _TSeq,
query: Selectable,
unwrap_mode: UnwrapMode | None = None,
) -> _TSeq:
# for raw queries we will use legacy mode by default
# because we can't determine if we should unwrap or not
if isinstance(query, (TextClause, FromStatement)): # noqa: SIM108
unwrap_mode = unwrap_mode or "legacy"
else:
unwrap_mode = unwrap_mode or "auto"
if unwrap_mode == "legacy":
items = unwrap_scalars(items) # type: ignore[ty:invalid-assignment]
elif unwrap_mode == "no-unwrap":
pass
elif unwrap_mode == "unwrap":
items = unwrap_scalars(items, force_unwrap=True) # type: ignore[ty:invalid-assignment]
elif unwrap_mode == "auto" and _should_unwrap_scalars(query):
items = unwrap_scalars(items, force_unwrap=True) # type: ignore[ty:invalid-assignment]
return items
@flow
def _total_flow(
query: Selectable,
conn: AnyConn,
count_query: Selectable | None,
subquery_count: bool,
) -> TotalFlow:
if count_query is None:
count_query = create_count_query(query, use_subquery=subquery_count)
total = yield conn.scalar(count_query)
return cast(int | None, total)
@flow
def _limit_offset_flow(query: Selectable, conn: AnyConn, raw_params: RawParams) -> LimitOffsetFlow:
query = create_paginate_query(query, raw_params)
items = yield conn.execute(query)
return items
@flow
def _cursor_flow(
query: Selectable,
conn: AnyConn,
unique: bool,
is_async: bool,
raw_params: CursorRawParams,
) -> CursorFlow:
query = _prepare_query_for_cursor(query)
if isinstance(query, TextClause):
raise ValueError("Cursor pagination cannot be used with raw SQL queries") # noqa: TRY004
if isinstance(query, FromStatement):
raise ValueError("Cursor pagination cannot be used with FromStatement queries") # noqa: TRY004
if paging is None: # pragma: no cover
raise ImportError("sqlakeyset is not installed")
if not getattr(query, "_order_by_clauses", True):
raise ValueError("Cursor pagination requires ordering")
_select_page = apaging.select_page if is_async else paging.select_page
page = yield _select_page(
conn, # type: ignore[ty:invalid-argument-type]
selectable=query, # type: ignore[ty:invalid-argument-type]
unique=unique,
per_page=raw_params.size,
page=raw_params.cursor, # type: ignore[ty:invalid-argument-type]
)
items = [*page]
data = {
"current": page.paging.bookmark_current,
"current_backwards": page.paging.bookmark_current_backwards,
"previous": page.paging.bookmark_previous if page.paging.has_previous else None,
"next_": page.paging.bookmark_next if page.paging.has_next else None,
}
return items, data
@flow
def _sqlalchemy_flow(
is_async: bool,
conn: SyncConn | AsyncConn,
query: Select[Unpack[TupleAny]],
params: AbstractParams | None = None,
*,
subquery_count: bool = True,
unwrap_mode: UnwrapMode | None = None,
count_query: Selectable | None = None,
transformer: ItemsTransformer | None = None,
additional_data: AdditionalData | None = None,
unique: bool = True,
config: Config | None = None,
) -> Any:
create_page_factory = create_page
if is_async:
create_page_factory = partial(greenlet_spawn, create_page_factory)
page = yield from generic_flow(
async_=is_async,
total_flow=partial(_total_flow, query, conn, count_query, subquery_count),
limit_offset_flow=partial(_limit_offset_flow, query, conn),
cursor_flow=partial(_cursor_flow, query, conn, unique, is_async),
params=params,
inner_transformer=partial(_inner_transformer, query=query, unwrap_mode=unwrap_mode, unique=unique),
transformer=transformer,
additional_data=additional_data,
config=config,
create_page_factory=create_page_factory,
)
return page
def _inner_transformer(
items: Sequence[Any],
/,
query: Selectable,
unwrap_mode: UnwrapMode | None,
unique: bool,
) -> Sequence[Any]:
with suppress(AttributeError):
items = _maybe_unique(items, unique)
return _unwrap_items(items, query, unwrap_mode)
def _get_sync_conn_from_async(conn: Any) -> SyncConn: # pragma: no cover
if isinstance(conn, async_scoped_session):
conn = conn()
with suppress(AttributeError):
return cast(Session, conn.sync_session)
with suppress(AttributeError):
return cast(Connection, conn.sync_connection)
raise TypeError("conn must be an AsyncConnection or AsyncSession")
# old deprecated paginate function that use sqlalchemy.orm.Query
@overload
def paginate(
query: Query[Any],
params: AbstractParams | None = None,
*,
subquery_count: bool = True,
unwrap_mode: UnwrapMode | None = None,
transformer: SyncItemsTransformer | None = None,
additional_data: AdditionalData | None = None,
unique: bool = True,
config: Config | None = None,
) -> Any:
pass
@overload
def paginate(
conn: SyncConn,
query: SelectableOrQuery,
params: AbstractParams | None = None,
*,
count_query: SelectableOrQuery | None = None,
subquery_count: bool = True,
unwrap_mode: UnwrapMode | None = None,
transformer: SyncItemsTransformer | None = None,
additional_data: AdditionalData | None = None,
unique: bool = True,
config: Config | None = None,
) -> Any:
pass
@overload
async def paginate(
conn: AsyncConn,
query: Selectable,
params: AbstractParams | None = None,
*,
count_query: Selectable | None = None,
subquery_count: bool = True,
unwrap_mode: UnwrapMode | None = None,
transformer: AsyncItemsTransformer | None = None,
additional_data: AdditionalData | None = None,
unique: bool = True,
config: Config | None = None,
) -> Any:
pass
def paginate(*args: Any, **kwargs: Any) -> Any:
try:
assert args
assert isinstance(args[0], Query)
query, count_query, conn, params, transformer, additional_data, unique, subquery_count, unwrap_mode, config = (
_old_paginate_sign(*args, **kwargs)
)
except (TypeError, AssertionError):
query, count_query, conn, params, transformer, additional_data, unique, subquery_count, unwrap_mode, config = (
_new_paginate_sign(*args, **kwargs)
)
try:
_get_sync_conn_from_async(conn)
except TypeError:
pass
else:
warnings.warn(
"Use `apaginate` instead. This function overload will be removed in v0.16.0",
DeprecationWarning,
stacklevel=2,
)
return apaginate(
conn=conn,
query=query,
params=params,
count_query=count_query,
subquery_count=subquery_count,
unwrap_mode=unwrap_mode,
transformer=transformer,
additional_data=additional_data,
unique=unique,
config=config,
)
return run_sync_flow(
_sqlalchemy_flow(
is_async=False,
conn=conn,
query=query,
params=params,
subquery_count=subquery_count,
unwrap_mode=unwrap_mode,
count_query=count_query,
transformer=transformer,
additional_data=additional_data,
unique=unique,
config=config,
),
)
def _old_paginate_sign(
query: Query[Any],
params: AbstractParams | None = None,
*,
subquery_count: bool = True,
unwrap_mode: UnwrapMode | None = None,
transformer: ItemsTransformer | None = None,
additional_data: AdditionalData | None = None,
unique: bool = True,
config: Config | None = None,
) -> tuple[
Select[Unpack[TupleAny]],
Selectable | None,
SyncConn,
AbstractParams | None,
ItemsTransformer | None,
AdditionalData | None,
bool,
bool,
UnwrapMode | None,
Config | None,
]:
if query.session is None:
raise ValueError("query.session is None")
session = query.session
query = _prepare_query(query) # type: ignore[ty:no-matching-overload]
return query, None, session, params, transformer, additional_data, unique, subquery_count, unwrap_mode, config
def _new_paginate_sign(
conn: SyncConn,
query: Select[Unpack[TupleAny]],
params: AbstractParams | None = None,
*,
subquery_count: bool = True,
unwrap_mode: UnwrapMode | None = None,
count_query: Selectable | None = None,
transformer: ItemsTransformer | None = None,
additional_data: AdditionalData | None = None,
unique: bool = True,
config: Config | None = None,
) -> tuple[
Select[Unpack[TupleAny]],
Selectable | None,
SyncConn,
AbstractParams | None,
ItemsTransformer | None,
AdditionalData | None,
bool,
bool,
UnwrapMode | None,
Config | None,
]:
query = _prepare_query(query)
count_query = _prepare_query(count_query) # type: ignore[ty:no-matching-overload]
return query, count_query, conn, params, transformer, additional_data, unique, subquery_count, unwrap_mode, config
async def apaginate(
conn: AsyncConn,
query: Selectable,
params: AbstractParams | None = None,
*,
count_query: Selectable | None = None,
subquery_count: bool = True,
unwrap_mode: UnwrapMode | None = None,
transformer: AsyncItemsTransformer | None = None,
additional_data: AdditionalData | None = None,
unique: bool = True,
config: Config | None = None,
) -> Any:
query = _prepare_query(query) # type: ignore[ty:no-matching-overload]
count_query = _prepare_query(count_query) # type: ignore[ty:no-matching-overload]
return await run_async_flow(
_sqlalchemy_flow(
is_async=True,
conn=conn,
query=query,
params=params,
subquery_count=subquery_count,
unwrap_mode=unwrap_mode,
count_query=count_query,
transformer=transformer,
additional_data=additional_data,
unique=unique,
config=config,
),
)