sqlrules-sqlite 1.0.1__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.
- sqlrules_sqlite-1.0.1/.gitignore +22 -0
- sqlrules_sqlite-1.0.1/LICENSE +21 -0
- sqlrules_sqlite-1.0.1/PKG-INFO +83 -0
- sqlrules_sqlite-1.0.1/README.md +61 -0
- sqlrules_sqlite-1.0.1/pyproject.toml +48 -0
- sqlrules_sqlite-1.0.1/src/sqlrules_sqlite/__init__.py +55 -0
- sqlrules_sqlite-1.0.1/src/sqlrules_sqlite/json.py +94 -0
- sqlrules_sqlite-1.0.1/src/sqlrules_sqlite/pattern.py +25 -0
- sqlrules_sqlite-1.0.1/src/sqlrules_sqlite/py.typed +0 -0
- sqlrules_sqlite-1.0.1/src/sqlrules_sqlite/regexp.py +30 -0
- sqlrules_sqlite-1.0.1/src/sqlrules_sqlite/type_check.py +213 -0
- sqlrules_sqlite-1.0.1/tests/test_sqlite_plugin.py +314 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
.venv/
|
|
2
|
+
venv/
|
|
3
|
+
__pycache__/
|
|
4
|
+
*.py[cod]
|
|
5
|
+
*$py.class
|
|
6
|
+
.pytest_cache/
|
|
7
|
+
.mypy_cache/
|
|
8
|
+
.ruff_cache/
|
|
9
|
+
.coverage
|
|
10
|
+
.coverage.*
|
|
11
|
+
htmlcov/
|
|
12
|
+
dist/
|
|
13
|
+
dist-plugins/
|
|
14
|
+
dist-missing/
|
|
15
|
+
build/
|
|
16
|
+
*.egg-info/
|
|
17
|
+
.DS_Store
|
|
18
|
+
.idea/
|
|
19
|
+
.vscode/
|
|
20
|
+
*.swp
|
|
21
|
+
*.swo
|
|
22
|
+
docs/_build/
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 SQLRules Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,83 @@
|
|
|
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).
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# sqlrules-sqlite
|
|
2
|
+
|
|
3
|
+
SQLite dialect plugin for [SQLRules](https://github.com/eddiethedean/sqlrules).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install sqlrules-sqlite
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
import sqlite3
|
|
15
|
+
from typing import Annotated, Any
|
|
16
|
+
|
|
17
|
+
from pydantic import BaseModel, Field
|
|
18
|
+
from sqlalchemy import Column, MetaData, String, Table, create_engine, text
|
|
19
|
+
|
|
20
|
+
from sqlrules import Compiler, JsonContains, JsonHasKey
|
|
21
|
+
from sqlrules_sqlite import SQLitePlugin, register_regexp
|
|
22
|
+
|
|
23
|
+
class RowFilter(BaseModel):
|
|
24
|
+
name: Annotated[str, Field(pattern=r"^A")]
|
|
25
|
+
meta: Annotated[dict[str, Any], JsonContains({"active": True}), JsonHasKey("active")]
|
|
26
|
+
|
|
27
|
+
table = Table(
|
|
28
|
+
"rows",
|
|
29
|
+
MetaData(),
|
|
30
|
+
Column("name", String),
|
|
31
|
+
Column("meta", String),
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
compiler = Compiler(plugins=[SQLitePlugin()], dialect="sqlite")
|
|
35
|
+
rules = compiler.compile(RowFilter, table)
|
|
36
|
+
|
|
37
|
+
engine = create_engine("sqlite://")
|
|
38
|
+
with engine.raw_connection() as conn:
|
|
39
|
+
# SQLAlchemy 2 may wrap the DBAPI connection; unwrap if needed.
|
|
40
|
+
dbapi = conn.driver_connection if hasattr(conn, "driver_connection") else conn
|
|
41
|
+
register_regexp(dbapi)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Operators
|
|
45
|
+
|
|
46
|
+
| IR operator | Notes |
|
|
47
|
+
|---|---|
|
|
48
|
+
| `pattern` | `column REGEXP pattern`; call `register_regexp(connection)` |
|
|
49
|
+
| `type_check` | `typeof` / `REGEXP` shape checks; text forms need `register_regexp` |
|
|
50
|
+
| `json_contains` | JSON1 `json_extract` equality for object keys |
|
|
51
|
+
| `json_has_key` | `json_type(column, '$.key') IS NOT NULL` |
|
|
52
|
+
|
|
53
|
+
Case-insensitive patterns (`re.IGNORECASE` / `PatternSpec.ignore_case`) are
|
|
54
|
+
encoded with a `(?i)` prefix understood by `register_regexp`.
|
|
55
|
+
|
|
56
|
+
## Security note
|
|
57
|
+
|
|
58
|
+
`register_regexp` installs a Python `re.search` UDF. Untrusted
|
|
59
|
+
`Field(pattern=...)` values can cause **CPU denial of service** (ReDoS) in
|
|
60
|
+
your process — not SQL injection. Prefer static/allowlisted patterns. See
|
|
61
|
+
[SECURITY](https://sqlrules.readthedocs.io/en/latest/SECURITY.html).
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.25"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "sqlrules-sqlite"
|
|
7
|
+
version = "1.0.1"
|
|
8
|
+
description = "SQLite dialect plugin for SQLRules (REGEXP helpers and JSON)."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
authors = [{ name = "SQLRules Contributors" }]
|
|
13
|
+
keywords = ["sqlrules", "sqlite", "pydantic", "sqlalchemy"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 5 - Production/Stable",
|
|
16
|
+
"License :: OSI Approved :: MIT License",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Typing :: Typed",
|
|
19
|
+
]
|
|
20
|
+
dependencies = [
|
|
21
|
+
"sqlrules>=1,<2",
|
|
22
|
+
"sqlalchemy>=2.0,<3",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[project.urls]
|
|
26
|
+
Homepage = "https://github.com/eddiethedean/sqlrules"
|
|
27
|
+
Repository = "https://github.com/eddiethedean/sqlrules"
|
|
28
|
+
Issues = "https://github.com/eddiethedean/sqlrules/issues"
|
|
29
|
+
|
|
30
|
+
[project.optional-dependencies]
|
|
31
|
+
dev = [
|
|
32
|
+
"pytest>=8.0",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[tool.hatch.build.targets.sdist]
|
|
36
|
+
include = [
|
|
37
|
+
"/src",
|
|
38
|
+
"/tests",
|
|
39
|
+
"/README.md",
|
|
40
|
+
"/LICENSE",
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
[tool.hatch.build.targets.wheel]
|
|
44
|
+
packages = ["src/sqlrules_sqlite"]
|
|
45
|
+
|
|
46
|
+
[tool.pytest.ini_options]
|
|
47
|
+
testpaths = ["tests"]
|
|
48
|
+
pythonpath = ["src"]
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from sqlrules.plugins import PLUGIN_API_VERSION
|
|
4
|
+
from sqlrules.translators import TranslatorRegistry
|
|
5
|
+
from sqlrules_sqlite.json import translate_json_contains, translate_json_has_key
|
|
6
|
+
from sqlrules_sqlite.pattern import translate_pattern
|
|
7
|
+
from sqlrules_sqlite.regexp import register_regexp
|
|
8
|
+
from sqlrules_sqlite.type_check import translate_type_check
|
|
9
|
+
|
|
10
|
+
__version__ = "1.0.1"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class SQLitePlugin:
|
|
14
|
+
"""Register SQLite-specific constraint translators.
|
|
15
|
+
|
|
16
|
+
The ``pattern`` and text-shaped ``type_check`` translators emit
|
|
17
|
+
``column REGEXP pattern``. Call :func:`register_regexp` on each SQLite
|
|
18
|
+
connection before executing the resulting SQL.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
name = "sqlite"
|
|
22
|
+
api_version = PLUGIN_API_VERSION
|
|
23
|
+
|
|
24
|
+
def register(self, registry: TranslatorRegistry) -> None:
|
|
25
|
+
registry.register_constraint(
|
|
26
|
+
"pattern",
|
|
27
|
+
translate_pattern,
|
|
28
|
+
on_conflict="replace",
|
|
29
|
+
)
|
|
30
|
+
registry.register_constraint(
|
|
31
|
+
"type_check",
|
|
32
|
+
translate_type_check,
|
|
33
|
+
on_conflict="replace",
|
|
34
|
+
)
|
|
35
|
+
registry.register_constraint(
|
|
36
|
+
"json_contains",
|
|
37
|
+
translate_json_contains,
|
|
38
|
+
on_conflict="replace",
|
|
39
|
+
)
|
|
40
|
+
registry.register_constraint(
|
|
41
|
+
"json_has_key",
|
|
42
|
+
translate_json_has_key,
|
|
43
|
+
on_conflict="replace",
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
__all__ = [
|
|
48
|
+
"SQLitePlugin",
|
|
49
|
+
"__version__",
|
|
50
|
+
"register_regexp",
|
|
51
|
+
"translate_json_contains",
|
|
52
|
+
"translate_json_has_key",
|
|
53
|
+
"translate_pattern",
|
|
54
|
+
"translate_type_check",
|
|
55
|
+
]
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from typing import Any, cast
|
|
5
|
+
|
|
6
|
+
from sqlalchemy import String, func, literal, type_coerce
|
|
7
|
+
from sqlalchemy.sql.elements import ColumnElement
|
|
8
|
+
|
|
9
|
+
from sqlrules.ir import CompilationContext, Constraint
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _json_path_for_key(key: Any) -> str:
|
|
13
|
+
"""Build a JSONPath for a single object key (never a full-path escape hatch)."""
|
|
14
|
+
text = str(key)
|
|
15
|
+
escaped = text.replace("\\", "\\\\").replace('"', '\\"')
|
|
16
|
+
return f'$."{escaped}"'
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _compact_dumps(value: Any) -> str:
|
|
20
|
+
return json.dumps(value, separators=(",", ":"))
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _extract_equals(
|
|
24
|
+
column: ColumnElement[Any],
|
|
25
|
+
path: str,
|
|
26
|
+
expected: Any,
|
|
27
|
+
) -> ColumnElement[bool]:
|
|
28
|
+
"""Compare ``json_extract`` to ``expected`` using SQLite JSON1 affinities."""
|
|
29
|
+
extracted = func.json_extract(column, path)
|
|
30
|
+
if expected is None:
|
|
31
|
+
return cast(ColumnElement[bool], func.json_type(column, path) == "null")
|
|
32
|
+
if isinstance(expected, bool):
|
|
33
|
+
return cast(ColumnElement[bool], extracted == (1 if expected else 0))
|
|
34
|
+
if isinstance(expected, (dict, list)):
|
|
35
|
+
compact = _compact_dumps(expected)
|
|
36
|
+
return cast(
|
|
37
|
+
ColumnElement[bool],
|
|
38
|
+
func.json(extracted) == func.json(literal(compact)),
|
|
39
|
+
)
|
|
40
|
+
if isinstance(expected, (int, float)):
|
|
41
|
+
return cast(ColumnElement[bool], extracted == expected)
|
|
42
|
+
if isinstance(expected, str):
|
|
43
|
+
return cast(ColumnElement[bool], extracted == expected)
|
|
44
|
+
compact = _compact_dumps(expected)
|
|
45
|
+
return cast(
|
|
46
|
+
ColumnElement[bool],
|
|
47
|
+
func.json(extracted) == func.json(literal(compact)),
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def translate_json_contains(
|
|
52
|
+
constraint: Constraint,
|
|
53
|
+
column: ColumnElement[Any],
|
|
54
|
+
context: CompilationContext,
|
|
55
|
+
) -> ColumnElement[bool]:
|
|
56
|
+
"""Translate ``json_contains`` using SQLite JSON1 ``json_extract``.
|
|
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
|
+
"""
|
|
63
|
+
value = constraint.value
|
|
64
|
+
if isinstance(value, dict):
|
|
65
|
+
if not value:
|
|
66
|
+
# Align with PostgreSQL ``@> '{}'``: require a non-NULL JSON object.
|
|
67
|
+
return cast(
|
|
68
|
+
ColumnElement[bool],
|
|
69
|
+
column.is_not(None) & (func.json_type(column) == "object"),
|
|
70
|
+
)
|
|
71
|
+
parts: list[ColumnElement[bool]] = []
|
|
72
|
+
for key, expected in value.items():
|
|
73
|
+
parts.append(_extract_equals(column, _json_path_for_key(key), expected))
|
|
74
|
+
expression = parts[0]
|
|
75
|
+
for part in parts[1:]:
|
|
76
|
+
expression = expression & part
|
|
77
|
+
return cast(ColumnElement[bool], expression)
|
|
78
|
+
|
|
79
|
+
# Scalar / array payload: compare whole-document JSON text via json().
|
|
80
|
+
compact = _compact_dumps(value)
|
|
81
|
+
return cast(
|
|
82
|
+
ColumnElement[bool],
|
|
83
|
+
func.json(type_coerce(column, String)) == func.json(literal(compact)),
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def translate_json_has_key(
|
|
88
|
+
constraint: Constraint,
|
|
89
|
+
column: ColumnElement[Any],
|
|
90
|
+
context: CompilationContext,
|
|
91
|
+
) -> ColumnElement[bool]:
|
|
92
|
+
"""Translate ``json_has_key`` via ``json_type(column, path) IS NOT NULL``."""
|
|
93
|
+
path = _json_path_for_key(constraint.value)
|
|
94
|
+
return cast(ColumnElement[bool], func.json_type(column, path).is_not(None))
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any, cast
|
|
4
|
+
|
|
5
|
+
from sqlalchemy.sql.elements import ColumnElement
|
|
6
|
+
|
|
7
|
+
from sqlrules.constraints import pattern_text
|
|
8
|
+
from sqlrules.ir import CompilationContext, Constraint
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def translate_pattern(
|
|
12
|
+
constraint: Constraint,
|
|
13
|
+
column: ColumnElement[Any],
|
|
14
|
+
context: CompilationContext,
|
|
15
|
+
) -> ColumnElement[bool]:
|
|
16
|
+
"""Translate ``pattern`` to SQLite ``column REGEXP pattern``.
|
|
17
|
+
|
|
18
|
+
Case-insensitive patterns are encoded with a ``(?i)`` prefix so
|
|
19
|
+
:func:`sqlrules_sqlite.register_regexp` can apply ``re.IGNORECASE``.
|
|
20
|
+
Callers must enable REGEXP on the SQLite connection before execution.
|
|
21
|
+
"""
|
|
22
|
+
pattern, ignore_case = pattern_text(constraint.value)
|
|
23
|
+
if ignore_case and not pattern.startswith("(?i)"):
|
|
24
|
+
pattern = f"(?i){pattern}"
|
|
25
|
+
return cast(ColumnElement[bool], column.op("REGEXP")(pattern))
|
|
File without changes
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
import sqlite3
|
|
5
|
+
|
|
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
|
+
|
|
14
|
+
The helper interprets an optional ``(?i)`` prefix (inserted by the
|
|
15
|
+
pattern translator for case-insensitive ``PatternSpec`` values).
|
|
16
|
+
|
|
17
|
+
Invalid patterns raise ``re.error`` (surfaced by SQLite as an
|
|
18
|
+
operational error) instead of silently matching nothing.
|
|
19
|
+
"""
|
|
20
|
+
|
|
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
|
+
|
|
30
|
+
connection.create_function("REGEXP", 2, regexp)
|
|
@@ -0,0 +1,213 @@
|
|
|
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)
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
import sqlite3
|
|
5
|
+
from typing import Annotated, Any
|
|
6
|
+
|
|
7
|
+
import pytest
|
|
8
|
+
from pydantic import BaseModel, Field
|
|
9
|
+
from sqlalchemy import Column, MetaData, String, Table, create_engine, select
|
|
10
|
+
from sqlalchemy.dialects import sqlite
|
|
11
|
+
from sqlrules_sqlite import SQLitePlugin, __version__, register_regexp
|
|
12
|
+
|
|
13
|
+
from sqlrules import Compiler, JsonContains, JsonHasKey
|
|
14
|
+
from sqlrules.conformance import run_basic_conformance
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def test_version() -> None:
|
|
18
|
+
assert __version__ == "1.0.1"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def test_conformance() -> None:
|
|
22
|
+
run_basic_conformance(SQLitePlugin(), operator="pattern")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_pattern_compiles() -> None:
|
|
26
|
+
"""SQLite REGEXP uses ``re.search`` (not Pydantic fullmatch); anchors are caller-owned."""
|
|
27
|
+
|
|
28
|
+
class Filter(BaseModel):
|
|
29
|
+
name: Annotated[str, Field(pattern=r"^A")]
|
|
30
|
+
|
|
31
|
+
table = Table("items", MetaData(), Column("name", String))
|
|
32
|
+
rules = Compiler(
|
|
33
|
+
plugins=[SQLitePlugin()],
|
|
34
|
+
dialect="sqlite",
|
|
35
|
+
cache=False,
|
|
36
|
+
).compile(Filter, table)
|
|
37
|
+
compiled = str(
|
|
38
|
+
rules["name"][0].compile(
|
|
39
|
+
dialect=sqlite.dialect(),
|
|
40
|
+
compile_kwargs={"literal_binds": True},
|
|
41
|
+
)
|
|
42
|
+
)
|
|
43
|
+
assert compiled == "items.name REGEXP '^A'"
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def test_pattern_ignore_case_prefix() -> None:
|
|
47
|
+
class Filter(BaseModel):
|
|
48
|
+
name: Annotated[str, Field(pattern=re.compile(r"^A", re.I))]
|
|
49
|
+
|
|
50
|
+
table = Table("items", MetaData(), Column("name", String))
|
|
51
|
+
rules = Compiler(
|
|
52
|
+
plugins=[SQLitePlugin()],
|
|
53
|
+
dialect="sqlite",
|
|
54
|
+
cache=False,
|
|
55
|
+
).compile(Filter, table)
|
|
56
|
+
compiled = str(rules["name"][0].compile(compile_kwargs={"literal_binds": True}))
|
|
57
|
+
assert compiled == "items.name REGEXP '(?i)^A'"
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def test_register_regexp_null_inputs_are_false() -> None:
|
|
61
|
+
conn = sqlite3.connect(":memory:")
|
|
62
|
+
register_regexp(conn)
|
|
63
|
+
assert conn.execute("SELECT NULL REGEXP '^a'").fetchone()[0] == 0
|
|
64
|
+
assert conn.execute("SELECT 'Abc' REGEXP NULL").fetchone()[0] == 0
|
|
65
|
+
conn.close()
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def test_register_regexp_matches() -> None:
|
|
69
|
+
conn = sqlite3.connect(":memory:")
|
|
70
|
+
register_regexp(conn)
|
|
71
|
+
assert conn.execute("SELECT 'Abc' REGEXP '(?i)^a'").fetchone()[0] == 1
|
|
72
|
+
assert conn.execute("SELECT 'Abc' REGEXP '^a'").fetchone()[0] == 0
|
|
73
|
+
# Empty pattern: re.search('', value) is True for any string.
|
|
74
|
+
assert conn.execute("SELECT 'Abc' REGEXP ''").fetchone()[0] == 1
|
|
75
|
+
conn.close()
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def test_pattern_translator_with_register_regexp_executes() -> None:
|
|
79
|
+
class Filter(BaseModel):
|
|
80
|
+
name: Annotated[str, Field(pattern=re.compile(r"^a", re.I))]
|
|
81
|
+
|
|
82
|
+
metadata = MetaData()
|
|
83
|
+
table = Table("items", metadata, Column("name", String))
|
|
84
|
+
rules = Compiler(
|
|
85
|
+
plugins=[SQLitePlugin()],
|
|
86
|
+
dialect="sqlite",
|
|
87
|
+
cache=False,
|
|
88
|
+
).compile(Filter, table)
|
|
89
|
+
|
|
90
|
+
engine = create_engine("sqlite://")
|
|
91
|
+
with engine.begin() as conn:
|
|
92
|
+
raw = conn.connection.dbapi_connection
|
|
93
|
+
register_regexp(raw) # type: ignore[arg-type]
|
|
94
|
+
metadata.create_all(conn)
|
|
95
|
+
conn.execute(table.insert(), [{"name": "Abc"}, {"name": "zzz"}])
|
|
96
|
+
stmt = select(table.c.name).where(*rules["name"])
|
|
97
|
+
rows = [row[0] for row in conn.execute(stmt)]
|
|
98
|
+
assert rows == ["Abc"]
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def test_json_operators_compile() -> None:
|
|
102
|
+
class Filter(BaseModel):
|
|
103
|
+
meta: Annotated[dict[str, Any], JsonContains({"active": True}), JsonHasKey("active")]
|
|
104
|
+
|
|
105
|
+
table = Table("items", MetaData(), Column("meta", String))
|
|
106
|
+
rules = Compiler(
|
|
107
|
+
plugins=[SQLitePlugin()],
|
|
108
|
+
dialect="sqlite",
|
|
109
|
+
cache=False,
|
|
110
|
+
).compile(Filter, table)
|
|
111
|
+
assert len(rules["meta"]) == 2
|
|
112
|
+
contains_sql = str(rules["meta"][0].compile(dialect=sqlite.dialect())).lower()
|
|
113
|
+
has_key_sql = str(rules["meta"][1].compile(dialect=sqlite.dialect())).lower()
|
|
114
|
+
assert "json_extract" in contains_sql
|
|
115
|
+
assert "json_type" in has_key_sql
|
|
116
|
+
assert "json_extract" not in has_key_sql
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def test_json_contains_executes_for_bool_str_int_null() -> None:
|
|
120
|
+
class Filter(BaseModel):
|
|
121
|
+
meta: Annotated[
|
|
122
|
+
dict[str, Any],
|
|
123
|
+
JsonContains({"active": True, "name": "hello", "n": 1, "missing": None}),
|
|
124
|
+
]
|
|
125
|
+
|
|
126
|
+
metadata = MetaData()
|
|
127
|
+
table = Table("items", metadata, Column("id", String), Column("meta", String))
|
|
128
|
+
rules = Compiler(
|
|
129
|
+
plugins=[SQLitePlugin()],
|
|
130
|
+
dialect="sqlite",
|
|
131
|
+
cache=False,
|
|
132
|
+
).compile(Filter, table)
|
|
133
|
+
|
|
134
|
+
engine = create_engine("sqlite://")
|
|
135
|
+
with engine.begin() as conn:
|
|
136
|
+
metadata.create_all(conn)
|
|
137
|
+
conn.execute(
|
|
138
|
+
table.insert(),
|
|
139
|
+
[
|
|
140
|
+
{
|
|
141
|
+
"id": "match",
|
|
142
|
+
"meta": '{"active": true, "name": "hello", "n": 1, "missing": null}',
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
"id": "partial",
|
|
146
|
+
"meta": '{"active": true, "name": "hello", "n": 2}',
|
|
147
|
+
},
|
|
148
|
+
{"id": "null", "meta": None},
|
|
149
|
+
],
|
|
150
|
+
)
|
|
151
|
+
rows = [
|
|
152
|
+
row[0]
|
|
153
|
+
for row in conn.execute(select(table.c.id).where(*rules["meta"]).order_by(table.c.id))
|
|
154
|
+
]
|
|
155
|
+
assert rows == ["match"]
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def test_json_contains_dotted_key_and_array_payload() -> None:
|
|
159
|
+
class Filter(BaseModel):
|
|
160
|
+
meta: Annotated[dict[str, Any], JsonContains({"a.b": 1})]
|
|
161
|
+
tags: Annotated[list[Any], JsonContains([1, 2])]
|
|
162
|
+
|
|
163
|
+
metadata = MetaData()
|
|
164
|
+
table = Table(
|
|
165
|
+
"items",
|
|
166
|
+
metadata,
|
|
167
|
+
Column("id", String),
|
|
168
|
+
Column("meta", String),
|
|
169
|
+
Column("tags", String),
|
|
170
|
+
)
|
|
171
|
+
rules = Compiler(
|
|
172
|
+
plugins=[SQLitePlugin()],
|
|
173
|
+
dialect="sqlite",
|
|
174
|
+
cache=False,
|
|
175
|
+
).compile(Filter, table)
|
|
176
|
+
|
|
177
|
+
engine = create_engine("sqlite://")
|
|
178
|
+
with engine.begin() as conn:
|
|
179
|
+
metadata.create_all(conn)
|
|
180
|
+
conn.execute(
|
|
181
|
+
table.insert(),
|
|
182
|
+
[
|
|
183
|
+
{"id": "match", "meta": '{"a.b": 1}', "tags": "[1,2]"},
|
|
184
|
+
{"id": "nested", "meta": '{"a": {"b": 1}}', "tags": "[1,2]"},
|
|
185
|
+
{"id": "spaced", "meta": '{"a.b": 1}', "tags": "[1, 2]"},
|
|
186
|
+
],
|
|
187
|
+
)
|
|
188
|
+
meta_rows = [
|
|
189
|
+
row[0]
|
|
190
|
+
for row in conn.execute(select(table.c.id).where(*rules["meta"]).order_by(table.c.id))
|
|
191
|
+
]
|
|
192
|
+
tag_rows = [
|
|
193
|
+
row[0]
|
|
194
|
+
for row in conn.execute(select(table.c.id).where(*rules["tags"]).order_by(table.c.id))
|
|
195
|
+
]
|
|
196
|
+
assert meta_rows == ["match", "spaced"]
|
|
197
|
+
assert tag_rows == ["match", "nested", "spaced"]
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def test_empty_json_contains_requires_object() -> None:
|
|
201
|
+
class Filter(BaseModel):
|
|
202
|
+
meta: Annotated[dict[str, Any], JsonContains({})]
|
|
203
|
+
|
|
204
|
+
metadata = MetaData()
|
|
205
|
+
table = Table("items", metadata, Column("id", String), Column("meta", String))
|
|
206
|
+
rules = Compiler(
|
|
207
|
+
plugins=[SQLitePlugin()],
|
|
208
|
+
dialect="sqlite",
|
|
209
|
+
cache=False,
|
|
210
|
+
).compile(Filter, table)
|
|
211
|
+
compiled = str(
|
|
212
|
+
rules["meta"][0].compile(
|
|
213
|
+
dialect=sqlite.dialect(),
|
|
214
|
+
compile_kwargs={"literal_binds": True},
|
|
215
|
+
)
|
|
216
|
+
).lower()
|
|
217
|
+
assert "json_type" in compiled
|
|
218
|
+
assert "object" in compiled
|
|
219
|
+
|
|
220
|
+
engine = create_engine("sqlite://")
|
|
221
|
+
with engine.begin() as conn:
|
|
222
|
+
metadata.create_all(conn)
|
|
223
|
+
conn.execute(
|
|
224
|
+
table.insert(),
|
|
225
|
+
[
|
|
226
|
+
{"id": "obj", "meta": "{}"},
|
|
227
|
+
{"id": "arr", "meta": "[]"},
|
|
228
|
+
{"id": "null", "meta": None},
|
|
229
|
+
],
|
|
230
|
+
)
|
|
231
|
+
rows = [
|
|
232
|
+
row[0]
|
|
233
|
+
for row in conn.execute(select(table.c.id).where(*rules["meta"]).order_by(table.c.id))
|
|
234
|
+
]
|
|
235
|
+
assert rows == ["obj"]
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def test_register_regexp_invalid_pattern_raises() -> None:
|
|
239
|
+
conn = sqlite3.connect(":memory:")
|
|
240
|
+
register_regexp(conn)
|
|
241
|
+
with pytest.raises(sqlite3.OperationalError):
|
|
242
|
+
conn.execute("SELECT 'Abc' REGEXP '['").fetchone()
|
|
243
|
+
conn.close()
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def test_type_check_int_integer_column() -> None:
|
|
247
|
+
from sqlalchemy import Integer
|
|
248
|
+
|
|
249
|
+
class Filter(BaseModel):
|
|
250
|
+
age: int
|
|
251
|
+
|
|
252
|
+
table = Table("users", MetaData(), Column("age", Integer))
|
|
253
|
+
rules = Compiler(
|
|
254
|
+
plugins=[SQLitePlugin()],
|
|
255
|
+
dialect="sqlite",
|
|
256
|
+
emit_type_checks=True,
|
|
257
|
+
cache=False,
|
|
258
|
+
).compile(Filter, table)
|
|
259
|
+
compiled = str(rules["age"][0].compile(dialect=sqlite.dialect()))
|
|
260
|
+
assert "IS NOT NULL" in compiled.upper()
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def test_type_check_lax_int_string_uses_regexp() -> None:
|
|
264
|
+
class Filter(BaseModel):
|
|
265
|
+
age: int
|
|
266
|
+
|
|
267
|
+
table = Table("rows", MetaData(), Column("age", String))
|
|
268
|
+
rules = Compiler(
|
|
269
|
+
plugins=[SQLitePlugin()],
|
|
270
|
+
dialect="sqlite",
|
|
271
|
+
emit_type_checks=True,
|
|
272
|
+
cache=False,
|
|
273
|
+
).compile(Filter, table)
|
|
274
|
+
compiled = str(
|
|
275
|
+
rules["age"][0].compile(
|
|
276
|
+
dialect=sqlite.dialect(),
|
|
277
|
+
compile_kwargs={"literal_binds": True},
|
|
278
|
+
)
|
|
279
|
+
)
|
|
280
|
+
assert "REGEXP" in compiled
|
|
281
|
+
assert "rows.age" in compiled
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def test_type_check_lax_int_string_executes_with_register_regexp() -> None:
|
|
285
|
+
class Filter(BaseModel):
|
|
286
|
+
age: int
|
|
287
|
+
|
|
288
|
+
metadata = MetaData()
|
|
289
|
+
table = Table("rows", metadata, Column("id", String), Column("age", String))
|
|
290
|
+
rules = Compiler(
|
|
291
|
+
plugins=[SQLitePlugin()],
|
|
292
|
+
dialect="sqlite",
|
|
293
|
+
emit_type_checks=True,
|
|
294
|
+
cache=False,
|
|
295
|
+
).compile(Filter, table)
|
|
296
|
+
|
|
297
|
+
engine = create_engine("sqlite://")
|
|
298
|
+
with engine.begin() as conn:
|
|
299
|
+
raw = conn.connection.dbapi_connection
|
|
300
|
+
register_regexp(raw) # type: ignore[arg-type]
|
|
301
|
+
metadata.create_all(conn)
|
|
302
|
+
conn.execute(
|
|
303
|
+
table.insert(),
|
|
304
|
+
[
|
|
305
|
+
{"id": "ok", "age": "42"},
|
|
306
|
+
{"id": "bad", "age": "x"},
|
|
307
|
+
{"id": "null", "age": None},
|
|
308
|
+
],
|
|
309
|
+
)
|
|
310
|
+
rows = [
|
|
311
|
+
row[0]
|
|
312
|
+
for row in conn.execute(select(table.c.id).where(*rules["age"]).order_by(table.c.id))
|
|
313
|
+
]
|
|
314
|
+
assert rows == ["ok"]
|