narabi-orm 0.1.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.
- narabi/__init__.py +27 -0
- narabi/__main__.py +5 -0
- narabi/cli.py +338 -0
- narabi/console.py +102 -0
- narabi/database.py +310 -0
- narabi/fields/__init__.py +13 -0
- narabi/fields/defaults.py +59 -0
- narabi/fields/markers.py +74 -0
- narabi/migrations/__init__.py +22 -0
- narabi/migrations/bootstrap.py +30 -0
- narabi/migrations/decorators.py +22 -0
- narabi/migrations/diff.py +96 -0
- narabi/migrations/dsl.py +255 -0
- narabi/migrations/errors.py +5 -0
- narabi/migrations/executor.py +299 -0
- narabi/migrations/generate.py +149 -0
- narabi/migrations/historical.py +158 -0
- narabi/migrations/history.py +160 -0
- narabi/migrations/models.py +57 -0
- narabi/migrations/postgres_executor.py +239 -0
- narabi/migrations/postgresql.py +268 -0
- narabi/migrations/preview.py +150 -0
- narabi/migrations/replay.py +40 -0
- narabi/migrations/reverse.py +127 -0
- narabi/migrations/schema.py +169 -0
- narabi/migrations/snapshot.py +52 -0
- narabi/migrations/sqlite.py +131 -0
- narabi/models/__init__.py +1 -0
- narabi/models/bulk.py +123 -0
- narabi/models/core.py +440 -0
- narabi/models/inflection.py +37 -0
- narabi/models/lifecycle.py +125 -0
- narabi/models/privacy.py +19 -0
- narabi/models/serialization.py +145 -0
- narabi/models/soft_delete.py +59 -0
- narabi/models/timestamps.py +47 -0
- narabi/models/validation.py +149 -0
- narabi/models/values.py +167 -0
- narabi/project.py +108 -0
- narabi/py.typed +0 -0
- narabi/pytest.py +11 -0
- narabi/query/__init__.py +7 -0
- narabi/query/expressions.py +180 -0
- narabi/query/relation.py +562 -0
- narabi/query/scopes.py +36 -0
- narabi/relations/__init__.py +6 -0
- narabi/relations/prefetch.py +66 -0
- narabi/relations/references.py +196 -0
- narabi/relations/relationships.py +185 -0
- narabi/schema.py +132 -0
- narabi/stubs.py +275 -0
- narabi_orm-0.1.0.dist-info/METADATA +104 -0
- narabi_orm-0.1.0.dist-info/RECORD +56 -0
- narabi_orm-0.1.0.dist-info/WHEEL +4 -0
- narabi_orm-0.1.0.dist-info/entry_points.txt +2 -0
- narabi_orm-0.1.0.dist-info/licenses/LICENSE +29 -0
narabi/query/__init__.py
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
"""Anonymous field expressions for ORM filters and ordering."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Callable
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Predicate:
|
|
8
|
+
def __and__(self, other: "Predicate") -> "Predicate":
|
|
9
|
+
return _combine(self, "AND", other)
|
|
10
|
+
|
|
11
|
+
def __or__(self, other: "Predicate") -> "Predicate":
|
|
12
|
+
return _combine(self, "OR", other)
|
|
13
|
+
|
|
14
|
+
def __invert__(self) -> "Predicate":
|
|
15
|
+
return _Not(self)
|
|
16
|
+
|
|
17
|
+
def __bool__(self) -> bool:
|
|
18
|
+
raise TypeError("Use &, | and ~ to combine query predicates")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _combine(left, operator, right):
|
|
22
|
+
if not isinstance(right, Predicate):
|
|
23
|
+
raise TypeError("Expected a query predicate")
|
|
24
|
+
return _Boolean(left, operator, right)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class Operand(Predicate):
|
|
28
|
+
def __eq__(self, value: object) -> Predicate:
|
|
29
|
+
return _Comparison(self, "=", value)
|
|
30
|
+
|
|
31
|
+
def __ne__(self, value: object) -> Predicate:
|
|
32
|
+
return _Comparison(self, "<>", value)
|
|
33
|
+
|
|
34
|
+
def __lt__(self, value: object) -> Predicate:
|
|
35
|
+
return _Comparison(self, "<", value)
|
|
36
|
+
|
|
37
|
+
def __le__(self, value: object) -> Predicate:
|
|
38
|
+
return _Comparison(self, "<=", value)
|
|
39
|
+
|
|
40
|
+
def __gt__(self, value: object) -> Predicate:
|
|
41
|
+
return _Comparison(self, ">", value)
|
|
42
|
+
|
|
43
|
+
def __ge__(self, value: object) -> Predicate:
|
|
44
|
+
return _Comparison(self, ">=", value)
|
|
45
|
+
|
|
46
|
+
def __neg__(self) -> "Descending":
|
|
47
|
+
return Descending(self)
|
|
48
|
+
|
|
49
|
+
def is_null(self) -> Predicate:
|
|
50
|
+
return _Comparison(self, "=", None)
|
|
51
|
+
|
|
52
|
+
def is_not_null(self) -> Predicate:
|
|
53
|
+
return _Comparison(self, "<>", None)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass(frozen=True, eq=False)
|
|
57
|
+
class Field(Operand):
|
|
58
|
+
_path: str
|
|
59
|
+
|
|
60
|
+
def __getattr__(self, name: str) -> "Field":
|
|
61
|
+
if name.startswith("__"):
|
|
62
|
+
raise AttributeError(name)
|
|
63
|
+
return Field(f"{self._path}.{name}")
|
|
64
|
+
|
|
65
|
+
def contains(self, text: str) -> Predicate:
|
|
66
|
+
if not isinstance(text, str):
|
|
67
|
+
raise TypeError("contains() expects a string")
|
|
68
|
+
return _Contains(self, text)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@dataclass(frozen=True, eq=False)
|
|
72
|
+
class Aggregate(Operand):
|
|
73
|
+
_function: str
|
|
74
|
+
_field: Field | None
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def count(field: str | Field | None = None) -> Aggregate:
|
|
78
|
+
"""Count rows, or non-null values of a field."""
|
|
79
|
+
return Aggregate("COUNT", None if field is None else _as_field(field))
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def sum(field: str | Field) -> Aggregate:
|
|
83
|
+
"""Sum the non-null values of a field."""
|
|
84
|
+
return Aggregate("SUM", _as_field(field))
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def avg(field: str | Field) -> Aggregate:
|
|
88
|
+
"""Average the non-null values of a field."""
|
|
89
|
+
return Aggregate("AVG", _as_field(field))
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _as_field(field: str | Field) -> Field:
|
|
93
|
+
if isinstance(field, str):
|
|
94
|
+
return Field(field)
|
|
95
|
+
if isinstance(field, Field):
|
|
96
|
+
return field
|
|
97
|
+
raise TypeError("Expected a field name or expression")
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
@dataclass(frozen=True)
|
|
101
|
+
class Descending:
|
|
102
|
+
field: Operand
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@dataclass(frozen=True, eq=False)
|
|
106
|
+
class _Comparison(Predicate):
|
|
107
|
+
field: Operand
|
|
108
|
+
operator: str
|
|
109
|
+
value: object
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
@dataclass(frozen=True, eq=False)
|
|
113
|
+
class _Contains(Predicate):
|
|
114
|
+
field: Field
|
|
115
|
+
text: str
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
@dataclass(frozen=True, eq=False)
|
|
119
|
+
class _Boolean(Predicate):
|
|
120
|
+
left: Predicate
|
|
121
|
+
operator: str
|
|
122
|
+
right: Predicate
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
@dataclass(frozen=True, eq=False)
|
|
126
|
+
class _Not(Predicate):
|
|
127
|
+
predicate: Predicate
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
class _Fields:
|
|
131
|
+
__slots__ = ()
|
|
132
|
+
|
|
133
|
+
def __getattr__(self, name: str) -> Field:
|
|
134
|
+
if name.startswith("__") and name.endswith("__"):
|
|
135
|
+
raise AttributeError(name)
|
|
136
|
+
return Field(name)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
it = _Fields()
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def compile_predicate(
|
|
143
|
+
predicate: Predicate,
|
|
144
|
+
column: Callable[[Operand], str],
|
|
145
|
+
bind: Callable[[object], str],
|
|
146
|
+
backend: str,
|
|
147
|
+
field_value: Callable[[str, object], object],
|
|
148
|
+
compare_value: Callable[[str, str, object], str | None],
|
|
149
|
+
) -> str:
|
|
150
|
+
def compile_node(node):
|
|
151
|
+
match node:
|
|
152
|
+
case Operand():
|
|
153
|
+
return column(node)
|
|
154
|
+
case _Comparison(operand, operator, value):
|
|
155
|
+
if isinstance(operand, Field):
|
|
156
|
+
expanded = compare_value(operand._path, operator, value)
|
|
157
|
+
if expanded is not None:
|
|
158
|
+
return expanded
|
|
159
|
+
left = column(operand)
|
|
160
|
+
if isinstance(operand, Field) and not isinstance(value, Operand):
|
|
161
|
+
value = field_value(operand._path, value)
|
|
162
|
+
if value is None:
|
|
163
|
+
if operator == "=":
|
|
164
|
+
return f"{left} IS NULL"
|
|
165
|
+
if operator == "<>":
|
|
166
|
+
return f"{left} IS NOT NULL"
|
|
167
|
+
raise ValueError("Null comparisons require equality or inequality")
|
|
168
|
+
right = column(value) if isinstance(value, Operand) else bind(value)
|
|
169
|
+
return f"{left} {operator} {right}"
|
|
170
|
+
case _Contains(field, text):
|
|
171
|
+
function = "INSTR" if backend == "sqlite" else "STRPOS"
|
|
172
|
+
return f"{function}({column(field)}, {bind(text)}) > 0"
|
|
173
|
+
case _Boolean(left, operator, right):
|
|
174
|
+
return f"({compile_node(left)} {operator} {compile_node(right)})"
|
|
175
|
+
case _Not(operand):
|
|
176
|
+
return f"NOT ({compile_node(operand)})"
|
|
177
|
+
case _:
|
|
178
|
+
raise TypeError("Expected a query predicate")
|
|
179
|
+
|
|
180
|
+
return compile_node(predicate)
|