dapper-sqls 1.1.3__py3-none-any.whl → 1.2.1__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.
- dapper_sqls/__init__.py +3 -1
- dapper_sqls/_types.py +25 -2
- dapper_sqls/async_dapper/async_executors.py +127 -52
- dapper_sqls/builders/model/model.py +418 -33
- dapper_sqls/builders/model/utils.py +334 -42
- dapper_sqls/builders/query.py +164 -43
- dapper_sqls/builders/stored.py +15 -5
- dapper_sqls/builders/stp.py +6 -2
- dapper_sqls/config.py +40 -31
- dapper_sqls/dapper/executors.py +130 -55
- dapper_sqls/http/__init__.py +4 -0
- dapper_sqls/http/aiohttp.py +155 -0
- dapper_sqls/http/decorators.py +123 -0
- dapper_sqls/http/models.py +58 -0
- dapper_sqls/http/request.py +140 -0
- dapper_sqls/models/__init__.py +3 -5
- dapper_sqls/models/base.py +246 -20
- dapper_sqls/models/connection.py +1 -1
- dapper_sqls/models/query_field.py +214 -0
- dapper_sqls/models/result.py +314 -44
- dapper_sqls/sqlite/__init__.py +1 -0
- dapper_sqls/sqlite/async_local_database.py +69 -5
- dapper_sqls/sqlite/decorators.py +69 -0
- dapper_sqls/sqlite/installer.py +8 -4
- dapper_sqls/sqlite/local_database.py +39 -5
- dapper_sqls/sqlite/models.py +25 -1
- dapper_sqls/sqlite/utils.py +2 -1
- dapper_sqls/utils.py +16 -4
- dapper_sqls-1.2.1.dist-info/METADATA +41 -0
- dapper_sqls-1.2.1.dist-info/RECORD +40 -0
- {dapper_sqls-1.1.3.dist-info → dapper_sqls-1.2.1.dist-info}/WHEEL +1 -1
- dapper_sqls-1.1.3.dist-info/METADATA +0 -10
- dapper_sqls-1.1.3.dist-info/RECORD +0 -33
- {dapper_sqls-1.1.3.dist-info → dapper_sqls-1.2.1.dist-info}/top_level.txt +0 -0
@@ -2,7 +2,10 @@
|
|
2
2
|
|
3
3
|
from itertools import groupby
|
4
4
|
import os
|
5
|
-
from .utils import create_content_orm,
|
5
|
+
from .utils import (create_content_orm, TableInformation, ColumnInformation, InformationSchemaRoutines, create_field, create_content_async_orm,
|
6
|
+
create_params_routine, get_parameters_with_defaults, create_queue_update, create_table_description, create_arg,
|
7
|
+
SqlTable)
|
8
|
+
from ...models import TableBaseModel
|
6
9
|
|
7
10
|
class TableBuilderData:
|
8
11
|
def __init__(self, table_schema : str, table_name : str, class_name : str, model : str, orm : str | None, async_orm : str | None):
|
@@ -41,13 +44,253 @@ class ModelBuilder(object):
|
|
41
44
|
|
42
45
|
def __init__(self, dapper):
|
43
46
|
self._dapper = dapper
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
47
|
+
|
48
|
+
self.query_columns = """
|
49
|
+
SELECT
|
50
|
+
c.TABLE_CATALOG,
|
51
|
+
c.TABLE_SCHEMA,
|
52
|
+
c.TABLE_NAME,
|
53
|
+
c.COLUMN_NAME,
|
54
|
+
c.DATA_TYPE,
|
55
|
+
c.CHARACTER_MAXIMUM_LENGTH,
|
56
|
+
c.IS_NULLABLE,
|
57
|
+
|
58
|
+
-- Coluna separada: IS_IDENTITY
|
59
|
+
CASE
|
60
|
+
WHEN COLUMNPROPERTY(OBJECT_ID(c.TABLE_SCHEMA + '.' + c.TABLE_NAME), c.COLUMN_NAME, 'IsIdentity') = 1
|
61
|
+
THEN 'YES'
|
62
|
+
ELSE 'NO'
|
63
|
+
END AS IS_IDENTITY,
|
64
|
+
|
65
|
+
CASE
|
66
|
+
WHEN i.is_primary_key = 1 THEN 'YES'
|
67
|
+
ELSE 'NO'
|
68
|
+
END AS IS_PRIMARY_KEY,
|
69
|
+
|
70
|
+
CASE
|
71
|
+
WHEN i.is_unique = 1 THEN 'YES'
|
72
|
+
ELSE 'NO'
|
73
|
+
END AS IS_UNIQUE,
|
74
|
+
|
75
|
+
-- Auto_Description como string JSON
|
76
|
+
CONCAT(
|
77
|
+
'{',
|
78
|
+
'"type": "',
|
79
|
+
c.DATA_TYPE COLLATE SQL_Latin1_General_CP1_CI_AS,
|
80
|
+
CASE
|
81
|
+
WHEN c.CHARACTER_MAXIMUM_LENGTH IS NOT NULL
|
82
|
+
THEN CONCAT('(',
|
83
|
+
CASE
|
84
|
+
WHEN c.CHARACTER_MAXIMUM_LENGTH = -1
|
85
|
+
THEN 'MAX'
|
86
|
+
ELSE CAST(c.CHARACTER_MAXIMUM_LENGTH AS VARCHAR)
|
87
|
+
END,
|
88
|
+
')')
|
89
|
+
ELSE ''
|
90
|
+
END,
|
91
|
+
'", ',
|
92
|
+
'"nullable": ', CASE WHEN c.IS_NULLABLE = 'YES' THEN 'true' ELSE 'false' END, ', ',
|
93
|
+
'"identity": ', CASE WHEN COLUMNPROPERTY(OBJECT_ID(c.TABLE_SCHEMA + '.' + c.TABLE_NAME), c.COLUMN_NAME, 'IsIdentity') = 1 THEN 'true' ELSE 'false' END, ', ',
|
94
|
+
'"default": ',
|
95
|
+
CASE
|
96
|
+
WHEN dc.definition IS NOT NULL
|
97
|
+
THEN '"' + REPLACE(REPLACE(dc.definition, '"', '\"'), '''', '''') + '"'
|
98
|
+
ELSE 'null'
|
99
|
+
END, ', ',
|
100
|
+
'"primary_key": ', CASE WHEN i.is_primary_key = 1 THEN 'true' ELSE 'false' END, ', ',
|
101
|
+
'"unique": ', CASE WHEN i.is_unique = 1 THEN 'true' ELSE 'false' END, ', ',
|
102
|
+
'"foreign_key": ',
|
103
|
+
CASE
|
104
|
+
WHEN fk_constraint.object_id IS NOT NULL THEN CONCAT(
|
105
|
+
'{',
|
106
|
+
'"name": "', fk_constraint.name COLLATE SQL_Latin1_General_CP1_CI_AS, '", ',
|
107
|
+
'"ref_table": "', ref_schema.name COLLATE SQL_Latin1_General_CP1_CI_AS, '.', ref_table.name COLLATE SQL_Latin1_General_CP1_CI_AS, '", ',
|
108
|
+
'"ref_column": "', ref_column.name COLLATE SQL_Latin1_General_CP1_CI_AS, '", ',
|
109
|
+
'"on_delete": "', fk_constraint.delete_referential_action_desc COLLATE SQL_Latin1_General_CP1_CI_AS, '", ',
|
110
|
+
'"on_update": "', fk_constraint.update_referential_action_desc COLLATE SQL_Latin1_General_CP1_CI_AS, '"',
|
111
|
+
'}'
|
112
|
+
)
|
113
|
+
ELSE 'false'
|
114
|
+
END,
|
115
|
+
'}'
|
116
|
+
) AS AUTO_COLUMN_DESCRIPTION,
|
117
|
+
|
118
|
+
col_ep.value AS COLUMN_DESCRIPTION
|
119
|
+
|
120
|
+
FROM
|
121
|
+
INFORMATION_SCHEMA.COLUMNS c
|
122
|
+
JOIN
|
123
|
+
(SELECT TABLE_NAME, TABLE_SCHEMA, TABLE_CATALOG FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE') t
|
124
|
+
ON c.TABLE_NAME = t.TABLE_NAME AND c.TABLE_SCHEMA = t.TABLE_SCHEMA
|
125
|
+
|
126
|
+
LEFT JOIN
|
127
|
+
sys.columns sc
|
128
|
+
ON sc.object_id = OBJECT_ID(c.TABLE_SCHEMA + '.' + c.TABLE_NAME)
|
129
|
+
AND sc.name = c.COLUMN_NAME
|
130
|
+
|
131
|
+
LEFT JOIN
|
132
|
+
sys.default_constraints dc
|
133
|
+
ON dc.parent_object_id = sc.object_id
|
134
|
+
AND dc.parent_column_id = sc.column_id
|
135
|
+
|
136
|
+
LEFT JOIN
|
137
|
+
sys.extended_properties col_ep
|
138
|
+
ON OBJECT_ID(c.TABLE_SCHEMA + '.' + c.TABLE_NAME) = col_ep.major_id
|
139
|
+
AND COLUMNPROPERTY(OBJECT_ID(c.TABLE_SCHEMA + '.' + c.TABLE_NAME), c.COLUMN_NAME, 'ColumnId') = col_ep.minor_id
|
140
|
+
AND col_ep.name = 'MS_Description'
|
141
|
+
|
142
|
+
LEFT JOIN
|
143
|
+
sys.index_columns ic
|
144
|
+
ON ic.object_id = sc.object_id AND ic.column_id = sc.column_id
|
145
|
+
|
146
|
+
LEFT JOIN
|
147
|
+
sys.indexes i
|
148
|
+
ON i.object_id = ic.object_id AND i.index_id = ic.index_id
|
149
|
+
|
150
|
+
LEFT JOIN
|
151
|
+
sys.foreign_key_columns fkc
|
152
|
+
ON fkc.parent_object_id = sc.object_id AND fkc.parent_column_id = sc.column_id
|
153
|
+
|
154
|
+
LEFT JOIN
|
155
|
+
sys.foreign_keys fk_constraint
|
156
|
+
ON fk_constraint.object_id = fkc.constraint_object_id
|
157
|
+
|
158
|
+
LEFT JOIN
|
159
|
+
sys.tables ref_table
|
160
|
+
ON ref_table.object_id = fkc.referenced_object_id
|
161
|
+
|
162
|
+
LEFT JOIN
|
163
|
+
sys.schemas ref_schema
|
164
|
+
ON ref_schema.schema_id = ref_table.schema_id
|
165
|
+
|
166
|
+
LEFT JOIN
|
167
|
+
sys.columns ref_column
|
168
|
+
ON ref_column.column_id = fkc.referenced_column_id AND ref_column.object_id = ref_table.object_id
|
169
|
+
"""
|
170
|
+
|
171
|
+
self.query_tables = """
|
172
|
+
WITH
|
173
|
+
col_data AS (
|
174
|
+
SELECT
|
175
|
+
c.object_id,
|
176
|
+
COUNT(*) AS column_count,
|
177
|
+
STRING_AGG('"' + c.name + '"' COLLATE DATABASE_DEFAULT, ',' )
|
178
|
+
WITHIN GROUP (ORDER BY c.column_id) AS identity_list
|
179
|
+
FROM sys.columns c
|
180
|
+
WHERE c.is_identity = 1
|
181
|
+
GROUP BY c.object_id
|
182
|
+
),
|
183
|
+
pk_data AS (
|
184
|
+
SELECT
|
185
|
+
i.object_id,
|
186
|
+
CONCAT(
|
187
|
+
'{',
|
188
|
+
'"name":"', i.name COLLATE DATABASE_DEFAULT, '",',
|
189
|
+
'"columns":[', STRING_AGG('"' + c.name + '"' COLLATE DATABASE_DEFAULT, ',')
|
190
|
+
WITHIN GROUP (ORDER BY ic.key_ordinal), ']',
|
191
|
+
'}'
|
192
|
+
) AS pk_json
|
193
|
+
FROM sys.indexes i
|
194
|
+
JOIN sys.index_columns ic
|
195
|
+
ON ic.object_id = i.object_id AND ic.index_id = i.index_id
|
196
|
+
JOIN sys.columns c
|
197
|
+
ON c.object_id = ic.object_id AND c.column_id = ic.column_id
|
198
|
+
WHERE i.is_primary_key = 1
|
199
|
+
GROUP BY i.object_id, i.name
|
200
|
+
),
|
201
|
+
UniqueIndexColumns AS (
|
202
|
+
SELECT
|
203
|
+
i.object_id,
|
204
|
+
i.name COLLATE DATABASE_DEFAULT AS index_name,
|
205
|
+
c.name COLLATE DATABASE_DEFAULT AS column_name,
|
206
|
+
ic.key_ordinal
|
207
|
+
FROM sys.indexes i
|
208
|
+
JOIN sys.index_columns ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id
|
209
|
+
JOIN sys.columns c ON c.object_id = ic.object_id AND c.column_id = ic.column_id
|
210
|
+
WHERE i.is_unique = 1 AND i.is_primary_key = 0
|
211
|
+
),
|
212
|
+
UniqueColumnsByIndex AS (
|
213
|
+
SELECT
|
214
|
+
object_id,
|
215
|
+
index_name,
|
216
|
+
STRING_AGG('"' + column_name + '"' COLLATE DATABASE_DEFAULT, ',')
|
217
|
+
WITHIN GROUP (ORDER BY key_ordinal) AS columns_json
|
218
|
+
FROM UniqueIndexColumns
|
219
|
+
GROUP BY object_id, index_name
|
220
|
+
),
|
221
|
+
UniqueConstraintsJSON AS (
|
222
|
+
SELECT
|
223
|
+
object_id,
|
224
|
+
'[' + STRING_AGG(
|
225
|
+
CONCAT(
|
226
|
+
'{',
|
227
|
+
'"name":"', index_name, '",',
|
228
|
+
'"columns":[', columns_json, ']',
|
229
|
+
'}'
|
230
|
+
) COLLATE DATABASE_DEFAULT, ','
|
231
|
+
) + ']' AS unique_json
|
232
|
+
FROM UniqueColumnsByIndex
|
233
|
+
GROUP BY object_id
|
234
|
+
),
|
235
|
+
ForeignKeysJSON AS (
|
236
|
+
SELECT
|
237
|
+
fkc.parent_object_id,
|
238
|
+
'[' + STRING_AGG(
|
239
|
+
CONCAT(
|
240
|
+
'{',
|
241
|
+
'"name":"', fk.name COLLATE DATABASE_DEFAULT, '",',
|
242
|
+
'"column":"', parent_col.name COLLATE DATABASE_DEFAULT, '",',
|
243
|
+
'"ref_table":"', ref_schema.name COLLATE DATABASE_DEFAULT, '.', ref_table.name COLLATE DATABASE_DEFAULT, '",',
|
244
|
+
'"ref_column":"', ref_col.name COLLATE DATABASE_DEFAULT, '",',
|
245
|
+
'"on_delete":"', fk.delete_referential_action_desc COLLATE DATABASE_DEFAULT, '",',
|
246
|
+
'"on_update":"', fk.update_referential_action_desc COLLATE DATABASE_DEFAULT, '"',
|
247
|
+
'}'
|
248
|
+
) COLLATE DATABASE_DEFAULT, ','
|
249
|
+
) + ']' AS foreign_keys_json
|
250
|
+
FROM sys.foreign_keys fk
|
251
|
+
JOIN sys.foreign_key_columns fkc
|
252
|
+
ON fk.object_id = fkc.constraint_object_id
|
253
|
+
JOIN sys.columns parent_col
|
254
|
+
ON parent_col.object_id = fkc.parent_object_id
|
255
|
+
AND parent_col.column_id = fkc.parent_column_id
|
256
|
+
JOIN sys.tables ref_table
|
257
|
+
ON ref_table.object_id = fkc.referenced_object_id
|
258
|
+
JOIN sys.columns ref_col
|
259
|
+
ON ref_col.object_id = fkc.referenced_object_id
|
260
|
+
AND ref_col.column_id = fkc.referenced_column_id
|
261
|
+
JOIN sys.schemas ref_schema
|
262
|
+
ON ref_schema.schema_id = ref_table.schema_id
|
263
|
+
GROUP BY fkc.parent_object_id
|
264
|
+
)
|
265
|
+
|
266
|
+
SELECT
|
267
|
+
t.TABLE_CATALOG,
|
268
|
+
t.TABLE_SCHEMA,
|
269
|
+
t.TABLE_NAME,
|
270
|
+
tbl_ep.value AS TABLE_DESCRIPTION,
|
271
|
+
CONCAT(
|
272
|
+
'{',
|
273
|
+
'"columns": ', ISNULL(col_count.column_count, 0), ', ',
|
274
|
+
'"identity": [', ISNULL(col_count.identity_list, ''), '], ',
|
275
|
+
'"primary_keys": ', ISNULL(pk.pk_json, 'null'), ', ',
|
276
|
+
'"unique_constraints": ', ISNULL(uc.unique_json, '[]'), ', ',
|
277
|
+
'"foreign_keys": ', ISNULL(fk.foreign_keys_json, '[]'),
|
278
|
+
'}'
|
279
|
+
) AS AUTO_TABLE_DESCRIPTION
|
280
|
+
FROM INFORMATION_SCHEMA.TABLES t
|
281
|
+
LEFT JOIN sys.extended_properties tbl_ep
|
282
|
+
ON tbl_ep.major_id = OBJECT_ID(t.TABLE_SCHEMA + '.' + t.TABLE_NAME)
|
283
|
+
AND tbl_ep.minor_id = 0
|
284
|
+
AND tbl_ep.name = 'MS_Description'
|
285
|
+
LEFT JOIN col_data col_count
|
286
|
+
ON col_count.object_id = OBJECT_ID(t.TABLE_SCHEMA + '.' + t.TABLE_NAME)
|
287
|
+
LEFT JOIN pk_data pk
|
288
|
+
ON pk.object_id = OBJECT_ID(t.TABLE_SCHEMA + '.' + t.TABLE_NAME)
|
289
|
+
LEFT JOIN UniqueConstraintsJSON uc
|
290
|
+
ON uc.object_id = OBJECT_ID(t.TABLE_SCHEMA + '.' + t.TABLE_NAME)
|
291
|
+
LEFT JOIN ForeignKeysJSON fk
|
292
|
+
ON fk.parent_object_id = OBJECT_ID(t.TABLE_SCHEMA + '.' + t.TABLE_NAME)
|
293
|
+
WHERE t.TABLE_TYPE = 'BASE TABLE';
|
51
294
|
"""
|
52
295
|
|
53
296
|
self.query_routines = f"""
|
@@ -74,23 +317,48 @@ class ModelBuilder(object):
|
|
74
317
|
@property
|
75
318
|
def dapper(self):
|
76
319
|
return self._dapper
|
77
|
-
|
78
|
-
def
|
320
|
+
|
321
|
+
def get_tables_data(self):
|
79
322
|
with self.dapper.query() as db:
|
80
323
|
information_schema_tables = db.fetchall(self.query_tables)
|
81
324
|
if not information_schema_tables.success:
|
82
325
|
return False
|
83
|
-
information_schema_tables = self.dapper.load(
|
326
|
+
information_schema_tables : list[TableInformation] = self.dapper.load(TableInformation, information_schema_tables)
|
327
|
+
|
328
|
+
data : dict[str, TableInformation] = {}
|
329
|
+
for table in information_schema_tables:
|
330
|
+
if not table.TABLE_NAME.startswith('__'):
|
331
|
+
key = f"{table.TABLE_CATALOG}{table.TABLE_SCHEMA}{table.TABLE_NAME}"
|
332
|
+
data[key] = table
|
333
|
+
return data
|
334
|
+
|
335
|
+
def get_columns_data(self):
|
336
|
+
with self.dapper.query() as db:
|
337
|
+
information_schema_tables = db.fetchall(self.query_columns)
|
338
|
+
if not information_schema_tables.success:
|
339
|
+
return False
|
340
|
+
|
341
|
+
information_schema_tables = self.dapper.load(ColumnInformation, information_schema_tables)
|
84
342
|
information_schema_tables = [table for table in information_schema_tables if not table.TABLE_NAME.startswith('__')]
|
85
343
|
|
344
|
+
# ✅ Remover colunas duplicadas com base em (TABLE_NAME, COLUMN_NAME)
|
345
|
+
unique_columns = {}
|
346
|
+
for col in information_schema_tables:
|
347
|
+
key = (col.TABLE_NAME, col.COLUMN_NAME)
|
348
|
+
if key not in unique_columns:
|
349
|
+
unique_columns[key] = col
|
350
|
+
information_schema_tables = list(unique_columns.values())
|
351
|
+
|
352
|
+
# Agrupamento por tabela
|
86
353
|
information_schema_tables.sort(key=lambda x: x.TABLE_NAME)
|
87
|
-
grouped_data = groupby(information_schema_tables, lambda
|
88
|
-
grouped_list
|
354
|
+
grouped_data = groupby(information_schema_tables, lambda x: x.TABLE_NAME)
|
355
|
+
grouped_list: list[list[ColumnInformation]] = [[obj for obj in group] for _, group in grouped_data]
|
356
|
+
|
89
357
|
if not grouped_list:
|
90
358
|
return False
|
91
359
|
return grouped_list
|
92
360
|
|
93
|
-
def
|
361
|
+
def get_routines_data(self):
|
94
362
|
with self.dapper.query() as db:
|
95
363
|
information_schema_routines = db.fetchall(self.query_routines)
|
96
364
|
if not information_schema_routines:
|
@@ -103,6 +371,86 @@ class ModelBuilder(object):
|
|
103
371
|
if not grouped_list:
|
104
372
|
return []
|
105
373
|
return grouped_list
|
374
|
+
|
375
|
+
def get_models_db(self, tables : dict[str, SqlTable]):
|
376
|
+
models : list[TableBaseModel] = []
|
377
|
+
for table in tables.values():
|
378
|
+
if not table.available:
|
379
|
+
continue
|
380
|
+
models.append(self.get_model_db(table))
|
381
|
+
return models
|
382
|
+
|
383
|
+
def get_model_db(self, table : SqlTable):
|
384
|
+
|
385
|
+
from datetime import datetime
|
386
|
+
from pydantic import Field
|
387
|
+
from typing import Union, Optional, ClassVar, Set
|
388
|
+
from dapper_sqls import (TableBaseModel, StringQueryField, NumericQueryField, BoolQueryField, DateQueryField, BytesQueryField,
|
389
|
+
JoinNumericCondition, JoinStringCondition, JoinBooleanCondition, JoinDateCondition, JoinBytesCondition)
|
390
|
+
|
391
|
+
|
392
|
+
table_description = create_table_description(table)
|
393
|
+
|
394
|
+
table_name = table.TABLE_NAME
|
395
|
+
class_name = table_name
|
396
|
+
schema = table.TABLE_SCHEMA
|
397
|
+
|
398
|
+
fields = [create_field(row) for row in table.COLUMNS if row.available]
|
399
|
+
fields_str = "\n ".join(fields)
|
400
|
+
|
401
|
+
identities = {d.COLUMN_NAME for d in table.COLUMNS if d.IS_IDENTITY == "YES" and d.available}
|
402
|
+
|
403
|
+
primary_keys = {d.COLUMN_NAME for d in table.COLUMNS if d.IS_PRIMARY_KEY == "YES" and d.available}
|
404
|
+
|
405
|
+
optional_fields = {d.COLUMN_NAME for d in table.COLUMNS if d.IS_NULLABLE == "YES" and d.available}
|
406
|
+
|
407
|
+
max_length_fields = {
|
408
|
+
d.COLUMN_NAME: d.CHARACTER_MAXIMUM_LENGTH
|
409
|
+
for d in table.COLUMNS
|
410
|
+
if d.CHARACTER_MAXIMUM_LENGTH is not None and d.CHARACTER_MAXIMUM_LENGTH > 0 and d.available
|
411
|
+
}
|
412
|
+
|
413
|
+
table_alias = f'_alias_{table_name.lower()}_alias_'
|
414
|
+
|
415
|
+
content_model = f'''# coding: utf-8
|
416
|
+
class {class_name}(TableBaseModel):
|
417
|
+
TABLE_NAME: ClassVar[str] = '[{schema}].[{table_name}]'
|
418
|
+
|
419
|
+
TABLE_ALIAS: ClassVar[str] = '{table_alias}'
|
420
|
+
|
421
|
+
DESCRIPTION : ClassVar[str] = '{table_description}'
|
422
|
+
|
423
|
+
IDENTITIES : ClassVar[Set[str]] = {identities}
|
424
|
+
|
425
|
+
PRIMARY_KEYs : ClassVar[Set[str]] = {primary_keys}
|
426
|
+
|
427
|
+
OPTIONAL_FIELDS : ClassVar[Set[str]] = {optional_fields}
|
428
|
+
|
429
|
+
MAX_LENGTH_FIELDS: ClassVar[dict[str, int]] = {max_length_fields}
|
430
|
+
|
431
|
+
{fields_str}
|
432
|
+
'''
|
433
|
+
local_vars = {}
|
434
|
+
exec(content_model, {
|
435
|
+
"TableBaseModel": TableBaseModel,
|
436
|
+
"StringQueryField": StringQueryField,
|
437
|
+
"NumericQueryField": NumericQueryField,
|
438
|
+
"BoolQueryField": BoolQueryField,
|
439
|
+
"DateQueryField": DateQueryField,
|
440
|
+
"BytesQueryField": BytesQueryField,
|
441
|
+
"JoinNumericCondition": JoinNumericCondition,
|
442
|
+
"JoinStringCondition": JoinStringCondition,
|
443
|
+
"JoinBooleanCondition": JoinBooleanCondition,
|
444
|
+
"JoinDateCondition": JoinDateCondition,
|
445
|
+
"JoinBytesCondition": JoinBytesCondition,
|
446
|
+
"datetime": datetime,
|
447
|
+
"Field": Field,
|
448
|
+
"Union": Union,
|
449
|
+
"Optional": Optional,
|
450
|
+
"ClassVar": ClassVar,
|
451
|
+
"Set": Set
|
452
|
+
}, local_vars)
|
453
|
+
return local_vars[class_name]
|
106
454
|
|
107
455
|
def create_model_db(self, dir_path : str, create_orm = True, create_stp = True, *, table_catalog : str | list[str] | tuple[str] = "all",
|
108
456
|
table_schema : str | list[str] | tuple[str] = "all",
|
@@ -125,12 +473,14 @@ class ModelBuilder(object):
|
|
125
473
|
create_stp = True
|
126
474
|
break
|
127
475
|
|
128
|
-
information_db = self.
|
476
|
+
information_db = self.get_columns_data()
|
129
477
|
information_routines = []
|
130
478
|
if create_stp:
|
131
|
-
information_routines = self.
|
479
|
+
information_routines = self.get_routines_data()
|
132
480
|
if not information_db:
|
133
481
|
return False
|
482
|
+
|
483
|
+
table_data = self.get_tables_data()
|
134
484
|
|
135
485
|
table_catalog = [table_catalog] if isinstance(table_catalog, str) and table_catalog != "all" else table_catalog
|
136
486
|
table_schema = [table_schema] if isinstance(table_schema, str) and table_schema != "all" else table_schema
|
@@ -151,34 +501,69 @@ class ModelBuilder(object):
|
|
151
501
|
if table_options:
|
152
502
|
if table_options.ignore_table:
|
153
503
|
continue
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
from typing import Union
|
161
|
-
|
162
|
-
'''
|
504
|
+
|
505
|
+
table_description = ""
|
506
|
+
key_table = f"{data[0].TABLE_CATALOG}{data[0].TABLE_SCHEMA}{data[0].TABLE_NAME}"
|
507
|
+
table_info = table_data.get(key_table)
|
508
|
+
if table_info:
|
509
|
+
table_description = create_table_description(table_info)
|
163
510
|
|
164
511
|
table_name = data[0].TABLE_NAME
|
165
|
-
class_name = table_name.replace("TBL_", "")
|
512
|
+
class_name = table_name #table_name.replace("TBL_", "")
|
166
513
|
schema = data[0].TABLE_SCHEMA
|
167
514
|
|
168
515
|
fields = [create_field(row) for row in data]
|
169
516
|
fields_str = "\n ".join(fields)
|
170
517
|
|
171
|
-
|
518
|
+
# original_fields = [create_field(row, False) for row in data]
|
519
|
+
# original_fields_str = "\n ".join(original_fields)
|
520
|
+
|
521
|
+
fields_args = [create_arg(row) for row in data]
|
522
|
+
fields_args_str = ", ".join(fields_args)
|
523
|
+
|
524
|
+
identities = {d.COLUMN_NAME for d in data if d.IS_IDENTITY == "YES"}
|
525
|
+
|
526
|
+
primary_keys = {d.COLUMN_NAME for d in data if d.IS_PRIMARY_KEY == "YES"}
|
527
|
+
|
528
|
+
optional_fields = {d.COLUMN_NAME for d in data if d.IS_NULLABLE == "YES"}
|
529
|
+
|
530
|
+
max_length_fields = {
|
531
|
+
d.COLUMN_NAME: d.CHARACTER_MAXIMUM_LENGTH
|
532
|
+
for d in data
|
533
|
+
if d.CHARACTER_MAXIMUM_LENGTH is not None and d.CHARACTER_MAXIMUM_LENGTH > 0
|
534
|
+
}
|
535
|
+
|
536
|
+
table_alias = f'_alias_{table_name.lower()}_alias_'
|
537
|
+
|
538
|
+
content_model = f'''# coding: utf-8
|
539
|
+
|
540
|
+
from dapper_sqls import (TableBaseModel, StringQueryField, NumericQueryField, BoolQueryField, DateQueryField, BytesQueryField,
|
541
|
+
JoinNumericCondition, JoinStringCondition, JoinBooleanCondition, JoinDateCondition, JoinBytesCondition)
|
542
|
+
from datetime import datetime
|
543
|
+
from pydantic import Field
|
544
|
+
from typing import Union, Optional, ClassVar, Set
|
545
|
+
|
172
546
|
class {class_name}(TableBaseModel):
|
173
|
-
|
547
|
+
TABLE_NAME: ClassVar[str] = '[{schema}].[{table_name}]'
|
174
548
|
|
549
|
+
TABLE_ALIAS: ClassVar[str] = '{table_alias}'
|
550
|
+
|
551
|
+
DESCRIPTION : ClassVar[str] = '{table_description}'
|
552
|
+
|
553
|
+
IDENTITIES : ClassVar[Set[str]] = {identities}
|
554
|
+
|
555
|
+
PRIMARY_KEYs : ClassVar[Set[str]] = {primary_keys}
|
556
|
+
|
557
|
+
OPTIONAL_FIELDS : ClassVar[Set[str]] = {optional_fields}
|
558
|
+
|
559
|
+
MAX_LENGTH_FIELDS: ClassVar[dict[str, int]] = {max_length_fields}
|
560
|
+
|
175
561
|
{fields_str}
|
562
|
+
|
563
|
+
{create_queue_update(fields_args_str)}
|
176
564
|
\n
|
177
565
|
'''
|
178
|
-
|
179
|
-
fields_args = [create_field(row, "None") for row in data]
|
180
|
-
fields_args_str = ", ".join(fields_args)
|
181
|
-
|
566
|
+
|
182
567
|
table_create_orm = create_orm
|
183
568
|
if table_options:
|
184
569
|
table_create_orm = table_options.create_orm
|
@@ -319,7 +704,7 @@ class async_stp(object):
|
|
319
704
|
file.write(''.join(content_file_async_rounine))
|
320
705
|
|
321
706
|
import_init_catalog = ""
|
322
|
-
class_init_catalog = f
|
707
|
+
class_init_catalog = f'''class {catalog}(object):\n'''
|
323
708
|
|
324
709
|
for schema, data in schema_data_tables.items():
|
325
710
|
import_init_catalog += f"from .{schema} import schema_{schema}\n"
|