-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcode_gen.py
136 lines (127 loc) · 5.61 KB
/
code_gen.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
user = ["user_name", "user_pw", "user_loc", 'user_university']
apt = ["apt_name", "apt_rating", "apt_contact"]
addr = ["addr_pin", "addr_name", "addr_crime", "addr_bus", "addr_dist"]
company = ["company_name", "company_rating"]
cond = ["cond_name", "cond_bed", "cond_bath", "cond_amen", "cond_rent"]
loc = ["loc_pin", "loc_apt"]
belong = ["belong_apt", "belong_company"]
def insert_gen(div, name, db_name):
without_dollar = ""
with_dollar = ""
with_or = ""
for col in div:
print("$"+col+" = $_POST[\""+col+"\"];")
for i in range(len(div) - 1):
without_dollar += "`"+div[i]+"`, "
with_dollar += "\'$"+div[i]+"\', "
with_or += "$"+div[i]+" == \"\" || "
without_dollar += "`"+div[len(div) - 1]+"`"
with_dollar += "\'$"+div[len(div) - 1]+"\'"
with_or += "$"+div[len(div) - 1]+" == \"\""
if_part = "if(" + with_or + ") {\n echo \"Empty input! Please try again!\";\n}"
print(if_part)
sql = "$sql = \"INSERT INTO `"+name+"`("+without_dollar+") VALUES("+with_dollar+")\";"
print(sql)
print("$db = mysql_select_db(\'"+db_name+"\', $conn);")
print("""if (!$db){
die (\"Failed: \" . mysql_error());
}
$result = mysql_query($sql, $conn);
if(!$result) {
die(\'Could not insert data: \' . mysql_error());
}
echo \"Successfully inserted!\";
$conn->close();""")
def update_gen(div, name, db_name):
temp = ""
for col in div:
temp += "$" + col + " "
print("$"+col+" = $_POST[\""+col+"\"];")
print("$"+name+"_w = $_POST[\""+name+"_where\"];")
temp2 = ""
for i in range(len(div) - 1):
print("if ($"+div[i]+" != \"\") {\n$"+div[i]+" = \""+div[i]+" = \'$"+div[i]+"\',\";\n}")
temp2 += "$"+div[i]+". \" \" ."
print("if ($"+div[len(div) - 1]+" != \"\") {\n$"+div[len(div) - 1]+" = \""+div[len(div) - 1]+" = \'$"+div[len(div) - 1]+"\',\";\n}")
temp2 += "$"+div[len(div) - 1]+";"
print("if ($"+name+"""_w == \"\") {
die (\"Failed: condition cannot be empty\");
}""")
print("$temp = "+temp2)
print("$temp = rtrim($temp, \", \");")
sql = "$sql = \"UPDATE `"+name+"` \" . \"SET $temp \" . \"WHERE $"+name+"_w\";"
print(sql)
print("$db = mysql_select_db(\'"+db_name+"\', $conn);")
print("""if (!$db){
die (\"Failed: \" . mysql_error());
}
$result = mysql_query($sql, $conn);
if(!$result) {
die(\"Could not update data: \" . mysql_error());
}
echo \"Successfully updated!\";
$conn->close();""")
def delete_gen(div, name, db_name):
where = "$"+name+"_w"
print(where + "= $_POST[\""+name+"_where\"];")
print("if ("+where+""" == \"\") {
die (\"Failed: condition cannot be empty\");
}""")
print("$sql = \"DELETE FROM `"+name+"` \" . \"WHERE "+where+"\";")
print("$db = mysql_select_db(\'"+db_name+"\', $conn);")
print("""if (!$db){
die (\"Failed: \" . mysql_error());
}
$result = mysql_query($sql, $conn);
if(!$result) {
die(\'Could not delete data: \' . mysql_error());
}
echo \"Successfully Deleted!\";
$conn->close();""")
def select_gen(div, name, db_name):
for col in div:
print("$"+col+" = $_POST[\""+col+"_c\"];")
print("$"+name+"_w = $_POST[\""+name+"_where\"];")
temp2 = ""
for i in range(len(div) - 1):
print("if ($"+div[i]+" != \"\") {\n$"+div[i]+" = \"`"+div[i]+"`,\";\n}")
temp2 += "$"+div[i]+". \" \" ."
print("if ($"+div[len(div) - 1]+" != \"\") {\n$"+div[len(div) - 1]+" = \"`"+div[len(div) - 1]+"`,\";\n}")
temp2 += "$"+div[len(div) - 1]+";"
print("if ($"+name+"""_w == \"\") {
die (\"Failed: condition cannot be empty\");
}""")
print("$temp = "+temp2)
print("$temp = rtrim($temp, \", \");")
sql = "\"SELECT $temp FROM `"+name+"` WHERE $"+name+"_w\";"
print("$sql = "+sql)
print("""$db = mysql_select_db(\'sxu46_test\', $conn);
if (!$db){
die (\"Failed: \" . mysql_error());
}
$result = mysql_query($sql, $conn);
if(!$result) {
die(\'Could not get data: \' . mysql_error());
}""")
temp = ""
for col in div:
temp += "\""+col+" :{$row[\'"+col+"\']} <br>\".\n"
temp += "\"---------------------------------------------- <br>\";"
print("while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {\necho\n" + temp + "\n}")
print("""echo \"Successfully Selected!\";
$conn->close();""")
print("-----------------------------------------------------------------")
select_gen(user, "user", "sxu46_test")
print("-----------------------------------------------------------------")
select_gen(apt, "apt", "sxu46_test")
print("-----------------------------------------------------------------")
select_gen(addr, "addr", "sxu46_test")
print("-----------------------------------------------------------------")
select_gen(company, "company", "sxu46_test")
print("-----------------------------------------------------------------")
select_gen(cond, "cond", "sxu46_test")
print("-----------------------------------------------------------------")
select_gen(loc, "loc", "sxu46_test")
print("-----------------------------------------------------------------")
select_gen(belong, "belong", "sxu46_test")
print("-----------------------------------------------------------------")