-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlgenerate.go
192 lines (170 loc) · 4.58 KB
/
sqlgenerate.go
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
package main
import (
"fmt"
"net/url"
"strings"
"github.com/go-sql-driver/mysql"
)
func updateSQL(tableName string, params url.Values, body map[string]string) (string, bool) {
if len(body) == 0 {
return statusCodeBadRequest, false
}
SQLQuery := "update `" + tableName + "` set "
init := false
for key, val := range body {
if init {
SQLQuery += ","
}
SQLQuery += "`" + key + "` = '" + strings.Replace(val, "'", "\\'", -1) + "' "
init = true
}
SQLQuery += " where "
init = false
for key, val := range params {
if init {
SQLQuery += " and "
}
SQLQuery += "`" + key + "` = '" + strings.Replace(val[0], "'", "\\'", -1) + "' "
init = true
}
logger(SQLQuery)
_, err := db.Exec(SQLQuery)
if err != nil {
fmt.Println("updateSQL", err)
if driverErr, ok := err.(*mysql.MySQLError); ok {
return sqlErrorCheck(driverErr.Number), false
}
return statusCodeServerError, false // default
}
return statusCodeOk, true
}
func insertSQL(tableName string, body map[string]string) (string, bool) {
if len(body) == 0 {
return statusCodeBadRequest, false
}
SQLQuery := buildInsertStatement(tableName, body)
logger(SQLQuery)
_, err := db.Exec(SQLQuery)
if err != nil {
fmt.Println("insertSQL", err)
if driverErr, ok := err.(*mysql.MySQLError); ok {
errCode := sqlErrorCheck(driverErr.Number)
if strings.EqualFold(errCode, statusCodeDuplicateEntry) {
if strings.Contains(err.Error(), "_u_id") {
return statusCodeDuplicateEntry, false
}
return statusCodeBadRequest, false
}
return errCode, false
}
return statusCodeServerError, false // default
}
return statusCodeCreated, true
}
func buildInsertStatement(tableName string, body map[string]string) string {
SQLQuery := "insert into `" + tableName + "` "
keys := " ("
values := " ("
init := false
for key, val := range body {
if init {
keys += ","
values += ","
}
keys += "`" + key + "`"
values += "'" + strings.Replace(val, "'", "\\'", -1) + "'"
init = true
}
keys += ")"
values += ")"
SQLQuery += keys + " values " + values
return SQLQuery
}
func deleteSQL(tableName string, params url.Values) (string, bool) {
if len(params) == 0 {
return statusCodeBadRequest, false
}
SQLQuery := "delete from `" + tableName + "` where "
init := false
for key, val := range params {
if init {
SQLQuery += " and "
}
SQLQuery += "`" + key + "` = '" + strings.Replace(val[0], "'", "\\'", -1) + "' "
init = true
}
logger(SQLQuery)
_, err := db.Exec(SQLQuery)
if err != nil {
fmt.Println("deleteSQL", err)
if driverErr, ok := err.(*mysql.MySQLError); ok {
return sqlErrorCheck(driverErr.Number), false
}
return statusCodeServerError, false // default
}
return statusCodeOk, true
}
func selectSQL(tableName string, params url.Values) ([]map[string]string, string, bool) {
where := ""
init := false
for key, val := range params {
if init {
where += " and "
}
where += "`" + key + "` = '" + strings.Replace(val[0], "'", "\\'", -1) + "' "
init = true
}
SQLQuery := "select * from `" + tableName + "`"
if strings.Compare(where, "") != 0 {
SQLQuery += " where " + where
}
return selectProcess(SQLQuery)
}
func selectProcess(SQLQuery string) ([]map[string]string, string, bool) {
logger(SQLQuery)
// SQLQuery = html.EscapeString(SQLQuery)
rows, err := db.Query(SQLQuery)
if err != nil {
fmt.Println("selectProcess", err)
if driverErr, ok := err.(*mysql.MySQLError); ok && driverErr != nil {
return []map[string]string{}, sqlErrorCheck(driverErr.Number), false
}
return []map[string]string{}, statusCodeServerError, false // default
}
defer rows.Close()
cols, err := rows.Columns()
if err != nil {
fmt.Println("selectProcess", err)
if driverErr, ok := err.(*mysql.MySQLError); ok {
return []map[string]string{}, sqlErrorCheck(driverErr.Number), false
}
return []map[string]string{}, statusCodeServerError, false // default
}
rawResult := make([][]byte, len(cols))
dest := make([]interface{}, len(cols))
data := []map[string]string{}
rest := map[string]string{}
for i := range rawResult {
dest[i] = &rawResult[i]
}
for rows.Next() {
rest = map[string]string{}
err = rows.Scan(dest...)
if err != nil {
fmt.Println("selectProcess", err)
if driverErr, ok := err.(*mysql.MySQLError); ok {
return []map[string]string{}, sqlErrorCheck(driverErr.Number), false
}
return []map[string]string{}, statusCodeServerError, false // default
}
for i, raw := range rawResult {
if raw == nil {
rest[cols[i]] = ""
} else {
rest[cols[i]] = string(raw)
}
}
data = append(data, rest)
}
return data, statusCodeOk, true
}