apexdevkit 2.4.4__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.4 → apexdevkit-2.5.1}/PKG-INFO +1 -1
- apexdevkit-2.5.1/apexdevkit/annotation/__init__.py +6 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/annotation/deprecate.py +11 -5
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/repository/sql/__init__.py +2 -1
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/repository/sql/mssql/__init__.py +2 -1
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/repository/sql/mssql/table.py +31 -38
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/pyproject.toml +1 -1
- apexdevkit-2.4.4/apexdevkit/annotation/__init__.py +0 -3
- apexdevkit-2.4.4/apexdevkit/query/__init__.py +0 -37
- apexdevkit-2.4.4/apexdevkit/query/generator.py +0 -411
- apexdevkit-2.4.4/apexdevkit/query/query.py +0 -169
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/LICENSE +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/README.md +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/__init__.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/date.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/error.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/fastapi/__init__.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/fastapi/builder.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/fastapi/dependable.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/fastapi/endpoints.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/fastapi/name.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/fastapi/response.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/fastapi/router.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/fastapi/schema.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/fastapi/service.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/formatter.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/key_fn.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/observer.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/py.typed +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/repository/__init__.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/repository/core/__init__.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/repository/core/database.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/repository/core/decorator.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/repository/core/interface.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/repository/core/mixin.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/repository/in_memory/__init__.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/repository/in_memory/mixin.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/repository/in_memory/repository.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/repository/in_memory/store.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/repository/sql/connector.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/repository/sql/field.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/repository/sql/mssql/repository.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/repository/sql/sqlite/__init__.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/repository/sql/sqlite/repository.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/repository/sql/sqlite/table.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/server.py +0 -0
- {apexdevkit-2.4.4 → apexdevkit-2.5.1}/apexdevkit/testing.py +0 -0
|
@@ -24,18 +24,19 @@ def deprecated(warning: str) -> Callable[[F], F]:
|
|
|
24
24
|
def decorator(obj: F) -> F:
|
|
25
25
|
if inspect.isfunction(obj) or inspect.ismethod(obj):
|
|
26
26
|
return _wrap_function(obj, warning)
|
|
27
|
+
|
|
27
28
|
if inspect.isclass(obj):
|
|
28
29
|
return cast("F", _deprecate_class(obj, warning))
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
) # pragma: no cover
|
|
30
|
+
|
|
31
|
+
raise TypeError(f"Unsupported type for deprecation: {type(obj)}")
|
|
32
32
|
|
|
33
33
|
return decorator
|
|
34
34
|
|
|
35
35
|
|
|
36
36
|
def _wrap_function[F: Callable[..., Any]](func: F, warning: str) -> F:
|
|
37
37
|
def wrapper(*args: Any, **kwargs: dict[str, Any]) -> Any:
|
|
38
|
-
|
|
38
|
+
notify(warning)
|
|
39
|
+
|
|
39
40
|
return func(*args, **kwargs)
|
|
40
41
|
|
|
41
42
|
return cast("F", wrapper)
|
|
@@ -44,7 +45,12 @@ def _wrap_function[F: Callable[..., Any]](func: F, warning: str) -> F:
|
|
|
44
45
|
def _deprecate_class(cls: type, warning: str) -> type:
|
|
45
46
|
class DeprecatedClass(cls): # type: ignore
|
|
46
47
|
def __init__(self, *args: Any, **kwargs: dict[str, Any]) -> None:
|
|
47
|
-
|
|
48
|
+
notify(warning)
|
|
49
|
+
|
|
48
50
|
super().__init__(*args, **kwargs)
|
|
49
51
|
|
|
50
52
|
return cast("type", DeprecatedClass)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def notify(message: str) -> None:
|
|
56
|
+
warn(message, category=DeprecationWarning, stacklevel=2)
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
from .field import NotNone, SqlField, SqlFieldManager
|
|
2
|
-
from .mssql import MsSqlRepository
|
|
2
|
+
from .mssql import MsSqlRepository, MsSqlTableBuilder
|
|
3
3
|
from .sqlite import SqliteRepository
|
|
4
4
|
|
|
5
5
|
__all__ = [
|
|
6
6
|
"MsSqlRepository",
|
|
7
|
+
"MsSqlTableBuilder",
|
|
7
8
|
"NotNone",
|
|
8
9
|
"SqlField",
|
|
9
10
|
"SqlFieldManager",
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
from .repository import MsSqlRepository, UnknownError
|
|
2
|
-
from .table import MsSqlTableBuilder, SqlTable
|
|
2
|
+
from .table import MsSqlTableBuilder, SqlTable, TableWithUser
|
|
3
3
|
|
|
4
4
|
__all__ = [
|
|
5
5
|
"MsSqlRepository",
|
|
6
6
|
"MsSqlTableBuilder",
|
|
7
7
|
"SqlTable",
|
|
8
|
+
"TableWithUser",
|
|
8
9
|
"UnknownError",
|
|
9
10
|
]
|
|
@@ -57,6 +57,10 @@ class MsSqlTableBuilder[T]:
|
|
|
57
57
|
fields: SqlFieldSet = field(default_factory=SqlFieldSet)
|
|
58
58
|
custom_filters: list[str] = field(default_factory=list)
|
|
59
59
|
|
|
60
|
+
@property
|
|
61
|
+
def target(self) -> str:
|
|
62
|
+
return f"[{self.schema}].[{self.table}]"
|
|
63
|
+
|
|
60
64
|
def with_schema(self, value: str) -> Self:
|
|
61
65
|
return replace(self, schema=value)
|
|
62
66
|
|
|
@@ -81,19 +85,16 @@ class MsSqlTableBuilder[T]:
|
|
|
81
85
|
if not self.schema or not self.table:
|
|
82
86
|
raise ValueError("Cannot build sql table.")
|
|
83
87
|
|
|
84
|
-
fields = (
|
|
85
|
-
SqlFieldManager.Builder.for_mssql()
|
|
86
|
-
.with_fields(self.fields)
|
|
87
|
-
.with_custom_filters(list(self.custom_filters))
|
|
88
|
-
.build()
|
|
89
|
-
)
|
|
90
|
-
|
|
91
88
|
return AutoMsSqlTable[T](
|
|
92
89
|
fields=self.fields,
|
|
93
90
|
templates=SqlTemplates(
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
91
|
+
target=self.target,
|
|
92
|
+
fields=(
|
|
93
|
+
SqlFieldManager.Builder.for_mssql()
|
|
94
|
+
.with_fields(self.fields)
|
|
95
|
+
.with_custom_filters(list(self.custom_filters))
|
|
96
|
+
.build()
|
|
97
|
+
),
|
|
97
98
|
),
|
|
98
99
|
)
|
|
99
100
|
|
|
@@ -114,12 +115,6 @@ class AutoMsSqlTable[T](SqlTable[T]):
|
|
|
114
115
|
def with_a(self, formatter: Formatter[Mapping[str, Any], T]) -> AutoMsSqlTable[T]:
|
|
115
116
|
return replace(self, formatter=formatter)
|
|
116
117
|
|
|
117
|
-
def for_a(self, username: str) -> AutoMsSqlTable[T] | AsUser[T]:
|
|
118
|
-
if not username:
|
|
119
|
-
return self
|
|
120
|
-
|
|
121
|
-
return AsUser(user=username, inner=self)
|
|
122
|
-
|
|
123
118
|
def count_all(self) -> DatabaseCommand:
|
|
124
119
|
return DatabaseCommand(
|
|
125
120
|
value=self.templates.count_all(),
|
|
@@ -174,14 +169,17 @@ class AutoMsSqlTable[T](SqlTable[T]):
|
|
|
174
169
|
|
|
175
170
|
@dataclass(frozen=True, kw_only=True)
|
|
176
171
|
class SqlTemplates:
|
|
177
|
-
|
|
178
|
-
table: str
|
|
172
|
+
target: str
|
|
179
173
|
fields: SqlFieldManager
|
|
180
174
|
|
|
175
|
+
@property
|
|
176
|
+
def columns(self) -> str:
|
|
177
|
+
return ", ".join(["[" + sql_field.name + "]" for sql_field in self.fields])
|
|
178
|
+
|
|
181
179
|
def count_all(self) -> str:
|
|
182
180
|
return f"""
|
|
183
181
|
SELECT count(*) AS n_items
|
|
184
|
-
FROM
|
|
182
|
+
FROM {self.target}
|
|
185
183
|
{self.fields.where_statement(include_id=False)}
|
|
186
184
|
REVERT
|
|
187
185
|
"""
|
|
@@ -199,7 +197,7 @@ class SqlTemplates:
|
|
|
199
197
|
["[" + field.name + "] AS " + field.name for field in self.fields]
|
|
200
198
|
)
|
|
201
199
|
where_statement = f"""
|
|
202
|
-
FROM
|
|
200
|
+
FROM {self.target}
|
|
203
201
|
{self.fields.where_statement(include_id=True, read_id=True)}
|
|
204
202
|
"""
|
|
205
203
|
except ValueError:
|
|
@@ -209,7 +207,7 @@ class SqlTemplates:
|
|
|
209
207
|
where_statement = ""
|
|
210
208
|
|
|
211
209
|
return f"""
|
|
212
|
-
INSERT INTO
|
|
210
|
+
INSERT INTO {self.target} (
|
|
213
211
|
{columns}
|
|
214
212
|
)
|
|
215
213
|
VALUES (
|
|
@@ -222,23 +220,19 @@ class SqlTemplates:
|
|
|
222
220
|
"""
|
|
223
221
|
|
|
224
222
|
def select(self) -> str:
|
|
225
|
-
columns = ", ".join(["[" + field.name + "]" for field in self.fields])
|
|
226
|
-
|
|
227
223
|
return f"""
|
|
228
224
|
SELECT
|
|
229
|
-
{columns}
|
|
230
|
-
FROM
|
|
225
|
+
{self.columns}
|
|
226
|
+
FROM {self.target}
|
|
231
227
|
{self.fields.where_statement(include_id=True)}
|
|
232
228
|
REVERT
|
|
233
229
|
"""
|
|
234
230
|
|
|
235
231
|
def select_all(self) -> str:
|
|
236
|
-
columns = ", ".join(["[" + field.name + "]" for field in self.fields])
|
|
237
|
-
|
|
238
232
|
return f"""
|
|
239
233
|
SELECT
|
|
240
|
-
{columns}
|
|
241
|
-
FROM
|
|
234
|
+
{self.columns}
|
|
235
|
+
FROM {self.target}
|
|
242
236
|
{self.fields.where_statement(include_id=False)}
|
|
243
237
|
{self.fields.order}
|
|
244
238
|
REVERT
|
|
@@ -254,7 +248,7 @@ class SqlTemplates:
|
|
|
254
248
|
)
|
|
255
249
|
|
|
256
250
|
return f"""
|
|
257
|
-
UPDATE
|
|
251
|
+
UPDATE {self.target}
|
|
258
252
|
SET
|
|
259
253
|
{updates}
|
|
260
254
|
{self.fields.where_statement(include_id=True)}
|
|
@@ -264,7 +258,7 @@ class SqlTemplates:
|
|
|
264
258
|
def delete(self) -> str:
|
|
265
259
|
return f"""
|
|
266
260
|
DELETE
|
|
267
|
-
FROM
|
|
261
|
+
FROM {self.target}
|
|
268
262
|
{self.fields.where_statement(include_id=True)}
|
|
269
263
|
REVERT
|
|
270
264
|
"""
|
|
@@ -272,27 +266,26 @@ class SqlTemplates:
|
|
|
272
266
|
def delete_all(self) -> str:
|
|
273
267
|
return f"""
|
|
274
268
|
DELETE
|
|
275
|
-
FROM
|
|
269
|
+
FROM {self.target}
|
|
276
270
|
{self.fields.where_statement(include_id=False)}
|
|
277
271
|
REVERT
|
|
278
272
|
"""
|
|
279
273
|
|
|
280
274
|
|
|
281
275
|
@dataclass(frozen=True, kw_only=True)
|
|
282
|
-
class
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
inner: AutoMsSqlTable[T]
|
|
276
|
+
class TableWithUser[T](SqlTable[T]):
|
|
277
|
+
name: str
|
|
278
|
+
inner: SqlTable[T]
|
|
286
279
|
|
|
287
280
|
def __post_init__(self) -> None:
|
|
288
|
-
if not self.
|
|
281
|
+
if not self.name:
|
|
289
282
|
raise ValueError("username must be provided")
|
|
290
283
|
|
|
291
284
|
def _wrap(self, command: DatabaseCommand) -> DatabaseCommand:
|
|
292
285
|
return DatabaseCommand(
|
|
293
286
|
"\n".join(
|
|
294
287
|
[
|
|
295
|
-
f"EXECUTE AS USER = '{self.
|
|
288
|
+
f"EXECUTE AS USER = '{self.name}'",
|
|
296
289
|
dedent(command.value),
|
|
297
290
|
]
|
|
298
291
|
),
|
|
@@ -1,37 +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
|
-
]
|
|
@@ -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
|
|
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) -> DefaultMsSqlFilter[T]:
|
|
43
|
-
self._database = value
|
|
44
|
-
|
|
45
|
-
return self
|
|
46
|
-
|
|
47
|
-
def with_query_builder(self, value: MsSqlQueryBuilder) -> DefaultMsSqlFilter[T]:
|
|
48
|
-
self._query_builder = value
|
|
49
|
-
|
|
50
|
-
return self
|
|
51
|
-
|
|
52
|
-
def with_options(self, value: QueryOptions) -> DefaultMsSqlFilter[T]:
|
|
53
|
-
self._options = value
|
|
54
|
-
|
|
55
|
-
return self
|
|
56
|
-
|
|
57
|
-
def with_formatter(self, value: _Loader[T]) -> DefaultMsSqlFilter[T]:
|
|
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) -> DefaultMsSqlAggregate:
|
|
80
|
-
self._database = value
|
|
81
|
-
|
|
82
|
-
return self
|
|
83
|
-
|
|
84
|
-
def with_query_builder(self, value: MsSqlQueryBuilder) -> DefaultMsSqlAggregate:
|
|
85
|
-
self._query_builder = value
|
|
86
|
-
|
|
87
|
-
return self
|
|
88
|
-
|
|
89
|
-
def with_options(self, value: FooterOptions) -> DefaultMsSqlAggregate:
|
|
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
|