-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.py
203 lines (140 loc) · 5.44 KB
/
temp.py
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import pandas as pd
from SQLconnection import df, tables_Id, table_attributes, table_constraint_keys,db
import re
import numpy as np
"""
1- retrive table name, strip it
2- append its attributes
3- retrive next table name, strip it check if table already in df.columns
4- if not repeat steps 2
Note: probably gonna need to use concat for different lengths of arrays formed by the different
attributes of the tables
print("table name and DB: ", "\n",df.head(5))
print("tables ID: ","\n", tables_Id.head(5))
print("tables attributes: ","\n", table_attributes.head(5))
"""
def get_tables_data(objective):
"""
returns all different tables in a databse with their attributes
"""
if objective is None:
raise Exception("search parameter needed")
new_df = pd.DataFrame()
if objective == "dbInfo":
for i in range(len(table_attributes)):
variable = table_attributes.iloc[i,2]
modified_var = re.split("(\d+)",variable)
modified_var = modified_var[0]
if modified_var not in new_df.columns:
a = []
b = []
c = []
d = []
temp_df = pd.DataFrame()
temp = i
while(table_attributes.iloc[temp,2]==variable):
a.append(table_attributes.iloc[temp,3])
b.append(table_attributes.iloc[temp,5])
c.append(table_attributes.iloc[temp,4])
d.append(" ")
temp = temp+1
temp_df[modified_var] = a
temp_df[modified_var+"_Data_Type"] = b
temp_df[modified_var+"_Nullable"] = c
temp_df[modified_var+"_Description"] = d
new_df = pd.concat([new_df,temp_df], axis =1)
else:
continue
elif objective == "dbConstraints":
if table_constraint_keys.empty:
raise Exception("Illegal argument exception: no constraints in database")
else:
for i in range(len(table_constraint_keys)):
variable = table_constraint_keys.iloc[i,1]
modified_var = re.split("(\d+)",variable)
modified_var = modified_var[0]
if modified_var not in new_df.columns:
a = []
b = []
temp_df = pd.DataFrame()
temp = i
while(table_constraint_keys.iloc[temp,1]==variable):
a.append(table_constraint_keys.iloc[temp,2])
b.append(table_constraint_keys.iloc[temp,0])
temp = temp+1
temp_df[modified_var] = a
temp_df[modified_var+"_constraints"] = b
new_df = pd.concat([new_df,temp_df], axis =1)
else:
continue
return new_df.T.rename_axis("Table_Name", axis = 0)
def getKeys(*args,**kwargs):
search = "PK"
columnName = "Primary Keys"
if table_constraint_keys.empty:
return "no constraints in database"
if "FK" in args:
search = "FK"
columnName = "Foreign Keys"
elif "PK" in args:
search = "PK"
columnName = "Primary Keys"
a = []
b = []
for i in range(len(table_constraint_keys)):
key = table_constraint_keys.iloc[i,0]
if key.startswith(search):
a.append(table_constraint_keys.iloc[i,2])
b.append(table_constraint_keys.iloc[i,1])
return pd.DataFrame({"Parent Table":b,columnName:a})
def unifyData(dataFrame,**kwargs):
if dataFrame.empty:
raise Exception("DataFrame cannot be empty")
else:
new_df = pd.DataFrame()
a = []
b = [[]]
for k,v in kwargs.items():
if k != "axis":
unifyOn = 0
else:
for k,v in kwargs.items():
if k =="axis":
unifyOn = v
else:
continue
for i in range(len(dataFrame)):
variable = dataFrame.iloc[i,unifyOn]
modified_var = re.split("(\d+)",variable)
modified_var = modified_var[0]
if modified_var not in a:
a.append(modified_var)
b.append(dataFrame.iloc[i,:])
else:
continue
new_df["unique_"+dataFrame.columns[unifyOn]] = a
for j in range(len(b[0])):
new_df[j] = b[:,j]
return new_df
print(table_attributes.head(5))
test = unifyData(table_attributes, axis =2)
print(test.head(10))
#test = getKeys()
#print(test.head(5))
#test.to_csv(db+"_PrimaryKeys.csv")
#
#
#
#
#tablesTest_dbConstraints = get_tables_data("dbConstraints")
#tablesTest_dbInfo = get_tables_data("dbInfo")
#print(tablesTest_dbConstraints.head(5))
#
## uncomment this to get output
#tablesTest_dbInfo.to_csv(db+"_Tables_attributes.csv")
#tablesTest_dbConstraints.to_csv(db+"_variables_constraints.csv")
def getDictData():
data = get_tables_data()
dataMod = data.transpose()
return dataMod.to_dict()
print("constraints table : ","\n",table_constraint_keys.head(5))