-
Notifications
You must be signed in to change notification settings - Fork 3
/
db.go
255 lines (212 loc) · 7.22 KB
/
db.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"database/sql"
"time"
_ "github.com/go-sql-driver/mysql"
)
type GetType int
const (
GetIn GetType = 1
GetOff GetType = 2
)
func addUserToken(user User) error {
mysql, err := sql.Open("mysql", config.Database.User+":"+config.Database.Passwd+"@tcp("+config.Database.IPAddr+":"+config.Database.Port+")/"+config.Database.DBname)
if err != nil { // error exists
return err
}
defer mysql.Close()
_, err = mysql.Exec("INSERT INTO User VALUES (?,?,NOW()) ON DUPLICATE KEY UPDATE token=?, UUID=?, registration_date=NOW()", user.Token, user.UUID, user.Token, user.UUID)
if err != nil { // error exists
return err
}
return nil
}
func addDriverStop(stopInput StopInput, getType GetType) (bool, error) {
mysql, err := sql.Open("mysql", config.Database.User+":"+config.Database.Passwd+"@tcp("+config.Database.IPAddr+":"+config.Database.Port+")/"+config.Database.DBname)
if err != nil { // error exists
return false, err
}
defer mysql.Close()
var gt string
var getInfo GetInfo
if getType == GetIn {
gt = "getIn"
getInfo.IsGetIn = true
} else {
gt = "getOut"
getInfo.IsGetOff = true
}
rows, err := mysql.Exec("INSERT INTO DriverStop VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE "+gt+"=?", stopInput.RouteID, stopInput.StationID, getInfo.IsGetIn, getInfo.IsGetOff, true)
if err != nil { // error exists
return false, err
}
n, err := rows.RowsAffected()
if err != nil { // error exists
return false, err
}
if n == 1 {
return true, nil
}
return false, nil
}
func addGetIn(r Reserv) error {
mysql, err := sql.Open("mysql", config.Database.User+":"+config.Database.Passwd+"@tcp("+config.Database.IPAddr+":"+config.Database.Port+")/"+config.Database.DBname)
if err != nil { // error exists
return err
}
defer mysql.Close()
_, err = mysql.Exec("INSERT INTO GetIn VALUES (?, ?, ?)", r.UUID, r.RouteID, r.StationID)
if err != nil { // error exists
return err
}
return nil
}
func addGetOut(r Reserv) error {
mysql, err := sql.Open("mysql", config.Database.User+":"+config.Database.Passwd+"@tcp("+config.Database.IPAddr+":"+config.Database.Port+")/"+config.Database.DBname)
if err != nil { // error exists
return err
}
defer mysql.Close()
_, err = mysql.Exec("INSERT INTO GetOut VALUES (?, ?, ?, ?)", r.UUID, r.RouteID, r.StationID, r.PlateNo)
if err != nil { // error exists
return err
}
return nil
}
func deleteGetIn(routeID string, stationID string) error {
mysql, err := sql.Open("mysql", config.Database.User+":"+config.Database.Passwd+"@tcp("+config.Database.IPAddr+":"+config.Database.Port+")/"+config.Database.DBname)
if err != nil { // error exists
return err
}
defer mysql.Close()
_, err = mysql.Exec("DELETE FROM GetIn WHERE routeID = ? AND stationID = ?", routeID, stationID)
if err != nil { // error exists
return err
}
return nil
}
func deleteGetOut(routeID string, stationID string, plateNo string) error {
mysql, err := sql.Open("mysql", config.Database.User+":"+config.Database.Passwd+"@tcp("+config.Database.IPAddr+":"+config.Database.Port+")/"+config.Database.DBname)
if err != nil { // error exists
return err
}
defer mysql.Close()
_, err = mysql.Exec("DELETE FROM GetOut WHERE routeID = ? AND stationID = ? AND plateNo = ?", routeID, stationID, plateNo)
if err != nil { // error exists
return err
}
return nil
}
func deleteDriverStop(routeID string, stationID string) error {
mysql, err := sql.Open("mysql", config.Database.User+":"+config.Database.Passwd+"@tcp("+config.Database.IPAddr+":"+config.Database.Port+")/"+config.Database.DBname)
if err != nil { // error exists
return err
}
defer mysql.Close()
_, err = mysql.Exec("DELETE FROM DriverStop WHERE routeID = ? AND stationID = ?", routeID, stationID)
if err != nil { // error exists
return err
}
return nil
}
func getTokenFromUUID(UUID string) (string, error) {
mysql, err := sql.Open("mysql", config.Database.User+":"+config.Database.Passwd+"@tcp("+config.Database.IPAddr+":"+config.Database.Port+")/"+config.Database.DBname)
if err != nil { // error exists
return "", err
}
defer mysql.Close()
var token string
err = mysql.QueryRow("SELECT token FROM User WHERE UUID=?", UUID).Scan(&token)
if err != nil {
return "", err
}
return token, nil
}
func addStaDirect(stationID string, direct string) error {
mysql, err := sql.Open("mysql", config.Database.User+":"+config.Database.Passwd+"@tcp("+config.Database.IPAddr+":"+config.Database.Port+")/"+config.Database.DBname)
if err != nil { // error exists
return err
}
defer mysql.Close()
_, err = mysql.Exec("INSERT INTO StationDirect VALUES (?,?) ON DUPLICATE KEY UPDATE direct=?", stationID, direct, direct)
if err != nil { // error exists
return err
}
return nil
}
func getStaDirect(stationID string) string {
mysql, err := sql.Open("mysql", config.Database.User+":"+config.Database.Passwd+"@tcp("+config.Database.IPAddr+":"+config.Database.Port+")/"+config.Database.DBname)
mysql.SetConnMaxLifetime(time.Second * 5)
var direct string
if err != nil { // error exists
ErrorLogger(err)
direct = ""
}
defer mysql.Close()
err = mysql.QueryRow("SELECT direct FROM StationDirect WHERE stationID=?", stationID).Scan(&direct)
if err != nil {
if err == sql.ErrNoRows {
direct = ""
} else {
ErrorLogger(err)
}
}
return direct
}
func getGetInUserTokens(routeID string, stationID string) ([]string, error) {
mysql, err := sql.Open("mysql", config.Database.User+":"+config.Database.Passwd+"@tcp("+config.Database.IPAddr+":"+config.Database.Port+")/"+config.Database.DBname)
if err != nil { // error exists
return nil, err
}
defer mysql.Close()
tokens := []string{}
var tempToken string
rows, err := mysql.Query("SELECT token FROM User INNER JOIN GetIn ON User.UUID = GetIn.UUID AND (routeID = ? AND stationID=?)", routeID, stationID)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(&tempToken)
if err != nil {
return nil, err
}
tokens = append(tokens, tempToken)
}
return tokens, nil
}
func getGetOutUserTokens(routeID string, stationID string, plateNo string) ([]string, error) {
mysql, err := sql.Open("mysql", config.Database.User+":"+config.Database.Passwd+"@tcp("+config.Database.IPAddr+":"+config.Database.Port+")/"+config.Database.DBname)
if err != nil { // error exists
return nil, err
}
defer mysql.Close()
tokens := []string{}
var tempToken string
rows, err := mysql.Query("SELECT token FROM User INNER JOIN GetOut ON User.UUID = GetOut.UUID AND (routeID = ? AND stationID=? AND plateNo=?)", routeID, stationID, plateNo)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(&tempToken)
if err != nil {
return nil, err
}
tokens = append(tokens, tempToken)
}
return tokens, nil
}
func getGetCount(routeID string, stationID string) (GetInfo, error) {
var getInfo GetInfo
mysql, err := sql.Open("mysql", config.Database.User+":"+config.Database.Passwd+"@tcp("+config.Database.IPAddr+":"+config.Database.Port+")/"+config.Database.DBname)
if err != nil { // error exists
return getInfo, err
}
defer mysql.Close()
err = mysql.QueryRow("SELECT getIn, getOut FROM DriverStop WHERE routeID=? AND stationID=?", routeID, stationID).Scan(&getInfo.IsGetIn, &getInfo.IsGetOff)
if err != nil {
return getInfo, err
}
return getInfo, nil
}