Upload script/convert.py with huggingface_hub
Browse files- script/convert.py +49 -0
script/convert.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def convert_obj_to_schema(obj:dict) -> str:
|
2 |
+
schema = f"CREATE DATABASE {obj['db_id']}; USE {obj['db_id']};"
|
3 |
+
|
4 |
+
tables = {}
|
5 |
+
for table_name in obj['table_names']:
|
6 |
+
table_name = "_".join(table_name.split(" "))
|
7 |
+
tables[table_name] = []
|
8 |
+
|
9 |
+
for col_idx, column in enumerate(obj['column_names']):
|
10 |
+
table_id, column_name = column
|
11 |
+
# _ combination
|
12 |
+
column_name = "_".join(column_name.split(" "))
|
13 |
+
|
14 |
+
if table_id == -1:
|
15 |
+
continue
|
16 |
+
table_name = obj['table_names'][table_id]
|
17 |
+
column_type = obj['column_types'][col_idx]
|
18 |
+
|
19 |
+
# _ combination
|
20 |
+
table_name = "_".join(table_name.split(" "))
|
21 |
+
|
22 |
+
column_attributes = [column_name, column_type]
|
23 |
+
if col_idx in obj['primary_keys']:
|
24 |
+
column_attributes.append("PRIMARY KEY")
|
25 |
+
|
26 |
+
tables[table_name].append(column_attributes)
|
27 |
+
|
28 |
+
for fk in obj['foreign_keys']:
|
29 |
+
if col_idx == fk[1]:
|
30 |
+
table_id_other, column_name_other = obj['column_names'][fk[0]]
|
31 |
+
table_name_other = obj['table_names'][table_id_other]
|
32 |
+
|
33 |
+
column_name_other = "_".join(column_name_other.split(" "))
|
34 |
+
table_name_other = "_".join(table_name_other.split(" "))
|
35 |
+
tables[table_name].append([f"FOREIGN KEY {column_name} REFERENCES {table_name_other}({column_name_other})"])
|
36 |
+
|
37 |
+
for table_name in tables:
|
38 |
+
|
39 |
+
column_attributes = ""
|
40 |
+
for idx, column_attribute in enumerate(tables[table_name]):
|
41 |
+
column_attributes += " ".join(column_attribute) + ","
|
42 |
+
column_attributes = column_attributes[:-1] # remove endless ','
|
43 |
+
|
44 |
+
schema += f" CREATE TABLE {table_name}({column_attributes});"
|
45 |
+
|
46 |
+
return {
|
47 |
+
"db_id": obj['db_id'],
|
48 |
+
"schema": schema
|
49 |
+
}
|