sqlphilosophy 0.3.0__tar.gz → 0.3.4__tar.gz
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.
- {sqlphilosophy-0.3.0/src/sqlphilosophy.egg-info → sqlphilosophy-0.3.4}/PKG-INFO +23 -3
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/README.md +20 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/pyproject.toml +3 -4
- sqlphilosophy-0.3.4/src/sqlphilosophy/VERSION +1 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy/_repository_shared.py +11 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy/aio/repository.py +41 -6
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy/strict.py +17 -4
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy/sync/repository.py +41 -6
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy/types.py +1 -1
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4/src/sqlphilosophy.egg-info}/PKG-INFO +23 -3
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy.egg-info/requires.txt +2 -2
- sqlphilosophy-0.3.0/src/sqlphilosophy/VERSION +0 -1
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/LICENSE +0 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/MANIFEST.in +0 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/setup.cfg +0 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy/__init__.py +0 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy/aio/__init__.py +0 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy/aio/protocols.py +0 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy/aio/query.py +0 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy/audit/__init__.py +0 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy/audit/context.py +0 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy/audit/fields.py +0 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy/audit/listener.py +0 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy/audit/model.py +0 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy/py.typed +0 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy/sorting.py +0 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy/sql.py +0 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy/sync/__init__.py +0 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy/sync/protocols.py +0 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy/sync/query.py +0 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy/trusted_sql.py +0 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy.egg-info/SOURCES.txt +0 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy.egg-info/dependency_links.txt +0 -0
- {sqlphilosophy-0.3.0 → sqlphilosophy-0.3.4}/src/sqlphilosophy.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sqlphilosophy
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.4
|
|
4
4
|
Summary: Portable SQLAlchemy repository kit: sync and async CRUD, statement builders, sort/pagination, and SQL helpers.
|
|
5
5
|
Author-email: Josh Martin <denverprogrammer@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -20,7 +20,7 @@ Classifier: Typing :: Typed
|
|
|
20
20
|
Requires-Python: <4.0,>=3.12
|
|
21
21
|
Description-Content-Type: text/markdown
|
|
22
22
|
License-File: LICENSE
|
|
23
|
-
Requires-Dist: servicephilosophy
|
|
23
|
+
Requires-Dist: servicephilosophy<0.2,>=0.1.2
|
|
24
24
|
Requires-Dist: sqlalchemy<3,>=2.0
|
|
25
25
|
Provides-Extra: async
|
|
26
26
|
Requires-Dist: greenlet>=3.0; extra == "async"
|
|
@@ -30,7 +30,7 @@ Requires-Dist: pytest-asyncio>=0.24; extra == "dev"
|
|
|
30
30
|
Requires-Dist: pytest-cov>=6; extra == "dev"
|
|
31
31
|
Requires-Dist: aiosqlite>=0.20; extra == "dev"
|
|
32
32
|
Requires-Dist: greenlet>=3.0; extra == "dev"
|
|
33
|
-
Requires-Dist: build
|
|
33
|
+
Requires-Dist: build<2,>=1.6.1; extra == "dev"
|
|
34
34
|
Requires-Dist: twine>=5; extra == "dev"
|
|
35
35
|
Requires-Dist: ruff>=0.15; extra == "dev"
|
|
36
36
|
Requires-Dist: mypy>=1.11; extra == "dev"
|
|
@@ -40,6 +40,8 @@ Dynamic: license-file
|
|
|
40
40
|
|
|
41
41
|
# sqlphilosophy
|
|
42
42
|
|
|
43
|
+
Current source and planned customer-subdomain impact: [current-state summary](docs/current-state-summary.md).
|
|
44
|
+
|
|
43
45
|
Portable SQLAlchemy repository kit: sync and async CRUD, fluent statement builders, sort/pagination, Core SQL helpers, and optional audit listeners.
|
|
44
46
|
|
|
45
47
|
| | |
|
|
@@ -180,3 +182,21 @@ See [RELEASING.md](./RELEASING.md) for GitHub + PyPI trusted publishing. See [CH
|
|
|
180
182
|
## License
|
|
181
183
|
|
|
182
184
|
MIT — see [LICENSE](./LICENSE).
|
|
185
|
+
|
|
186
|
+
## Expression lookups and scope overrides (0.3+)
|
|
187
|
+
|
|
188
|
+
Both repository bases provide `find_entity(criteria=..., lock=False)`, `find_row(criteria=..., columns=..., lock=False)` and `count_scoped(criteria=...)`. Criteria must not be empty; projected lookups also require columns. Entity lookups return a mapped object, projected lookups return RowMapping, and absence returns None. These methods never commit.
|
|
189
|
+
|
|
190
|
+
Override `apply_scope(statement)` for additional predicates on these helpers. It runs after projection initialization, because `select_columns`/`select_entity` reset the builder's SELECT. This hook does not implicitly scope existing CRUD helpers or arbitrary builder calls: keep ownership checks in named application repository methods. Existing `first`, `get_by_id`, `count` and statement APIs remain available and unchanged.
|
|
191
|
+
|
|
192
|
+
Published 0.3.3 provides these lookup APIs. The 0.3.4 source prepares compatible
|
|
193
|
+
strict-decoder, documentation and build-metadata fixes. Use a named application
|
|
194
|
+
repository method to own each model-specific projection, ordering and policy;
|
|
195
|
+
compose its query with `statement()` or these lookup helpers instead of
|
|
196
|
+
scattering `select()` calls through controllers/services. Core SQL helpers remain
|
|
197
|
+
available for explicit SQL boundaries; a repository does not supply authorization.
|
|
198
|
+
|
|
199
|
+
The supported dependency range remains SQLAlchemy 2.x. DeliveryPlus native's
|
|
200
|
+
2.0.49 constraint and RuleAtlas's separate 2.1.5 minimum are host decisions;
|
|
201
|
+
upgrading this package does not silently change either host lock. Coordinated
|
|
202
|
+
candidate verification exercises both SQLAlchemy versions before release.
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# sqlphilosophy
|
|
2
2
|
|
|
3
|
+
Current source and planned customer-subdomain impact: [current-state summary](docs/current-state-summary.md).
|
|
4
|
+
|
|
3
5
|
Portable SQLAlchemy repository kit: sync and async CRUD, fluent statement builders, sort/pagination, Core SQL helpers, and optional audit listeners.
|
|
4
6
|
|
|
5
7
|
| | |
|
|
@@ -140,3 +142,21 @@ See [RELEASING.md](./RELEASING.md) for GitHub + PyPI trusted publishing. See [CH
|
|
|
140
142
|
## License
|
|
141
143
|
|
|
142
144
|
MIT — see [LICENSE](./LICENSE).
|
|
145
|
+
|
|
146
|
+
## Expression lookups and scope overrides (0.3+)
|
|
147
|
+
|
|
148
|
+
Both repository bases provide `find_entity(criteria=..., lock=False)`, `find_row(criteria=..., columns=..., lock=False)` and `count_scoped(criteria=...)`. Criteria must not be empty; projected lookups also require columns. Entity lookups return a mapped object, projected lookups return RowMapping, and absence returns None. These methods never commit.
|
|
149
|
+
|
|
150
|
+
Override `apply_scope(statement)` for additional predicates on these helpers. It runs after projection initialization, because `select_columns`/`select_entity` reset the builder's SELECT. This hook does not implicitly scope existing CRUD helpers or arbitrary builder calls: keep ownership checks in named application repository methods. Existing `first`, `get_by_id`, `count` and statement APIs remain available and unchanged.
|
|
151
|
+
|
|
152
|
+
Published 0.3.3 provides these lookup APIs. The 0.3.4 source prepares compatible
|
|
153
|
+
strict-decoder, documentation and build-metadata fixes. Use a named application
|
|
154
|
+
repository method to own each model-specific projection, ordering and policy;
|
|
155
|
+
compose its query with `statement()` or these lookup helpers instead of
|
|
156
|
+
scattering `select()` calls through controllers/services. Core SQL helpers remain
|
|
157
|
+
available for explicit SQL boundaries; a repository does not supply authorization.
|
|
158
|
+
|
|
159
|
+
The supported dependency range remains SQLAlchemy 2.x. DeliveryPlus native's
|
|
160
|
+
2.0.49 constraint and RuleAtlas's separate 2.1.5 minimum are host decisions;
|
|
161
|
+
upgrading this package does not silently change either host lock. Coordinated
|
|
162
|
+
candidate verification exercises both SQLAlchemy versions before release.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
[build-system]
|
|
2
|
-
requires = ["setuptools>=
|
|
2
|
+
requires = ["setuptools>=84.2.1,<85", "wheel>=0.46,<1"]
|
|
3
3
|
build-backend = "setuptools.build_meta"
|
|
4
4
|
|
|
5
5
|
[project]
|
|
@@ -24,7 +24,7 @@ classifiers = [
|
|
|
24
24
|
]
|
|
25
25
|
dynamic = ["version"]
|
|
26
26
|
dependencies = [
|
|
27
|
-
"servicephilosophy>=0.1.0",
|
|
27
|
+
"servicephilosophy>=0.1.2,<0.2",
|
|
28
28
|
"sqlalchemy>=2.0,<3",
|
|
29
29
|
]
|
|
30
30
|
|
|
@@ -38,7 +38,7 @@ dev = [
|
|
|
38
38
|
"pytest-cov>=6",
|
|
39
39
|
"aiosqlite>=0.20",
|
|
40
40
|
"greenlet>=3.0",
|
|
41
|
-
"build>=1.2",
|
|
41
|
+
"build>=1.6.1,<2",
|
|
42
42
|
"twine>=5",
|
|
43
43
|
"ruff>=0.15",
|
|
44
44
|
"mypy>=1.11",
|
|
@@ -81,7 +81,6 @@ exclude_also = [
|
|
|
81
81
|
|
|
82
82
|
[tool.mypy]
|
|
83
83
|
python_version = "3.12"
|
|
84
|
-
plugins = ["sqlalchemy.ext.mypy.plugin"]
|
|
85
84
|
warn_unused_ignores = true
|
|
86
85
|
|
|
87
86
|
[tool.ruff]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.3.4
|
|
@@ -8,6 +8,7 @@ from typing import Any, Literal, cast
|
|
|
8
8
|
|
|
9
9
|
from sqlalchemy import func
|
|
10
10
|
from sqlalchemy.orm import DeclarativeBase
|
|
11
|
+
from sqlalchemy.sql import SQLColumnExpression
|
|
11
12
|
|
|
12
13
|
from sqlphilosophy.audit.model import AuditMixin
|
|
13
14
|
from sqlphilosophy.types import PrimaryKey, RowMapping, RowValue, SqlFilter
|
|
@@ -112,3 +113,13 @@ def criteria_delete_allowed(criteria: Sequence[SqlFilter]) -> bool:
|
|
|
112
113
|
def bulk_update_allowed(values: RowMapping) -> bool:
|
|
113
114
|
"""True when ``update_where`` has values to apply."""
|
|
114
115
|
return bool(values)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def require_lookup_inputs(
|
|
119
|
+
criteria: Sequence[SqlFilter], columns: Sequence[SQLColumnExpression[object]] | None = None
|
|
120
|
+
) -> None:
|
|
121
|
+
"""Reject accidental unscoped or empty-projection expression lookups."""
|
|
122
|
+
if not criteria:
|
|
123
|
+
raise ValueError("lookup criteria must not be empty")
|
|
124
|
+
if columns is not None and not columns:
|
|
125
|
+
raise ValueError("lookup projection must not be empty")
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
from collections.abc import Sequence
|
|
6
|
-
from typing import Any, cast
|
|
6
|
+
from typing import Any, SupportsInt, cast
|
|
7
7
|
|
|
8
8
|
from servicephilosophy import ServiceRepository
|
|
9
9
|
from sqlalchemy import delete, func, select, update
|
|
@@ -11,6 +11,7 @@ from sqlalchemy import inspect as sa_inspect
|
|
|
11
11
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
12
12
|
from sqlalchemy.orm import DeclarativeBase
|
|
13
13
|
from sqlalchemy.orm.interfaces import LoaderOption
|
|
14
|
+
from sqlalchemy.sql import SQLColumnExpression
|
|
14
15
|
|
|
15
16
|
from sqlphilosophy._repository_shared import (
|
|
16
17
|
bulk_update_allowed,
|
|
@@ -19,6 +20,7 @@ from sqlphilosophy._repository_shared import (
|
|
|
19
20
|
lookup_not_found_message,
|
|
20
21
|
plan_partial_update,
|
|
21
22
|
require_batch_size,
|
|
23
|
+
require_lookup_inputs,
|
|
22
24
|
require_mappings_page_limits,
|
|
23
25
|
require_page_and_limit,
|
|
24
26
|
require_single_column_primary_key,
|
|
@@ -55,6 +57,11 @@ class AsyncBaseRepository[T: DeclarativeBase, U: AsyncRepositoryFactory](Service
|
|
|
55
57
|
self._session = session
|
|
56
58
|
self._pk_column = require_single_column_primary_key(model, self.inspect_model(model))
|
|
57
59
|
|
|
60
|
+
@property
|
|
61
|
+
def session(self) -> AsyncSession:
|
|
62
|
+
"""Caller-owned session; exposed for composition ownership checks."""
|
|
63
|
+
return self._session
|
|
64
|
+
|
|
58
65
|
@classmethod
|
|
59
66
|
def inspect_model(cls, model: type[DeclarativeBase]) -> Any:
|
|
60
67
|
"""Return SQLAlchemy ORM mapper inspection for ``model``."""
|
|
@@ -101,7 +108,7 @@ class AsyncBaseRepository[T: DeclarativeBase, U: AsyncRepositoryFactory](Service
|
|
|
101
108
|
async def scalar_count(self, stmt: SqlSelect, params: SqlBindParams | None = None) -> int:
|
|
102
109
|
"""Execute a scalar count/select statement and return ``int``."""
|
|
103
110
|
result = await self._session.execute(stmt, params or {})
|
|
104
|
-
return int(result.scalar_one())
|
|
111
|
+
return int(cast("SupportsInt", result.scalar_one()))
|
|
105
112
|
|
|
106
113
|
async def iter_mappings(self, stmt: SqlSelect, params: SqlBindParams | None = None):
|
|
107
114
|
"""Yield each result row as a plain ``dict``."""
|
|
@@ -308,20 +315,20 @@ class AsyncBaseRepository[T: DeclarativeBase, U: AsyncRepositoryFactory](Service
|
|
|
308
315
|
*,
|
|
309
316
|
criteria: Sequence[SqlFilter],
|
|
310
317
|
values: RowMapping,
|
|
311
|
-
params:
|
|
318
|
+
params: RowMapping | None = None,
|
|
312
319
|
) -> int:
|
|
313
320
|
"""Bulk UPDATE rows matching ``criteria``; returns affected row count."""
|
|
314
321
|
if not bulk_update_allowed(values):
|
|
315
322
|
return 0
|
|
316
323
|
stmt = update(self.model).where(*criteria).values(**values)
|
|
317
|
-
result = await self._session.execute(stmt, params
|
|
324
|
+
result = await self._session.execute(stmt, dict(params) if params is not None else {})
|
|
318
325
|
return cursor_rowcount(result)
|
|
319
326
|
|
|
320
327
|
async def delete_where(
|
|
321
328
|
self,
|
|
322
329
|
*,
|
|
323
330
|
criteria: Sequence[SqlFilter],
|
|
324
|
-
params:
|
|
331
|
+
params: RowMapping | None = None,
|
|
325
332
|
) -> int:
|
|
326
333
|
"""Delete rows matching ``criteria`` via PK lookup + ``delete_many``."""
|
|
327
334
|
if not criteria_delete_allowed(criteria):
|
|
@@ -329,7 +336,7 @@ class AsyncBaseRepository[T: DeclarativeBase, U: AsyncRepositoryFactory](Service
|
|
|
329
336
|
pk_key = self._pk_column.key
|
|
330
337
|
builder = self.statement().select_columns(self._pk_column).where(*criteria)
|
|
331
338
|
if params:
|
|
332
|
-
builder = builder.with_params(params)
|
|
339
|
+
builder = builder.with_params(dict(params))
|
|
333
340
|
rows = await builder.mappings().all()
|
|
334
341
|
ids = extract_primary_keys(rows, pk_key)
|
|
335
342
|
return await self.delete_many(ids)
|
|
@@ -374,6 +381,34 @@ class AsyncBaseRepository[T: DeclarativeBase, U: AsyncRepositoryFactory](Service
|
|
|
374
381
|
await self._session.commit()
|
|
375
382
|
return total
|
|
376
383
|
|
|
384
|
+
def apply_scope(self, statement: AsyncStatementQueryBuilder[T]) -> AsyncStatementQueryBuilder[T]:
|
|
385
|
+
"""Host override hook, invoked after projection initialization."""
|
|
386
|
+
return statement
|
|
387
|
+
|
|
388
|
+
async def find_entity(self, *, criteria: Sequence[SqlFilter], lock: bool = False) -> T | None:
|
|
389
|
+
require_lookup_inputs(criteria)
|
|
390
|
+
query = self.apply_scope(self.statement()).where(*criteria)
|
|
391
|
+
if lock:
|
|
392
|
+
query = query.with_for_update()
|
|
393
|
+
return await query.scalars().first()
|
|
394
|
+
|
|
395
|
+
async def find_row(
|
|
396
|
+
self,
|
|
397
|
+
*,
|
|
398
|
+
criteria: Sequence[SqlFilter],
|
|
399
|
+
columns: Sequence[SQLColumnExpression[object]],
|
|
400
|
+
lock: bool = False,
|
|
401
|
+
) -> RowMapping | None:
|
|
402
|
+
require_lookup_inputs(criteria, columns)
|
|
403
|
+
query = self.apply_scope(self.statement().select_columns(*columns)).where(*criteria)
|
|
404
|
+
if lock:
|
|
405
|
+
query = query.with_for_update()
|
|
406
|
+
return await query.mappings().first()
|
|
407
|
+
|
|
408
|
+
async def count_scoped(self, *, criteria: Sequence[SqlFilter]) -> int:
|
|
409
|
+
require_lookup_inputs(criteria)
|
|
410
|
+
return await self.apply_scope(self.statement().select_entity()).where(*criteria).count()
|
|
411
|
+
|
|
377
412
|
def statement(self) -> AsyncStatementQueryBuilder[T]:
|
|
378
413
|
"""Return a fluent statement builder for reads on this model (default read path)."""
|
|
379
414
|
factory = self.maybe_factory
|
|
@@ -6,7 +6,7 @@ from collections.abc import Callable, Mapping
|
|
|
6
6
|
from datetime import date, datetime
|
|
7
7
|
from enum import Enum
|
|
8
8
|
from math import isfinite
|
|
9
|
-
from typing import TypeGuard
|
|
9
|
+
from typing import TypeGuard, cast
|
|
10
10
|
from uuid import UUID
|
|
11
11
|
|
|
12
12
|
from sqlphilosophy.types import JSONValue
|
|
@@ -99,9 +99,21 @@ def strict_row_enum[EnumT: Enum](row: Row, key: str, enum_type: type[EnumT]) ->
|
|
|
99
99
|
raise _type_error(key, enum_type.__name__, value)
|
|
100
100
|
|
|
101
101
|
|
|
102
|
+
def _plain_list(value: object) -> list[object] | None:
|
|
103
|
+
if type(value) is not list:
|
|
104
|
+
return None
|
|
105
|
+
# Exact built-in type proof preserves identity without inspecting __class__.
|
|
106
|
+
return cast(list[object], value)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def _is_str_list(value: object) -> TypeGuard[list[str]]:
|
|
110
|
+
items = _plain_list(value)
|
|
111
|
+
return items is not None and all(type(item) is str for item in items)
|
|
112
|
+
|
|
113
|
+
|
|
102
114
|
def strict_row_str_list(row: Row, key: str) -> list[str]:
|
|
103
115
|
value = _required(row, key)
|
|
104
|
-
if
|
|
116
|
+
if not _is_str_list(value):
|
|
105
117
|
raise _type_error(key, "list[str]", value)
|
|
106
118
|
return value
|
|
107
119
|
|
|
@@ -111,8 +123,9 @@ def is_strict_json_value(value: object) -> TypeGuard[JSONValue]:
|
|
|
111
123
|
return True
|
|
112
124
|
if type(value) is float:
|
|
113
125
|
return isfinite(value)
|
|
114
|
-
|
|
115
|
-
|
|
126
|
+
items = _plain_list(value)
|
|
127
|
+
if items is not None:
|
|
128
|
+
return all(is_strict_json_value(item) for item in items)
|
|
116
129
|
if type(value) is dict:
|
|
117
130
|
return all(type(key) is str and is_strict_json_value(item) for key, item in value.items())
|
|
118
131
|
return False
|
|
@@ -3,13 +3,14 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
from collections.abc import Sequence
|
|
6
|
-
from typing import Any, cast
|
|
6
|
+
from typing import Any, SupportsInt, cast
|
|
7
7
|
|
|
8
8
|
from servicephilosophy import ServiceRepository
|
|
9
9
|
from sqlalchemy import delete, func, select, update
|
|
10
10
|
from sqlalchemy import inspect as sa_inspect
|
|
11
11
|
from sqlalchemy.orm import DeclarativeBase, Session
|
|
12
12
|
from sqlalchemy.orm.interfaces import LoaderOption
|
|
13
|
+
from sqlalchemy.sql import SQLColumnExpression
|
|
13
14
|
|
|
14
15
|
from sqlphilosophy._repository_shared import (
|
|
15
16
|
bulk_update_allowed,
|
|
@@ -17,6 +18,7 @@ from sqlphilosophy._repository_shared import (
|
|
|
17
18
|
extract_primary_keys,
|
|
18
19
|
lookup_not_found_message,
|
|
19
20
|
require_batch_size,
|
|
21
|
+
require_lookup_inputs,
|
|
20
22
|
require_page_and_limit,
|
|
21
23
|
require_single_column_primary_key,
|
|
22
24
|
)
|
|
@@ -52,6 +54,11 @@ class BaseRepository[T: DeclarativeBase, U: RepositoryFactory](ServiceRepository
|
|
|
52
54
|
self._session = session
|
|
53
55
|
self._pk_column = require_single_column_primary_key(model, self.inspect_model(model))
|
|
54
56
|
|
|
57
|
+
@property
|
|
58
|
+
def session(self) -> Session:
|
|
59
|
+
"""Caller-owned session; exposed for composition ownership checks."""
|
|
60
|
+
return self._session
|
|
61
|
+
|
|
55
62
|
@classmethod
|
|
56
63
|
def inspect_model(cls, model: type[DeclarativeBase]) -> Any:
|
|
57
64
|
"""Return SQLAlchemy ORM mapper inspection for ``model``."""
|
|
@@ -87,7 +94,7 @@ class BaseRepository[T: DeclarativeBase, U: RepositoryFactory](ServiceRepository
|
|
|
87
94
|
|
|
88
95
|
def scalar_count(self, stmt: SqlSelect, params: SqlBindParams | None = None) -> int:
|
|
89
96
|
"""Execute a scalar count/select statement and return ``int``."""
|
|
90
|
-
return int(self._session.execute(stmt, params or {}).scalar_one())
|
|
97
|
+
return int(cast("SupportsInt", self._session.execute(stmt, params or {}).scalar_one()))
|
|
91
98
|
|
|
92
99
|
def iter_mappings(self, stmt: SqlSelect, params: SqlBindParams | None = None):
|
|
93
100
|
"""Yield each result row as a plain ``dict``."""
|
|
@@ -274,20 +281,20 @@ class BaseRepository[T: DeclarativeBase, U: RepositoryFactory](ServiceRepository
|
|
|
274
281
|
*,
|
|
275
282
|
criteria: Sequence[SqlFilter],
|
|
276
283
|
values: RowMapping,
|
|
277
|
-
params:
|
|
284
|
+
params: RowMapping | None = None,
|
|
278
285
|
) -> int:
|
|
279
286
|
"""Bulk UPDATE rows matching ``criteria``; returns affected row count."""
|
|
280
287
|
if not bulk_update_allowed(values):
|
|
281
288
|
return 0
|
|
282
289
|
stmt = update(self.model).where(*criteria).values(**values)
|
|
283
|
-
result = self._session.execute(stmt, params
|
|
290
|
+
result = self._session.execute(stmt, dict(params) if params is not None else {})
|
|
284
291
|
return cursor_rowcount(result)
|
|
285
292
|
|
|
286
293
|
def delete_where(
|
|
287
294
|
self,
|
|
288
295
|
*,
|
|
289
296
|
criteria: Sequence[SqlFilter],
|
|
290
|
-
params:
|
|
297
|
+
params: RowMapping | None = None,
|
|
291
298
|
) -> int:
|
|
292
299
|
"""Delete rows matching ``criteria`` via PK lookup + ``delete_many``."""
|
|
293
300
|
if not criteria_delete_allowed(criteria):
|
|
@@ -295,7 +302,7 @@ class BaseRepository[T: DeclarativeBase, U: RepositoryFactory](ServiceRepository
|
|
|
295
302
|
pk_key = self._pk_column.key
|
|
296
303
|
builder = self.statement().select_columns(self._pk_column).where(*criteria)
|
|
297
304
|
if params:
|
|
298
|
-
builder = builder.with_params(params)
|
|
305
|
+
builder = builder.with_params(dict(params))
|
|
299
306
|
rows = builder.mappings().all()
|
|
300
307
|
ids = extract_primary_keys(rows, pk_key)
|
|
301
308
|
return self.delete_many(ids)
|
|
@@ -334,6 +341,34 @@ class BaseRepository[T: DeclarativeBase, U: RepositoryFactory](ServiceRepository
|
|
|
334
341
|
self._session.commit()
|
|
335
342
|
return total
|
|
336
343
|
|
|
344
|
+
def apply_scope(self, statement: StatementQueryBuilder[T]) -> StatementQueryBuilder[T]:
|
|
345
|
+
"""Host override hook, invoked after projection initialization."""
|
|
346
|
+
return statement
|
|
347
|
+
|
|
348
|
+
def find_entity(self, *, criteria: Sequence[SqlFilter], lock: bool = False) -> T | None:
|
|
349
|
+
require_lookup_inputs(criteria)
|
|
350
|
+
query = self.apply_scope(self.statement()).where(*criteria)
|
|
351
|
+
if lock:
|
|
352
|
+
query = query.with_for_update()
|
|
353
|
+
return query.scalars().first()
|
|
354
|
+
|
|
355
|
+
def find_row(
|
|
356
|
+
self,
|
|
357
|
+
*,
|
|
358
|
+
criteria: Sequence[SqlFilter],
|
|
359
|
+
columns: Sequence[SQLColumnExpression[object]],
|
|
360
|
+
lock: bool = False,
|
|
361
|
+
) -> RowMapping | None:
|
|
362
|
+
require_lookup_inputs(criteria, columns)
|
|
363
|
+
query = self.apply_scope(self.statement().select_columns(*columns)).where(*criteria)
|
|
364
|
+
if lock:
|
|
365
|
+
query = query.with_for_update()
|
|
366
|
+
return query.mappings().first()
|
|
367
|
+
|
|
368
|
+
def count_scoped(self, *, criteria: Sequence[SqlFilter]) -> int:
|
|
369
|
+
require_lookup_inputs(criteria)
|
|
370
|
+
return self.apply_scope(self.statement().select_entity()).where(*criteria).count()
|
|
371
|
+
|
|
337
372
|
def statement(self) -> StatementQueryBuilder[T]:
|
|
338
373
|
"""Return a fluent statement builder for reads on this model (default read path)."""
|
|
339
374
|
factory = self.maybe_factory
|
|
@@ -65,4 +65,4 @@ type OrmModel = type[DeclarativeBase]
|
|
|
65
65
|
|
|
66
66
|
def cursor_rowcount(result: Result[Any]) -> int:
|
|
67
67
|
"""Return affected row count from a DML ``execute`` result."""
|
|
68
|
-
return int(cast(CursorResult[Any], result).rowcount or 0)
|
|
68
|
+
return int(cast("CursorResult[Any]", result).rowcount or 0)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: sqlphilosophy
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.4
|
|
4
4
|
Summary: Portable SQLAlchemy repository kit: sync and async CRUD, statement builders, sort/pagination, and SQL helpers.
|
|
5
5
|
Author-email: Josh Martin <denverprogrammer@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -20,7 +20,7 @@ Classifier: Typing :: Typed
|
|
|
20
20
|
Requires-Python: <4.0,>=3.12
|
|
21
21
|
Description-Content-Type: text/markdown
|
|
22
22
|
License-File: LICENSE
|
|
23
|
-
Requires-Dist: servicephilosophy
|
|
23
|
+
Requires-Dist: servicephilosophy<0.2,>=0.1.2
|
|
24
24
|
Requires-Dist: sqlalchemy<3,>=2.0
|
|
25
25
|
Provides-Extra: async
|
|
26
26
|
Requires-Dist: greenlet>=3.0; extra == "async"
|
|
@@ -30,7 +30,7 @@ Requires-Dist: pytest-asyncio>=0.24; extra == "dev"
|
|
|
30
30
|
Requires-Dist: pytest-cov>=6; extra == "dev"
|
|
31
31
|
Requires-Dist: aiosqlite>=0.20; extra == "dev"
|
|
32
32
|
Requires-Dist: greenlet>=3.0; extra == "dev"
|
|
33
|
-
Requires-Dist: build
|
|
33
|
+
Requires-Dist: build<2,>=1.6.1; extra == "dev"
|
|
34
34
|
Requires-Dist: twine>=5; extra == "dev"
|
|
35
35
|
Requires-Dist: ruff>=0.15; extra == "dev"
|
|
36
36
|
Requires-Dist: mypy>=1.11; extra == "dev"
|
|
@@ -40,6 +40,8 @@ Dynamic: license-file
|
|
|
40
40
|
|
|
41
41
|
# sqlphilosophy
|
|
42
42
|
|
|
43
|
+
Current source and planned customer-subdomain impact: [current-state summary](docs/current-state-summary.md).
|
|
44
|
+
|
|
43
45
|
Portable SQLAlchemy repository kit: sync and async CRUD, fluent statement builders, sort/pagination, Core SQL helpers, and optional audit listeners.
|
|
44
46
|
|
|
45
47
|
| | |
|
|
@@ -180,3 +182,21 @@ See [RELEASING.md](./RELEASING.md) for GitHub + PyPI trusted publishing. See [CH
|
|
|
180
182
|
## License
|
|
181
183
|
|
|
182
184
|
MIT — see [LICENSE](./LICENSE).
|
|
185
|
+
|
|
186
|
+
## Expression lookups and scope overrides (0.3+)
|
|
187
|
+
|
|
188
|
+
Both repository bases provide `find_entity(criteria=..., lock=False)`, `find_row(criteria=..., columns=..., lock=False)` and `count_scoped(criteria=...)`. Criteria must not be empty; projected lookups also require columns. Entity lookups return a mapped object, projected lookups return RowMapping, and absence returns None. These methods never commit.
|
|
189
|
+
|
|
190
|
+
Override `apply_scope(statement)` for additional predicates on these helpers. It runs after projection initialization, because `select_columns`/`select_entity` reset the builder's SELECT. This hook does not implicitly scope existing CRUD helpers or arbitrary builder calls: keep ownership checks in named application repository methods. Existing `first`, `get_by_id`, `count` and statement APIs remain available and unchanged.
|
|
191
|
+
|
|
192
|
+
Published 0.3.3 provides these lookup APIs. The 0.3.4 source prepares compatible
|
|
193
|
+
strict-decoder, documentation and build-metadata fixes. Use a named application
|
|
194
|
+
repository method to own each model-specific projection, ordering and policy;
|
|
195
|
+
compose its query with `statement()` or these lookup helpers instead of
|
|
196
|
+
scattering `select()` calls through controllers/services. Core SQL helpers remain
|
|
197
|
+
available for explicit SQL boundaries; a repository does not supply authorization.
|
|
198
|
+
|
|
199
|
+
The supported dependency range remains SQLAlchemy 2.x. DeliveryPlus native's
|
|
200
|
+
2.0.49 constraint and RuleAtlas's separate 2.1.5 minimum are host decisions;
|
|
201
|
+
upgrading this package does not silently change either host lock. Coordinated
|
|
202
|
+
candidate verification exercises both SQLAlchemy versions before release.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
servicephilosophy
|
|
1
|
+
servicephilosophy<0.2,>=0.1.2
|
|
2
2
|
sqlalchemy<3,>=2.0
|
|
3
3
|
|
|
4
4
|
[async]
|
|
@@ -10,7 +10,7 @@ pytest-asyncio>=0.24
|
|
|
10
10
|
pytest-cov>=6
|
|
11
11
|
aiosqlite>=0.20
|
|
12
12
|
greenlet>=3.0
|
|
13
|
-
build
|
|
13
|
+
build<2,>=1.6.1
|
|
14
14
|
twine>=5
|
|
15
15
|
ruff>=0.15
|
|
16
16
|
mypy>=1.11
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
0.3.0
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|