sqlrules-sqlite 1.0.1__py3-none-any.whl → 2.0.0__py3-none-any.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.
- sqlrules_sqlite/__init__.py +50 -10
- sqlrules_sqlite/json.py +75 -35
- sqlrules_sqlite/length.py +35 -0
- sqlrules_sqlite/pattern.py +2 -1
- sqlrules_sqlite/regexp.py +5 -22
- sqlrules_sqlite/runtime.py +34 -0
- sqlrules_sqlite-2.0.0.dist-info/METADATA +84 -0
- sqlrules_sqlite-2.0.0.dist-info/RECORD +11 -0
- {sqlrules_sqlite-1.0.1.dist-info → sqlrules_sqlite-2.0.0.dist-info}/WHEEL +1 -1
- sqlrules_sqlite/type_check.py +0 -213
- sqlrules_sqlite-1.0.1.dist-info/METADATA +0 -83
- sqlrules_sqlite-1.0.1.dist-info/RECORD +0 -10
- {sqlrules_sqlite-1.0.1.dist-info → sqlrules_sqlite-2.0.0.dist-info}/licenses/LICENSE +0 -0
sqlrules_sqlite/__init__.py
CHANGED
|
@@ -1,35 +1,75 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
from collections.abc import Mapping
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from sqlrules.backend import prepare_sqlite_scalar
|
|
7
|
+
from sqlrules.ir import CompilationContext, PreparedValue, RuleField
|
|
3
8
|
from sqlrules.plugins import PLUGIN_API_VERSION
|
|
4
9
|
from sqlrules.translators import TranslatorRegistry
|
|
5
10
|
from sqlrules_sqlite.json import translate_json_contains, translate_json_has_key
|
|
11
|
+
from sqlrules_sqlite.length import translate_max_length, translate_min_length
|
|
6
12
|
from sqlrules_sqlite.pattern import translate_pattern
|
|
7
13
|
from sqlrules_sqlite.regexp import register_regexp
|
|
8
|
-
from sqlrules_sqlite.
|
|
14
|
+
from sqlrules_sqlite.runtime import register_sqlite_functions
|
|
9
15
|
|
|
10
|
-
__version__ = "
|
|
16
|
+
__version__ = "2.0.0"
|
|
11
17
|
|
|
12
18
|
|
|
13
19
|
class SQLitePlugin:
|
|
14
20
|
"""Register SQLite-specific constraint translators.
|
|
15
21
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
22
|
+
Pattern and text-coercion expressions emit ``column REGEXP pattern``;
|
|
23
|
+
length constraints use SQLRules' Unicode-aware character counter. Call
|
|
24
|
+
:func:`register_sqlite_functions` on each SQLite connection before
|
|
25
|
+
executing SQL that needs these functions. ``register_regexp`` remains as a
|
|
26
|
+
backward-compatible alias.
|
|
19
27
|
"""
|
|
20
28
|
|
|
21
29
|
name = "sqlite"
|
|
22
30
|
api_version = PLUGIN_API_VERSION
|
|
23
31
|
|
|
32
|
+
def __init__(self, *, server_version: tuple[int, ...] | None = None) -> None:
|
|
33
|
+
self.server_version = tuple(server_version) if server_version is not None else None
|
|
34
|
+
|
|
35
|
+
def capabilities(self) -> Mapping[str, Any]:
|
|
36
|
+
return {
|
|
37
|
+
"backend": self.name,
|
|
38
|
+
"server_version": self.server_version,
|
|
39
|
+
"native_scalar_types": ("bool", "int", "float", "str"),
|
|
40
|
+
"safe_text_numeric_conversion": True,
|
|
41
|
+
"safe_text_numeric_targets": ("int", "float"),
|
|
42
|
+
"assumptions": (
|
|
43
|
+
"Runtime typeof() storage classes determine bool/int/float/text values.",
|
|
44
|
+
"Register SQLRules SQLite functions on each connection when using text "
|
|
45
|
+
"coercion, patterns, or string length constraints.",
|
|
46
|
+
"Date/time/UUID and exact Decimal checks require "
|
|
47
|
+
"a future explicit storage adapter.",
|
|
48
|
+
),
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
def prepare_value(
|
|
52
|
+
self,
|
|
53
|
+
column: Any,
|
|
54
|
+
field: RuleField,
|
|
55
|
+
context: CompilationContext,
|
|
56
|
+
) -> PreparedValue:
|
|
57
|
+
return prepare_sqlite_scalar(column, field, context)
|
|
58
|
+
|
|
24
59
|
def register(self, registry: TranslatorRegistry) -> None:
|
|
25
60
|
registry.register_constraint(
|
|
26
|
-
"
|
|
27
|
-
|
|
61
|
+
"min_length",
|
|
62
|
+
translate_min_length,
|
|
28
63
|
on_conflict="replace",
|
|
29
64
|
)
|
|
30
65
|
registry.register_constraint(
|
|
31
|
-
"
|
|
32
|
-
|
|
66
|
+
"max_length",
|
|
67
|
+
translate_max_length,
|
|
68
|
+
on_conflict="replace",
|
|
69
|
+
)
|
|
70
|
+
registry.register_constraint(
|
|
71
|
+
"pattern",
|
|
72
|
+
translate_pattern,
|
|
33
73
|
on_conflict="replace",
|
|
34
74
|
)
|
|
35
75
|
registry.register_constraint(
|
|
@@ -48,8 +88,8 @@ __all__ = [
|
|
|
48
88
|
"SQLitePlugin",
|
|
49
89
|
"__version__",
|
|
50
90
|
"register_regexp",
|
|
91
|
+
"register_sqlite_functions",
|
|
51
92
|
"translate_json_contains",
|
|
52
93
|
"translate_json_has_key",
|
|
53
94
|
"translate_pattern",
|
|
54
|
-
"translate_type_check",
|
|
55
95
|
]
|
sqlrules_sqlite/json.py
CHANGED
|
@@ -3,7 +3,7 @@ from __future__ import annotations
|
|
|
3
3
|
import json
|
|
4
4
|
from typing import Any, cast
|
|
5
5
|
|
|
6
|
-
from sqlalchemy import String, func, literal, type_coerce
|
|
6
|
+
from sqlalchemy import String, and_, case, func, literal, select, type_coerce
|
|
7
7
|
from sqlalchemy.sql.elements import ColumnElement
|
|
8
8
|
|
|
9
9
|
from sqlrules.ir import CompilationContext, Constraint
|
|
@@ -20,27 +20,70 @@ def _compact_dumps(value: Any) -> str:
|
|
|
20
20
|
return json.dumps(value, separators=(",", ":"))
|
|
21
21
|
|
|
22
22
|
|
|
23
|
-
def
|
|
24
|
-
|
|
23
|
+
def _safe_json(column: ColumnElement[Any]) -> ColumnElement[Any]:
|
|
24
|
+
"""Replace malformed documents before any JSON1 function can inspect them."""
|
|
25
|
+
return case(
|
|
26
|
+
(func.json_valid(column), type_coerce(column, String)),
|
|
27
|
+
else_=literal("{}"),
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _json_child_count(
|
|
32
|
+
document: ColumnElement[Any],
|
|
33
|
+
path: str,
|
|
34
|
+
) -> ColumnElement[Any]:
|
|
35
|
+
children = func.json_each(document, path).table_valued("key").alias()
|
|
36
|
+
return select(func.count()).select_from(children).scalar_subquery()
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _join_json_path(parent: str, child: str) -> str:
|
|
40
|
+
return f"{parent}{child[1:]}"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _json_value_equals(
|
|
44
|
+
document: ColumnElement[Any],
|
|
25
45
|
path: str,
|
|
26
46
|
expected: Any,
|
|
27
47
|
) -> ColumnElement[bool]:
|
|
28
|
-
"""Compare
|
|
29
|
-
|
|
48
|
+
"""Compare JSON values structurally, independent of object key order."""
|
|
49
|
+
json_type = func.json_type(document, path)
|
|
50
|
+
extracted = func.json_extract(document, path)
|
|
30
51
|
if expected is None:
|
|
31
|
-
return cast(ColumnElement[bool],
|
|
52
|
+
return cast(ColumnElement[bool], json_type == "null")
|
|
32
53
|
if isinstance(expected, bool):
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
54
|
+
expected_type = "true" if expected else "false"
|
|
55
|
+
return cast(ColumnElement[bool], json_type == expected_type)
|
|
56
|
+
if isinstance(expected, dict):
|
|
57
|
+
parts: list[ColumnElement[bool]] = [
|
|
58
|
+
cast(ColumnElement[bool], json_type == "object"),
|
|
59
|
+
cast(ColumnElement[bool], _json_child_count(document, path) == len(expected)),
|
|
60
|
+
]
|
|
61
|
+
parts.extend(
|
|
62
|
+
_json_value_equals(
|
|
63
|
+
document,
|
|
64
|
+
_join_json_path(path, _json_path_for_key(key)),
|
|
65
|
+
child,
|
|
66
|
+
)
|
|
67
|
+
for key, child in expected.items()
|
|
68
|
+
)
|
|
69
|
+
return cast(ColumnElement[bool], and_(*parts))
|
|
70
|
+
if isinstance(expected, list):
|
|
71
|
+
parts = [
|
|
72
|
+
cast(ColumnElement[bool], json_type == "array"),
|
|
73
|
+
cast(ColumnElement[bool], _json_child_count(document, path) == len(expected)),
|
|
74
|
+
]
|
|
75
|
+
parts.extend(
|
|
76
|
+
_json_value_equals(document, f"{path}[{index}]", child)
|
|
77
|
+
for index, child in enumerate(expected)
|
|
78
|
+
)
|
|
79
|
+
return cast(ColumnElement[bool], and_(*parts))
|
|
80
|
+
if isinstance(expected, (int, float)):
|
|
36
81
|
return cast(
|
|
37
82
|
ColumnElement[bool],
|
|
38
|
-
|
|
83
|
+
json_type.in_(("integer", "real")) & (extracted == expected),
|
|
39
84
|
)
|
|
40
|
-
if isinstance(expected, (int, float)):
|
|
41
|
-
return cast(ColumnElement[bool], extracted == expected)
|
|
42
85
|
if isinstance(expected, str):
|
|
43
|
-
return cast(ColumnElement[bool], extracted == expected)
|
|
86
|
+
return cast(ColumnElement[bool], (json_type == "text") & (extracted == expected))
|
|
44
87
|
compact = _compact_dumps(expected)
|
|
45
88
|
return cast(
|
|
46
89
|
ColumnElement[bool],
|
|
@@ -53,34 +96,28 @@ def translate_json_contains(
|
|
|
53
96
|
column: ColumnElement[Any],
|
|
54
97
|
context: CompilationContext,
|
|
55
98
|
) -> ColumnElement[bool]:
|
|
56
|
-
"""Translate
|
|
57
|
-
|
|
58
|
-
For object payloads, checks that each top-level key extracts to the
|
|
59
|
-
expected JSON value. This is a deterministic subset of JSON containment
|
|
60
|
-
suitable for common filter models; nested deep-merge semantics are not
|
|
61
|
-
emulated.
|
|
62
|
-
"""
|
|
99
|
+
"""Translate the supported SQLite JSON containment subset safely."""
|
|
63
100
|
value = constraint.value
|
|
101
|
+
safe_json = _safe_json(column)
|
|
102
|
+
valid_document = func.json_valid(column)
|
|
64
103
|
if isinstance(value, dict):
|
|
65
104
|
if not value:
|
|
66
|
-
# Align with PostgreSQL ``@> '{}'``: require a non-NULL JSON object.
|
|
67
105
|
return cast(
|
|
68
106
|
ColumnElement[bool],
|
|
69
|
-
column.is_not(None) & (func.json_type(
|
|
107
|
+
column.is_not(None) & valid_document & (func.json_type(safe_json) == "object"),
|
|
70
108
|
)
|
|
71
|
-
parts
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
compact = _compact_dumps(value)
|
|
109
|
+
parts = [
|
|
110
|
+
_json_value_equals(safe_json, _json_path_for_key(key), expected)
|
|
111
|
+
for key, expected in value.items()
|
|
112
|
+
]
|
|
113
|
+
return cast(
|
|
114
|
+
ColumnElement[bool],
|
|
115
|
+
valid_document & (func.json_type(safe_json) == "object") & and_(*parts),
|
|
116
|
+
)
|
|
117
|
+
|
|
81
118
|
return cast(
|
|
82
119
|
ColumnElement[bool],
|
|
83
|
-
|
|
120
|
+
valid_document & _json_value_equals(safe_json, "$", value),
|
|
84
121
|
)
|
|
85
122
|
|
|
86
123
|
|
|
@@ -89,6 +126,9 @@ def translate_json_has_key(
|
|
|
89
126
|
column: ColumnElement[Any],
|
|
90
127
|
context: CompilationContext,
|
|
91
128
|
) -> ColumnElement[bool]:
|
|
92
|
-
"""Translate ``json_has_key``
|
|
129
|
+
"""Translate ``json_has_key`` without evaluating JSON1 on malformed input."""
|
|
93
130
|
path = _json_path_for_key(constraint.value)
|
|
94
|
-
return cast(
|
|
131
|
+
return cast(
|
|
132
|
+
ColumnElement[bool],
|
|
133
|
+
func.json_valid(column) & func.json_type(_safe_json(column), path).is_not(None),
|
|
134
|
+
)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any, cast
|
|
4
|
+
|
|
5
|
+
from sqlalchemy import func
|
|
6
|
+
from sqlalchemy.sql.elements import ColumnElement
|
|
7
|
+
|
|
8
|
+
from sqlrules.ir import CompilationContext, Constraint
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def translate_min_length(
|
|
12
|
+
constraint: Constraint,
|
|
13
|
+
column: ColumnElement[Any],
|
|
14
|
+
context: CompilationContext,
|
|
15
|
+
) -> ColumnElement[bool]:
|
|
16
|
+
"""Use the registered Python-compatible code-point counter."""
|
|
17
|
+
return cast(
|
|
18
|
+
ColumnElement[bool],
|
|
19
|
+
func.sqlrules_char_length(column) >= constraint.value,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def translate_max_length(
|
|
24
|
+
constraint: Constraint,
|
|
25
|
+
column: ColumnElement[Any],
|
|
26
|
+
context: CompilationContext,
|
|
27
|
+
) -> ColumnElement[bool]:
|
|
28
|
+
"""Use the registered Python-compatible code-point counter."""
|
|
29
|
+
return cast(
|
|
30
|
+
ColumnElement[bool],
|
|
31
|
+
func.sqlrules_char_length(column) <= constraint.value,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
__all__ = ["translate_max_length", "translate_min_length"]
|
sqlrules_sqlite/pattern.py
CHANGED
|
@@ -16,7 +16,8 @@ def translate_pattern(
|
|
|
16
16
|
"""Translate ``pattern`` to SQLite ``column REGEXP pattern``.
|
|
17
17
|
|
|
18
18
|
Case-insensitive patterns are encoded with a ``(?i)`` prefix so
|
|
19
|
-
:func:`sqlrules_sqlite.
|
|
19
|
+
:func:`sqlrules_sqlite.register_sqlite_functions` can apply
|
|
20
|
+
``re.IGNORECASE``.
|
|
20
21
|
Callers must enable REGEXP on the SQLite connection before execution.
|
|
21
22
|
"""
|
|
22
23
|
pattern, ignore_case = pattern_text(constraint.value)
|
sqlrules_sqlite/regexp.py
CHANGED
|
@@ -1,30 +1,13 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
-
import re
|
|
4
3
|
import sqlite3
|
|
5
4
|
|
|
5
|
+
from sqlrules_sqlite.runtime import register_sqlite_functions
|
|
6
6
|
|
|
7
|
-
def register_regexp(connection: sqlite3.Connection) -> None:
|
|
8
|
-
"""Register a flag-aware ``REGEXP`` function on a SQLite connection.
|
|
9
|
-
|
|
10
|
-
The SQLRules SQLite ``pattern`` translator emits
|
|
11
|
-
``column REGEXP pattern``. SQLite does not ship REGEXP by default;
|
|
12
|
-
call this once per connection before executing compiled SQL.
|
|
13
7
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
Invalid patterns raise ``re.error`` (surfaced by SQLite as an
|
|
18
|
-
operational error) instead of silently matching nothing.
|
|
19
|
-
"""
|
|
8
|
+
def register_regexp(connection: sqlite3.Connection) -> None:
|
|
9
|
+
"""Backward-compatible alias for :func:`register_sqlite_functions`."""
|
|
10
|
+
register_sqlite_functions(connection)
|
|
20
11
|
|
|
21
|
-
def regexp(pattern: str | None, value: str | None) -> bool:
|
|
22
|
-
if pattern is None or value is None:
|
|
23
|
-
return False
|
|
24
|
-
flags = 0
|
|
25
|
-
if pattern.startswith("(?i)"):
|
|
26
|
-
flags |= re.IGNORECASE
|
|
27
|
-
pattern = pattern[4:]
|
|
28
|
-
return re.search(pattern, value, flags) is not None
|
|
29
12
|
|
|
30
|
-
|
|
13
|
+
__all__ = ["register_regexp"]
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
import sqlite3
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def _regexp(pattern: str | None, value: str | None) -> bool:
|
|
9
|
+
# SQLite may evaluate a REGEXP branch even when a neighboring typeof()
|
|
10
|
+
# predicate is false. Treat non-text source values as a non-match so
|
|
11
|
+
# mixed-storage tables cannot raise Python TypeError.
|
|
12
|
+
if not isinstance(pattern, str) or not isinstance(value, str):
|
|
13
|
+
return False
|
|
14
|
+
flags = 0
|
|
15
|
+
if pattern.startswith("(?i)"):
|
|
16
|
+
flags |= re.IGNORECASE
|
|
17
|
+
pattern = pattern[4:]
|
|
18
|
+
return re.search(pattern, value, flags) is not None
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _char_length(value: Any) -> int | None:
|
|
22
|
+
"""Use Python's Unicode code-point count, including text after U+0000."""
|
|
23
|
+
return len(value) if isinstance(value, str) else None
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def register_sqlite_functions(connection: sqlite3.Connection) -> None:
|
|
27
|
+
"""Register SQLRules functions needed by SQLite rules on this connection.
|
|
28
|
+
|
|
29
|
+
SQLite does not provide REGEXP, and its built-in length(TEXT) stops at the
|
|
30
|
+
first U+0000. SQLRules uses these callbacks for regex/text coercion and
|
|
31
|
+
exact Python-compatible string length constraints.
|
|
32
|
+
"""
|
|
33
|
+
connection.create_function("REGEXP", 2, _regexp)
|
|
34
|
+
connection.create_function("sqlrules_char_length", 1, _char_length)
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: sqlrules-sqlite
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: SQLite dialect plugin for SQLRules (REGEXP, Unicode length, and JSON helpers).
|
|
5
|
+
Project-URL: Homepage, https://github.com/eddiethedean/sqlrules
|
|
6
|
+
Project-URL: Repository, https://github.com/eddiethedean/sqlrules
|
|
7
|
+
Project-URL: Issues, https://github.com/eddiethedean/sqlrules/issues
|
|
8
|
+
Author: SQLRules Contributors
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: pydantic,sqlalchemy,sqlite,sqlrules
|
|
12
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Typing :: Typed
|
|
16
|
+
Requires-Python: >=3.10
|
|
17
|
+
Requires-Dist: sqlalchemy<3,>=2.0
|
|
18
|
+
Requires-Dist: sqlrules<3,>=2
|
|
19
|
+
Provides-Extra: dev
|
|
20
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
|
|
23
|
+
# sqlrules-sqlite
|
|
24
|
+
|
|
25
|
+
SQLite backend provider for [SQLRules](https://github.com/eddiethedean/sqlrules).
|
|
26
|
+
The package version follows the core 2.x line.
|
|
27
|
+
|
|
28
|
+
## Install
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
pip install "sqlrules>=2,<3" "sqlrules-sqlite>=2,<3"
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Use
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
from typing import Annotated, Any
|
|
38
|
+
|
|
39
|
+
from pydantic import Field
|
|
40
|
+
from sqlalchemy import Column, JSON, MetaData, String, Table, create_engine, event
|
|
41
|
+
|
|
42
|
+
from sqlrules import Compiler, JsonContains, RuleSchema, where
|
|
43
|
+
from sqlrules_sqlite import SQLitePlugin, register_sqlite_functions
|
|
44
|
+
|
|
45
|
+
rows = Table("rows", MetaData(), Column("name", String), Column("meta", JSON))
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class RowRules(RuleSchema):
|
|
49
|
+
name: Annotated[str, Field(pattern=r"^A")]
|
|
50
|
+
meta: Annotated[dict[str, Any], JsonContains({"active": True})]
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
engine = create_engine("sqlite:///app.db")
|
|
54
|
+
event.listen(
|
|
55
|
+
engine,
|
|
56
|
+
"connect",
|
|
57
|
+
lambda dbapi_connection, _: register_sqlite_functions(dbapi_connection),
|
|
58
|
+
)
|
|
59
|
+
compiled = Compiler(plugins=[SQLitePlugin()]).compile(RowRules, rows)
|
|
60
|
+
statement = rows.select().where(*where(compiled))
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Capabilities
|
|
64
|
+
|
|
65
|
+
- Runtime `typeof()` checks for SQLite integer, real, and text storage classes
|
|
66
|
+
- Lax text-to-int/float coercion and bool values stored as integer 0 or 1
|
|
67
|
+
- JSON1 helpers that treat malformed documents as non-matches
|
|
68
|
+
- `REGEXP` pattern matching and Unicode-aware string length through
|
|
69
|
+
`register_sqlite_functions()`
|
|
70
|
+
|
|
71
|
+
Strict bool, exact Decimal, and SQLAlchemy-emulated date/time/UUID values need
|
|
72
|
+
explicit storage adapters and raise `CapabilityError`. Text coercion, patterns,
|
|
73
|
+
and string length constraints require the registered SQLRules SQLite functions
|
|
74
|
+
on each connection. `register_regexp()` remains as a backward-compatible alias.
|
|
75
|
+
See the
|
|
76
|
+
[type support matrix](https://sqlrules.readthedocs.io/en/latest/TYPE_SUPPORT.html).
|
|
77
|
+
|
|
78
|
+
## Pattern cost
|
|
79
|
+
|
|
80
|
+
`register_sqlite_functions()` runs Python's `re.search` for each regex row and
|
|
81
|
+
Python's `len()` for each length-checked string row. Untrusted patterns can
|
|
82
|
+
cause CPU denial of service through catastrophic backtracking. Prefer static
|
|
83
|
+
or allowlisted patterns. See the SQLRules
|
|
84
|
+
[security notes](https://sqlrules.readthedocs.io/en/latest/SECURITY.html).
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
sqlrules_sqlite/__init__.py,sha256=o24arJiu4TFstqKLfPL9P0TApYLexhU4V49UrM4EDKk,3253
|
|
2
|
+
sqlrules_sqlite/json.py,sha256=GmRD0dKi0chsKHFazoPINR3EZrzv-mY79jlmgQKFpIQ,4548
|
|
3
|
+
sqlrules_sqlite/length.py,sha256=6b2tHN0ZSKz1hLJR1UAHlJkjiYZ_5HOlAKmndOz1rdI,919
|
|
4
|
+
sqlrules_sqlite/pattern.py,sha256=3G0MS429eg5ZB7txFYHdAI53qXoc7Nnu8Oe7Q3WBaHw,881
|
|
5
|
+
sqlrules_sqlite/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
6
|
+
sqlrules_sqlite/regexp.py,sha256=sujtKjwg0EeNpc7yxCITzDkn5lhxRVt2TKUyKAD8J6Q,326
|
|
7
|
+
sqlrules_sqlite/runtime.py,sha256=W-H2UeiJr2rZ8uR4AC7TmyOb7TOBEc1hh0IkD2zOSG4,1284
|
|
8
|
+
sqlrules_sqlite-2.0.0.dist-info/METADATA,sha256=BtTaB1TJtxYOVKjgiutKbyE0AOIWFep0PTfkvsib9Wc,2990
|
|
9
|
+
sqlrules_sqlite-2.0.0.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
|
|
10
|
+
sqlrules_sqlite-2.0.0.dist-info/licenses/LICENSE,sha256=cLMlkCH6RhjWAkXBWR7LazV98TOG0WkPtCKB5zqqkAs,1078
|
|
11
|
+
sqlrules_sqlite-2.0.0.dist-info/RECORD,,
|
sqlrules_sqlite/type_check.py
DELETED
|
@@ -1,213 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
from datetime import date, datetime, time
|
|
4
|
-
from decimal import Decimal
|
|
5
|
-
from typing import Any, cast
|
|
6
|
-
from uuid import UUID
|
|
7
|
-
|
|
8
|
-
from sqlalchemy import Boolean, Date, DateTime, Float, Integer, Numeric, String, Time
|
|
9
|
-
from sqlalchemy.sql import func
|
|
10
|
-
from sqlalchemy.sql.elements import ColumnElement
|
|
11
|
-
from sqlalchemy.sql.sqltypes import NullType
|
|
12
|
-
from sqlalchemy.types import TypeEngine
|
|
13
|
-
|
|
14
|
-
from sqlrules.constraints import type_spec
|
|
15
|
-
from sqlrules.errors import UnsupportedConstraintError
|
|
16
|
-
from sqlrules.ir import CompilationContext, Constraint, TypeSpec
|
|
17
|
-
|
|
18
|
-
_INT_TEXT = r"^[+-]?(0|[1-9]\d*)$"
|
|
19
|
-
_FLOAT_TEXT = r"^[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$"
|
|
20
|
-
_DATE_TEXT = r"^\d{4}-\d{2}-\d{2}$"
|
|
21
|
-
_DATETIME_TEXT = r"^\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(\.\d+)?([+-]\d{2}:?\d{2}|Z)?$"
|
|
22
|
-
_TIME_TEXT = r"^\d{2}:\d{2}:\d{2}(\.\d+)?$"
|
|
23
|
-
_UUID_TEXT = (
|
|
24
|
-
r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-"
|
|
25
|
-
r"[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
|
|
26
|
-
)
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
def _is_type(column: ColumnElement[Any], *bases: type[TypeEngine[Any]]) -> bool:
|
|
30
|
-
col_type = column.type
|
|
31
|
-
if isinstance(col_type, NullType):
|
|
32
|
-
return False
|
|
33
|
-
return isinstance(col_type, bases)
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
def _regexp(column: ColumnElement[Any], pattern: str) -> ColumnElement[bool]:
|
|
37
|
-
return cast(ColumnElement[bool], column.op("REGEXP")(pattern))
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
def _wrap_none(
|
|
41
|
-
column: ColumnElement[Any],
|
|
42
|
-
predicate: ColumnElement[bool],
|
|
43
|
-
*,
|
|
44
|
-
allow_none: bool,
|
|
45
|
-
) -> ColumnElement[bool]:
|
|
46
|
-
if allow_none:
|
|
47
|
-
return cast(ColumnElement[bool], column.is_(None) | predicate)
|
|
48
|
-
return predicate
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
def _unsupported(field: str, spec: TypeSpec, suggestion: str) -> None:
|
|
52
|
-
raise UnsupportedConstraintError(
|
|
53
|
-
field=field,
|
|
54
|
-
operator="type_check",
|
|
55
|
-
value=spec,
|
|
56
|
-
suggestion=suggestion,
|
|
57
|
-
)
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
def _predicate_int(
|
|
61
|
-
column: ColumnElement[Any],
|
|
62
|
-
spec: TypeSpec,
|
|
63
|
-
field: str,
|
|
64
|
-
) -> ColumnElement[bool]:
|
|
65
|
-
if _is_type(column, Integer):
|
|
66
|
-
return cast(ColumnElement[bool], column.isnot(None))
|
|
67
|
-
if spec.strict:
|
|
68
|
-
# SQLite typeof can distinguish integer storage even on loosely typed columns.
|
|
69
|
-
return cast(ColumnElement[bool], func.typeof(column) == "integer")
|
|
70
|
-
if _is_type(column, String):
|
|
71
|
-
return _regexp(column, _INT_TEXT)
|
|
72
|
-
if _is_type(column, Float, Numeric):
|
|
73
|
-
return cast(ColumnElement[bool], column == column.cast(Integer).cast(Float))
|
|
74
|
-
# Affinity-agnostic: integer typeof OR integer-shaped text.
|
|
75
|
-
return cast(
|
|
76
|
-
ColumnElement[bool],
|
|
77
|
-
(func.typeof(column) == "integer") | _regexp(column, _INT_TEXT),
|
|
78
|
-
)
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
def _predicate_bool(
|
|
82
|
-
column: ColumnElement[Any],
|
|
83
|
-
spec: TypeSpec,
|
|
84
|
-
field: str,
|
|
85
|
-
) -> ColumnElement[bool]:
|
|
86
|
-
if spec.strict:
|
|
87
|
-
if _is_type(column, Boolean, Integer):
|
|
88
|
-
return cast(ColumnElement[bool], column.in_((True, False, 0, 1)))
|
|
89
|
-
return cast(
|
|
90
|
-
ColumnElement[bool],
|
|
91
|
-
(func.typeof(column) == "integer") & column.in_((0, 1)),
|
|
92
|
-
)
|
|
93
|
-
_unsupported(
|
|
94
|
-
field,
|
|
95
|
-
spec,
|
|
96
|
-
"SQLite lax bool type_check is not supported. Use strict=True.",
|
|
97
|
-
)
|
|
98
|
-
raise AssertionError("unreachable")
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
def _predicate_str(
|
|
102
|
-
column: ColumnElement[Any],
|
|
103
|
-
spec: TypeSpec,
|
|
104
|
-
field: str,
|
|
105
|
-
) -> ColumnElement[bool]:
|
|
106
|
-
if _is_type(column, String):
|
|
107
|
-
return cast(ColumnElement[bool], column.isnot(None))
|
|
108
|
-
return cast(ColumnElement[bool], func.typeof(column) == "text")
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
def _predicate_float(
|
|
112
|
-
column: ColumnElement[Any],
|
|
113
|
-
spec: TypeSpec,
|
|
114
|
-
field: str,
|
|
115
|
-
) -> ColumnElement[bool]:
|
|
116
|
-
if _is_type(column, Float, Numeric, Integer):
|
|
117
|
-
return cast(ColumnElement[bool], column.isnot(None))
|
|
118
|
-
if spec.strict:
|
|
119
|
-
return cast(
|
|
120
|
-
ColumnElement[bool],
|
|
121
|
-
func.typeof(column).in_(("real", "integer")),
|
|
122
|
-
)
|
|
123
|
-
if _is_type(column, String):
|
|
124
|
-
return _regexp(column, _FLOAT_TEXT)
|
|
125
|
-
return cast(
|
|
126
|
-
ColumnElement[bool],
|
|
127
|
-
func.typeof(column).in_(("real", "integer")) | _regexp(column, _FLOAT_TEXT),
|
|
128
|
-
)
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
def _predicate_temporal(
|
|
132
|
-
column: ColumnElement[Any],
|
|
133
|
-
spec: TypeSpec,
|
|
134
|
-
field: str,
|
|
135
|
-
*,
|
|
136
|
-
sa_types: tuple[type[TypeEngine[Any]], ...],
|
|
137
|
-
text_pattern: str,
|
|
138
|
-
) -> ColumnElement[bool]:
|
|
139
|
-
if _is_type(column, *sa_types):
|
|
140
|
-
return cast(ColumnElement[bool], column.isnot(None))
|
|
141
|
-
if spec.strict and not _is_type(column, String):
|
|
142
|
-
type_name = spec.python_type.__name__
|
|
143
|
-
_unsupported(
|
|
144
|
-
field,
|
|
145
|
-
spec,
|
|
146
|
-
f"SQLite strict {type_name} type_check requires a typed or String column.",
|
|
147
|
-
)
|
|
148
|
-
return _regexp(column, text_pattern)
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
def _predicate_uuid(
|
|
152
|
-
column: ColumnElement[Any],
|
|
153
|
-
spec: TypeSpec,
|
|
154
|
-
field: str,
|
|
155
|
-
) -> ColumnElement[bool]:
|
|
156
|
-
if "uuid" in type(column.type).__name__.lower():
|
|
157
|
-
return cast(ColumnElement[bool], column.isnot(None))
|
|
158
|
-
if _is_type(column, String) or not spec.strict:
|
|
159
|
-
return _regexp(column, _UUID_TEXT)
|
|
160
|
-
_unsupported(
|
|
161
|
-
field,
|
|
162
|
-
spec,
|
|
163
|
-
"SQLite strict UUID type_check requires a UUID or String column.",
|
|
164
|
-
)
|
|
165
|
-
raise AssertionError("unreachable")
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
def _build_predicate(
|
|
169
|
-
column: ColumnElement[Any],
|
|
170
|
-
spec: TypeSpec,
|
|
171
|
-
field: str,
|
|
172
|
-
) -> ColumnElement[bool]:
|
|
173
|
-
python_type = spec.python_type
|
|
174
|
-
if python_type is int:
|
|
175
|
-
return _predicate_int(column, spec, field)
|
|
176
|
-
if python_type is bool:
|
|
177
|
-
return _predicate_bool(column, spec, field)
|
|
178
|
-
if python_type is str:
|
|
179
|
-
return _predicate_str(column, spec, field)
|
|
180
|
-
if python_type in {float, Decimal}:
|
|
181
|
-
return _predicate_float(column, spec, field)
|
|
182
|
-
if python_type is date:
|
|
183
|
-
return _predicate_temporal(column, spec, field, sa_types=(Date,), text_pattern=_DATE_TEXT)
|
|
184
|
-
if python_type is datetime:
|
|
185
|
-
return _predicate_temporal(
|
|
186
|
-
column, spec, field, sa_types=(DateTime,), text_pattern=_DATETIME_TEXT
|
|
187
|
-
)
|
|
188
|
-
if python_type is time:
|
|
189
|
-
return _predicate_temporal(column, spec, field, sa_types=(Time,), text_pattern=_TIME_TEXT)
|
|
190
|
-
if python_type is UUID:
|
|
191
|
-
return _predicate_uuid(column, spec, field)
|
|
192
|
-
_unsupported(
|
|
193
|
-
field,
|
|
194
|
-
spec,
|
|
195
|
-
f"SQLite type_check has no translator for {python_type!r}.",
|
|
196
|
-
)
|
|
197
|
-
raise AssertionError("unreachable")
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
def translate_type_check(
|
|
201
|
-
constraint: Constraint,
|
|
202
|
-
column: ColumnElement[Any],
|
|
203
|
-
context: CompilationContext,
|
|
204
|
-
) -> ColumnElement[bool]:
|
|
205
|
-
"""Translate ``type_check`` using SQLite ``typeof`` / ``REGEXP``.
|
|
206
|
-
|
|
207
|
-
Text-shape checks use ``REGEXP``; call :func:`register_regexp` on the
|
|
208
|
-
connection before executing the SQL.
|
|
209
|
-
"""
|
|
210
|
-
del context
|
|
211
|
-
spec = type_spec(constraint.value)
|
|
212
|
-
predicate = _build_predicate(column, spec, constraint.field)
|
|
213
|
-
return _wrap_none(column, predicate, allow_none=spec.allow_none)
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: sqlrules-sqlite
|
|
3
|
-
Version: 1.0.1
|
|
4
|
-
Summary: SQLite dialect plugin for SQLRules (REGEXP helpers and JSON).
|
|
5
|
-
Project-URL: Homepage, https://github.com/eddiethedean/sqlrules
|
|
6
|
-
Project-URL: Repository, https://github.com/eddiethedean/sqlrules
|
|
7
|
-
Project-URL: Issues, https://github.com/eddiethedean/sqlrules/issues
|
|
8
|
-
Author: SQLRules Contributors
|
|
9
|
-
License-Expression: MIT
|
|
10
|
-
License-File: LICENSE
|
|
11
|
-
Keywords: pydantic,sqlalchemy,sqlite,sqlrules
|
|
12
|
-
Classifier: Development Status :: 5 - Production/Stable
|
|
13
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
-
Classifier: Programming Language :: Python :: 3
|
|
15
|
-
Classifier: Typing :: Typed
|
|
16
|
-
Requires-Python: >=3.10
|
|
17
|
-
Requires-Dist: sqlalchemy<3,>=2.0
|
|
18
|
-
Requires-Dist: sqlrules<2,>=1
|
|
19
|
-
Provides-Extra: dev
|
|
20
|
-
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
21
|
-
Description-Content-Type: text/markdown
|
|
22
|
-
|
|
23
|
-
# sqlrules-sqlite
|
|
24
|
-
|
|
25
|
-
SQLite dialect plugin for [SQLRules](https://github.com/eddiethedean/sqlrules).
|
|
26
|
-
|
|
27
|
-
## Install
|
|
28
|
-
|
|
29
|
-
```bash
|
|
30
|
-
pip install sqlrules-sqlite
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
## Usage
|
|
34
|
-
|
|
35
|
-
```python
|
|
36
|
-
import sqlite3
|
|
37
|
-
from typing import Annotated, Any
|
|
38
|
-
|
|
39
|
-
from pydantic import BaseModel, Field
|
|
40
|
-
from sqlalchemy import Column, MetaData, String, Table, create_engine, text
|
|
41
|
-
|
|
42
|
-
from sqlrules import Compiler, JsonContains, JsonHasKey
|
|
43
|
-
from sqlrules_sqlite import SQLitePlugin, register_regexp
|
|
44
|
-
|
|
45
|
-
class RowFilter(BaseModel):
|
|
46
|
-
name: Annotated[str, Field(pattern=r"^A")]
|
|
47
|
-
meta: Annotated[dict[str, Any], JsonContains({"active": True}), JsonHasKey("active")]
|
|
48
|
-
|
|
49
|
-
table = Table(
|
|
50
|
-
"rows",
|
|
51
|
-
MetaData(),
|
|
52
|
-
Column("name", String),
|
|
53
|
-
Column("meta", String),
|
|
54
|
-
)
|
|
55
|
-
|
|
56
|
-
compiler = Compiler(plugins=[SQLitePlugin()], dialect="sqlite")
|
|
57
|
-
rules = compiler.compile(RowFilter, table)
|
|
58
|
-
|
|
59
|
-
engine = create_engine("sqlite://")
|
|
60
|
-
with engine.raw_connection() as conn:
|
|
61
|
-
# SQLAlchemy 2 may wrap the DBAPI connection; unwrap if needed.
|
|
62
|
-
dbapi = conn.driver_connection if hasattr(conn, "driver_connection") else conn
|
|
63
|
-
register_regexp(dbapi)
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
## Operators
|
|
67
|
-
|
|
68
|
-
| IR operator | Notes |
|
|
69
|
-
|---|---|
|
|
70
|
-
| `pattern` | `column REGEXP pattern`; call `register_regexp(connection)` |
|
|
71
|
-
| `type_check` | `typeof` / `REGEXP` shape checks; text forms need `register_regexp` |
|
|
72
|
-
| `json_contains` | JSON1 `json_extract` equality for object keys |
|
|
73
|
-
| `json_has_key` | `json_type(column, '$.key') IS NOT NULL` |
|
|
74
|
-
|
|
75
|
-
Case-insensitive patterns (`re.IGNORECASE` / `PatternSpec.ignore_case`) are
|
|
76
|
-
encoded with a `(?i)` prefix understood by `register_regexp`.
|
|
77
|
-
|
|
78
|
-
## Security note
|
|
79
|
-
|
|
80
|
-
`register_regexp` installs a Python `re.search` UDF. Untrusted
|
|
81
|
-
`Field(pattern=...)` values can cause **CPU denial of service** (ReDoS) in
|
|
82
|
-
your process — not SQL injection. Prefer static/allowlisted patterns. See
|
|
83
|
-
[SECURITY](https://sqlrules.readthedocs.io/en/latest/SECURITY.html).
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
sqlrules_sqlite/__init__.py,sha256=oLODNHzgXbK1XjmI5YcpnDKf7JXYJfj0Wr2Gcnc35ds,1575
|
|
2
|
-
sqlrules_sqlite/json.py,sha256=B9QCjD8FR7nja8Ct4O7fb2VzTPhALg3b_ODayBeKHBY,3326
|
|
3
|
-
sqlrules_sqlite/pattern.py,sha256=K1JBoM1bkxYRgWKz_Ti7o_IDFKtIeg9uYplr0ThVJBY,867
|
|
4
|
-
sqlrules_sqlite/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
|
-
sqlrules_sqlite/regexp.py,sha256=j3c_J3dnbAdXfKkhhb1CxiIPi1QB_ai5Mt30ODrmx3M,1038
|
|
6
|
-
sqlrules_sqlite/type_check.py,sha256=JQXzgdLWmdAnGXOBPza4yMwVhrWWeYyforv-hQSd9k4,6742
|
|
7
|
-
sqlrules_sqlite-1.0.1.dist-info/METADATA,sha256=yCg5Z14W7BSP9IquHzYJFKuu-oUzzHdDooT1R0qKnzU,2694
|
|
8
|
-
sqlrules_sqlite-1.0.1.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
|
|
9
|
-
sqlrules_sqlite-1.0.1.dist-info/licenses/LICENSE,sha256=cLMlkCH6RhjWAkXBWR7LazV98TOG0WkPtCKB5zqqkAs,1078
|
|
10
|
-
sqlrules_sqlite-1.0.1.dist-info/RECORD,,
|
|
File without changes
|