apexdevkit 2.4.3__py3-none-any.whl → 2.4.4__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/sql/__init__.py +2 -2
- apexdevkit/repository/sql/field.py +196 -163
- apexdevkit/repository/sql/mssql/table.py +40 -90
- apexdevkit/repository/sql/sqlite/table.py +14 -7
- {apexdevkit-2.4.3.dist-info → apexdevkit-2.4.4.dist-info}/METADATA +1 -1
- {apexdevkit-2.4.3.dist-info → apexdevkit-2.4.4.dist-info}/RECORD +8 -8
- {apexdevkit-2.4.3.dist-info → apexdevkit-2.4.4.dist-info}/WHEEL +0 -0
- {apexdevkit-2.4.3.dist-info → apexdevkit-2.4.4.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
from .field import NotNone,
|
|
1
|
+
from .field import NotNone, SqlField, SqlFieldManager
|
|
2
2
|
from .mssql import MsSqlRepository
|
|
3
3
|
from .sqlite import SqliteRepository
|
|
4
4
|
|
|
5
5
|
__all__ = [
|
|
6
6
|
"MsSqlRepository",
|
|
7
7
|
"NotNone",
|
|
8
|
-
"
|
|
8
|
+
"SqlField",
|
|
9
9
|
"SqlFieldManager",
|
|
10
10
|
"SqliteRepository",
|
|
11
11
|
]
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
-
from
|
|
4
|
-
from
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
from dataclasses import dataclass, field, replace
|
|
5
|
+
from typing import TYPE_CHECKING, Any, Self
|
|
6
|
+
|
|
7
|
+
from pypebbles import FluentDict
|
|
5
8
|
|
|
6
9
|
if TYPE_CHECKING:
|
|
7
|
-
from collections.abc import Iterator, Mapping
|
|
10
|
+
from collections.abc import Iterable, Iterator, Mapping
|
|
8
11
|
|
|
9
12
|
|
|
10
13
|
@dataclass(frozen=True)
|
|
@@ -13,8 +16,9 @@ class NotNone:
|
|
|
13
16
|
|
|
14
17
|
|
|
15
18
|
@dataclass(frozen=True)
|
|
16
|
-
class
|
|
19
|
+
class SqlField:
|
|
17
20
|
name: str
|
|
21
|
+
|
|
18
22
|
is_id: bool = False
|
|
19
23
|
is_selectable: bool = False
|
|
20
24
|
is_composite: bool = False
|
|
@@ -33,132 +37,128 @@ class _SqlField:
|
|
|
33
37
|
is_fixed: bool = False
|
|
34
38
|
fixed_value: Any | None = None
|
|
35
39
|
|
|
40
|
+
@classmethod
|
|
41
|
+
def named(cls, name: str) -> Self:
|
|
42
|
+
return cls(name=name)
|
|
36
43
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
_name: str = field(init=False)
|
|
40
|
-
_is_id: bool = False
|
|
41
|
-
_is_selectable: bool = False
|
|
42
|
-
_is_composite: bool = False
|
|
43
|
-
_include_in_insert: bool = True
|
|
44
|
-
_include_in_update: bool = True
|
|
44
|
+
def as_id(self) -> Self:
|
|
45
|
+
return replace(self, is_id=True)
|
|
45
46
|
|
|
46
|
-
|
|
47
|
-
|
|
47
|
+
def selectable(self) -> Self:
|
|
48
|
+
return replace(self, is_selectable=True)
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
def composite(self) -> Self:
|
|
51
|
+
return replace(self, is_composite=True)
|
|
51
52
|
|
|
52
|
-
|
|
53
|
-
|
|
53
|
+
def order_by(self, *, descending: bool = False) -> Self:
|
|
54
|
+
return replace(self, is_ordered=True, is_descending=descending)
|
|
54
55
|
|
|
55
|
-
|
|
56
|
-
|
|
56
|
+
def derive_on_insert(self) -> Self:
|
|
57
|
+
return replace(self, include_in_insert=False)
|
|
57
58
|
|
|
58
|
-
def
|
|
59
|
-
self
|
|
59
|
+
def not_editable(self) -> Self:
|
|
60
|
+
return replace(self, include_in_update=False)
|
|
60
61
|
|
|
61
|
-
|
|
62
|
+
def parent(self, value: Any | None) -> Self:
|
|
63
|
+
return replace(self, is_parent=True, parent_value=value)
|
|
62
64
|
|
|
63
|
-
def
|
|
64
|
-
self
|
|
65
|
+
def filter(self, values: list[Any | NotNone | None]) -> Self:
|
|
66
|
+
return replace(self, is_filter=True, filter_values=values)
|
|
65
67
|
|
|
66
|
-
|
|
68
|
+
def fixed(self, value: Any | None) -> Self:
|
|
69
|
+
return replace(self, is_fixed=True, fixed_value=value)
|
|
67
70
|
|
|
68
|
-
def as_selectable(self) -> SqlFieldBuilder:
|
|
69
|
-
self._is_selectable = True
|
|
70
71
|
|
|
71
|
-
|
|
72
|
+
@dataclass(frozen=True)
|
|
73
|
+
class SqlFieldSet:
|
|
74
|
+
items: list[SqlField] = field(default_factory=list)
|
|
72
75
|
|
|
73
|
-
def
|
|
74
|
-
self.
|
|
76
|
+
def __iter__(self) -> Iterator[SqlField]:
|
|
77
|
+
return iter(self.items)
|
|
75
78
|
|
|
76
|
-
|
|
79
|
+
@property
|
|
80
|
+
def id_field(self) -> SqlField:
|
|
81
|
+
for item in self.items:
|
|
82
|
+
if item.is_id:
|
|
83
|
+
return item
|
|
77
84
|
|
|
78
|
-
|
|
79
|
-
self._is_ordered = True
|
|
80
|
-
self._is_descending = descending
|
|
85
|
+
raise RuntimeError("FieldSet does not contain Id field.")
|
|
81
86
|
|
|
82
|
-
|
|
87
|
+
def contains_id(self) -> bool:
|
|
88
|
+
return any(i.is_id for i in self.items)
|
|
83
89
|
|
|
84
|
-
def
|
|
85
|
-
self.
|
|
90
|
+
def add(self, item: SqlField) -> Self:
|
|
91
|
+
self.items.append(self.validate(item))
|
|
86
92
|
|
|
87
93
|
return self
|
|
88
94
|
|
|
89
|
-
def
|
|
90
|
-
|
|
95
|
+
def validate(self, item: SqlField) -> SqlField:
|
|
96
|
+
if item.is_id and self.contains_id():
|
|
97
|
+
raise ValueError("Pass only one identifier key.")
|
|
91
98
|
|
|
92
|
-
|
|
99
|
+
if item.is_parent and any(i.is_parent for i in self.items):
|
|
100
|
+
raise ValueError("Pass only one parent key.")
|
|
93
101
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
self._parent_value = value
|
|
102
|
+
if not item.is_filter and isinstance(item.fixed_value, NotNone):
|
|
103
|
+
raise ValueError("Only filter fields can be 'not null'.")
|
|
97
104
|
|
|
98
|
-
return
|
|
105
|
+
return item
|
|
99
106
|
|
|
100
|
-
def
|
|
101
|
-
self.
|
|
102
|
-
self._filter_values = values
|
|
107
|
+
def for_ordering(self) -> Iterable[SqlField]:
|
|
108
|
+
return (item for item in self.items if item.is_ordered)
|
|
103
109
|
|
|
104
|
-
|
|
110
|
+
def for_filtering(self) -> Iterable[SqlField]:
|
|
111
|
+
return (item for item in self.items if item.is_filter and item.filter_values)
|
|
105
112
|
|
|
106
|
-
def
|
|
107
|
-
|
|
108
|
-
self._fixed_value = value
|
|
113
|
+
def as_payload(self) -> Mapping[str, Any]:
|
|
114
|
+
payload = {}
|
|
109
115
|
|
|
110
|
-
|
|
116
|
+
for item in self.items:
|
|
117
|
+
if item.is_parent:
|
|
118
|
+
payload[item.name] = item.parent_value
|
|
111
119
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
is_descending=self._is_descending,
|
|
122
|
-
is_parent=self._is_parent,
|
|
123
|
-
parent_value=self._parent_value,
|
|
124
|
-
is_filter=self._is_filter,
|
|
125
|
-
filter_values=self._filter_values,
|
|
126
|
-
is_fixed=self._is_fixed,
|
|
127
|
-
fixed_value=self._fixed_value,
|
|
128
|
-
)
|
|
120
|
+
if item.is_fixed:
|
|
121
|
+
payload[item.name] = item.fixed_value
|
|
122
|
+
|
|
123
|
+
if item.is_filter:
|
|
124
|
+
for index, value in enumerate(item.filter_values):
|
|
125
|
+
if value is not None and not isinstance(value, NotNone):
|
|
126
|
+
payload[item.name + "_filter_" + str(index)] = value
|
|
127
|
+
|
|
128
|
+
return payload
|
|
129
129
|
|
|
130
130
|
|
|
131
131
|
@dataclass
|
|
132
132
|
class SqlFieldManager:
|
|
133
|
-
fields:
|
|
133
|
+
fields: SqlFieldSet
|
|
134
134
|
custom_filters: list[str]
|
|
135
|
-
|
|
136
|
-
|
|
135
|
+
|
|
136
|
+
placeholder_value: PlaceHolder
|
|
137
|
+
placeholder_key: PlaceHolder
|
|
137
138
|
|
|
138
139
|
@property
|
|
139
|
-
def
|
|
140
|
-
|
|
141
|
-
if result is None:
|
|
142
|
-
raise ValueError("Id field is required.")
|
|
143
|
-
return result.name
|
|
140
|
+
def id_name(self) -> str:
|
|
141
|
+
return self.fields.id_field.name
|
|
144
142
|
|
|
145
143
|
@property
|
|
146
144
|
def composite(self) -> list[str]:
|
|
147
145
|
names = [key.name for key in self.fields if key.is_composite]
|
|
148
|
-
|
|
146
|
+
|
|
147
|
+
return [self.id_name] if len(names) == 0 else names
|
|
149
148
|
|
|
150
149
|
@property
|
|
151
150
|
def order(self) -> str:
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
151
|
+
fields = [
|
|
152
|
+
f"{item.name} DESC" if item.is_descending else item.name
|
|
153
|
+
for item in self.fields.for_ordering()
|
|
155
154
|
]
|
|
156
|
-
if len(ordering) > 0:
|
|
157
|
-
return "ORDER BY " + ", ".join(order_clauses)
|
|
158
155
|
|
|
159
|
-
|
|
156
|
+
if not fields:
|
|
157
|
+
return ""
|
|
160
158
|
|
|
161
|
-
|
|
159
|
+
return "ORDER BY " + ", ".join(fields)
|
|
160
|
+
|
|
161
|
+
def __iter__(self) -> Iterator[SqlField]:
|
|
162
162
|
yield from self.fields
|
|
163
163
|
|
|
164
164
|
def where_statement(
|
|
@@ -182,50 +182,38 @@ class SqlFieldManager:
|
|
|
182
182
|
return ""
|
|
183
183
|
|
|
184
184
|
def with_fixed(self, raw: Mapping[str, Any]) -> dict[str, Any]:
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
for key in self.fields:
|
|
188
|
-
if key.is_parent:
|
|
189
|
-
data[key.name] = key.parent_value
|
|
190
|
-
if key.is_fixed:
|
|
191
|
-
data[key.name] = key.fixed_value
|
|
192
|
-
if key.is_filter:
|
|
193
|
-
for index, value in enumerate(key.filter_values):
|
|
194
|
-
if value is not None and not isinstance(value, NotNone):
|
|
195
|
-
data[key.name + "_filter_" + str(index)] = value
|
|
196
|
-
return data
|
|
185
|
+
return FluentDict(raw).merge(self.fields.as_payload())
|
|
197
186
|
|
|
198
187
|
def _parent_filter(self) -> str:
|
|
199
188
|
result = next((key for key in self.fields if key.is_parent), None)
|
|
200
|
-
if result is not None:
|
|
201
|
-
if result.parent_value is None:
|
|
202
|
-
return self.key_formatter.replace("x", result.name) + " IS NULL"
|
|
203
|
-
return (
|
|
204
|
-
self.key_formatter.replace("x", result.name)
|
|
205
|
-
+ " = "
|
|
206
|
-
+ self.value_formatter.replace("x", result.name)
|
|
207
|
-
)
|
|
208
189
|
|
|
209
|
-
|
|
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
|
+
)
|
|
210
201
|
|
|
211
202
|
def _id_filter(self, *, include_id: bool, read_id: bool) -> str:
|
|
212
203
|
if not include_id:
|
|
213
204
|
return ""
|
|
214
205
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
id_field = next((key for key in self.fields if key.is_id), None)
|
|
218
|
-
if id_field is None:
|
|
219
|
-
raise ValueError("Id field is required.")
|
|
206
|
+
id_field = self.fields.id_field
|
|
220
207
|
|
|
221
208
|
value_expr = (
|
|
222
|
-
self.
|
|
209
|
+
self.placeholder_value.replace_with(id_field.name)
|
|
223
210
|
if not read_id or id_field.include_in_insert
|
|
224
211
|
else "SCOPE_IDENTITY()"
|
|
225
212
|
)
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
213
|
+
|
|
214
|
+
clauses = [
|
|
215
|
+
f"{self.placeholder_key.replace_with(id_field.name)} = {value_expr}",
|
|
216
|
+
]
|
|
229
217
|
|
|
230
218
|
if not read_id:
|
|
231
219
|
selectable_fields = [
|
|
@@ -234,8 +222,8 @@ class SqlFieldManager:
|
|
|
234
222
|
if selectable.is_selectable and not selectable.is_id
|
|
235
223
|
]
|
|
236
224
|
for selectable in selectable_fields:
|
|
237
|
-
key_expr = self.
|
|
238
|
-
val_expr = self.
|
|
225
|
+
key_expr = self.placeholder_key.replace_with(selectable.name)
|
|
226
|
+
val_expr = self.placeholder_value.replace_with(id_field.name)
|
|
239
227
|
clauses.append(f"{key_expr} = {val_expr}")
|
|
240
228
|
|
|
241
229
|
if len(clauses) == 1:
|
|
@@ -244,66 +232,111 @@ class SqlFieldManager:
|
|
|
244
232
|
return "(" + " OR ".join(clauses) + ")"
|
|
245
233
|
|
|
246
234
|
def _general_filters(self) -> str:
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
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
|
+
)
|
|
272
267
|
|
|
273
268
|
@dataclass
|
|
274
269
|
class Builder:
|
|
270
|
+
placeholder_value: PlaceHolder
|
|
271
|
+
placeholder_key: PlaceHolder
|
|
272
|
+
|
|
273
|
+
fields: SqlFieldSet = field(default_factory=SqlFieldSet)
|
|
275
274
|
custom_filters: list[str] = field(default_factory=list)
|
|
276
275
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
276
|
+
@classmethod
|
|
277
|
+
def for_sqlite(cls) -> Self:
|
|
278
|
+
return cls(
|
|
279
|
+
placeholder_key=SqliteKeyPlaceholder(),
|
|
280
|
+
placeholder_value=SqliteValuePlaceholder(),
|
|
281
|
+
)
|
|
280
282
|
|
|
281
|
-
|
|
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:
|
|
282
291
|
self.fields = fields
|
|
283
292
|
|
|
284
293
|
return self
|
|
285
294
|
|
|
286
|
-
def with_custom_filters(self, fields: list[str]) ->
|
|
295
|
+
def with_custom_filters(self, fields: list[str]) -> Self:
|
|
287
296
|
self.custom_filters = fields
|
|
288
297
|
|
|
289
298
|
return self
|
|
290
299
|
|
|
291
|
-
def
|
|
292
|
-
|
|
293
|
-
|
|
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
|
+
)
|
|
294
307
|
|
|
295
|
-
return self
|
|
296
308
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
309
|
+
class PlaceHolder(ABC):
|
|
310
|
+
def is_null(self, value: str) -> str:
|
|
311
|
+
return self.replace_with(value) + " IS NULL"
|
|
300
312
|
|
|
301
|
-
|
|
313
|
+
def is_not_null(self, value: str) -> str:
|
|
314
|
+
return self.replace_with(value) + " IS NOT NULL"
|
|
302
315
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
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}"
|
|
@@ -4,14 +4,15 @@ from dataclasses import dataclass, field, replace
|
|
|
4
4
|
from textwrap import dedent
|
|
5
5
|
from typing import TYPE_CHECKING, Any, Self
|
|
6
6
|
|
|
7
|
-
from
|
|
7
|
+
from pypebbles import FluentDict
|
|
8
|
+
|
|
8
9
|
from apexdevkit.error import ExistsError
|
|
9
10
|
from apexdevkit.formatter import NoFormatter
|
|
10
11
|
from apexdevkit.repository.core import DatabaseCommand
|
|
11
|
-
from apexdevkit.repository.sql.field import
|
|
12
|
+
from apexdevkit.repository.sql.field import SqlField, SqlFieldManager, SqlFieldSet
|
|
12
13
|
|
|
13
14
|
if TYPE_CHECKING:
|
|
14
|
-
from collections.abc import Iterable,
|
|
15
|
+
from collections.abc import Iterable, Mapping
|
|
15
16
|
|
|
16
17
|
from apexdevkit.formatter import Formatter
|
|
17
18
|
|
|
@@ -48,70 +49,24 @@ class SqlTable[T]: # pragma: no cover
|
|
|
48
49
|
return ExistsError(duplicate).with_duplicate(lambda _: "Unknown")
|
|
49
50
|
|
|
50
51
|
|
|
51
|
-
@dataclass(frozen=True)
|
|
52
|
-
class SqlFieldSet:
|
|
53
|
-
items: list[_SqlField] = field(init=False, default_factory=list)
|
|
54
|
-
|
|
55
|
-
def __iter__(self) -> Iterator[_SqlField]:
|
|
56
|
-
return iter(self.items)
|
|
57
|
-
|
|
58
|
-
def contains_id(self) -> bool:
|
|
59
|
-
return any(i.is_id for i in self.items)
|
|
60
|
-
|
|
61
|
-
def add(self, item: _SqlField) -> Self:
|
|
62
|
-
self.items.append(self.validate(item))
|
|
63
|
-
|
|
64
|
-
return self
|
|
65
|
-
|
|
66
|
-
def validate(self, item: _SqlField) -> _SqlField:
|
|
67
|
-
if item.is_id and self.contains_id():
|
|
68
|
-
raise ValueError("Pass only one identifier key.")
|
|
69
|
-
|
|
70
|
-
if item.is_parent and any(i.is_parent for i in self.items):
|
|
71
|
-
raise ValueError("Pass only one parent key.")
|
|
72
|
-
|
|
73
|
-
if not item.is_filter and isinstance(item.fixed_value, NotNone):
|
|
74
|
-
raise ValueError("Only filter fields can be 'not null'.")
|
|
75
|
-
|
|
76
|
-
return item
|
|
77
|
-
|
|
78
|
-
|
|
79
52
|
@dataclass(frozen=True)
|
|
80
53
|
class MsSqlTableBuilder[T]:
|
|
81
|
-
username: str = ""
|
|
82
54
|
schema: str = ""
|
|
83
55
|
table: str = ""
|
|
84
56
|
|
|
85
57
|
fields: SqlFieldSet = field(default_factory=SqlFieldSet)
|
|
86
58
|
custom_filters: list[str] = field(default_factory=list)
|
|
87
59
|
|
|
88
|
-
formatter: Formatter[Mapping[str, Any], T] = field(default_factory=NoFormatter)
|
|
89
|
-
|
|
90
|
-
@deprecated("Use .build().for_a(username) instead")
|
|
91
|
-
def with_username(self, value: str) -> Self:
|
|
92
|
-
return replace(self, username=value)
|
|
93
|
-
|
|
94
60
|
def with_schema(self, value: str) -> Self:
|
|
95
61
|
return replace(self, schema=value)
|
|
96
62
|
|
|
97
63
|
def with_table(self, value: str) -> Self:
|
|
98
64
|
return replace(self, table=value)
|
|
99
65
|
|
|
100
|
-
|
|
101
|
-
def with_formatter(self, value: Formatter[Mapping[str, Any], T]) -> Self:
|
|
102
|
-
return replace(self, formatter=value)
|
|
103
|
-
|
|
104
|
-
@deprecated("Use .with_a/and_a method(s) instead")
|
|
105
|
-
def with_fields(self, value: Iterable[_SqlField]) -> Self:
|
|
106
|
-
for sql_field in value:
|
|
107
|
-
self.with_a(sql_field=sql_field)
|
|
108
|
-
|
|
109
|
-
return self
|
|
110
|
-
|
|
111
|
-
def and_a(self, sql_field: _SqlField) -> Self:
|
|
66
|
+
def and_a(self, sql_field: SqlField) -> Self:
|
|
112
67
|
return self.with_a(sql_field=sql_field)
|
|
113
68
|
|
|
114
|
-
def with_a(self, sql_field:
|
|
69
|
+
def with_a(self, sql_field: SqlField) -> Self:
|
|
115
70
|
self.fields.add(sql_field)
|
|
116
71
|
|
|
117
72
|
return self
|
|
@@ -119,7 +74,7 @@ class MsSqlTableBuilder[T]:
|
|
|
119
74
|
def with_custom_filters(self, filters: Iterable[str]) -> Self:
|
|
120
75
|
return replace(self, custom_filters=list(filters))
|
|
121
76
|
|
|
122
|
-
def build(self) -> AutoMsSqlTable[T]
|
|
77
|
+
def build(self) -> AutoMsSqlTable[T]:
|
|
123
78
|
if not self.fields.contains_id():
|
|
124
79
|
raise ValueError("Id field is not provided!")
|
|
125
80
|
|
|
@@ -127,34 +82,35 @@ class MsSqlTableBuilder[T]:
|
|
|
127
82
|
raise ValueError("Cannot build sql table.")
|
|
128
83
|
|
|
129
84
|
fields = (
|
|
130
|
-
SqlFieldManager.Builder()
|
|
131
|
-
.with_fields(
|
|
85
|
+
SqlFieldManager.Builder.for_mssql()
|
|
86
|
+
.with_fields(self.fields)
|
|
132
87
|
.with_custom_filters(list(self.custom_filters))
|
|
133
|
-
.for_mssql()
|
|
134
88
|
.build()
|
|
135
89
|
)
|
|
136
90
|
|
|
137
|
-
return (
|
|
138
|
-
|
|
91
|
+
return AutoMsSqlTable[T](
|
|
92
|
+
fields=self.fields,
|
|
93
|
+
templates=SqlTemplates(
|
|
94
|
+
schema=self.schema,
|
|
95
|
+
table=self.table,
|
|
139
96
|
fields=fields,
|
|
140
|
-
|
|
141
|
-
schema=self.schema,
|
|
142
|
-
table=self.table,
|
|
143
|
-
fields=fields,
|
|
144
|
-
),
|
|
145
|
-
)
|
|
146
|
-
.with_a(formatter=self.formatter)
|
|
147
|
-
.for_a(username=self.username)
|
|
97
|
+
),
|
|
148
98
|
)
|
|
149
99
|
|
|
150
100
|
|
|
151
101
|
@dataclass(frozen=True, kw_only=True)
|
|
152
102
|
class AutoMsSqlTable[T](SqlTable[T]):
|
|
153
|
-
fields:
|
|
154
|
-
|
|
103
|
+
fields: SqlFieldSet
|
|
104
|
+
templates: SqlTemplates
|
|
155
105
|
|
|
156
106
|
formatter: Formatter[Mapping[str, Any], T] = field(default_factory=NoFormatter)
|
|
157
107
|
|
|
108
|
+
def dump(self, item: T) -> FluentDict[Any]:
|
|
109
|
+
return FluentDict(self.formatter.dump(item))
|
|
110
|
+
|
|
111
|
+
def dump_id(self, item_id: str) -> FluentDict[str]:
|
|
112
|
+
return FluentDict({self.fields.id_field.name: item_id})
|
|
113
|
+
|
|
158
114
|
def with_a(self, formatter: Formatter[Mapping[str, Any], T]) -> AutoMsSqlTable[T]:
|
|
159
115
|
return replace(self, formatter=formatter)
|
|
160
116
|
|
|
@@ -166,44 +122,44 @@ class AutoMsSqlTable[T](SqlTable[T]):
|
|
|
166
122
|
|
|
167
123
|
def count_all(self) -> DatabaseCommand:
|
|
168
124
|
return DatabaseCommand(
|
|
169
|
-
value=self.
|
|
170
|
-
payload=self.fields.
|
|
125
|
+
value=self.templates.count_all(),
|
|
126
|
+
payload=self.fields.as_payload(),
|
|
171
127
|
)
|
|
172
128
|
|
|
173
129
|
def insert(self, item: T) -> DatabaseCommand:
|
|
174
130
|
return DatabaseCommand(
|
|
175
|
-
value=self.
|
|
176
|
-
payload=self.
|
|
131
|
+
value=self.templates.insert(),
|
|
132
|
+
payload=self.dump(item).merge(self.fields.as_payload()),
|
|
177
133
|
)
|
|
178
134
|
|
|
179
135
|
def select(self, item_id: str) -> DatabaseCommand:
|
|
180
136
|
return DatabaseCommand(
|
|
181
|
-
value=self.
|
|
182
|
-
payload=self.
|
|
137
|
+
value=self.templates.select(),
|
|
138
|
+
payload=self.dump_id(item_id).merge(self.fields.as_payload()),
|
|
183
139
|
)
|
|
184
140
|
|
|
185
141
|
def select_all(self) -> DatabaseCommand:
|
|
186
142
|
return DatabaseCommand(
|
|
187
|
-
value=self.
|
|
188
|
-
payload=self.fields.
|
|
143
|
+
value=self.templates.select_all(),
|
|
144
|
+
payload=self.fields.as_payload(),
|
|
189
145
|
)
|
|
190
146
|
|
|
191
147
|
def update(self, item: T) -> DatabaseCommand:
|
|
192
148
|
return DatabaseCommand(
|
|
193
|
-
value=self.
|
|
194
|
-
payload=self.
|
|
149
|
+
value=self.templates.update(),
|
|
150
|
+
payload=self.dump(item).merge(self.fields.as_payload()),
|
|
195
151
|
)
|
|
196
152
|
|
|
197
153
|
def delete(self, item_id: str) -> DatabaseCommand:
|
|
198
154
|
return DatabaseCommand(
|
|
199
|
-
value=self.
|
|
200
|
-
payload=self.
|
|
155
|
+
value=self.templates.delete(),
|
|
156
|
+
payload=self.dump_id(item_id).merge(self.fields.as_payload()),
|
|
201
157
|
)
|
|
202
158
|
|
|
203
159
|
def delete_all(self) -> DatabaseCommand:
|
|
204
160
|
return DatabaseCommand(
|
|
205
|
-
value=self.
|
|
206
|
-
payload=self.fields.
|
|
161
|
+
value=self.templates.delete_all(),
|
|
162
|
+
payload=self.fields.as_payload(),
|
|
207
163
|
)
|
|
208
164
|
|
|
209
165
|
def load(self, data: Mapping[str, Any]) -> T:
|
|
@@ -212,12 +168,12 @@ class AutoMsSqlTable[T](SqlTable[T]):
|
|
|
212
168
|
def exists(self, duplicate: T) -> ExistsError:
|
|
213
169
|
raw = self.formatter.dump(duplicate)
|
|
214
170
|
return ExistsError(duplicate).with_duplicate(
|
|
215
|
-
lambda _: f"{self.fields.
|
|
171
|
+
lambda _: f"{self.fields.id_field.name}<{raw[self.fields.id_field.name]}>"
|
|
216
172
|
)
|
|
217
173
|
|
|
218
174
|
|
|
219
175
|
@dataclass(frozen=True, kw_only=True)
|
|
220
|
-
class
|
|
176
|
+
class SqlTemplates:
|
|
221
177
|
schema: str
|
|
222
178
|
table: str
|
|
223
179
|
fields: SqlFieldManager
|
|
@@ -238,7 +194,7 @@ class SqlTemplateGenerator:
|
|
|
238
194
|
[f"%({key.name})s" for key in self.fields if key.include_in_insert]
|
|
239
195
|
)
|
|
240
196
|
try:
|
|
241
|
-
_ = self.fields.
|
|
197
|
+
_ = self.fields.id_name
|
|
242
198
|
output = ", ".join(
|
|
243
199
|
["[" + field.name + "] AS " + field.name for field in self.fields]
|
|
244
200
|
)
|
|
@@ -332,12 +288,6 @@ class AsUser[T](SqlTable[T]):
|
|
|
332
288
|
if not self.user:
|
|
333
289
|
raise ValueError("username must be provided")
|
|
334
290
|
|
|
335
|
-
def with_a(self, formatter: Formatter[Mapping[str, Any], T]) -> AsUser[T]:
|
|
336
|
-
return replace(self, inner=self.inner.with_a(formatter=formatter))
|
|
337
|
-
|
|
338
|
-
def for_a(self, username: str) -> AsUser[T]:
|
|
339
|
-
return replace(self, user=username)
|
|
340
|
-
|
|
341
291
|
def _wrap(self, command: DatabaseCommand) -> DatabaseCommand:
|
|
342
292
|
return DatabaseCommand(
|
|
343
293
|
"\n".join(
|
|
@@ -5,7 +5,12 @@ from typing import TYPE_CHECKING, Any, Self
|
|
|
5
5
|
|
|
6
6
|
from apexdevkit.error import ExistsError
|
|
7
7
|
from apexdevkit.repository.core import DatabaseCommand
|
|
8
|
-
from apexdevkit.repository.sql.field import
|
|
8
|
+
from apexdevkit.repository.sql.field import (
|
|
9
|
+
NotNone,
|
|
10
|
+
SqlField,
|
|
11
|
+
SqlFieldManager,
|
|
12
|
+
SqlFieldSet,
|
|
13
|
+
)
|
|
9
14
|
|
|
10
15
|
if TYPE_CHECKING:
|
|
11
16
|
from collections.abc import Iterable, Mapping
|
|
@@ -49,7 +54,7 @@ class SqlTable[T]: # pragma: no cover
|
|
|
49
54
|
class SqliteTableBuilder[T]:
|
|
50
55
|
table_name: str | None = None
|
|
51
56
|
formatter: Formatter[Mapping[str, Any], T] | None = None
|
|
52
|
-
fields: list[
|
|
57
|
+
fields: list[SqlField] | None = None
|
|
53
58
|
custom_filters: list[str] | None = None
|
|
54
59
|
|
|
55
60
|
def with_name(self, value: str) -> Self:
|
|
@@ -58,7 +63,7 @@ class SqliteTableBuilder[T]:
|
|
|
58
63
|
def with_formatter(self, value: Formatter[Mapping[str, Any], T]) -> Self:
|
|
59
64
|
return replace(self, formatter=value)
|
|
60
65
|
|
|
61
|
-
def with_fields(self, value: Iterable[
|
|
66
|
+
def with_fields(self, value: Iterable[SqlField]) -> Self:
|
|
62
67
|
key_list = list(value)
|
|
63
68
|
if len([key for key in key_list if key.is_id]) != 1:
|
|
64
69
|
raise ValueError("Pass only one identifier key.")
|
|
@@ -85,14 +90,16 @@ class SqliteTableBuilder[T]:
|
|
|
85
90
|
if not self.table_name or not self.formatter or not self.fields:
|
|
86
91
|
raise ValueError("Parameter missing.")
|
|
87
92
|
|
|
88
|
-
field_manager = SqlFieldManager.Builder().with_fields(
|
|
93
|
+
field_manager = SqlFieldManager.Builder.for_sqlite().with_fields(
|
|
94
|
+
SqlFieldSet(self.fields)
|
|
95
|
+
)
|
|
89
96
|
if self.custom_filters and len(self.custom_filters) > 0:
|
|
90
97
|
field_manager = field_manager.with_custom_filters(self.custom_filters)
|
|
91
98
|
|
|
92
99
|
return _DefaultSqlTable(
|
|
93
100
|
self.table_name,
|
|
94
101
|
self.formatter,
|
|
95
|
-
field_manager.
|
|
102
|
+
field_manager.build(),
|
|
96
103
|
)
|
|
97
104
|
|
|
98
105
|
|
|
@@ -135,7 +142,7 @@ class _DefaultSqlTable[T](SqlTable[T]):
|
|
|
135
142
|
{columns}
|
|
136
143
|
FROM {self.table_name.upper()}
|
|
137
144
|
{self.fields.where_statement(include_id=True)};
|
|
138
|
-
""").with_data(self.fields.with_fixed({self.fields.
|
|
145
|
+
""").with_data(self.fields.with_fixed({self.fields.id_name: item_id}))
|
|
139
146
|
|
|
140
147
|
def select_duplicate(self, item: T) -> DatabaseCommand:
|
|
141
148
|
raw = self.formatter.dump(item)
|
|
@@ -184,7 +191,7 @@ class _DefaultSqlTable[T](SqlTable[T]):
|
|
|
184
191
|
DELETE
|
|
185
192
|
FROM {self.table_name.upper()}
|
|
186
193
|
{self.fields.where_statement(include_id=True)};
|
|
187
|
-
""").with_data(self.fields.with_fixed({self.fields.
|
|
194
|
+
""").with_data(self.fields.with_fixed({self.fields.id_name: item_id}))
|
|
188
195
|
|
|
189
196
|
def delete_all(self) -> DatabaseCommand:
|
|
190
197
|
return DatabaseCommand(f"""
|
|
@@ -29,18 +29,18 @@ apexdevkit/repository/in_memory/__init__.py,sha256=EKNQeD9Zt3dNME4fuPGiukMFx4_6Z
|
|
|
29
29
|
apexdevkit/repository/in_memory/mixin.py,sha256=aFr5Cg0B_g_i5ZTZ1fzlunfG-7W3MV0_xDyw-mbfQUw,579
|
|
30
30
|
apexdevkit/repository/in_memory/repository.py,sha256=-xLqrQN-o--doDVEKhqyUcVZfgp6_8_fjnxGGUCUsbY,2206
|
|
31
31
|
apexdevkit/repository/in_memory/store.py,sha256=K7vw3w4yctLGbrknGT16b3CZnloZZIHSFD2AkUIv_80,1260
|
|
32
|
-
apexdevkit/repository/sql/__init__.py,sha256=
|
|
32
|
+
apexdevkit/repository/sql/__init__.py,sha256=1Da1PF6Jvqwd1HopBZHZRRZ9kHBWUcBCO09z2W1I13o,253
|
|
33
33
|
apexdevkit/repository/sql/connector.py,sha256=0DacXgPdjR405YiKLXSaHDsLnf5TZmAOo58CgdvuSbM,2473
|
|
34
|
-
apexdevkit/repository/sql/field.py,sha256=
|
|
34
|
+
apexdevkit/repository/sql/field.py,sha256=kOvKFBl43kLmsBgkgTYxaMV-vLbRVPZei_kVUFInTTY,9755
|
|
35
35
|
apexdevkit/repository/sql/mssql/__init__.py,sha256=dIMytBRcoMliByJynZ0UuVG3R1nXw6Dpg_gUckasLlM,200
|
|
36
36
|
apexdevkit/repository/sql/mssql/repository.py,sha256=_ieZ_Hfi46KjOhVnc81n68cPYC-o4RAe0pSS4_CxP1w,2493
|
|
37
|
-
apexdevkit/repository/sql/mssql/table.py,sha256=
|
|
37
|
+
apexdevkit/repository/sql/mssql/table.py,sha256=in1JiRQRluvu02C-HYVrBaemVUP1WJwLVrsrsXgvzuk,9824
|
|
38
38
|
apexdevkit/repository/sql/sqlite/__init__.py,sha256=dG08No5TFPL4P2ymZkVBgDxxBq-UPcCUNIZVr_JOmXg,178
|
|
39
39
|
apexdevkit/repository/sql/sqlite/repository.py,sha256=QmsOlMcVKYhR9j-IFdPSSV1A0GVNw3lqV7faXhRuoFw,2173
|
|
40
|
-
apexdevkit/repository/sql/sqlite/table.py,sha256=
|
|
40
|
+
apexdevkit/repository/sql/sqlite/table.py,sha256=Ud1qfY886KSLlpY4iOsZs1XUxKMhhW6xEttYfNOVrgM,7123
|
|
41
41
|
apexdevkit/server.py,sha256=zo72jPZ0hCT_BlD-cpKaLetMSsYf5KGW3Dd0CCISSKA,4343
|
|
42
42
|
apexdevkit/testing.py,sha256=7Xc0HMWgkj8vjdBI7KS86Uknhlbaitw6U20sI_7IblQ,4505
|
|
43
|
-
apexdevkit-2.4.
|
|
44
|
-
apexdevkit-2.4.
|
|
45
|
-
apexdevkit-2.4.
|
|
46
|
-
apexdevkit-2.4.
|
|
43
|
+
apexdevkit-2.4.4.dist-info/METADATA,sha256=PfNdNwAIEJR79dZLinn9VPslb7tBFnuMSUb1askfr78,616
|
|
44
|
+
apexdevkit-2.4.4.dist-info/WHEEL,sha256=L-WvLdSBvJWHXv5zrQxDvNRnIgoBzLUpcbuiw19KCuQ,88
|
|
45
|
+
apexdevkit-2.4.4.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
|
|
46
|
+
apexdevkit-2.4.4.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|