scim2-sqlalchemy 0.1.1__tar.gz → 0.2.0__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.
- {scim2_sqlalchemy-0.1.1 → scim2_sqlalchemy-0.2.0}/PKG-INFO +2 -2
- {scim2_sqlalchemy-0.1.1 → scim2_sqlalchemy-0.2.0}/pyproject.toml +11 -7
- {scim2_sqlalchemy-0.1.1 → scim2_sqlalchemy-0.2.0}/pyproject.toml.orig +11 -7
- {scim2_sqlalchemy-0.1.1 → scim2_sqlalchemy-0.2.0}/src/scim2_sqlalchemy/comparison.py +83 -13
- {scim2_sqlalchemy-0.1.1 → scim2_sqlalchemy-0.2.0}/src/scim2_sqlalchemy/conversion.py +27 -6
- {scim2_sqlalchemy-0.1.1 → scim2_sqlalchemy-0.2.0}/src/scim2_sqlalchemy/generation.py +32 -10
- scim2_sqlalchemy-0.2.0/src/scim2_sqlalchemy/keyset.py +141 -0
- {scim2_sqlalchemy-0.1.1 → scim2_sqlalchemy-0.2.0}/src/scim2_sqlalchemy/mapping.py +237 -44
- {scim2_sqlalchemy-0.1.1 → scim2_sqlalchemy-0.2.0}/src/scim2_sqlalchemy/query.py +126 -25
- {scim2_sqlalchemy-0.1.1 → scim2_sqlalchemy-0.2.0}/src/scim2_sqlalchemy/storage.py +172 -115
- scim2_sqlalchemy-0.1.1/src/scim2_sqlalchemy/keyset.py +0 -143
- {scim2_sqlalchemy-0.1.1 → scim2_sqlalchemy-0.2.0}/LICENSE +0 -0
- {scim2_sqlalchemy-0.1.1 → scim2_sqlalchemy-0.2.0}/README.md +0 -0
- {scim2_sqlalchemy-0.1.1 → scim2_sqlalchemy-0.2.0}/src/scim2_sqlalchemy/__init__.py +0 -0
- {scim2_sqlalchemy-0.1.1 → scim2_sqlalchemy-0.2.0}/src/scim2_sqlalchemy/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: scim2-sqlalchemy
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: Painless SCIM integration for SQLAlchemy
|
|
5
5
|
Keywords: scim,scim2,provisioning,sqlalchemy,rfc7643,rfc7644
|
|
6
6
|
Author: Éloi Rivard
|
|
@@ -19,7 +19,7 @@ Classifier: Environment :: Web Environment
|
|
|
19
19
|
Classifier: Programming Language :: Python
|
|
20
20
|
Classifier: Operating System :: OS Independent
|
|
21
21
|
Requires-Dist: scim2-models>=0.12.2
|
|
22
|
-
Requires-Dist: scim2-server>=0.8.
|
|
22
|
+
Requires-Dist: scim2-server>=0.8.8
|
|
23
23
|
Requires-Dist: sqlalchemy>=2.0.41
|
|
24
24
|
Requires-Dist: sqlalchemy[asyncio]>=2.0.41 ; extra == 'asyncio'
|
|
25
25
|
Requires-Python: >=3.11
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
[build-system]
|
|
2
|
-
requires = ["uv_build>=0.12.13,<0.
|
|
2
|
+
requires = ["uv_build>=0.12.13,<0.14.0"]
|
|
3
3
|
build-backend = "uv_build"
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "scim2-sqlalchemy"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.2.0"
|
|
8
8
|
description = "Painless SCIM integration for SQLAlchemy"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = "Apache-2.0"
|
|
@@ -33,7 +33,7 @@ classifiers = [
|
|
|
33
33
|
requires-python = ">=3.11"
|
|
34
34
|
dependencies = [
|
|
35
35
|
"scim2-models>=0.12.2",
|
|
36
|
-
"scim2-server>=0.8.
|
|
36
|
+
"scim2-server>=0.8.8",
|
|
37
37
|
"sqlalchemy>=2.0.41",
|
|
38
38
|
]
|
|
39
39
|
|
|
@@ -56,14 +56,17 @@ dev = [
|
|
|
56
56
|
"mypy>=2.3.1",
|
|
57
57
|
"prek>=0.1.0",
|
|
58
58
|
"psycopg[binary]>=3.3.6",
|
|
59
|
-
"pytest>=
|
|
59
|
+
"pytest>=9.1.1",
|
|
60
60
|
"pytest-cov>=7.1.0",
|
|
61
61
|
"pytest-postgresql>=9.1.0",
|
|
62
|
-
"scim2-client>=0.
|
|
63
|
-
"scim2-server[testing]>=0.8.
|
|
64
|
-
"scim2-tester>=0.
|
|
62
|
+
"scim2-client>=0.14.0",
|
|
63
|
+
"scim2-server[testing]>=0.8.8",
|
|
64
|
+
"scim2-tester[testing]>=0.6.2",
|
|
65
65
|
"tox-uv>=1.36.0",
|
|
66
66
|
"werkzeug>=3.1.8",
|
|
67
|
+
"pytest-mysql>=5.0.1",
|
|
68
|
+
"pymysql>=1.2.4",
|
|
69
|
+
"asyncmy>=0.2.18",
|
|
67
70
|
]
|
|
68
71
|
doc = [
|
|
69
72
|
"shibuya>=2024.5.15",
|
|
@@ -126,6 +129,7 @@ testpaths = [
|
|
|
126
129
|
addopts = "--doctest-modules --doctest-glob='*.rst' --ignore=doc/conf.py"
|
|
127
130
|
pythonpath = ["doc/_examples"]
|
|
128
131
|
doctest_optionflags = "ALLOW_UNICODE ELLIPSIS NORMALIZE_WHITESPACE"
|
|
132
|
+
filterwarnings = ["error::scim2_tester.testing.ScimDeviationWarning"]
|
|
129
133
|
|
|
130
134
|
[tool.uv]
|
|
131
135
|
exclude-newer = "14 days"
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
[build-system]
|
|
2
|
-
requires = ["uv_build>=0.12.13,<0.
|
|
2
|
+
requires = ["uv_build>=0.12.13,<0.14.0"]
|
|
3
3
|
build-backend = "uv_build"
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "scim2-sqlalchemy"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.2.0"
|
|
8
8
|
description = "Painless SCIM integration for SQLAlchemy"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = "Apache-2.0"
|
|
@@ -29,7 +29,7 @@ authors = [
|
|
|
29
29
|
requires-python = ">=3.11"
|
|
30
30
|
dependencies = [
|
|
31
31
|
"scim2-models>=0.12.2",
|
|
32
|
-
"scim2-server>=0.8.
|
|
32
|
+
"scim2-server>=0.8.8",
|
|
33
33
|
"sqlalchemy>=2.0.41",
|
|
34
34
|
]
|
|
35
35
|
|
|
@@ -50,14 +50,17 @@ dev = [
|
|
|
50
50
|
"mypy>=2.3.1",
|
|
51
51
|
"prek>=0.1.0",
|
|
52
52
|
"psycopg[binary]>=3.3.6",
|
|
53
|
-
"pytest>=
|
|
53
|
+
"pytest>=9.1.1",
|
|
54
54
|
"pytest-cov>=7.1.0",
|
|
55
55
|
"pytest-postgresql>=9.1.0",
|
|
56
|
-
"scim2-client>=0.
|
|
57
|
-
"scim2-server[testing]>=0.8.
|
|
58
|
-
"scim2-tester>=0.
|
|
56
|
+
"scim2-client>=0.14.0",
|
|
57
|
+
"scim2-server[testing]>=0.8.8",
|
|
58
|
+
"scim2-tester[testing]>=0.6.2",
|
|
59
59
|
"tox-uv>=1.36.0",
|
|
60
60
|
"werkzeug>=3.1.8",
|
|
61
|
+
"pytest-mysql>=5.0.1",
|
|
62
|
+
"pymysql>=1.2.4",
|
|
63
|
+
"asyncmy>=0.2.18",
|
|
61
64
|
]
|
|
62
65
|
doc = [
|
|
63
66
|
"shibuya>=2024.5.15",
|
|
@@ -114,6 +117,7 @@ testpaths = ["tests", "doc"]
|
|
|
114
117
|
addopts = "--doctest-modules --doctest-glob='*.rst' --ignore=doc/conf.py"
|
|
115
118
|
pythonpath = ["doc/_examples"]
|
|
116
119
|
doctest_optionflags = "ALLOW_UNICODE ELLIPSIS NORMALIZE_WHITESPACE"
|
|
120
|
+
filterwarnings = ["error::scim2_tester.testing.ScimDeviationWarning"]
|
|
117
121
|
|
|
118
122
|
[tool.uv]
|
|
119
123
|
exclude-newer = "14 days"
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import unicodedata
|
|
2
2
|
import warnings
|
|
3
|
+
from abc import ABC
|
|
4
|
+
from abc import abstractmethod
|
|
3
5
|
from collections.abc import Iterable
|
|
4
6
|
from typing import Any
|
|
5
7
|
|
|
@@ -8,11 +10,16 @@ from scim2_models import ScimPolicy
|
|
|
8
10
|
from scim2_models import default_comparison_key
|
|
9
11
|
from scim2_models.path import CompareOperator
|
|
10
12
|
from sqlalchemy import ColumnElement
|
|
13
|
+
from sqlalchemy import Dialect
|
|
14
|
+
from sqlalchemy import FunctionElement
|
|
11
15
|
from sqlalchemy import String
|
|
12
16
|
from sqlalchemy import func
|
|
13
17
|
from sqlalchemy import literal
|
|
14
18
|
from sqlalchemy import literal_column
|
|
19
|
+
from sqlalchemy.ext.compiler import compiles
|
|
20
|
+
from sqlalchemy.sql.compiler import SQLCompiler
|
|
15
21
|
|
|
22
|
+
from .mapping import _MARIADB
|
|
16
23
|
from .mapping import ResourceMapping
|
|
17
24
|
from .mapping import _Column
|
|
18
25
|
|
|
@@ -26,6 +33,9 @@ _SUBSTRINGS = {
|
|
|
26
33
|
|
|
27
34
|
_KEY_FUNCTION = "scim2_key"
|
|
28
35
|
|
|
36
|
+
_MARIADB_VERSION = (10, 10)
|
|
37
|
+
"""The first MariaDB version with the uca1400 collations."""
|
|
38
|
+
|
|
29
39
|
|
|
30
40
|
def _escape(value: str) -> str:
|
|
31
41
|
"""Escape the LIKE wildcards of a value, so that they match themselves."""
|
|
@@ -72,7 +82,26 @@ class _Comparator:
|
|
|
72
82
|
return method(pattern, escape=_ESCAPE) # type: ignore[no-any-return]
|
|
73
83
|
|
|
74
84
|
|
|
75
|
-
class
|
|
85
|
+
class _Formed(_Comparator, ABC):
|
|
86
|
+
"""Compare strings in a form the database computes on both sides.
|
|
87
|
+
|
|
88
|
+
The value of a filter is normalized to NFC in Python first.
|
|
89
|
+
"""
|
|
90
|
+
|
|
91
|
+
@abstractmethod
|
|
92
|
+
def _form(self, column: _Column, expression: Any) -> ColumnElement[Any]:
|
|
93
|
+
"""Return a string expression in the form it is compared under."""
|
|
94
|
+
|
|
95
|
+
def column(self, column: _Column) -> ColumnElement[Any]:
|
|
96
|
+
"""Return a string column in the form it is compared under."""
|
|
97
|
+
return self._form(column, column.compared)
|
|
98
|
+
|
|
99
|
+
def operand(self, column: _Column, value: str) -> Any:
|
|
100
|
+
"""Return a string normalized to NFC, in the form it is compared under."""
|
|
101
|
+
return self._form(column, literal(unicodedata.normalize("NFC", value)))
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class _Normalizer(_Formed):
|
|
76
105
|
"""Compare strings in PostgreSQL, normalized to NFC on both sides.
|
|
77
106
|
|
|
78
107
|
The lower() of PostgreSQL follows the locale of the database, so the
|
|
@@ -89,13 +118,39 @@ class _Normalizer(_Comparator):
|
|
|
89
118
|
)
|
|
90
119
|
return normalized.collate("C")
|
|
91
120
|
|
|
92
|
-
def column(self, column: _Column) -> ColumnElement[Any]:
|
|
93
|
-
"""Return a string column normalized to NFC."""
|
|
94
|
-
return self._form(column, column.compared)
|
|
95
121
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
122
|
+
class _Utf8mb4(FunctionElement[str]):
|
|
123
|
+
"""A string converted to utf8mb4, the character set of the MariaDB collations."""
|
|
124
|
+
|
|
125
|
+
type = String()
|
|
126
|
+
inherit_cache = True
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
@compiles(_Utf8mb4)
|
|
130
|
+
def _compile_utf8mb4(element: _Utf8mb4, compiler: SQLCompiler, **kw: Any) -> str:
|
|
131
|
+
"""Write the conversion of a string to utf8mb4."""
|
|
132
|
+
(expression,) = element.clauses
|
|
133
|
+
return f"CONVERT({compiler.process(expression, **kw)} USING utf8mb4)"
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
class _CodePoints(_Formed):
|
|
137
|
+
"""Compare strings in MariaDB by code point.
|
|
138
|
+
|
|
139
|
+
The default collations ignore the case and the accents, and the trailing
|
|
140
|
+
spaces. utf8mb4_nopad_bin compares and orders by code point, as
|
|
141
|
+
scim2-models does. lower() only knows recent letters under a uca1400
|
|
142
|
+
collation. MariaDB has no normalize(), so a stored value is compared in
|
|
143
|
+
the form it was written in.
|
|
144
|
+
"""
|
|
145
|
+
|
|
146
|
+
def _form(self, column: _Column, expression: Any) -> ColumnElement[Any]:
|
|
147
|
+
"""Return a string expression in utf8mb4, lowered if the column ignores the case, under a binary collation."""
|
|
148
|
+
converted: ColumnElement[Any] = _Utf8mb4(expression)
|
|
149
|
+
if column.casefolded:
|
|
150
|
+
converted = func.lower(
|
|
151
|
+
converted.collate("utf8mb4_uca1400_nopad_as_cs"), type_=String
|
|
152
|
+
)
|
|
153
|
+
return converted.collate("utf8mb4_nopad_bin")
|
|
99
154
|
|
|
100
155
|
|
|
101
156
|
class _KeyFunction(_Comparator):
|
|
@@ -157,17 +212,32 @@ class _KeyFunction(_Comparator):
|
|
|
157
212
|
|
|
158
213
|
|
|
159
214
|
def _comparator(
|
|
160
|
-
dialect:
|
|
215
|
+
dialect: Dialect, policy: ScimPolicy, mappings: Iterable[ResourceMapping]
|
|
161
216
|
) -> _Comparator:
|
|
162
|
-
"""Return how a database compares strings, warning when it cannot follow the policy.
|
|
163
|
-
|
|
217
|
+
"""Return how a database compares strings, warning when it cannot follow the policy.
|
|
218
|
+
|
|
219
|
+
MySQL and MariaDB before 10.10 raise a ValueError.
|
|
220
|
+
"""
|
|
221
|
+
if dialect.name == "sqlite":
|
|
164
222
|
return _KeyFunction(policy, mappings)
|
|
223
|
+
mariadb = dialect.name in _MARIADB
|
|
224
|
+
if mariadb and not (
|
|
225
|
+
getattr(dialect, "is_mariadb", False)
|
|
226
|
+
and dialect.server_version_info is not None
|
|
227
|
+
and dialect.server_version_info >= _MARIADB_VERSION
|
|
228
|
+
):
|
|
229
|
+
raise ValueError(
|
|
230
|
+
"The storage needs MariaDB 10.10 or later: MySQL and the older "
|
|
231
|
+
"versions of MariaDB cannot compare strings by code point"
|
|
232
|
+
)
|
|
165
233
|
if policy.comparison_key is not default_comparison_key:
|
|
166
234
|
warnings.warn(
|
|
167
|
-
f"The {dialect} storage cannot compare strings with the comparison
|
|
168
|
-
"of the policy: filters and uniqueness checks only approach it",
|
|
235
|
+
f"The {dialect.name} storage cannot compare strings with the comparison "
|
|
236
|
+
"key of the policy: filters and uniqueness checks only approach it",
|
|
169
237
|
stacklevel=2,
|
|
170
238
|
)
|
|
171
|
-
if dialect == "postgresql":
|
|
239
|
+
if dialect.name == "postgresql":
|
|
172
240
|
return _Normalizer(policy)
|
|
241
|
+
if mariadb:
|
|
242
|
+
return _CodePoints(policy)
|
|
173
243
|
return _Comparator(policy)
|
|
@@ -172,8 +172,10 @@ def _from_scim(
|
|
|
172
172
|
if not collection.writable:
|
|
173
173
|
continue
|
|
174
174
|
if collection.owned:
|
|
175
|
-
|
|
176
|
-
|
|
175
|
+
value = [
|
|
176
|
+
_write_entry(collection, entry)
|
|
177
|
+
for entry in _entries(collection, resource)
|
|
178
|
+
]
|
|
177
179
|
elif collection.single:
|
|
178
180
|
value = next(iter(links[collection]), None)
|
|
179
181
|
else:
|
|
@@ -188,8 +190,6 @@ def _write_entry(collection: _Collection, entry: Any) -> Any:
|
|
|
188
190
|
setattr(record, collection.columns[None].key, _as_stored(entry))
|
|
189
191
|
return record
|
|
190
192
|
for column in collection.columns.values():
|
|
191
|
-
if not column.writable:
|
|
192
|
-
continue
|
|
193
193
|
value = getattr(entry, column.binding.target_field_name, None)
|
|
194
194
|
if value is not None:
|
|
195
195
|
setattr(record, column.key, _as_stored(value))
|
|
@@ -229,9 +229,12 @@ def _check_storable(
|
|
|
229
229
|
for a new one. A value equal to the stored one changes nothing, so a
|
|
230
230
|
resource read and sent back is accepted. A missing value changes nothing
|
|
231
231
|
either, except for a stored value whose column cannot hold none: removing it
|
|
232
|
-
raises MutabilityException.
|
|
233
|
-
|
|
232
|
+
raises MutabilityException. A new resource needs a value for each column
|
|
233
|
+
that cannot hold none and has no default. The read-only attributes of the
|
|
234
|
+
model of the resource are skipped, as RFC 7644 §3.5.1 has them ignored, and
|
|
235
|
+
so are the derived ones.
|
|
234
236
|
"""
|
|
237
|
+
_check_entries(mapping, resource)
|
|
235
238
|
model = type(resource)
|
|
236
239
|
for path in Path[model].iter_paths(): # type: ignore[valid-type]
|
|
237
240
|
binding = path.resolve()
|
|
@@ -252,6 +255,10 @@ def _check_storable(
|
|
|
252
255
|
current = _comparable(Path(binding.urn).get(stored, strict=False))
|
|
253
256
|
if storage == _Storage.written:
|
|
254
257
|
_check_enumerated(mapping, binding, value)
|
|
258
|
+
if value is None and stored is None and mapping._required(binding.urn):
|
|
259
|
+
raise InvalidValueException(
|
|
260
|
+
detail=f"'{binding.urn}' is required on this server"
|
|
261
|
+
)
|
|
255
262
|
if (
|
|
256
263
|
value is None
|
|
257
264
|
and current is not None
|
|
@@ -278,6 +285,20 @@ def _check_storable(
|
|
|
278
285
|
)
|
|
279
286
|
|
|
280
287
|
|
|
288
|
+
def _check_entries(mapping: ResourceMapping, resource: Resource[Any]) -> None:
|
|
289
|
+
"""Refuse an entry without a value that its column needs, rather than failing in the database."""
|
|
290
|
+
for collection in mapping._all_collections():
|
|
291
|
+
if not collection.owned or collection.simple or not collection.writable:
|
|
292
|
+
continue
|
|
293
|
+
required = [column for column in collection.columns.values() if column.required]
|
|
294
|
+
for entry in _entries(collection, resource):
|
|
295
|
+
for column in required:
|
|
296
|
+
if getattr(entry, column.binding.target_field_name, None) is None:
|
|
297
|
+
raise InvalidValueException(
|
|
298
|
+
detail=f"'{column.binding.urn}' is required on this server"
|
|
299
|
+
)
|
|
300
|
+
|
|
301
|
+
|
|
281
302
|
def _check_enumerated(
|
|
282
303
|
mapping: ResourceMapping, binding: AttributeBinding, value: Any
|
|
283
304
|
) -> None:
|
|
@@ -17,7 +17,7 @@ from scim2_models import Uniqueness
|
|
|
17
17
|
from sqlalchemy import Boolean
|
|
18
18
|
from sqlalchemy import Column
|
|
19
19
|
from sqlalchemy import DateTime
|
|
20
|
-
from sqlalchemy import
|
|
20
|
+
from sqlalchemy import Double
|
|
21
21
|
from sqlalchemy import ForeignKey
|
|
22
22
|
from sqlalchemy import Integer
|
|
23
23
|
from sqlalchemy import LargeBinary
|
|
@@ -25,11 +25,14 @@ from sqlalchemy import MetaData
|
|
|
25
25
|
from sqlalchemy import String
|
|
26
26
|
from sqlalchemy import Table
|
|
27
27
|
from sqlalchemy import Uuid
|
|
28
|
+
from sqlalchemy.dialects import mysql
|
|
28
29
|
from sqlalchemy.orm import DeclarativeBase
|
|
29
30
|
from sqlalchemy.orm import mapped_column
|
|
30
31
|
from sqlalchemy.orm import relationship
|
|
31
32
|
from sqlalchemy.types import TypeEngine
|
|
32
33
|
|
|
34
|
+
from .mapping import _MARIADB
|
|
35
|
+
from .mapping import _MARIADB_TEXT
|
|
33
36
|
from .mapping import Attribute
|
|
34
37
|
from .mapping import Link
|
|
35
38
|
from .mapping import Many
|
|
@@ -41,13 +44,24 @@ _MEMBERS = "urn:ietf:params:scim:schemas:core:2.0:Group:members"
|
|
|
41
44
|
_MAX_NAME_LENGTH = 63
|
|
42
45
|
"""The longest identifier PostgreSQL keeps."""
|
|
43
46
|
|
|
47
|
+
|
|
48
|
+
def _string() -> TypeEngine[Any]:
|
|
49
|
+
"""Return the type of a string column, a TEXT on MariaDB."""
|
|
50
|
+
return String().with_variant(_MARIADB_TEXT, *_MARIADB)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _date_time() -> TypeEngine[Any]:
|
|
54
|
+
"""Return the type of a date column, which keeps the microseconds on MariaDB too."""
|
|
55
|
+
return DateTime(timezone=True).with_variant(mysql.DATETIME(fsp=6), *_MARIADB)
|
|
56
|
+
|
|
57
|
+
|
|
44
58
|
_SQL_TYPES: dict[SchemaAttribute.Type, Callable[[], TypeEngine[Any]]] = {
|
|
45
|
-
SchemaAttribute.Type.string:
|
|
46
|
-
SchemaAttribute.Type.reference:
|
|
59
|
+
SchemaAttribute.Type.string: _string,
|
|
60
|
+
SchemaAttribute.Type.reference: _string,
|
|
47
61
|
SchemaAttribute.Type.boolean: Boolean,
|
|
48
62
|
SchemaAttribute.Type.integer: Integer,
|
|
49
|
-
SchemaAttribute.Type.decimal:
|
|
50
|
-
SchemaAttribute.Type.date_time:
|
|
63
|
+
SchemaAttribute.Type.decimal: Double,
|
|
64
|
+
SchemaAttribute.Type.date_time: _date_time,
|
|
51
65
|
SchemaAttribute.Type.binary: LargeBinary,
|
|
52
66
|
}
|
|
53
67
|
|
|
@@ -195,6 +209,12 @@ class _Generator:
|
|
|
195
209
|
continue
|
|
196
210
|
if attribute.mutability == Mutability.read_only:
|
|
197
211
|
continue
|
|
212
|
+
if (
|
|
213
|
+
attribute.multi_valued
|
|
214
|
+
and attribute.mutability == Mutability.write_only
|
|
215
|
+
):
|
|
216
|
+
self.decisions[urn] = False
|
|
217
|
+
continue
|
|
198
218
|
if attribute.type != SchemaAttribute.Type.complex:
|
|
199
219
|
if self._ask_column(model, urn, attribute):
|
|
200
220
|
clear.append(urn)
|
|
@@ -206,6 +226,10 @@ class _Generator:
|
|
|
206
226
|
if (
|
|
207
227
|
not sub.multi_valued
|
|
208
228
|
and sub.mutability != Mutability.read_only
|
|
229
|
+
and not (
|
|
230
|
+
attribute.multi_valued
|
|
231
|
+
and sub.mutability == Mutability.write_only
|
|
232
|
+
)
|
|
209
233
|
and self._ask_column(model, sub_urn, sub)
|
|
210
234
|
):
|
|
211
235
|
clear.append(sub_urn)
|
|
@@ -334,15 +358,15 @@ class _Generator:
|
|
|
334
358
|
version = mapped_column(Integer, nullable=False)
|
|
335
359
|
for key, value, path in [
|
|
336
360
|
("id", mapped_column(Uuid, primary_key=True, default=uuid.uuid4), "id"),
|
|
337
|
-
("external_id", mapped_column(
|
|
361
|
+
("external_id", mapped_column(_string()), "externalId"),
|
|
338
362
|
(
|
|
339
363
|
"meta_created",
|
|
340
|
-
mapped_column(
|
|
364
|
+
mapped_column(_date_time(), nullable=False),
|
|
341
365
|
"meta.created",
|
|
342
366
|
),
|
|
343
367
|
(
|
|
344
368
|
"meta_last_modified",
|
|
345
|
-
mapped_column(
|
|
369
|
+
mapped_column(_date_time(), nullable=False),
|
|
346
370
|
"meta.lastModified",
|
|
347
371
|
),
|
|
348
372
|
("meta_version", version, "meta.version"),
|
|
@@ -560,8 +584,6 @@ class _Generator:
|
|
|
560
584
|
urn: str,
|
|
561
585
|
) -> None:
|
|
562
586
|
"""Add the child table of a multi-valued simple attribute, with a value column."""
|
|
563
|
-
if attribute.mutability == Mutability.write_only:
|
|
564
|
-
return
|
|
565
587
|
child, key = self._child(name, record_class, path, key)
|
|
566
588
|
child.add(
|
|
567
589
|
"value",
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import decimal
|
|
2
|
+
from collections.abc import Sequence
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from scim2_models import InvalidCursorException
|
|
6
|
+
from scim2_server.keyset import Position
|
|
7
|
+
from sqlalchemy import BigInteger
|
|
8
|
+
from sqlalchemy import ColumnElement
|
|
9
|
+
from sqlalchemy import Integer
|
|
10
|
+
from sqlalchemy import and_
|
|
11
|
+
from sqlalchemy import false
|
|
12
|
+
from sqlalchemy import literal
|
|
13
|
+
from sqlalchemy import nulls_first
|
|
14
|
+
from sqlalchemy import nulls_last
|
|
15
|
+
from sqlalchemy import or_
|
|
16
|
+
from sqlalchemy import tuple_
|
|
17
|
+
from sqlalchemy.ext.compiler import compiles
|
|
18
|
+
from sqlalchemy.sql.compiler import SQLCompiler
|
|
19
|
+
from sqlalchemy.sql.visitors import InternalTraversal
|
|
20
|
+
|
|
21
|
+
from .mapping import _INTEGERS
|
|
22
|
+
from .mapping import _python_type
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class _Sorted(ColumnElement[Any]):
|
|
26
|
+
"""A sort term on a value, placing the NULL values first or last."""
|
|
27
|
+
|
|
28
|
+
inherit_cache = True
|
|
29
|
+
_traverse_internals = [
|
|
30
|
+
("value", InternalTraversal.dp_clauseelement),
|
|
31
|
+
("ascending", InternalTraversal.dp_boolean),
|
|
32
|
+
("last", InternalTraversal.dp_boolean),
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
def __init__(self, value: Any, ascending: bool, last: bool) -> None:
|
|
36
|
+
self.value = value
|
|
37
|
+
self.ascending = ascending
|
|
38
|
+
self.last = last
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@compiles(_Sorted)
|
|
42
|
+
def _compile_sorted(element: _Sorted, compiler: SQLCompiler, **kw: Any) -> str:
|
|
43
|
+
"""Write the sort term with NULLS FIRST or NULLS LAST."""
|
|
44
|
+
term = element.value.asc() if element.ascending else element.value.desc()
|
|
45
|
+
placed = nulls_last(term) if element.last else nulls_first(term)
|
|
46
|
+
return compiler.process(placed, **kw)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@compiles(_Sorted, "mariadb")
|
|
50
|
+
@compiles(_Sorted, "mysql")
|
|
51
|
+
def _compile_sorted_mariadb(element: _Sorted, compiler: SQLCompiler, **kw: Any) -> str:
|
|
52
|
+
"""Write the sort term for MariaDB, which has no NULLS FIRST or NULLS LAST.
|
|
53
|
+
|
|
54
|
+
MariaDB places NULL first when ascending and last when descending. A sort
|
|
55
|
+
on whether the value is NULL comes first when it places NULL otherwise.
|
|
56
|
+
"""
|
|
57
|
+
key = element.value
|
|
58
|
+
term = compiler.process(key.asc() if element.ascending else key.desc(), **kw)
|
|
59
|
+
if element.last != element.ascending:
|
|
60
|
+
return term
|
|
61
|
+
nulls = key.is_(None)
|
|
62
|
+
placed = nulls.asc() if element.last else nulls.desc()
|
|
63
|
+
return f"{compiler.process(placed, **kw)}, {term}"
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _key_order(key: Any, ascending: bool) -> Any:
|
|
67
|
+
"""Per RFC 7644 §3.4.2.3, resources without a value come last when ascending and first when descending."""
|
|
68
|
+
return _Sorted(key, ascending, last=ascending)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _keyset_order(
|
|
72
|
+
key: Any, ties: Sequence[Any], descending: bool, forward: bool
|
|
73
|
+
) -> list[Any]:
|
|
74
|
+
"""Return the ORDER BY terms of a search: the sort value, then the ties.
|
|
75
|
+
|
|
76
|
+
A previous page is read backward, in the reverse order of the sort.
|
|
77
|
+
"""
|
|
78
|
+
terms = [] if key is None else [_key_order(key, descending != forward)]
|
|
79
|
+
return [*terms, *(tie.asc() if forward else tie.desc() for tie in ties)]
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _beyond(
|
|
83
|
+
key: Any,
|
|
84
|
+
ties: Sequence[Any],
|
|
85
|
+
anchor: Position,
|
|
86
|
+
values: Sequence[Any],
|
|
87
|
+
descending: bool,
|
|
88
|
+
) -> ColumnElement[bool]:
|
|
89
|
+
"""Return the condition keeping the rows past the anchor, in the reading direction.
|
|
90
|
+
|
|
91
|
+
values are the ties of the anchor, in the types of the ties. The
|
|
92
|
+
condition on the sort value is written out, as a row comparison would not
|
|
93
|
+
place NULL where the sort does.
|
|
94
|
+
"""
|
|
95
|
+
tie_after: ColumnElement[bool]
|
|
96
|
+
if len(ties) == 1:
|
|
97
|
+
tie_after = ties[0] > values[0] if anchor.forward else ties[0] < values[0]
|
|
98
|
+
else:
|
|
99
|
+
row, bound = tuple_(*ties), tuple_(*values)
|
|
100
|
+
tie_after = row > bound if anchor.forward else row < bound
|
|
101
|
+
if key is None:
|
|
102
|
+
return tie_after
|
|
103
|
+
|
|
104
|
+
ascending = descending != anchor.forward
|
|
105
|
+
if anchor.key is None:
|
|
106
|
+
key_after = key.is_not(None) if not ascending else false()
|
|
107
|
+
return or_(key_after, and_(key.is_(None), tie_after))
|
|
108
|
+
value = _sort_value(key, anchor.key)
|
|
109
|
+
key_after = or_(key > value, key.is_(None)) if ascending else key < value
|
|
110
|
+
return or_(key_after, and_(key == value, tie_after))
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
_ACCEPTED: dict[type, tuple[type, ...]] = {
|
|
114
|
+
float: (float, int),
|
|
115
|
+
decimal.Decimal: (decimal.Decimal, int),
|
|
116
|
+
}
|
|
117
|
+
"""The types of the sort values a key of decimal numbers accepts."""
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _sort_value(key: Any, value: Any) -> Any:
|
|
121
|
+
"""Return the sort value of an anchor, ready to compare with the sort key.
|
|
122
|
+
|
|
123
|
+
A value of another type than the key is refused, such as a cursor given
|
|
124
|
+
before the sort attribute moved to another column. An integer is a value
|
|
125
|
+
of a key of decimal numbers, since a database may return a whole number
|
|
126
|
+
as an integer. An integer no database stores is refused too. An integer
|
|
127
|
+
compared with a key of integers is sent as a BIGINT, as filters send it.
|
|
128
|
+
"""
|
|
129
|
+
python_type = _python_type(key)
|
|
130
|
+
if python_type is not None and (
|
|
131
|
+
not isinstance(value, _ACCEPTED.get(python_type, python_type))
|
|
132
|
+
or (isinstance(value, bool) and python_type is not bool)
|
|
133
|
+
):
|
|
134
|
+
raise InvalidCursorException
|
|
135
|
+
if not isinstance(value, int) or isinstance(value, bool):
|
|
136
|
+
return value
|
|
137
|
+
if value not in _INTEGERS:
|
|
138
|
+
raise InvalidCursorException
|
|
139
|
+
if isinstance(key.type, Integer):
|
|
140
|
+
return literal(value, BigInteger)
|
|
141
|
+
return value
|