apexdevkit 2.4.2__py3-none-any.whl → 2.4.3__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/mssql/table.py +226 -157
- apexdevkit/repository/sql/sqlite/repository.py +1 -1
- apexdevkit/repository/sql/sqlite/table.py +14 -35
- {apexdevkit-2.4.2.dist-info → apexdevkit-2.4.3.dist-info}/METADATA +1 -1
- {apexdevkit-2.4.2.dist-info → apexdevkit-2.4.3.dist-info}/RECORD +7 -7
- {apexdevkit-2.4.2.dist-info → apexdevkit-2.4.3.dist-info}/WHEEL +0 -0
- {apexdevkit-2.4.2.dist-info → apexdevkit-2.4.3.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
-
from dataclasses import dataclass
|
|
4
|
-
from
|
|
3
|
+
from dataclasses import dataclass, field, replace
|
|
4
|
+
from textwrap import dedent
|
|
5
|
+
from typing import TYPE_CHECKING, Any, Self
|
|
5
6
|
|
|
7
|
+
from apexdevkit.annotation import deprecated
|
|
6
8
|
from apexdevkit.error import ExistsError
|
|
9
|
+
from apexdevkit.formatter import NoFormatter
|
|
7
10
|
from apexdevkit.repository.core import DatabaseCommand
|
|
8
11
|
from apexdevkit.repository.sql.field import NotNone, SqlFieldManager, _SqlField
|
|
9
12
|
|
|
10
13
|
if TYPE_CHECKING:
|
|
11
|
-
from collections.abc import Iterable, Mapping
|
|
14
|
+
from collections.abc import Iterable, Iterator, Mapping
|
|
12
15
|
|
|
13
16
|
from apexdevkit.formatter import Formatter
|
|
14
17
|
|
|
@@ -23,179 +26,211 @@ class SqlTable[T]: # pragma: no cover
|
|
|
23
26
|
def select(self, item_id: str) -> DatabaseCommand:
|
|
24
27
|
raise NotImplementedError
|
|
25
28
|
|
|
29
|
+
def select_duplicate(self, item: T) -> DatabaseCommand:
|
|
30
|
+
raise NotImplementedError
|
|
31
|
+
|
|
26
32
|
def select_all(self) -> DatabaseCommand:
|
|
27
33
|
raise NotImplementedError
|
|
28
34
|
|
|
29
|
-
def
|
|
35
|
+
def update(self, item: T) -> DatabaseCommand:
|
|
30
36
|
raise NotImplementedError
|
|
31
37
|
|
|
32
|
-
def
|
|
38
|
+
def delete(self, item_id: str) -> DatabaseCommand:
|
|
33
39
|
raise NotImplementedError
|
|
34
40
|
|
|
35
|
-
def
|
|
41
|
+
def delete_all(self) -> DatabaseCommand:
|
|
36
42
|
raise NotImplementedError
|
|
37
43
|
|
|
38
44
|
def load(self, data: Mapping[str, Any]) -> T:
|
|
39
45
|
raise NotImplementedError
|
|
40
46
|
|
|
41
47
|
def exists(self, duplicate: T) -> ExistsError:
|
|
42
|
-
|
|
48
|
+
return ExistsError(duplicate).with_duplicate(lambda _: "Unknown")
|
|
43
49
|
|
|
44
50
|
|
|
45
|
-
@dataclass
|
|
46
|
-
class
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
def count_all(self) -> DatabaseCommand:
|
|
50
|
-
return self.table.count_all()
|
|
51
|
+
@dataclass(frozen=True)
|
|
52
|
+
class SqlFieldSet:
|
|
53
|
+
items: list[_SqlField] = field(init=False, default_factory=list)
|
|
51
54
|
|
|
52
|
-
def
|
|
53
|
-
return self.
|
|
55
|
+
def __iter__(self) -> Iterator[_SqlField]:
|
|
56
|
+
return iter(self.items)
|
|
54
57
|
|
|
55
|
-
def
|
|
56
|
-
return self.
|
|
58
|
+
def contains_id(self) -> bool:
|
|
59
|
+
return any(i.is_id for i in self.items)
|
|
57
60
|
|
|
58
|
-
def
|
|
59
|
-
|
|
61
|
+
def add(self, item: _SqlField) -> Self:
|
|
62
|
+
self.items.append(self.validate(item))
|
|
60
63
|
|
|
61
|
-
|
|
62
|
-
return self.table.delete(item_id)
|
|
64
|
+
return self
|
|
63
65
|
|
|
64
|
-
def
|
|
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.")
|
|
66
69
|
|
|
67
|
-
|
|
68
|
-
|
|
70
|
+
if item.is_parent and any(i.is_parent for i in self.items):
|
|
71
|
+
raise ValueError("Pass only one parent key.")
|
|
69
72
|
|
|
70
|
-
|
|
71
|
-
|
|
73
|
+
if not item.is_filter and isinstance(item.fixed_value, NotNone):
|
|
74
|
+
raise ValueError("Only filter fields can be 'not null'.")
|
|
72
75
|
|
|
73
|
-
|
|
74
|
-
return self.table.exists(duplicate)
|
|
76
|
+
return item
|
|
75
77
|
|
|
76
78
|
|
|
77
79
|
@dataclass(frozen=True)
|
|
78
80
|
class MsSqlTableBuilder[T]:
|
|
79
|
-
username: str
|
|
80
|
-
schema: str
|
|
81
|
-
table: str
|
|
82
|
-
|
|
83
|
-
fields:
|
|
84
|
-
custom_filters: list[str]
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
81
|
+
username: str = ""
|
|
82
|
+
schema: str = ""
|
|
83
|
+
table: str = ""
|
|
84
|
+
|
|
85
|
+
fields: SqlFieldSet = field(default_factory=SqlFieldSet)
|
|
86
|
+
custom_filters: list[str] = field(default_factory=list)
|
|
87
|
+
|
|
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
|
+
def with_schema(self, value: str) -> Self:
|
|
95
|
+
return replace(self, schema=value)
|
|
96
|
+
|
|
97
|
+
def with_table(self, value: str) -> Self:
|
|
98
|
+
return replace(self, table=value)
|
|
99
|
+
|
|
100
|
+
@deprecated("Use .build().with_a(formatter) instead")
|
|
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:
|
|
112
|
+
return self.with_a(sql_field=sql_field)
|
|
113
|
+
|
|
114
|
+
def with_a(self, sql_field: _SqlField) -> Self:
|
|
115
|
+
self.fields.add(sql_field)
|
|
116
|
+
|
|
117
|
+
return self
|
|
118
|
+
|
|
119
|
+
def with_custom_filters(self, filters: Iterable[str]) -> Self:
|
|
120
|
+
return replace(self, custom_filters=list(filters))
|
|
121
|
+
|
|
122
|
+
def build(self) -> AutoMsSqlTable[T] | AsUser[T]:
|
|
123
|
+
if not self.fields.contains_id():
|
|
124
|
+
raise ValueError("Id field is not provided!")
|
|
125
|
+
|
|
126
|
+
if not self.schema or not self.table:
|
|
127
|
+
raise ValueError("Cannot build sql table.")
|
|
128
|
+
|
|
129
|
+
fields = (
|
|
130
|
+
SqlFieldManager.Builder()
|
|
131
|
+
.with_fields(list(self.fields))
|
|
132
|
+
.with_custom_filters(list(self.custom_filters))
|
|
133
|
+
.for_mssql()
|
|
134
|
+
.build()
|
|
94
135
|
)
|
|
95
136
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
137
|
+
return (
|
|
138
|
+
AutoMsSqlTable[T](
|
|
139
|
+
fields=fields,
|
|
140
|
+
generator=SqlTemplateGenerator(
|
|
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)
|
|
104
148
|
)
|
|
105
149
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
150
|
+
|
|
151
|
+
@dataclass(frozen=True, kw_only=True)
|
|
152
|
+
class AutoMsSqlTable[T](SqlTable[T]):
|
|
153
|
+
fields: SqlFieldManager
|
|
154
|
+
generator: SqlTemplateGenerator
|
|
155
|
+
|
|
156
|
+
formatter: Formatter[Mapping[str, Any], T] = field(default_factory=NoFormatter)
|
|
157
|
+
|
|
158
|
+
def with_a(self, formatter: Formatter[Mapping[str, Any], T]) -> AutoMsSqlTable[T]:
|
|
159
|
+
return replace(self, formatter=formatter)
|
|
160
|
+
|
|
161
|
+
def for_a(self, username: str) -> AutoMsSqlTable[T] | AsUser[T]:
|
|
162
|
+
if not username:
|
|
163
|
+
return self
|
|
164
|
+
|
|
165
|
+
return AsUser(user=username, inner=self)
|
|
166
|
+
|
|
167
|
+
def count_all(self) -> DatabaseCommand:
|
|
168
|
+
return DatabaseCommand(
|
|
169
|
+
value=self.generator.count_all(),
|
|
170
|
+
payload=self.fields.with_fixed({}),
|
|
114
171
|
)
|
|
115
172
|
|
|
116
|
-
def
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
self.username,
|
|
121
|
-
self.schema,
|
|
122
|
-
self.table,
|
|
123
|
-
value,
|
|
124
|
-
self.fields,
|
|
125
|
-
self.custom_filters,
|
|
173
|
+
def insert(self, item: T) -> DatabaseCommand:
|
|
174
|
+
return DatabaseCommand(
|
|
175
|
+
value=self.generator.insert(),
|
|
176
|
+
payload=self.fields.with_fixed(self.formatter.dump(item)),
|
|
126
177
|
)
|
|
127
178
|
|
|
128
|
-
def
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
if len([key for key in key_list if key.is_parent]) > 1:
|
|
133
|
-
raise ValueError("Pass only one parent key.")
|
|
134
|
-
if (
|
|
135
|
-
len(
|
|
136
|
-
[
|
|
137
|
-
key
|
|
138
|
-
for key in key_list
|
|
139
|
-
if not key.is_filter and isinstance(key.fixed_value, NotNone)
|
|
140
|
-
]
|
|
141
|
-
)
|
|
142
|
-
> 0
|
|
143
|
-
):
|
|
144
|
-
raise ValueError("Only filter fields can be 'not null'.")
|
|
145
|
-
return MsSqlTableBuilder[T](
|
|
146
|
-
self.username,
|
|
147
|
-
self.schema,
|
|
148
|
-
self.table,
|
|
149
|
-
self.formatter,
|
|
150
|
-
key_list,
|
|
151
|
-
self.custom_filters,
|
|
179
|
+
def select(self, item_id: str) -> DatabaseCommand:
|
|
180
|
+
return DatabaseCommand(
|
|
181
|
+
value=self.generator.select(),
|
|
182
|
+
payload=self.fields.with_fixed({self.fields.id: item_id}),
|
|
152
183
|
)
|
|
153
184
|
|
|
154
|
-
def
|
|
155
|
-
return
|
|
156
|
-
self.
|
|
157
|
-
self.
|
|
158
|
-
self.table,
|
|
159
|
-
self.formatter,
|
|
160
|
-
self.fields,
|
|
161
|
-
list(filters),
|
|
185
|
+
def select_all(self) -> DatabaseCommand:
|
|
186
|
+
return DatabaseCommand(
|
|
187
|
+
value=self.generator.select_all(),
|
|
188
|
+
payload=self.fields.with_fixed({}),
|
|
162
189
|
)
|
|
163
190
|
|
|
164
|
-
def
|
|
165
|
-
|
|
166
|
-
|
|
191
|
+
def update(self, item: T) -> DatabaseCommand:
|
|
192
|
+
return DatabaseCommand(
|
|
193
|
+
value=self.generator.update(),
|
|
194
|
+
payload=self.fields.with_fixed(self.formatter.dump(item)),
|
|
195
|
+
)
|
|
167
196
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
197
|
+
def delete(self, item_id: str) -> DatabaseCommand:
|
|
198
|
+
return DatabaseCommand(
|
|
199
|
+
value=self.generator.delete(),
|
|
200
|
+
payload=self.fields.with_fixed({self.fields.id: item_id}),
|
|
201
|
+
)
|
|
171
202
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
self.
|
|
175
|
-
self.
|
|
176
|
-
field_manager.for_mssql().build(),
|
|
177
|
-
self.username,
|
|
203
|
+
def delete_all(self) -> DatabaseCommand:
|
|
204
|
+
return DatabaseCommand(
|
|
205
|
+
value=self.generator.delete_all(),
|
|
206
|
+
payload=self.fields.with_fixed({}),
|
|
178
207
|
)
|
|
179
208
|
|
|
209
|
+
def load(self, data: Mapping[str, Any]) -> T:
|
|
210
|
+
return self.formatter.load(data)
|
|
180
211
|
|
|
181
|
-
|
|
182
|
-
|
|
212
|
+
def exists(self, duplicate: T) -> ExistsError:
|
|
213
|
+
raw = self.formatter.dump(duplicate)
|
|
214
|
+
return ExistsError(duplicate).with_duplicate(
|
|
215
|
+
lambda _: f"{self.fields.id}<{raw[self.fields.id]}>"
|
|
216
|
+
)
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
@dataclass(frozen=True, kw_only=True)
|
|
220
|
+
class SqlTemplateGenerator:
|
|
183
221
|
schema: str
|
|
184
222
|
table: str
|
|
185
|
-
formatter: Formatter[Mapping[str, Any], T]
|
|
186
223
|
fields: SqlFieldManager
|
|
187
|
-
username: str | None = None
|
|
188
224
|
|
|
189
|
-
def count_all(self) ->
|
|
190
|
-
return
|
|
191
|
-
{self._user_check}
|
|
225
|
+
def count_all(self) -> str:
|
|
226
|
+
return f"""
|
|
192
227
|
SELECT count(*) AS n_items
|
|
193
228
|
FROM [{self.schema}].[{self.table}]
|
|
194
229
|
{self.fields.where_statement(include_id=False)}
|
|
195
230
|
REVERT
|
|
196
|
-
"""
|
|
231
|
+
"""
|
|
197
232
|
|
|
198
|
-
def insert(self
|
|
233
|
+
def insert(self) -> str:
|
|
199
234
|
columns = ", ".join(
|
|
200
235
|
["[" + field.name + "]" for field in self.fields if field.include_in_insert]
|
|
201
236
|
)
|
|
@@ -217,8 +252,7 @@ class DefaultSqlTable[T](SqlTable[T]):
|
|
|
217
252
|
)
|
|
218
253
|
where_statement = ""
|
|
219
254
|
|
|
220
|
-
return
|
|
221
|
-
{self._user_check}
|
|
255
|
+
return f"""
|
|
222
256
|
INSERT INTO [{self.schema}].[{self.table}] (
|
|
223
257
|
{columns}
|
|
224
258
|
)
|
|
@@ -229,34 +263,32 @@ class DefaultSqlTable[T](SqlTable[T]):
|
|
|
229
263
|
{output}
|
|
230
264
|
{where_statement}
|
|
231
265
|
REVERT
|
|
232
|
-
"""
|
|
266
|
+
"""
|
|
233
267
|
|
|
234
|
-
def select(self
|
|
268
|
+
def select(self) -> str:
|
|
235
269
|
columns = ", ".join(["[" + field.name + "]" for field in self.fields])
|
|
236
270
|
|
|
237
|
-
return
|
|
238
|
-
{self._user_check}
|
|
271
|
+
return f"""
|
|
239
272
|
SELECT
|
|
240
273
|
{columns}
|
|
241
274
|
FROM [{self.schema}].[{self.table}]
|
|
242
275
|
{self.fields.where_statement(include_id=True)}
|
|
243
276
|
REVERT
|
|
244
|
-
"""
|
|
277
|
+
"""
|
|
245
278
|
|
|
246
|
-
def select_all(self) ->
|
|
279
|
+
def select_all(self) -> str:
|
|
247
280
|
columns = ", ".join(["[" + field.name + "]" for field in self.fields])
|
|
248
281
|
|
|
249
|
-
return
|
|
250
|
-
{self._user_check}
|
|
282
|
+
return f"""
|
|
251
283
|
SELECT
|
|
252
284
|
{columns}
|
|
253
285
|
FROM [{self.schema}].[{self.table}]
|
|
254
286
|
{self.fields.where_statement(include_id=False)}
|
|
255
287
|
{self.fields.order}
|
|
256
288
|
REVERT
|
|
257
|
-
"""
|
|
289
|
+
"""
|
|
258
290
|
|
|
259
|
-
def update(self
|
|
291
|
+
def update(self) -> str:
|
|
260
292
|
updates = ", ".join(
|
|
261
293
|
[
|
|
262
294
|
f"{field.name} = %({field.name})s"
|
|
@@ -265,44 +297,81 @@ class DefaultSqlTable[T](SqlTable[T]):
|
|
|
265
297
|
]
|
|
266
298
|
)
|
|
267
299
|
|
|
268
|
-
return
|
|
269
|
-
{self._user_check}
|
|
300
|
+
return f"""
|
|
270
301
|
UPDATE [{self.schema}].[{self.table}]
|
|
271
302
|
SET
|
|
272
303
|
{updates}
|
|
273
304
|
{self.fields.where_statement(include_id=True)}
|
|
274
305
|
REVERT
|
|
275
|
-
"""
|
|
306
|
+
"""
|
|
276
307
|
|
|
277
|
-
def delete(self
|
|
278
|
-
return
|
|
279
|
-
{self._user_check}
|
|
308
|
+
def delete(self) -> str:
|
|
309
|
+
return f"""
|
|
280
310
|
DELETE
|
|
281
311
|
FROM [{self.schema}].[{self.table}]
|
|
282
312
|
{self.fields.where_statement(include_id=True)}
|
|
283
313
|
REVERT
|
|
284
|
-
"""
|
|
314
|
+
"""
|
|
285
315
|
|
|
286
|
-
def delete_all(self) ->
|
|
287
|
-
return
|
|
288
|
-
{self._user_check}
|
|
316
|
+
def delete_all(self) -> str:
|
|
317
|
+
return f"""
|
|
289
318
|
DELETE
|
|
290
319
|
FROM [{self.schema}].[{self.table}]
|
|
291
320
|
{self.fields.where_statement(include_id=False)}
|
|
292
321
|
REVERT
|
|
293
|
-
"""
|
|
322
|
+
"""
|
|
294
323
|
|
|
295
|
-
def load(self, data: Mapping[str, Any]) -> T:
|
|
296
|
-
return self.formatter.load(data)
|
|
297
324
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
325
|
+
@dataclass(frozen=True, kw_only=True)
|
|
326
|
+
class AsUser[T](SqlTable[T]):
|
|
327
|
+
user: str
|
|
328
|
+
|
|
329
|
+
inner: AutoMsSqlTable[T]
|
|
330
|
+
|
|
331
|
+
def __post_init__(self) -> None:
|
|
332
|
+
if not self.user:
|
|
333
|
+
raise ValueError("username must be provided")
|
|
334
|
+
|
|
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
|
+
def _wrap(self, command: DatabaseCommand) -> DatabaseCommand:
|
|
342
|
+
return DatabaseCommand(
|
|
343
|
+
"\n".join(
|
|
344
|
+
[
|
|
345
|
+
f"EXECUTE AS USER = '{self.user}'",
|
|
346
|
+
dedent(command.value),
|
|
347
|
+
]
|
|
348
|
+
),
|
|
349
|
+
payload=command.payload,
|
|
302
350
|
)
|
|
303
351
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
return
|
|
352
|
+
def count_all(self) -> DatabaseCommand:
|
|
353
|
+
return self._wrap(self.inner.count_all())
|
|
354
|
+
|
|
355
|
+
def insert(self, item: T) -> DatabaseCommand:
|
|
356
|
+
return self._wrap(self.inner.insert(item))
|
|
357
|
+
|
|
358
|
+
def select(self, item_id: str) -> DatabaseCommand:
|
|
359
|
+
return self._wrap(self.inner.select(item_id))
|
|
360
|
+
|
|
361
|
+
def select_all(self) -> DatabaseCommand:
|
|
362
|
+
return self._wrap(self.inner.select_all())
|
|
363
|
+
|
|
364
|
+
def update(self, item: T) -> DatabaseCommand:
|
|
365
|
+
return self._wrap(self.inner.update(item))
|
|
366
|
+
|
|
367
|
+
def delete(self, item_id: str) -> DatabaseCommand:
|
|
368
|
+
return self._wrap(self.inner.delete(item_id))
|
|
369
|
+
|
|
370
|
+
def delete_all(self) -> DatabaseCommand:
|
|
371
|
+
return self._wrap(self.inner.delete_all())
|
|
372
|
+
|
|
373
|
+
def load(self, data: Mapping[str, Any]) -> T:
|
|
374
|
+
return self.inner.load(data)
|
|
375
|
+
|
|
376
|
+
def exists(self, duplicate: T) -> ExistsError:
|
|
377
|
+
return self.inner.exists(duplicate)
|
|
@@ -40,7 +40,7 @@ class SqliteRepository(ContainsMixin, Repository[ItemT]):
|
|
|
40
40
|
item = self.table.load(
|
|
41
41
|
self.db.execute(self.table.select_duplicate(item)).fetch_one()
|
|
42
42
|
)
|
|
43
|
-
self.table.
|
|
43
|
+
self.table.exists(item).fire()
|
|
44
44
|
return item
|
|
45
45
|
|
|
46
46
|
def read(self, item_id: str) -> ItemT:
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
-
from dataclasses import dataclass
|
|
4
|
-
from typing import TYPE_CHECKING, Any
|
|
3
|
+
from dataclasses import dataclass, replace
|
|
4
|
+
from typing import TYPE_CHECKING, Any, Self
|
|
5
5
|
|
|
6
6
|
from apexdevkit.error import ExistsError
|
|
7
7
|
from apexdevkit.repository.core import DatabaseCommand
|
|
@@ -41,8 +41,8 @@ class SqlTable[T]: # pragma: no cover
|
|
|
41
41
|
def load(self, data: Mapping[str, Any]) -> T:
|
|
42
42
|
raise NotImplementedError
|
|
43
43
|
|
|
44
|
-
def
|
|
45
|
-
return ExistsError(
|
|
44
|
+
def exists(self, duplicate: T) -> ExistsError:
|
|
45
|
+
return ExistsError(duplicate).with_duplicate(lambda _: "Unknown")
|
|
46
46
|
|
|
47
47
|
|
|
48
48
|
@dataclass(frozen=True)
|
|
@@ -52,25 +52,13 @@ class SqliteTableBuilder[T]:
|
|
|
52
52
|
fields: list[_SqlField] | None = None
|
|
53
53
|
custom_filters: list[str] | None = None
|
|
54
54
|
|
|
55
|
-
def with_name(self, value: str) ->
|
|
56
|
-
return
|
|
57
|
-
value,
|
|
58
|
-
self.formatter,
|
|
59
|
-
self.fields,
|
|
60
|
-
self.custom_filters,
|
|
61
|
-
)
|
|
55
|
+
def with_name(self, value: str) -> Self:
|
|
56
|
+
return replace(self, table_name=value)
|
|
62
57
|
|
|
63
|
-
def with_formatter(
|
|
64
|
-
self, value
|
|
65
|
-
) -> SqliteTableBuilder[T]:
|
|
66
|
-
return SqliteTableBuilder[T](
|
|
67
|
-
self.table_name,
|
|
68
|
-
value,
|
|
69
|
-
self.fields,
|
|
70
|
-
self.custom_filters,
|
|
71
|
-
)
|
|
58
|
+
def with_formatter(self, value: Formatter[Mapping[str, Any], T]) -> Self:
|
|
59
|
+
return replace(self, formatter=value)
|
|
72
60
|
|
|
73
|
-
def with_fields(self, value: Iterable[_SqlField]) ->
|
|
61
|
+
def with_fields(self, value: Iterable[_SqlField]) -> Self:
|
|
74
62
|
key_list = list(value)
|
|
75
63
|
if len([key for key in key_list if key.is_id]) != 1:
|
|
76
64
|
raise ValueError("Pass only one identifier key.")
|
|
@@ -87,20 +75,11 @@ class SqliteTableBuilder[T]:
|
|
|
87
75
|
> 0
|
|
88
76
|
):
|
|
89
77
|
raise ValueError("Only filter fields can be 'not null'.")
|
|
90
|
-
return SqliteTableBuilder[T](
|
|
91
|
-
self.table_name,
|
|
92
|
-
self.formatter,
|
|
93
|
-
key_list,
|
|
94
|
-
self.custom_filters,
|
|
95
|
-
)
|
|
96
78
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
self.fields,
|
|
102
|
-
list(filters),
|
|
103
|
-
)
|
|
79
|
+
return replace(self, fields=key_list)
|
|
80
|
+
|
|
81
|
+
def with_custom_filters(self, filters: Iterable[str]) -> Self:
|
|
82
|
+
return replace(self, custom_filters=list(filters))
|
|
104
83
|
|
|
105
84
|
def build(self) -> SqlTable[T]:
|
|
106
85
|
if not self.table_name or not self.formatter or not self.fields:
|
|
@@ -217,7 +196,7 @@ class _DefaultSqlTable[T](SqlTable[T]):
|
|
|
217
196
|
def load(self, data: Mapping[str, Any]) -> T:
|
|
218
197
|
return self.formatter.load(data)
|
|
219
198
|
|
|
220
|
-
def
|
|
199
|
+
def exists(self, item: T) -> ExistsError:
|
|
221
200
|
raw = self.formatter.dump(item)
|
|
222
201
|
return ExistsError(item).with_duplicate(
|
|
223
202
|
lambda _: ",".join(
|
|
@@ -34,13 +34,13 @@ apexdevkit/repository/sql/connector.py,sha256=0DacXgPdjR405YiKLXSaHDsLnf5TZmAOo5
|
|
|
34
34
|
apexdevkit/repository/sql/field.py,sha256=IgzHXIHyi6zNDzbmcT1rc7ZXUCp8jfwlCOvtCtcSejA,9221
|
|
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=6LKY6sgOjC7Zh1Npa2U96A3BF7EjTNd_huRZ0BATfVk,11547
|
|
38
38
|
apexdevkit/repository/sql/sqlite/__init__.py,sha256=dG08No5TFPL4P2ymZkVBgDxxBq-UPcCUNIZVr_JOmXg,178
|
|
39
|
-
apexdevkit/repository/sql/sqlite/repository.py,sha256=
|
|
40
|
-
apexdevkit/repository/sql/sqlite/table.py,sha256=
|
|
39
|
+
apexdevkit/repository/sql/sqlite/repository.py,sha256=QmsOlMcVKYhR9j-IFdPSSV1A0GVNw3lqV7faXhRuoFw,2173
|
|
40
|
+
apexdevkit/repository/sql/sqlite/table.py,sha256=OsuEJm1Oyo6q_NF6ru-tjHlcwtVB6O2Z_anRi2Xgttw,7049
|
|
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.3.dist-info/METADATA,sha256=Bjz6-PIZW7cHZrxaU8OWqS8c-LBGwiN4tSNU4vmSi7o,616
|
|
44
|
+
apexdevkit-2.4.3.dist-info/WHEEL,sha256=L-WvLdSBvJWHXv5zrQxDvNRnIgoBzLUpcbuiw19KCuQ,88
|
|
45
|
+
apexdevkit-2.4.3.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
|
|
46
|
+
apexdevkit-2.4.3.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|