apexdevkit 2.4.5__tar.gz → 2.5.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.
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/PKG-INFO +1 -1
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/pyproject.toml +1 -4
- apexdevkit-2.4.5/apexdevkit/query/__init__.py +0 -41
- apexdevkit-2.4.5/apexdevkit/query/generator.py +0 -411
- apexdevkit-2.4.5/apexdevkit/query/query.py +0 -169
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/LICENSE +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/README.md +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/__init__.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/annotation/__init__.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/annotation/deprecate.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/date.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/error.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/fastapi/__init__.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/fastapi/builder.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/fastapi/dependable.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/fastapi/endpoints.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/fastapi/name.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/fastapi/response.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/fastapi/router.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/fastapi/schema.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/fastapi/service.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/formatter.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/key_fn.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/observer.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/py.typed +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/repository/__init__.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/repository/core/__init__.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/repository/core/database.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/repository/core/decorator.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/repository/core/interface.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/repository/core/mixin.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/repository/in_memory/__init__.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/repository/in_memory/mixin.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/repository/in_memory/repository.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/repository/in_memory/store.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/repository/sql/__init__.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/repository/sql/connector.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/repository/sql/field.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/repository/sql/mssql/__init__.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/repository/sql/mssql/repository.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/repository/sql/mssql/table.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/repository/sql/sqlite/__init__.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/repository/sql/sqlite/repository.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/repository/sql/sqlite/table.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/server.py +0 -0
- {apexdevkit-2.4.5 → apexdevkit-2.5.1}/apexdevkit/testing.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "apexdevkit"
|
|
3
|
-
version = "2.
|
|
3
|
+
version = "2.5.1"
|
|
4
4
|
description = "Apex Development Tools for python."
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
authors = [
|
|
@@ -103,9 +103,6 @@ source = [
|
|
|
103
103
|
branch = true
|
|
104
104
|
|
|
105
105
|
[tool.coverage.report]
|
|
106
|
-
omit = [
|
|
107
|
-
"apexdevkit/query/*",
|
|
108
|
-
]
|
|
109
106
|
exclude_also = [
|
|
110
107
|
"@(abc\\.)?abstractmethod",
|
|
111
108
|
"class .*\\bProtocol\\)",
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
from apexdevkit.query.query import (
|
|
2
|
-
Aggregation,
|
|
3
|
-
AggregationOption,
|
|
4
|
-
DateValue,
|
|
5
|
-
Filter,
|
|
6
|
-
FooterOptions,
|
|
7
|
-
Leaf,
|
|
8
|
-
ListValue,
|
|
9
|
-
NullValue,
|
|
10
|
-
NumericValue,
|
|
11
|
-
Operation,
|
|
12
|
-
Operator,
|
|
13
|
-
Page,
|
|
14
|
-
QueryOptions,
|
|
15
|
-
Sort,
|
|
16
|
-
StringValue,
|
|
17
|
-
Summary,
|
|
18
|
-
)
|
|
19
|
-
|
|
20
|
-
__all__ = [
|
|
21
|
-
"Aggregation",
|
|
22
|
-
"AggregationOption",
|
|
23
|
-
"DateValue",
|
|
24
|
-
"Filter",
|
|
25
|
-
"FooterOptions",
|
|
26
|
-
"Leaf",
|
|
27
|
-
"ListValue",
|
|
28
|
-
"NullValue",
|
|
29
|
-
"NumericValue",
|
|
30
|
-
"Operation",
|
|
31
|
-
"Operator",
|
|
32
|
-
"Page",
|
|
33
|
-
"QueryOptions",
|
|
34
|
-
"Sort",
|
|
35
|
-
"StringValue",
|
|
36
|
-
"Summary",
|
|
37
|
-
]
|
|
38
|
-
|
|
39
|
-
from apexdevkit.annotation import notify
|
|
40
|
-
|
|
41
|
-
notify("apexdevkit.query is deprecated, will be removed in future versions")
|
|
@@ -1,411 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
from collections import defaultdict
|
|
4
|
-
from dataclasses import dataclass, field
|
|
5
|
-
from typing import TYPE_CHECKING, Any, ClassVar, Protocol, Self
|
|
6
|
-
|
|
7
|
-
from apexdevkit.annotation import deprecated
|
|
8
|
-
from apexdevkit.error import ForbiddenError
|
|
9
|
-
from apexdevkit.query.query import (
|
|
10
|
-
Aggregation,
|
|
11
|
-
AggregationOption,
|
|
12
|
-
Filter,
|
|
13
|
-
FooterOptions,
|
|
14
|
-
Leaf,
|
|
15
|
-
Operation,
|
|
16
|
-
Operator,
|
|
17
|
-
Page,
|
|
18
|
-
QueryOptions,
|
|
19
|
-
Sort,
|
|
20
|
-
StringValue,
|
|
21
|
-
Summary,
|
|
22
|
-
SummaryExtractor,
|
|
23
|
-
)
|
|
24
|
-
from apexdevkit.repository import Database, DatabaseCommand
|
|
25
|
-
|
|
26
|
-
if TYPE_CHECKING:
|
|
27
|
-
from collections.abc import Iterable, Mapping
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
class _Loader[T](Protocol):
|
|
31
|
-
def load(self, raw: Mapping[str, Any]) -> T:
|
|
32
|
-
pass
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
@dataclass
|
|
36
|
-
class DefaultMsSqlFilter[T]:
|
|
37
|
-
_database: Database = field(init=False)
|
|
38
|
-
_query_builder: MsSqlQueryBuilder = field(init=False)
|
|
39
|
-
_options: QueryOptions = field(init=False)
|
|
40
|
-
_formatter: _Loader[T] = field(init=False)
|
|
41
|
-
|
|
42
|
-
def with_database(self, value: Database) -> Self:
|
|
43
|
-
self._database = value
|
|
44
|
-
|
|
45
|
-
return self
|
|
46
|
-
|
|
47
|
-
def with_query_builder(self, value: MsSqlQueryBuilder) -> Self:
|
|
48
|
-
self._query_builder = value
|
|
49
|
-
|
|
50
|
-
return self
|
|
51
|
-
|
|
52
|
-
def with_options(self, value: QueryOptions) -> Self:
|
|
53
|
-
self._options = value
|
|
54
|
-
|
|
55
|
-
return self
|
|
56
|
-
|
|
57
|
-
def with_formatter(self, value: _Loader[T]) -> Self:
|
|
58
|
-
self._formatter = value
|
|
59
|
-
|
|
60
|
-
return self
|
|
61
|
-
|
|
62
|
-
def retrieve(self) -> Iterable[T]:
|
|
63
|
-
if not any(sort.name == "id" for sort in self._options.ordering):
|
|
64
|
-
self._options.ordering.append(Sort(name="id", is_descending=False))
|
|
65
|
-
|
|
66
|
-
result = self._database.execute(
|
|
67
|
-
self._query_builder.filter(self._options)
|
|
68
|
-
).fetch_all()
|
|
69
|
-
|
|
70
|
-
yield from [self._formatter.load(raw) for raw in result]
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
@dataclass
|
|
74
|
-
class DefaultMsSqlAggregate:
|
|
75
|
-
_database: Database = field(init=False)
|
|
76
|
-
_query_builder: MsSqlQueryBuilder = field(init=False)
|
|
77
|
-
_options: FooterOptions = field(init=False)
|
|
78
|
-
|
|
79
|
-
def with_database(self, value: Database) -> Self:
|
|
80
|
-
self._database = value
|
|
81
|
-
|
|
82
|
-
return self
|
|
83
|
-
|
|
84
|
-
def with_query_builder(self, value: MsSqlQueryBuilder) -> Self:
|
|
85
|
-
self._query_builder = value
|
|
86
|
-
|
|
87
|
-
return self
|
|
88
|
-
|
|
89
|
-
def with_options(self, value: FooterOptions) -> Self:
|
|
90
|
-
self._options = value
|
|
91
|
-
|
|
92
|
-
return self
|
|
93
|
-
|
|
94
|
-
def retrieve(self) -> Iterable[Summary]:
|
|
95
|
-
result = self._database.execute(
|
|
96
|
-
self._query_builder.aggregate(self._options)
|
|
97
|
-
).fetch_one()
|
|
98
|
-
|
|
99
|
-
return [
|
|
100
|
-
SummaryExtractor(aggregation).with_value(value)
|
|
101
|
-
for aggregation in self._options.aggregations
|
|
102
|
-
for value in [
|
|
103
|
-
result[
|
|
104
|
-
f"{aggregation.name or 'general'}"
|
|
105
|
-
f"_{aggregation.aggregation.value.lower()}"
|
|
106
|
-
]
|
|
107
|
-
]
|
|
108
|
-
]
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
class SqlGenerator(Protocol):
|
|
112
|
-
def generate(self) -> str:
|
|
113
|
-
pass
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
@dataclass
|
|
117
|
-
class ConstantSqlGenerator:
|
|
118
|
-
value: str
|
|
119
|
-
|
|
120
|
-
def generate(self) -> str:
|
|
121
|
-
return self.value
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
@dataclass
|
|
125
|
-
class NoSqlGenerator(ConstantSqlGenerator):
|
|
126
|
-
value: str = ""
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
@dataclass
|
|
130
|
-
class MsSqlQuery:
|
|
131
|
-
user: SqlGenerator
|
|
132
|
-
selection: SqlGenerator
|
|
133
|
-
filter: SqlGenerator
|
|
134
|
-
|
|
135
|
-
condition: SqlGenerator = field(default_factory=NoSqlGenerator)
|
|
136
|
-
ordering: SqlGenerator = field(default_factory=NoSqlGenerator)
|
|
137
|
-
paging: SqlGenerator = field(default_factory=NoSqlGenerator)
|
|
138
|
-
|
|
139
|
-
def generate(self) -> DatabaseCommand:
|
|
140
|
-
return DatabaseCommand(f"""
|
|
141
|
-
{self.user.generate()}
|
|
142
|
-
{self.selection.generate()}
|
|
143
|
-
{self.filter.generate()}
|
|
144
|
-
{self.condition.generate()}
|
|
145
|
-
{self.ordering.generate()}
|
|
146
|
-
{self.paging.generate()}
|
|
147
|
-
""")
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
@dataclass
|
|
151
|
-
class MsSqlQueryBuilder:
|
|
152
|
-
source: str = field(init=False)
|
|
153
|
-
username: str = field(init=False)
|
|
154
|
-
_fields: list[MsSqlField] = field(init=False)
|
|
155
|
-
|
|
156
|
-
def with_source(self, value: str) -> MsSqlQueryBuilder:
|
|
157
|
-
self.source = value
|
|
158
|
-
|
|
159
|
-
return self
|
|
160
|
-
|
|
161
|
-
def with_username(self, value: str) -> MsSqlQueryBuilder:
|
|
162
|
-
self.username = value
|
|
163
|
-
|
|
164
|
-
return self
|
|
165
|
-
|
|
166
|
-
def with_fields(self, values: list[MsSqlField]) -> MsSqlQueryBuilder:
|
|
167
|
-
self._fields = values
|
|
168
|
-
|
|
169
|
-
return self
|
|
170
|
-
|
|
171
|
-
@deprecated(".with_translations is deprecated, use .with_fields instead")
|
|
172
|
-
def with_translations(self, value: dict[str, str]) -> MsSqlQueryBuilder:
|
|
173
|
-
self._fields = [MsSqlField(name, alias) for alias, name in value.items()]
|
|
174
|
-
|
|
175
|
-
return self
|
|
176
|
-
|
|
177
|
-
def aggregate(self, footer: FooterOptions) -> DatabaseCommand:
|
|
178
|
-
return MsSqlQuery(
|
|
179
|
-
user=MsSqlUserGenerator(self.username),
|
|
180
|
-
selection=MsSqlFooterGenerator(footer.aggregations, self._fields),
|
|
181
|
-
filter=MsSqlSourceGenerator(self.source, footer.filter),
|
|
182
|
-
condition=MsSqlConditionGenerator(footer.condition, self._fields),
|
|
183
|
-
).generate()
|
|
184
|
-
|
|
185
|
-
def filter(self, options: QueryOptions) -> DatabaseCommand:
|
|
186
|
-
return MsSqlQuery(
|
|
187
|
-
user=MsSqlUserGenerator(self.username),
|
|
188
|
-
selection=MsSqlSelectionGenerator(self._fields),
|
|
189
|
-
filter=MsSqlSourceGenerator(self.source, options.filter),
|
|
190
|
-
condition=MsSqlConditionGenerator(options.condition, self._fields),
|
|
191
|
-
ordering=MsSqlOrderGenerator(options.ordering, self._fields),
|
|
192
|
-
paging=MsSqlPagingGenerator(options.paging),
|
|
193
|
-
).generate()
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
@dataclass
|
|
197
|
-
class MsSqlUserGenerator:
|
|
198
|
-
username: str
|
|
199
|
-
|
|
200
|
-
def generate(self) -> str:
|
|
201
|
-
return f"EXECUTE AS USER = '{self.username}'"
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
@dataclass
|
|
205
|
-
class MsSqlField:
|
|
206
|
-
name: str
|
|
207
|
-
|
|
208
|
-
alias: str = ""
|
|
209
|
-
|
|
210
|
-
def as_select_part(self) -> str:
|
|
211
|
-
result = f"[{self.name}]"
|
|
212
|
-
|
|
213
|
-
if self.alias:
|
|
214
|
-
result += f" AS [{self.alias}]"
|
|
215
|
-
|
|
216
|
-
return result
|
|
217
|
-
|
|
218
|
-
def as_order_part(self, *, is_descending: bool = False) -> str:
|
|
219
|
-
result = self.name
|
|
220
|
-
|
|
221
|
-
if is_descending:
|
|
222
|
-
result += " DESC"
|
|
223
|
-
|
|
224
|
-
return result
|
|
225
|
-
|
|
226
|
-
def as_aggregation_part(self, option: Aggregation) -> str:
|
|
227
|
-
return f"{option.value}({self.name}) AS {self.alias}_{option.value.lower()}"
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
@dataclass
|
|
231
|
-
class MsSqlSelectionGenerator:
|
|
232
|
-
fields: list[MsSqlField] = field(default_factory=list)
|
|
233
|
-
|
|
234
|
-
def generate(self) -> str:
|
|
235
|
-
fields = ", ".join([f.as_select_part() for f in self.fields])
|
|
236
|
-
|
|
237
|
-
return f"SELECT {fields}"
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
@dataclass
|
|
241
|
-
class MsSqlFooterGenerator:
|
|
242
|
-
aggregations: list[AggregationOption]
|
|
243
|
-
fields: list[MsSqlField]
|
|
244
|
-
|
|
245
|
-
def generate(self) -> str:
|
|
246
|
-
fields = ", ".join(
|
|
247
|
-
[
|
|
248
|
-
self.field_for(footer.name).as_aggregation_part(footer.aggregation)
|
|
249
|
-
for footer in self.aggregations
|
|
250
|
-
]
|
|
251
|
-
)
|
|
252
|
-
|
|
253
|
-
return f"SELECT {fields}"
|
|
254
|
-
|
|
255
|
-
def field_for(self, name: str | None) -> MsSqlField:
|
|
256
|
-
if name is None:
|
|
257
|
-
return MsSqlField("*", alias="general")
|
|
258
|
-
|
|
259
|
-
for f in self.fields:
|
|
260
|
-
if f.alias == name:
|
|
261
|
-
return f
|
|
262
|
-
|
|
263
|
-
raise ForbiddenError(message=f"Invalid field name: {name}")
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
@dataclass
|
|
267
|
-
class MsSqlOrderGenerator:
|
|
268
|
-
ordering: list[Sort]
|
|
269
|
-
|
|
270
|
-
fields: list[MsSqlField] = field(default_factory=list)
|
|
271
|
-
|
|
272
|
-
def generate(self) -> str:
|
|
273
|
-
if not self.ordering:
|
|
274
|
-
return ""
|
|
275
|
-
|
|
276
|
-
clause = ", ".join(self.generate_one(item) for item in self.ordering)
|
|
277
|
-
|
|
278
|
-
return f"ORDER BY {clause}"
|
|
279
|
-
|
|
280
|
-
def generate_one(self, item: Sort) -> str:
|
|
281
|
-
return self.field_for(item.name).as_order_part(is_descending=item.is_descending)
|
|
282
|
-
|
|
283
|
-
def field_for(self, name: str) -> MsSqlField:
|
|
284
|
-
for f in self.fields:
|
|
285
|
-
if f.alias == name:
|
|
286
|
-
return f
|
|
287
|
-
|
|
288
|
-
if f.alias == "" and f.name == name:
|
|
289
|
-
return f
|
|
290
|
-
|
|
291
|
-
raise ForbiddenError(message=f"Invalid field name: {name}")
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
@dataclass
|
|
295
|
-
class MsSqlSourceGenerator:
|
|
296
|
-
source: str
|
|
297
|
-
filter: Filter | None = None
|
|
298
|
-
|
|
299
|
-
def generate(self) -> str:
|
|
300
|
-
return f"FROM {self.source}{self._argument_list()}"
|
|
301
|
-
|
|
302
|
-
def _argument_list(self) -> str:
|
|
303
|
-
if self.filter is None:
|
|
304
|
-
return ""
|
|
305
|
-
|
|
306
|
-
return (
|
|
307
|
-
"("
|
|
308
|
-
+ ",".join(
|
|
309
|
-
arg.as_arg() if arg is not None else "null" for arg in self.filter.args
|
|
310
|
-
)
|
|
311
|
-
+ ")"
|
|
312
|
-
)
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
@dataclass
|
|
316
|
-
class MsSqlConditionGenerator:
|
|
317
|
-
condition: Operator | None
|
|
318
|
-
fields: list[MsSqlField]
|
|
319
|
-
|
|
320
|
-
def generate(self) -> str:
|
|
321
|
-
if self.condition is None:
|
|
322
|
-
return ""
|
|
323
|
-
|
|
324
|
-
return f"WHERE ({self._traverse(self.condition)})"
|
|
325
|
-
|
|
326
|
-
def _traverse(self, node: Operator) -> str:
|
|
327
|
-
match node.operation:
|
|
328
|
-
case Operation.NOT:
|
|
329
|
-
assert len(node.operands) == 1
|
|
330
|
-
assert isinstance(node.operands[0], Operator)
|
|
331
|
-
|
|
332
|
-
return f"NOT ({self._traverse(node.operands[0])})"
|
|
333
|
-
case Operation.AND | Operation.OR:
|
|
334
|
-
assert len(node.operands) > 1
|
|
335
|
-
|
|
336
|
-
return f" {node.operation.value} ".join(
|
|
337
|
-
[f"({self._traverse(operand)})" for operand in node.operands] # type: ignore
|
|
338
|
-
)
|
|
339
|
-
case _:
|
|
340
|
-
assert len(node.operands) == 1
|
|
341
|
-
|
|
342
|
-
return OperationEvaluator(
|
|
343
|
-
node.operation,
|
|
344
|
-
fields=self.fields,
|
|
345
|
-
).evaluate_for(
|
|
346
|
-
node.operands[0], # type: ignore
|
|
347
|
-
)
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
@dataclass
|
|
351
|
-
class MsSqlPagingGenerator:
|
|
352
|
-
paging: Page
|
|
353
|
-
|
|
354
|
-
def generate(self) -> str:
|
|
355
|
-
length = self.paging.length or 200
|
|
356
|
-
offset = self.paging.offset or 0
|
|
357
|
-
page = self.paging.page or 1
|
|
358
|
-
|
|
359
|
-
offset = (page - 1) * length + offset
|
|
360
|
-
|
|
361
|
-
return f"OFFSET {offset} ROWS FETCH NEXT {length} ROWS ONLY"
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
@dataclass
|
|
365
|
-
class OperationEvaluator:
|
|
366
|
-
operation: Operation
|
|
367
|
-
fields: list[MsSqlField]
|
|
368
|
-
|
|
369
|
-
_TEMPLATES: ClassVar[dict[Operation, str]] = defaultdict(
|
|
370
|
-
lambda: "[{column}] {operation} {a}",
|
|
371
|
-
{
|
|
372
|
-
Operation.BETWEEN: "[{column}] BETWEEN {a} AND {b}",
|
|
373
|
-
Operation.RANGE: "[{column}] >= {a} AND [{column}] < {b}",
|
|
374
|
-
Operation.NULL: "([{column}]) IS NULL",
|
|
375
|
-
Operation.BLANK: "(([{column}]) IS NULL) OR (LEN([{column}]) = 0)",
|
|
376
|
-
Operation.IN: "[{column}] IN ({values})",
|
|
377
|
-
Operation.CONTAINS: "[{column}] LIKE N'%{raw_a}%'",
|
|
378
|
-
Operation.LIKE: "[{column}] LIKE N'{raw_a}'",
|
|
379
|
-
Operation.BEGINS: "[{column}] LIKE N'{raw_a}%'",
|
|
380
|
-
Operation.ENDS: "[{column}] LIKE N'%{raw_a}'",
|
|
381
|
-
},
|
|
382
|
-
)
|
|
383
|
-
|
|
384
|
-
def evaluate_for(self, node: Leaf) -> str:
|
|
385
|
-
return self._TEMPLATES[self.operation].format(
|
|
386
|
-
operation=self.operation.value,
|
|
387
|
-
column=self._column_for(node),
|
|
388
|
-
raw_a=self._get_raw_value(node),
|
|
389
|
-
a=self._get_value(node, 0),
|
|
390
|
-
b=self._get_value(node, 1),
|
|
391
|
-
values=", ".join(val.eval() for val in node.values),
|
|
392
|
-
)
|
|
393
|
-
|
|
394
|
-
def _column_for(self, node: Leaf) -> str:
|
|
395
|
-
for f in self.fields:
|
|
396
|
-
if f.alias == node.name:
|
|
397
|
-
return f.name
|
|
398
|
-
|
|
399
|
-
if f.alias == "" and f.name == node.name:
|
|
400
|
-
return f.name
|
|
401
|
-
|
|
402
|
-
raise ForbiddenError(message=f"Invalid field name: {node.name}")
|
|
403
|
-
|
|
404
|
-
def _get_raw_value(self, node: Leaf) -> str | None:
|
|
405
|
-
raw_a = self._get_value(node, 0)
|
|
406
|
-
return (
|
|
407
|
-
raw_a[2:-1] if raw_a and isinstance(node.values[0], StringValue) else raw_a
|
|
408
|
-
)
|
|
409
|
-
|
|
410
|
-
def _get_value(self, node: Leaf, index: int) -> str | None:
|
|
411
|
-
return node.values[index].eval() if len(node.values) > index else None
|
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
from dataclasses import dataclass
|
|
4
|
-
from decimal import Decimal
|
|
5
|
-
from enum import Enum
|
|
6
|
-
from typing import Any
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
@dataclass
|
|
10
|
-
class FooterOptions:
|
|
11
|
-
filter: Filter | None
|
|
12
|
-
condition: Operator | None
|
|
13
|
-
aggregations: list[AggregationOption]
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
@dataclass
|
|
17
|
-
class QueryOptions:
|
|
18
|
-
filter: Filter | None
|
|
19
|
-
condition: Operator | None
|
|
20
|
-
ordering: list[Sort]
|
|
21
|
-
paging: Page
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
@dataclass
|
|
25
|
-
class AggregationOption:
|
|
26
|
-
name: str | None
|
|
27
|
-
aggregation: Aggregation
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
@dataclass
|
|
31
|
-
class Sort:
|
|
32
|
-
name: str
|
|
33
|
-
is_descending: bool
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
@dataclass
|
|
37
|
-
class Page:
|
|
38
|
-
page: int | None
|
|
39
|
-
length: int | None
|
|
40
|
-
offset: int | None
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
@dataclass
|
|
44
|
-
class Summary:
|
|
45
|
-
name: str | None
|
|
46
|
-
aggregation: Aggregation
|
|
47
|
-
result: NumericValue | StringValue | DateValue | NullValue
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
@dataclass
|
|
51
|
-
class SummaryExtractor:
|
|
52
|
-
aggregation: AggregationOption
|
|
53
|
-
|
|
54
|
-
def with_value(self, value: Any) -> Summary:
|
|
55
|
-
return Summary(
|
|
56
|
-
self.aggregation.name, self.aggregation.aggregation, self._value(value)
|
|
57
|
-
)
|
|
58
|
-
|
|
59
|
-
def _value(self, value: Any) -> NumericValue | DateValue | StringValue | NullValue:
|
|
60
|
-
if value is None:
|
|
61
|
-
return NullValue()
|
|
62
|
-
if isinstance(value, int | float | Decimal):
|
|
63
|
-
return NumericValue.from_decimal(Decimal(value))
|
|
64
|
-
if self.aggregation.name and (
|
|
65
|
-
"date" in self.aggregation.name.lower()
|
|
66
|
-
or "time" in self.aggregation.name.lower()
|
|
67
|
-
):
|
|
68
|
-
return DateValue(str(value))
|
|
69
|
-
return StringValue(str(value))
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
@dataclass
|
|
73
|
-
class NumericValue:
|
|
74
|
-
value: int
|
|
75
|
-
exponent: int = 1
|
|
76
|
-
|
|
77
|
-
def eval(self) -> str:
|
|
78
|
-
return str(self.value / self.exponent)
|
|
79
|
-
|
|
80
|
-
def as_arg(self) -> str:
|
|
81
|
-
return self.eval()
|
|
82
|
-
|
|
83
|
-
@classmethod
|
|
84
|
-
def from_decimal(cls, value: Decimal) -> NumericValue:
|
|
85
|
-
exponent = 10 ** (len(str(value).split(".")[1]) - 1) if "." in str(value) else 1
|
|
86
|
-
return cls(value=int(Decimal(value) * exponent), exponent=exponent)
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
@dataclass
|
|
90
|
-
class StringValue:
|
|
91
|
-
value: str
|
|
92
|
-
|
|
93
|
-
def eval(self) -> str:
|
|
94
|
-
return f"N'{self.value}'"
|
|
95
|
-
|
|
96
|
-
def as_arg(self) -> str:
|
|
97
|
-
return f"'{self.value}'"
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
@dataclass
|
|
101
|
-
class DateValue:
|
|
102
|
-
date: str
|
|
103
|
-
|
|
104
|
-
def eval(self) -> str:
|
|
105
|
-
date = self.date.replace("T", " ")
|
|
106
|
-
return f"convert(datetime, '{date}.000', 121)"
|
|
107
|
-
|
|
108
|
-
def as_arg(self) -> str:
|
|
109
|
-
return f"'{self.date[:10]}'"
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
@dataclass
|
|
113
|
-
class ListValue:
|
|
114
|
-
values: list[NumericValue | StringValue | DateValue]
|
|
115
|
-
|
|
116
|
-
def as_arg(self) -> str:
|
|
117
|
-
args = ",".join(arg.as_arg()[1:-1] for arg in self.values)
|
|
118
|
-
return f"'{args}'"
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
@dataclass(frozen=True)
|
|
122
|
-
class NullValue:
|
|
123
|
-
pass
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
@dataclass
|
|
127
|
-
class Leaf:
|
|
128
|
-
name: str
|
|
129
|
-
values: list[NumericValue | StringValue | DateValue]
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
@dataclass
|
|
133
|
-
class Operator:
|
|
134
|
-
operation: Operation
|
|
135
|
-
operands: list[Operator | Leaf]
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
@dataclass
|
|
139
|
-
class Filter:
|
|
140
|
-
args: list[NumericValue | StringValue | DateValue | ListValue | None]
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
class Operation(Enum):
|
|
144
|
-
BETWEEN = "BETWEEN"
|
|
145
|
-
RANGE = "RANGE"
|
|
146
|
-
NULL = "NULL"
|
|
147
|
-
BLANK = "BLANK"
|
|
148
|
-
IN = "IN"
|
|
149
|
-
CONTAINS = "CONTAINS"
|
|
150
|
-
LIKE = "LIKE"
|
|
151
|
-
BEGINS = "BEGINS"
|
|
152
|
-
ENDS = "ENDS"
|
|
153
|
-
EQUALS = "="
|
|
154
|
-
NOT_EQUALS = "<>"
|
|
155
|
-
GREATER = ">"
|
|
156
|
-
GREATER_OR_EQUALS = ">="
|
|
157
|
-
LESS = "<"
|
|
158
|
-
LESS_OR_EQUALS = "<="
|
|
159
|
-
AND = "AND"
|
|
160
|
-
OR = "OR"
|
|
161
|
-
NOT = "NOT"
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
class Aggregation(Enum):
|
|
165
|
-
COUNT = "COUNT"
|
|
166
|
-
MAX = "MAX"
|
|
167
|
-
MIN = "MIN"
|
|
168
|
-
SUM = "SUM"
|
|
169
|
-
AVG = "AVG"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|