apexdevkit 2.4.5__py3-none-any.whl → 2.5.2__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.
- apexdevkit/repository/core/database.py +5 -0
- apexdevkit/repository/sql/__init__.py +2 -1
- apexdevkit/repository/sql/field.py +35 -221
- apexdevkit/repository/sql/filters.py +104 -0
- apexdevkit/repository/sql/mssql/table.py +190 -127
- apexdevkit/repository/sql/operations.py +78 -0
- apexdevkit/repository/sql/placeholders.py +134 -0
- apexdevkit/repository/sql/sqlite/table.py +11 -15
- apexdevkit/repository/sql/statements.py +194 -0
- {apexdevkit-2.4.5.dist-info → apexdevkit-2.5.2.dist-info}/METADATA +1 -1
- {apexdevkit-2.4.5.dist-info → apexdevkit-2.5.2.dist-info}/RECORD +13 -12
- apexdevkit/query/__init__.py +0 -41
- apexdevkit/query/generator.py +0 -411
- apexdevkit/query/query.py +0 -169
- {apexdevkit-2.4.5.dist-info → apexdevkit-2.5.2.dist-info}/WHEEL +0 -0
- {apexdevkit-2.4.5.dist-info → apexdevkit-2.5.2.dist-info}/licenses/LICENSE +0 -0
|
@@ -4,6 +4,7 @@ from collections.abc import Iterable, Mapping
|
|
|
4
4
|
from contextlib import AbstractContextManager
|
|
5
5
|
from copy import deepcopy
|
|
6
6
|
from dataclasses import dataclass, field
|
|
7
|
+
from textwrap import dedent
|
|
7
8
|
from typing import Any, Protocol
|
|
8
9
|
|
|
9
10
|
_RawData = Mapping[str, Any]
|
|
@@ -81,6 +82,10 @@ class DatabaseCommand:
|
|
|
81
82
|
value: str = field(default_factory=str)
|
|
82
83
|
payload: _RawData | list[_RawData] = field(default_factory=dict)
|
|
83
84
|
|
|
85
|
+
@property
|
|
86
|
+
def sql(self) -> str:
|
|
87
|
+
return dedent(self.value.expandtabs(4))
|
|
88
|
+
|
|
84
89
|
def with_data(
|
|
85
90
|
self, value: _RawData | None = None, **fields: Any
|
|
86
91
|
) -> DatabaseCommand:
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
from .field import NotNone, SqlField
|
|
1
|
+
from .field import NotNone, SqlField
|
|
2
2
|
from .mssql import MsSqlRepository, MsSqlTableBuilder
|
|
3
3
|
from .sqlite import SqliteRepository
|
|
4
|
+
from .statements import SqlFieldManager
|
|
4
5
|
|
|
5
6
|
__all__ = [
|
|
6
7
|
"MsSqlRepository",
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
-
from abc import ABC, abstractmethod
|
|
4
3
|
from dataclasses import dataclass, field, replace
|
|
4
|
+
from enum import StrEnum
|
|
5
5
|
from typing import TYPE_CHECKING, Any, Self
|
|
6
6
|
|
|
7
|
-
from pypebbles import FluentDict
|
|
8
|
-
|
|
9
7
|
if TYPE_CHECKING:
|
|
10
8
|
from collections.abc import Iterable, Iterator, Mapping
|
|
11
9
|
|
|
@@ -15,6 +13,15 @@ class NotNone:
|
|
|
15
13
|
pass
|
|
16
14
|
|
|
17
15
|
|
|
16
|
+
class OrderDirection(StrEnum):
|
|
17
|
+
NONE = "NONE"
|
|
18
|
+
ASCENDING = "ASC"
|
|
19
|
+
DESCENDING = "DESC"
|
|
20
|
+
|
|
21
|
+
def is_descending(self) -> bool:
|
|
22
|
+
return self == OrderDirection.DESCENDING
|
|
23
|
+
|
|
24
|
+
|
|
18
25
|
@dataclass(frozen=True)
|
|
19
26
|
class SqlField:
|
|
20
27
|
name: str
|
|
@@ -25,8 +32,7 @@ class SqlField:
|
|
|
25
32
|
include_in_insert: bool = True
|
|
26
33
|
include_in_update: bool = True
|
|
27
34
|
|
|
28
|
-
|
|
29
|
-
is_descending: bool = False
|
|
35
|
+
order: OrderDirection = OrderDirection.NONE
|
|
30
36
|
|
|
31
37
|
is_parent: bool = False
|
|
32
38
|
parent_value: Any | None = None
|
|
@@ -37,6 +43,10 @@ class SqlField:
|
|
|
37
43
|
is_fixed: bool = False
|
|
38
44
|
fixed_value: Any | None = None
|
|
39
45
|
|
|
46
|
+
@property
|
|
47
|
+
def filters(self) -> Iterable[tuple[int, Any | NotNone | None]]:
|
|
48
|
+
yield from enumerate(self.filter_values)
|
|
49
|
+
|
|
40
50
|
@classmethod
|
|
41
51
|
def named(cls, name: str) -> Self:
|
|
42
52
|
return cls(name=name)
|
|
@@ -51,7 +61,10 @@ class SqlField:
|
|
|
51
61
|
return replace(self, is_composite=True)
|
|
52
62
|
|
|
53
63
|
def order_by(self, *, descending: bool = False) -> Self:
|
|
54
|
-
return replace(
|
|
64
|
+
return replace(
|
|
65
|
+
self,
|
|
66
|
+
order=OrderDirection.DESCENDING if descending else OrderDirection.ASCENDING,
|
|
67
|
+
)
|
|
55
68
|
|
|
56
69
|
def derive_on_insert(self) -> Self:
|
|
57
70
|
return replace(self, include_in_insert=False)
|
|
@@ -84,6 +97,14 @@ class SqlFieldSet:
|
|
|
84
97
|
|
|
85
98
|
raise RuntimeError("FieldSet does not contain Id field.")
|
|
86
99
|
|
|
100
|
+
@property
|
|
101
|
+
def parent(self) -> SqlField | None:
|
|
102
|
+
for item in self.items:
|
|
103
|
+
if item.is_parent:
|
|
104
|
+
return item
|
|
105
|
+
|
|
106
|
+
return None
|
|
107
|
+
|
|
87
108
|
def contains_id(self) -> bool:
|
|
88
109
|
return any(i.is_id for i in self.items)
|
|
89
110
|
|
|
@@ -105,11 +126,18 @@ class SqlFieldSet:
|
|
|
105
126
|
return item
|
|
106
127
|
|
|
107
128
|
def for_ordering(self) -> Iterable[SqlField]:
|
|
108
|
-
return (item for item in self.items if item.
|
|
129
|
+
return (item for item in self.items if item.order != OrderDirection.NONE)
|
|
109
130
|
|
|
110
131
|
def for_filtering(self) -> Iterable[SqlField]:
|
|
111
132
|
return (item for item in self.items if item.is_filter and item.filter_values)
|
|
112
133
|
|
|
134
|
+
def for_update(self) -> Iterable[SqlField]:
|
|
135
|
+
return (
|
|
136
|
+
item
|
|
137
|
+
for item in self.items
|
|
138
|
+
if not item.is_id and not item.is_parent and item.include_in_update
|
|
139
|
+
)
|
|
140
|
+
|
|
113
141
|
def as_payload(self) -> Mapping[str, Any]:
|
|
114
142
|
payload = {}
|
|
115
143
|
|
|
@@ -126,217 +154,3 @@ class SqlFieldSet:
|
|
|
126
154
|
payload[item.name + "_filter_" + str(index)] = value
|
|
127
155
|
|
|
128
156
|
return payload
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
@dataclass
|
|
132
|
-
class SqlFieldManager:
|
|
133
|
-
fields: SqlFieldSet
|
|
134
|
-
custom_filters: list[str]
|
|
135
|
-
|
|
136
|
-
placeholder_value: PlaceHolder
|
|
137
|
-
placeholder_key: PlaceHolder
|
|
138
|
-
|
|
139
|
-
@property
|
|
140
|
-
def id_name(self) -> str:
|
|
141
|
-
return self.fields.id_field.name
|
|
142
|
-
|
|
143
|
-
@property
|
|
144
|
-
def composite(self) -> list[str]:
|
|
145
|
-
names = [key.name for key in self.fields if key.is_composite]
|
|
146
|
-
|
|
147
|
-
return [self.id_name] if len(names) == 0 else names
|
|
148
|
-
|
|
149
|
-
@property
|
|
150
|
-
def order(self) -> str:
|
|
151
|
-
fields = [
|
|
152
|
-
f"{item.name} DESC" if item.is_descending else item.name
|
|
153
|
-
for item in self.fields.for_ordering()
|
|
154
|
-
]
|
|
155
|
-
|
|
156
|
-
if not fields:
|
|
157
|
-
return ""
|
|
158
|
-
|
|
159
|
-
return "ORDER BY " + ", ".join(fields)
|
|
160
|
-
|
|
161
|
-
def __iter__(self) -> Iterator[SqlField]:
|
|
162
|
-
yield from self.fields
|
|
163
|
-
|
|
164
|
-
def where_statement(
|
|
165
|
-
self,
|
|
166
|
-
*,
|
|
167
|
-
include_id: bool = False,
|
|
168
|
-
read_id: bool = False,
|
|
169
|
-
) -> str:
|
|
170
|
-
statements = [
|
|
171
|
-
statement
|
|
172
|
-
for statement in [
|
|
173
|
-
self._parent_filter(),
|
|
174
|
-
self._id_filter(include_id=include_id, read_id=read_id),
|
|
175
|
-
self._general_filters(),
|
|
176
|
-
]
|
|
177
|
-
if statement != ""
|
|
178
|
-
]
|
|
179
|
-
if len(statements) > 0:
|
|
180
|
-
return "WHERE " + " AND ".join(statements)
|
|
181
|
-
|
|
182
|
-
return ""
|
|
183
|
-
|
|
184
|
-
def with_fixed(self, raw: Mapping[str, Any]) -> dict[str, Any]:
|
|
185
|
-
return FluentDict(raw).merge(self.fields.as_payload())
|
|
186
|
-
|
|
187
|
-
def _parent_filter(self) -> str:
|
|
188
|
-
result = next((key for key in self.fields if key.is_parent), None)
|
|
189
|
-
|
|
190
|
-
if result is None:
|
|
191
|
-
return ""
|
|
192
|
-
|
|
193
|
-
if result.parent_value is None:
|
|
194
|
-
return self.placeholder_key.is_null(result.name)
|
|
195
|
-
|
|
196
|
-
return (
|
|
197
|
-
self.placeholder_key.replace_with(result.name)
|
|
198
|
-
+ " = "
|
|
199
|
-
+ self.placeholder_value.replace_with(result.name)
|
|
200
|
-
)
|
|
201
|
-
|
|
202
|
-
def _id_filter(self, *, include_id: bool, read_id: bool) -> str:
|
|
203
|
-
if not include_id:
|
|
204
|
-
return ""
|
|
205
|
-
|
|
206
|
-
id_field = self.fields.id_field
|
|
207
|
-
|
|
208
|
-
value_expr = (
|
|
209
|
-
self.placeholder_value.replace_with(id_field.name)
|
|
210
|
-
if not read_id or id_field.include_in_insert
|
|
211
|
-
else "SCOPE_IDENTITY()"
|
|
212
|
-
)
|
|
213
|
-
|
|
214
|
-
clauses = [
|
|
215
|
-
f"{self.placeholder_key.replace_with(id_field.name)} = {value_expr}",
|
|
216
|
-
]
|
|
217
|
-
|
|
218
|
-
if not read_id:
|
|
219
|
-
selectable_fields = [
|
|
220
|
-
selectable
|
|
221
|
-
for selectable in self.fields
|
|
222
|
-
if selectable.is_selectable and not selectable.is_id
|
|
223
|
-
]
|
|
224
|
-
for selectable in selectable_fields:
|
|
225
|
-
key_expr = self.placeholder_key.replace_with(selectable.name)
|
|
226
|
-
val_expr = self.placeholder_value.replace_with(id_field.name)
|
|
227
|
-
clauses.append(f"{key_expr} = {val_expr}")
|
|
228
|
-
|
|
229
|
-
if len(clauses) == 1:
|
|
230
|
-
return clauses[0]
|
|
231
|
-
|
|
232
|
-
return "(" + " OR ".join(clauses) + ")"
|
|
233
|
-
|
|
234
|
-
def _general_filters(self) -> str:
|
|
235
|
-
return " AND ".join(
|
|
236
|
-
[
|
|
237
|
-
*self.custom_filters,
|
|
238
|
-
*self.standard_filters,
|
|
239
|
-
]
|
|
240
|
-
)
|
|
241
|
-
|
|
242
|
-
@property
|
|
243
|
-
def standard_filters(self) -> Iterable[str]:
|
|
244
|
-
yield from (
|
|
245
|
-
f"({self.filter_for_a(sql_field=item)})"
|
|
246
|
-
for item in self.fields.for_filtering()
|
|
247
|
-
)
|
|
248
|
-
|
|
249
|
-
def filter_for_a(self, sql_field: SqlField) -> str:
|
|
250
|
-
return " OR ".join(
|
|
251
|
-
self.filter_with_a(sql_field.name, value, index)
|
|
252
|
-
for index, value in enumerate(sql_field.filter_values)
|
|
253
|
-
)
|
|
254
|
-
|
|
255
|
-
def filter_with_a(self, name: str, value: Any | NotNone | None, index: int) -> str:
|
|
256
|
-
if value is None:
|
|
257
|
-
return self.placeholder_key.is_null(name)
|
|
258
|
-
|
|
259
|
-
if isinstance(value, NotNone):
|
|
260
|
-
return self.placeholder_key.is_not_null(name)
|
|
261
|
-
|
|
262
|
-
return (
|
|
263
|
-
self.placeholder_key.replace_with(name)
|
|
264
|
-
+ " = "
|
|
265
|
-
+ self.placeholder_value.replace_with(name + "_filter_" + str(index))
|
|
266
|
-
)
|
|
267
|
-
|
|
268
|
-
@dataclass
|
|
269
|
-
class Builder:
|
|
270
|
-
placeholder_value: PlaceHolder
|
|
271
|
-
placeholder_key: PlaceHolder
|
|
272
|
-
|
|
273
|
-
fields: SqlFieldSet = field(default_factory=SqlFieldSet)
|
|
274
|
-
custom_filters: list[str] = field(default_factory=list)
|
|
275
|
-
|
|
276
|
-
@classmethod
|
|
277
|
-
def for_sqlite(cls) -> Self:
|
|
278
|
-
return cls(
|
|
279
|
-
placeholder_key=SqliteKeyPlaceholder(),
|
|
280
|
-
placeholder_value=SqliteValuePlaceholder(),
|
|
281
|
-
)
|
|
282
|
-
|
|
283
|
-
@classmethod
|
|
284
|
-
def for_mssql(cls) -> Self:
|
|
285
|
-
return cls(
|
|
286
|
-
placeholder_key=MsSqlKeyPlaceholder(),
|
|
287
|
-
placeholder_value=MsSqlValuePlaceholder(),
|
|
288
|
-
)
|
|
289
|
-
|
|
290
|
-
def with_fields(self, fields: SqlFieldSet) -> Self:
|
|
291
|
-
self.fields = fields
|
|
292
|
-
|
|
293
|
-
return self
|
|
294
|
-
|
|
295
|
-
def with_custom_filters(self, fields: list[str]) -> Self:
|
|
296
|
-
self.custom_filters = fields
|
|
297
|
-
|
|
298
|
-
return self
|
|
299
|
-
|
|
300
|
-
def build(self) -> SqlFieldManager:
|
|
301
|
-
return SqlFieldManager(
|
|
302
|
-
fields=self.fields,
|
|
303
|
-
custom_filters=self.custom_filters,
|
|
304
|
-
placeholder_value=self.placeholder_value,
|
|
305
|
-
placeholder_key=self.placeholder_key,
|
|
306
|
-
)
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
class PlaceHolder(ABC):
|
|
310
|
-
def is_null(self, value: str) -> str:
|
|
311
|
-
return self.replace_with(value) + " IS NULL"
|
|
312
|
-
|
|
313
|
-
def is_not_null(self, value: str) -> str:
|
|
314
|
-
return self.replace_with(value) + " IS NOT NULL"
|
|
315
|
-
|
|
316
|
-
@abstractmethod
|
|
317
|
-
def replace_with(self, value: str) -> str:
|
|
318
|
-
pass
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
@dataclass(frozen=True)
|
|
322
|
-
class MsSqlKeyPlaceholder(PlaceHolder):
|
|
323
|
-
def replace_with(self, value: str) -> str:
|
|
324
|
-
return f"[{value}]"
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
@dataclass(frozen=True)
|
|
328
|
-
class MsSqlValuePlaceholder(PlaceHolder):
|
|
329
|
-
def replace_with(self, value: str) -> str:
|
|
330
|
-
return f"%({value})s"
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
@dataclass(frozen=True)
|
|
334
|
-
class SqliteKeyPlaceholder(PlaceHolder):
|
|
335
|
-
def replace_with(self, value: str) -> str:
|
|
336
|
-
return f"{value}"
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
@dataclass(frozen=True)
|
|
340
|
-
class SqliteValuePlaceholder(PlaceHolder):
|
|
341
|
-
def replace_with(self, value: str) -> str:
|
|
342
|
-
return f":{value}"
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass, field, replace
|
|
4
|
+
from typing import TYPE_CHECKING, Protocol, Self
|
|
5
|
+
|
|
6
|
+
from apexdevkit.repository.sql.operations import SqlAnd, SqlOperation, SqlOr
|
|
7
|
+
|
|
8
|
+
if TYPE_CHECKING:
|
|
9
|
+
from collections.abc import Iterable
|
|
10
|
+
|
|
11
|
+
from apexdevkit.repository.sql import SqlField
|
|
12
|
+
from apexdevkit.repository.sql.field import SqlFieldSet
|
|
13
|
+
from apexdevkit.repository.sql.placeholders import SqlPlaceHolder
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass(frozen=True)
|
|
17
|
+
class ComboFilter:
|
|
18
|
+
items: list[FilterLink] = field(default_factory=list)
|
|
19
|
+
|
|
20
|
+
def append(self, item: FilterLink) -> Self:
|
|
21
|
+
return replace(self, items=[*self.items, item])
|
|
22
|
+
|
|
23
|
+
def prepend(self, item: FilterLink) -> Self:
|
|
24
|
+
return replace(self, items=[item, *self.items])
|
|
25
|
+
|
|
26
|
+
def __str__(self) -> str:
|
|
27
|
+
return str(self.compile())
|
|
28
|
+
|
|
29
|
+
def compile(self) -> SqlOperation:
|
|
30
|
+
return SqlAnd().add_many(item.compile() for item in self.items)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class FilterLink(Protocol):
|
|
34
|
+
def compile(self) -> SqlOperation:
|
|
35
|
+
pass
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@dataclass(frozen=True, kw_only=True)
|
|
39
|
+
class ParentFilter:
|
|
40
|
+
parent: SqlField | None
|
|
41
|
+
placeholder: SqlPlaceHolder
|
|
42
|
+
|
|
43
|
+
def compile(self) -> SqlOperation:
|
|
44
|
+
if self.parent is None:
|
|
45
|
+
return SqlAnd()
|
|
46
|
+
|
|
47
|
+
return SqlAnd().add(
|
|
48
|
+
self.placeholder.with_condition(self.parent.parent_value).compile(
|
|
49
|
+
self.parent.name
|
|
50
|
+
)
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@dataclass(frozen=True, kw_only=True)
|
|
55
|
+
class StandardFilter:
|
|
56
|
+
fields: Iterable[SqlField]
|
|
57
|
+
placeholder: SqlPlaceHolder
|
|
58
|
+
|
|
59
|
+
def compile(self) -> SqlOperation:
|
|
60
|
+
return SqlAnd().add_many(self.compile_one(item) for item in self.fields)
|
|
61
|
+
|
|
62
|
+
def compile_one(self, item: SqlField) -> SqlOperation:
|
|
63
|
+
return SqlOr().add_many(
|
|
64
|
+
self.placeholder.filter(index=index)
|
|
65
|
+
.with_condition(value)
|
|
66
|
+
.compile(item.name)
|
|
67
|
+
for index, value in item.filters
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@dataclass(frozen=True, kw_only=True)
|
|
72
|
+
class CustomFilter:
|
|
73
|
+
items: Iterable[str] = field(default_factory=list)
|
|
74
|
+
|
|
75
|
+
def compile(self) -> SqlOperation:
|
|
76
|
+
return SqlAnd().add_many(self.items)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
@dataclass(frozen=True, kw_only=True)
|
|
80
|
+
class InsertedIdFilter:
|
|
81
|
+
id_field: SqlField
|
|
82
|
+
placeholder: SqlPlaceHolder
|
|
83
|
+
|
|
84
|
+
def compile(self) -> SqlOperation:
|
|
85
|
+
return SqlAnd().add(
|
|
86
|
+
self.placeholder.named(self.id_field.name).compile(
|
|
87
|
+
self.id_field.name
|
|
88
|
+
if self.id_field.include_in_insert
|
|
89
|
+
else "SCOPE_IDENTITY()"
|
|
90
|
+
)
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
@dataclass(frozen=True, kw_only=True)
|
|
95
|
+
class IdFilter:
|
|
96
|
+
fields: SqlFieldSet
|
|
97
|
+
placeholder: SqlPlaceHolder
|
|
98
|
+
|
|
99
|
+
def compile(self) -> SqlOperation:
|
|
100
|
+
return SqlOr().add_many(
|
|
101
|
+
self.placeholder.named(f.name).compile(self.fields.id_field.name)
|
|
102
|
+
for f in self.fields
|
|
103
|
+
if f.is_selectable or f.is_id
|
|
104
|
+
)
|