-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity_gen.py
More file actions
97 lines (73 loc) · 2.81 KB
/
Copy pathentity_gen.py
File metadata and controls
97 lines (73 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import pyodbc
import os
from data_types import DataTypes
from pathlib import Path
from sys import argv
def capitalizeString(val: str) -> str:
return ''.join([letter[0].upper() + letter[1:] for letter in val.split()])
class EntityGenerator():
def __init__(self, conn_file: str):
conn = pyodbc.connect(conn_file)
self.query = conn.cursor()
def gen_entity(self, table_name: str) -> str:
try:
self.table_name = table_name
entity = self.query.execute(f'exec sp_help {table_name}')
entity.nextset()
values = []
for row in entity:
values.append([row[0], row[1], row[3], row[6], 'no'])
for _ in range(4):
entity.nextset()
for row in entity:
primary_keys = [key.strip() for key in row[-1].split(',')]
for key in primary_keys:
for val in values:
if key == val[0]:
val[-1] = 'yes'
entity_string = """
[Table("{tb}")]
public virtual {name}
{{""".format(tb=table_name,
name=str(table_name.replace('_', '')))
for field in values:
column = """
{is_key}
[Column("{col_name}")]
public {col_type}{nullable} {field_name} {{ get; set; }}
""".format(is_key='[Key]' if field[-1] == 'yes' else '',
col_name=field[0],
nullable='?' if
(field[3] == 'yes'
and DataTypes[field[1]].value != 'string') else '',
col_type=DataTypes[field[1]].value,
field_name=capitalizeString(field[0].replace(
'_', '')))
entity_string += column
entity_string += """}"""
return entity_string
except:
print(f'Não foi possivel gerar a tabela')
return None
def save_file(self, gen_value: str) -> bool:
path = os.path.join(os.path.expanduser('~'), 'Desktop',
f'{self.table_name}.cs')
if not os.path.isfile(path):
Path(path).touch()
with open(path, 'a') as file:
file.write(gen_value)
def main() -> None:
if (len(argv) < 2):
print('passe o nome da tabela como parametro!')
return
file = ''
with open('./db_file.txt', 'r') as f:
file = f.read()
eg = EntityGenerator(file)
gen_val = eg.gen_entity(str(argv[1]))
if (gen_val is None):
print('Não foi possivel gerar a entidade')
return
eg.save_file(gen_val)
if __name__ == "__main__":
main()