dj-turso 0.1.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.
- dj_turso-0.1.0/.gitignore +20 -0
- dj_turso-0.1.0/LICENSE +21 -0
- dj_turso-0.1.0/PKG-INFO +97 -0
- dj_turso-0.1.0/README.md +64 -0
- dj_turso-0.1.0/dj_turso/__init__.py +0 -0
- dj_turso-0.1.0/dj_turso/base.py +144 -0
- dj_turso-0.1.0/dj_turso/compiler.py +255 -0
- dj_turso-0.1.0/dj_turso/creation.py +136 -0
- dj_turso-0.1.0/dj_turso/cursor.py +127 -0
- dj_turso-0.1.0/dj_turso/drivers.py +111 -0
- dj_turso-0.1.0/dj_turso/exceptions.py +53 -0
- dj_turso-0.1.0/dj_turso/features.py +19 -0
- dj_turso-0.1.0/dj_turso/introspection.py +74 -0
- dj_turso-0.1.0/dj_turso/operations.py +263 -0
- dj_turso-0.1.0/dj_turso/py.typed +0 -0
- dj_turso-0.1.0/dj_turso/session.py +526 -0
- dj_turso-0.1.0/pyproject.toml +200 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
*.db
|
|
2
|
+
*.db-shm
|
|
3
|
+
*.db-wal
|
|
4
|
+
*.egg-info/
|
|
5
|
+
*.pyc
|
|
6
|
+
.coverage
|
|
7
|
+
.coverage.*
|
|
8
|
+
.env
|
|
9
|
+
.mypy_cache/
|
|
10
|
+
.pytest_cache/
|
|
11
|
+
.python-version
|
|
12
|
+
.ruff_cache/
|
|
13
|
+
.tox/
|
|
14
|
+
.venv/
|
|
15
|
+
__pycache__/
|
|
16
|
+
build/
|
|
17
|
+
coverage.xml
|
|
18
|
+
dist/
|
|
19
|
+
junit*.xml
|
|
20
|
+
reference/
|
dj_turso-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lincoln Loop
|
|
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.
|
dj_turso-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: dj-turso
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Django database backend for Turso, local files and Turso Cloud.
|
|
5
|
+
Project-URL: Changelog, https://github.com/lincolnloop/dj-turso/releases
|
|
6
|
+
Project-URL: Homepage, https://github.com/lincolnloop/dj-turso
|
|
7
|
+
Project-URL: Issues, https://github.com/lincolnloop/dj-turso/issues
|
|
8
|
+
Project-URL: Repository, https://github.com/lincolnloop/dj-turso
|
|
9
|
+
Author-email: Marc Gibbons <marc@lincolnloop.com>, Lincoln Loop <info@lincolnloop.com>
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: database,django,sqlite,turso
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Environment :: Web Environment
|
|
15
|
+
Classifier: Framework :: Django
|
|
16
|
+
Classifier: Framework :: Django :: 6.1
|
|
17
|
+
Classifier: Intended Audience :: Developers
|
|
18
|
+
Classifier: Operating System :: OS Independent
|
|
19
|
+
Classifier: Programming Language :: Python
|
|
20
|
+
Classifier: Programming Language :: Python :: 3
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
25
|
+
Classifier: Topic :: Database
|
|
26
|
+
Classifier: Typing :: Typed
|
|
27
|
+
Requires-Python: >=3.12
|
|
28
|
+
Requires-Dist: django<6.2,>=6.1
|
|
29
|
+
Requires-Dist: pyturso<0.9,>=0.8.3
|
|
30
|
+
Provides-Extra: serverless
|
|
31
|
+
Requires-Dist: turso-serverless<0.1.1,>=0.1.0; extra == 'serverless'
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
|
|
34
|
+
# dj-turso
|
|
35
|
+
|
|
36
|
+
<!-- prettier-ignore-start -->
|
|
37
|
+
[](https://github.com/lincolnloop/dj-turso/actions/workflows/test.yml?query=branch%3Amain+event%3Apush)
|
|
38
|
+
[](https://codecov.io/gh/lincolnloop/dj-turso)
|
|
39
|
+
[](https://pypi.org/project/dj-turso/)
|
|
40
|
+
[](https://pypi.org/project/dj-turso/)
|
|
41
|
+
[](https://pypi.org/project/dj-turso/)
|
|
42
|
+
[](https://github.com/lincolnloop/dj-turso/blob/main/LICENSE)
|
|
43
|
+
<!-- prettier-ignore-end -->
|
|
44
|
+
|
|
45
|
+
A Django database backend for [Turso](https://turso.tech), the Rust rewrite of SQLite.
|
|
46
|
+
Use a local database file in development and Turso Cloud in production, with one engine.
|
|
47
|
+
|
|
48
|
+
**Status: alpha.** Requires Django 6.1 and Python 3.12+.
|
|
49
|
+
|
|
50
|
+
## Install
|
|
51
|
+
|
|
52
|
+
```shell
|
|
53
|
+
pip install dj-turso
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Use a local file
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
DATABASES = {
|
|
60
|
+
"default": {
|
|
61
|
+
"ENGINE": "dj_turso",
|
|
62
|
+
"NAME": "default.db",
|
|
63
|
+
},
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Use Turso Cloud
|
|
68
|
+
|
|
69
|
+
```shell
|
|
70
|
+
pip install "dj-turso[serverless]"
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
DATABASES = {
|
|
75
|
+
"default": {
|
|
76
|
+
"ENGINE": "dj_turso",
|
|
77
|
+
"NAME": "turso://my-database.turso.io",
|
|
78
|
+
"OPTIONS": {"auth_token": os.environ["TURSO_AUTH_TOKEN"]},
|
|
79
|
+
},
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Good to know
|
|
84
|
+
|
|
85
|
+
- A local file opens in one process at a time: fine for `runserver` and tests, not for a
|
|
86
|
+
server with several workers. Use Turso Cloud there.
|
|
87
|
+
- A Turso Cloud database must be on the Turso engine, not libSQL.
|
|
88
|
+
- Date lookups need the active time zone to match the database's, UTC by default.
|
|
89
|
+
|
|
90
|
+
## Learn more
|
|
91
|
+
|
|
92
|
+
- [Testing your project](https://github.com/lincolnloop/dj-turso/blob/main/docs/testing.md)
|
|
93
|
+
- [Compatibility](https://github.com/lincolnloop/dj-turso/blob/main/docs/compatibility.md):
|
|
94
|
+
regular expressions, unsupported features, known Turso bugs
|
|
95
|
+
- [Example project](https://github.com/lincolnloop/dj-turso/tree/main/examples/polls)
|
|
96
|
+
- [Contributing](https://github.com/lincolnloop/dj-turso/blob/main/CONTRIBUTING.md)
|
|
97
|
+
- [Changelog](https://github.com/lincolnloop/dj-turso/releases)
|
dj_turso-0.1.0/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# dj-turso
|
|
2
|
+
|
|
3
|
+
<!-- prettier-ignore-start -->
|
|
4
|
+
[](https://github.com/lincolnloop/dj-turso/actions/workflows/test.yml?query=branch%3Amain+event%3Apush)
|
|
5
|
+
[](https://codecov.io/gh/lincolnloop/dj-turso)
|
|
6
|
+
[](https://pypi.org/project/dj-turso/)
|
|
7
|
+
[](https://pypi.org/project/dj-turso/)
|
|
8
|
+
[](https://pypi.org/project/dj-turso/)
|
|
9
|
+
[](https://github.com/lincolnloop/dj-turso/blob/main/LICENSE)
|
|
10
|
+
<!-- prettier-ignore-end -->
|
|
11
|
+
|
|
12
|
+
A Django database backend for [Turso](https://turso.tech), the Rust rewrite of SQLite.
|
|
13
|
+
Use a local database file in development and Turso Cloud in production, with one engine.
|
|
14
|
+
|
|
15
|
+
**Status: alpha.** Requires Django 6.1 and Python 3.12+.
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```shell
|
|
20
|
+
pip install dj-turso
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Use a local file
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
DATABASES = {
|
|
27
|
+
"default": {
|
|
28
|
+
"ENGINE": "dj_turso",
|
|
29
|
+
"NAME": "default.db",
|
|
30
|
+
},
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Use Turso Cloud
|
|
35
|
+
|
|
36
|
+
```shell
|
|
37
|
+
pip install "dj-turso[serverless]"
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
DATABASES = {
|
|
42
|
+
"default": {
|
|
43
|
+
"ENGINE": "dj_turso",
|
|
44
|
+
"NAME": "turso://my-database.turso.io",
|
|
45
|
+
"OPTIONS": {"auth_token": os.environ["TURSO_AUTH_TOKEN"]},
|
|
46
|
+
},
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Good to know
|
|
51
|
+
|
|
52
|
+
- A local file opens in one process at a time: fine for `runserver` and tests, not for a
|
|
53
|
+
server with several workers. Use Turso Cloud there.
|
|
54
|
+
- A Turso Cloud database must be on the Turso engine, not libSQL.
|
|
55
|
+
- Date lookups need the active time zone to match the database's, UTC by default.
|
|
56
|
+
|
|
57
|
+
## Learn more
|
|
58
|
+
|
|
59
|
+
- [Testing your project](https://github.com/lincolnloop/dj-turso/blob/main/docs/testing.md)
|
|
60
|
+
- [Compatibility](https://github.com/lincolnloop/dj-turso/blob/main/docs/compatibility.md):
|
|
61
|
+
regular expressions, unsupported features, known Turso bugs
|
|
62
|
+
- [Example project](https://github.com/lincolnloop/dj-turso/tree/main/examples/polls)
|
|
63
|
+
- [Contributing](https://github.com/lincolnloop/dj-turso/blob/main/CONTRIBUTING.md)
|
|
64
|
+
- [Changelog](https://github.com/lincolnloop/dj-turso/releases)
|
|
File without changes
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import collections.abc
|
|
2
|
+
import typing as t
|
|
3
|
+
|
|
4
|
+
from django.db import utils as db_utils
|
|
5
|
+
from django.db.backends.sqlite3 import base as sqlite_base
|
|
6
|
+
from django.utils import asyncio, functional
|
|
7
|
+
|
|
8
|
+
from dj_turso import (
|
|
9
|
+
creation,
|
|
10
|
+
cursor,
|
|
11
|
+
drivers,
|
|
12
|
+
exceptions,
|
|
13
|
+
features,
|
|
14
|
+
introspection,
|
|
15
|
+
operations,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
# Options Django's sqlite3 backend sets for the stdlib module that no Turso
|
|
19
|
+
# driver accepts.
|
|
20
|
+
STDLIB_CONNECT_KWARGS = ("check_same_thread", "detect_types", "uri")
|
|
21
|
+
|
|
22
|
+
# The SQLite release Turso's dialect tracks. Constant, not the driver's report:
|
|
23
|
+
# remote drivers report none, and Turso lags SQLite where a version would claim.
|
|
24
|
+
SQLITE_DIALECT_VERSION = (3, 50, 4)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class DatabaseWrapper(sqlite_base.DatabaseWrapper):
|
|
28
|
+
display_name = "Turso"
|
|
29
|
+
init_commands: list[str]
|
|
30
|
+
creation_class = creation.DatabaseCreation
|
|
31
|
+
features_class = features.DatabaseFeatures
|
|
32
|
+
introspection_class = introspection.DatabaseIntrospection
|
|
33
|
+
ops_class = operations.DatabaseOperations
|
|
34
|
+
sql_dangling_foreign_keys = (
|
|
35
|
+
"SELECT referring.%(primary_key)s, referring.%(column)s "
|
|
36
|
+
"FROM %(table)s AS referring LEFT JOIN %(referenced_table)s AS referred "
|
|
37
|
+
"ON referring.%(column)s = referred.%(referenced_column)s "
|
|
38
|
+
"WHERE referring.%(column)s IS NOT NULL "
|
|
39
|
+
"AND referred.%(referenced_column)s IS NULL"
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
def __init__(self, settings_dict: dict[str, t.Any], alias: str = "default") -> None:
|
|
43
|
+
super().__init__(settings_dict, alias)
|
|
44
|
+
self.Database = drivers.get_database_module(str(settings_dict["NAME"]))
|
|
45
|
+
# Test database setup swaps NAME in place; keep what was configured.
|
|
46
|
+
self.configured_name_is_remote = drivers.is_remote_name(
|
|
47
|
+
str(settings_dict["NAME"])
|
|
48
|
+
)
|
|
49
|
+
# Whether Turso compiles each regex lookup's pattern, by lookup and pattern.
|
|
50
|
+
self.compilable_regexes: dict[tuple[str, t.Any], bool] = {}
|
|
51
|
+
|
|
52
|
+
def get_connection_params(self) -> dict[str, t.Any]:
|
|
53
|
+
kwargs = super().get_connection_params()
|
|
54
|
+
for name in STDLIB_CONNECT_KWARGS:
|
|
55
|
+
del kwargs[name]
|
|
56
|
+
database = str(kwargs["database"])
|
|
57
|
+
# A remote database's token has no use on its local test database.
|
|
58
|
+
if self.configured_name_is_remote and not drivers.is_remote_name(database):
|
|
59
|
+
kwargs.pop("auth_token", None)
|
|
60
|
+
return kwargs | {"database": database, "isolation_level": None}
|
|
61
|
+
|
|
62
|
+
def get_database_version(self) -> tuple[int, ...]:
|
|
63
|
+
return SQLITE_DIALECT_VERSION
|
|
64
|
+
|
|
65
|
+
# Only whether turso_version() exists tells Turso from libSQL: its value differs
|
|
66
|
+
# between the local driver and Turso Cloud, and so orders nothing.
|
|
67
|
+
def check_database_version_supported(self) -> None:
|
|
68
|
+
super().check_database_version_supported()
|
|
69
|
+
self.get_turso_version()
|
|
70
|
+
|
|
71
|
+
def get_turso_version(self) -> str:
|
|
72
|
+
try:
|
|
73
|
+
return self.turso_version
|
|
74
|
+
except db_utils.OperationalError as exc:
|
|
75
|
+
if "no such function: turso_version" not in str(exc):
|
|
76
|
+
raise
|
|
77
|
+
raise exceptions.EngineNotSupportedError from exc
|
|
78
|
+
|
|
79
|
+
@functional.cached_property
|
|
80
|
+
def turso_version(self) -> str:
|
|
81
|
+
with self.temporary_connection() as cursor:
|
|
82
|
+
cursor.execute("SELECT turso_version()")
|
|
83
|
+
row = cursor.fetchone()
|
|
84
|
+
return str(row[0])
|
|
85
|
+
|
|
86
|
+
@asyncio.async_unsafe
|
|
87
|
+
def get_new_connection(self, conn_params: dict[str, t.Any]) -> t.Any:
|
|
88
|
+
# Test database setup swaps NAME after init, possibly from a URL to a file.
|
|
89
|
+
self.Database = drivers.get_database_module(conn_params["database"])
|
|
90
|
+
with drivers.relabel_engine_errors():
|
|
91
|
+
connection = self.Database.connect(**conn_params)
|
|
92
|
+
init_commands = filter(None, map(str.strip, self.init_commands))
|
|
93
|
+
self.Database.set_up_session(
|
|
94
|
+
connection, ["PRAGMA foreign_keys = ON", *init_commands]
|
|
95
|
+
)
|
|
96
|
+
return connection
|
|
97
|
+
|
|
98
|
+
def check_constraints(
|
|
99
|
+
self, table_names: collections.abc.Iterable[str] | None = None
|
|
100
|
+
) -> None:
|
|
101
|
+
# Turso answers PRAGMA foreign_key_check with no rows, so join each
|
|
102
|
+
# foreign key to its target the way Django's MySQL backend does. Like
|
|
103
|
+
# the pragma, a table without a primary key reports its rowid.
|
|
104
|
+
quote_name = self.ops.quote_name
|
|
105
|
+
with self.cursor() as cursor:
|
|
106
|
+
if table_names is None:
|
|
107
|
+
table_names = self.introspection.table_names(cursor)
|
|
108
|
+
for table_name in table_names:
|
|
109
|
+
primary_key_column_name = (
|
|
110
|
+
self.introspection.get_primary_key_column(cursor, table_name)
|
|
111
|
+
or "rowid"
|
|
112
|
+
)
|
|
113
|
+
relations = self.introspection.get_relations(cursor, table_name)
|
|
114
|
+
for column_name, relation in relations.items():
|
|
115
|
+
referenced_column_name, referenced_table_name = relation[:2]
|
|
116
|
+
cursor.execute(
|
|
117
|
+
self.sql_dangling_foreign_keys
|
|
118
|
+
% {
|
|
119
|
+
"primary_key": quote_name(primary_key_column_name),
|
|
120
|
+
"column": quote_name(column_name),
|
|
121
|
+
"table": quote_name(table_name),
|
|
122
|
+
"referenced_table": quote_name(referenced_table_name),
|
|
123
|
+
"referenced_column": quote_name(referenced_column_name),
|
|
124
|
+
}
|
|
125
|
+
)
|
|
126
|
+
bad_row = cursor.fetchone()
|
|
127
|
+
if bad_row is not None:
|
|
128
|
+
primary_key_value, bad_value = bad_row
|
|
129
|
+
message = (
|
|
130
|
+
f"The row in table '{table_name}' with primary key "
|
|
131
|
+
f"'{primary_key_value}' has an invalid foreign key: "
|
|
132
|
+
f"{table_name}.{column_name} contains a value "
|
|
133
|
+
f"'{bad_value}' that does not have a corresponding "
|
|
134
|
+
f"value in {referenced_table_name}."
|
|
135
|
+
f"{referenced_column_name}."
|
|
136
|
+
)
|
|
137
|
+
raise db_utils.IntegrityError(message)
|
|
138
|
+
|
|
139
|
+
def is_usable(self) -> bool:
|
|
140
|
+
usable: bool = self.Database.is_usable(self.connection)
|
|
141
|
+
return usable
|
|
142
|
+
|
|
143
|
+
def create_cursor(self, name: str | None = None) -> t.Any:
|
|
144
|
+
return cursor.TursoCursorWrapper(self.connection.cursor())
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
"""Native SQL for functions Django's sqlite3 backend runs in Python."""
|
|
2
|
+
|
|
3
|
+
import collections.abc
|
|
4
|
+
import typing as t
|
|
5
|
+
|
|
6
|
+
import django.core.exceptions
|
|
7
|
+
from django.db.models import aggregates, expressions, functions, lookups
|
|
8
|
+
from django.db.models.sql import compiler
|
|
9
|
+
|
|
10
|
+
from dj_turso import base, exceptions, operations
|
|
11
|
+
|
|
12
|
+
SQLParams = tuple[str, tuple[t.Any, ...]]
|
|
13
|
+
|
|
14
|
+
# 53 random bits, as many as a float holds exactly, scaled into [0, 1).
|
|
15
|
+
RANDOM_TEMPLATE = "((RANDOM() & 9007199254740991) / 9007199254740992.0)"
|
|
16
|
+
|
|
17
|
+
# Django stores a UUID as 32 lowercase hex digits; Turso's functions return a blob.
|
|
18
|
+
NATIVE_TEMPLATES: dict[type[expressions.Func], str] = {
|
|
19
|
+
functions.Cot: "(1 / TAN(%(expressions)s))",
|
|
20
|
+
functions.Random: RANDOM_TEMPLATE,
|
|
21
|
+
functions.UUID4: "LOWER(HEX(UUID4()))",
|
|
22
|
+
functions.UUID7: "LOWER(HEX(UUID7()))",
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
# Turso, like SQLite, ignores a negative ROUND precision; scale to units and back.
|
|
26
|
+
# POWER returns a real, so integer operands do not divide as integers. Past 10^308
|
|
27
|
+
# it overflows to infinity, where the quotient is 0; capped, 0 times it is not NULL.
|
|
28
|
+
ROUND_TEMPLATE = (
|
|
29
|
+
"(ROUND(%(expressions)s / {scale}, MAX({precision}, 0)) * MIN({scale}, 1e308))"
|
|
30
|
+
)
|
|
31
|
+
# Truncated to an integer, as ROUND truncates its own precision.
|
|
32
|
+
ROUND_SCALE_TEMPLATE = "POWER(10, -MIN(CAST({precision} AS INTEGER), 0))"
|
|
33
|
+
|
|
34
|
+
HASH_FUNCTIONS = (
|
|
35
|
+
functions.MD5,
|
|
36
|
+
functions.SHA1,
|
|
37
|
+
functions.SHA224,
|
|
38
|
+
functions.SHA256,
|
|
39
|
+
functions.SHA384,
|
|
40
|
+
functions.SHA512,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
# Turso has no XOR operator. Each operand repeats, with its parameters, so the
|
|
45
|
+
# expression stays inline where CHECK constraints and indexes allow no subquery.
|
|
46
|
+
BITXOR_TEMPLATE = "(({lhs} | {rhs}) - ({lhs} & {rhs}))"
|
|
47
|
+
|
|
48
|
+
DATE_ARITHMETIC_CONNECTORS = (expressions.Combinable.ADD, expressions.Combinable.SUB)
|
|
49
|
+
|
|
50
|
+
# Turso's STDDEV is the sample deviation; a single value has a population one of 0.
|
|
51
|
+
STDDEV_POP_TEMPLATE = "(COALESCE({stddev}, 0) * SQRT(({count} - 1.0) / {count}))"
|
|
52
|
+
|
|
53
|
+
# The aggregate of each bit, from the lowest: BIT_XOR keeps the parity of the ones.
|
|
54
|
+
BIT_AGGREGATE_FUNCTIONS = {"BIT_AND": "MIN", "BIT_OR": "MAX", "BIT_XOR": "SUM"}
|
|
55
|
+
BIT_TEMPLATE = "((%(function)s((%(expressions)s >> {bit}) & 1)%(filter)s & 1) << {bit})"
|
|
56
|
+
INTEGER_BITS = 64
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class SQLCompiler(compiler.SQLCompiler):
|
|
60
|
+
def compile(self, node: t.Any) -> t.Any:
|
|
61
|
+
if isinstance(node, HASH_FUNCTIONS):
|
|
62
|
+
raise exceptions.HashFunctionNotSupportedError(type(node).__name__)
|
|
63
|
+
if isinstance(node, lookups.Regex):
|
|
64
|
+
check_regex(t.cast("base.DatabaseWrapper", self.connection), node)
|
|
65
|
+
if (
|
|
66
|
+
isinstance(node, expressions.DurationExpression)
|
|
67
|
+
and node.connector in DATE_ARITHMETIC_CONNECTORS
|
|
68
|
+
):
|
|
69
|
+
return compile_date_arithmetic(self, node)
|
|
70
|
+
if (
|
|
71
|
+
isinstance(node, expressions.CombinedExpression)
|
|
72
|
+
and not isinstance(node, expressions.DurationExpression)
|
|
73
|
+
and node.connector == expressions.Combinable.BITXOR
|
|
74
|
+
):
|
|
75
|
+
return compile_bitxor(self, node)
|
|
76
|
+
if isinstance(node, functions.Round) and may_have_negative_precision(node):
|
|
77
|
+
return compile_round(self, node)
|
|
78
|
+
if isinstance(node, aggregates.Aggregate):
|
|
79
|
+
aggregate_compiler = AGGREGATE_COMPILERS.get(node.function)
|
|
80
|
+
if aggregate_compiler is not None:
|
|
81
|
+
return aggregate_compiler(self, node)
|
|
82
|
+
for function, template in NATIVE_TEMPLATES.items():
|
|
83
|
+
if isinstance(node, function):
|
|
84
|
+
# as_sqlite keeps Django's NUMERIC cast of decimal results.
|
|
85
|
+
return node.as_sqlite(self, self.connection, template=template)
|
|
86
|
+
return super().compile(node)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class SQLInsertCompiler(SQLCompiler, compiler.SQLInsertCompiler):
|
|
90
|
+
pass
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
class SQLDeleteCompiler(SQLCompiler, compiler.SQLDeleteCompiler):
|
|
94
|
+
pass
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class SQLUpdateCompiler(SQLCompiler, compiler.SQLUpdateCompiler):
|
|
98
|
+
pass
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class SQLAggregateCompiler(SQLCompiler, compiler.SQLAggregateCompiler):
|
|
102
|
+
pass
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
# A pattern known only once the query runs, such as a column's, goes unchecked.
|
|
106
|
+
def check_regex(connection: base.DatabaseWrapper, node: lookups.Regex) -> None:
|
|
107
|
+
pattern = get_literal_pattern(node)
|
|
108
|
+
# Every regex lookup names itself: regex or iregex.
|
|
109
|
+
lookup_name = t.cast("str", node.lookup_name)
|
|
110
|
+
if pattern is not None and not is_regex_compilable(
|
|
111
|
+
connection, lookup_name, pattern
|
|
112
|
+
):
|
|
113
|
+
raise exceptions.RegexNotSupportedError(pattern)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def get_literal_pattern(node: lookups.Regex) -> t.Any:
|
|
117
|
+
if node.rhs_is_direct_value():
|
|
118
|
+
return node.rhs
|
|
119
|
+
if isinstance(node.rhs, expressions.Value):
|
|
120
|
+
return node.rhs.value
|
|
121
|
+
return None
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
# Turso's regexp() is NULL for a pattern its Rust regex crate cannot compile, which
|
|
125
|
+
# would silently match nothing. The probe sends the pattern as the lookup's operator.
|
|
126
|
+
def is_regex_compilable(
|
|
127
|
+
connection: base.DatabaseWrapper, lookup_name: str, pattern: t.Any
|
|
128
|
+
) -> bool:
|
|
129
|
+
key = (lookup_name, pattern)
|
|
130
|
+
if key not in connection.compilable_regexes:
|
|
131
|
+
operand = connection.operators[lookup_name].removeprefix("REGEXP ")
|
|
132
|
+
with connection.cursor() as cursor:
|
|
133
|
+
cursor.execute(f"SELECT regexp({operand}, '')", [pattern])
|
|
134
|
+
connection.compilable_regexes[key] = cursor.fetchone()[0] is not None
|
|
135
|
+
return connection.compilable_regexes[key]
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
# A precision known only once the query runs, such as a column's, may be negative.
|
|
139
|
+
def may_have_negative_precision(node: functions.Round) -> bool:
|
|
140
|
+
precision = node.get_source_expressions()[1]
|
|
141
|
+
return not isinstance(precision, expressions.Value) or precision.value < 0
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
# Func's own as_sqlite keeps Django's NUMERIC cast of decimal results, which Round's
|
|
145
|
+
# would refuse for a negative precision. The precision repeats, with its parameters.
|
|
146
|
+
def compile_round(compiler: SQLCompiler, node: functions.Round) -> SQLParams:
|
|
147
|
+
value, precision = node.get_source_expressions()
|
|
148
|
+
precision_sql, precision_params = compiler.compile(precision)
|
|
149
|
+
precision_sql = precision_sql.replace("%", "%%")
|
|
150
|
+
rounded = node.copy()
|
|
151
|
+
rounded.set_source_expressions([value])
|
|
152
|
+
sql, params = expressions.Func.as_sqlite(
|
|
153
|
+
rounded,
|
|
154
|
+
compiler,
|
|
155
|
+
compiler.connection,
|
|
156
|
+
template=ROUND_TEMPLATE.format(
|
|
157
|
+
precision=precision_sql,
|
|
158
|
+
scale=ROUND_SCALE_TEMPLATE.format(precision=precision_sql),
|
|
159
|
+
),
|
|
160
|
+
)
|
|
161
|
+
return sql, (*params, *precision_params, *precision_params, *precision_params)
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def compile_bitxor(
|
|
165
|
+
compiler: SQLCompiler, node: expressions.CombinedExpression
|
|
166
|
+
) -> SQLParams:
|
|
167
|
+
return operations.fill_template(
|
|
168
|
+
BITXOR_TEMPLATE, lhs=compiler.compile(node.lhs), rhs=compiler.compile(node.rhs)
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def compile_date_arithmetic(
|
|
173
|
+
compiler: SQLCompiler, node: expressions.DurationExpression
|
|
174
|
+
) -> SQLParams:
|
|
175
|
+
compiler.connection.ops.check_expression_support(node)
|
|
176
|
+
temporal, duration = node.lhs, node.rhs
|
|
177
|
+
if is_duration(temporal):
|
|
178
|
+
temporal, duration = duration, temporal
|
|
179
|
+
sql, params = operations.get_date_arithmetic_sql(
|
|
180
|
+
node.connector, compiler.compile(temporal), compiler.compile(duration)
|
|
181
|
+
)
|
|
182
|
+
return f"({sql})", params
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def is_duration(expression: t.Any) -> bool:
|
|
186
|
+
try:
|
|
187
|
+
output_field = expression.output_field
|
|
188
|
+
except django.core.exceptions.FieldError:
|
|
189
|
+
return False
|
|
190
|
+
internal_type: str = output_field.get_internal_type()
|
|
191
|
+
return internal_type == "DurationField"
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def compile_any_value(compiler: SQLCompiler, node: t.Any) -> SQLParams:
|
|
195
|
+
return compile_call(compiler, node, "MIN")
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def compile_stddev_samp(compiler: SQLCompiler, node: t.Any) -> SQLParams:
|
|
199
|
+
return compile_call(compiler, node, "STDDEV")
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def compile_stddev_pop(compiler: SQLCompiler, node: t.Any) -> SQLParams:
|
|
203
|
+
return operations.fill_template(
|
|
204
|
+
STDDEV_POP_TEMPLATE,
|
|
205
|
+
count=compile_call(compiler, node, "COUNT"),
|
|
206
|
+
stddev=compile_call(compiler, node, "STDDEV"),
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def compile_var_samp(compiler: SQLCompiler, node: t.Any) -> SQLParams:
|
|
211
|
+
return operations.fill_template(
|
|
212
|
+
"POWER({stddev}, 2)", stddev=compile_stddev_samp(compiler, node)
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def compile_var_pop(compiler: SQLCompiler, node: t.Any) -> SQLParams:
|
|
217
|
+
return operations.fill_template(
|
|
218
|
+
"POWER({stddev}, 2)", stddev=compile_stddev_pop(compiler, node)
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
# One aggregate per bit, each compiled on its own to carry its own parameters.
|
|
223
|
+
def compile_bit_aggregate(compiler: SQLCompiler, node: t.Any) -> SQLParams:
|
|
224
|
+
function = BIT_AGGREGATE_FUNCTIONS[node.function]
|
|
225
|
+
terms = [
|
|
226
|
+
node.as_sqlite(
|
|
227
|
+
compiler,
|
|
228
|
+
compiler.connection,
|
|
229
|
+
function=function,
|
|
230
|
+
template=BIT_TEMPLATE.format(bit=bit),
|
|
231
|
+
)
|
|
232
|
+
for bit in range(INTEGER_BITS)
|
|
233
|
+
]
|
|
234
|
+
return "(" + " | ".join(sql for sql, _ in terms) + ")", tuple(
|
|
235
|
+
param for _, params in terms for param in params
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def compile_call(compiler: SQLCompiler, node: t.Any, function: str) -> SQLParams:
|
|
240
|
+
sql, params = node.as_sqlite(compiler, compiler.connection, function=function)
|
|
241
|
+
return sql, tuple(params)
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
AGGREGATE_COMPILERS: dict[
|
|
245
|
+
str | None, collections.abc.Callable[[SQLCompiler, t.Any], SQLParams]
|
|
246
|
+
] = {
|
|
247
|
+
"ANY_VALUE": compile_any_value,
|
|
248
|
+
"BIT_AND": compile_bit_aggregate,
|
|
249
|
+
"BIT_OR": compile_bit_aggregate,
|
|
250
|
+
"BIT_XOR": compile_bit_aggregate,
|
|
251
|
+
"STDDEV_POP": compile_stddev_pop,
|
|
252
|
+
"STDDEV_SAMP": compile_stddev_samp,
|
|
253
|
+
"VAR_POP": compile_var_pop,
|
|
254
|
+
"VAR_SAMP": compile_var_samp,
|
|
255
|
+
}
|