-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathusers.go
301 lines (257 loc) · 6.11 KB
/
users.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package proxysql
import (
"database/sql"
"fmt"
"github.com/go-sql-driver/mysql"
"github.com/juju/errors"
)
type (
Users struct {
Username string `db:"username" json:"username"`
Password string `db:"password" json:"password"`
Active uint64 `db:"active" json:"active"`
UseSsl uint64 `db:"use_ssl" json:"use_ssl"`
DefaultHostgroup uint64 `db:"default_hostgroup" json:"default_hostgroup"`
DefaultSchema string `db:"default_schema" json:"default_schema"`
SchemaLocked uint64 `db:"schema_locked" json:"schema_locked"`
TransactionPersistent uint64 `db:"transaction_persistent" json:"transaction_persistent"`
FastForward uint64 `db:"fast_forward" json:"fast_forward"`
Backend uint64 `db:"backend" json:"backend"`
Frontend uint64 `db:"frontend" json:"frontend"`
MaxConnections uint64 `db:"max_connections" json:"max_connections"`
}
)
const (
/*add a new users*/
StmtAddOneUser = `
INSERT INTO
mysql_users(username,password,default_hostgroup,default_schema)
VALUES(%q,%q,%d,%q)`
/*delete a user*/
StmtDeleteOneUser = `
DELETE FROM
mysql_users
WHERE
username = %q
AND
backend = %d
AND
frontend = %d
`
/*list all users*/
StmtFindAllUserInfo = `
SELECT
ifnull(username,""),
ifnull(password,""),
ifnull(active,0),
ifnull(use_ssl,0),
ifnull(default_hostgroup,0),
ifnull(default_schema,""),
ifnull(schema_locked,0),
ifnull(transaction_persistent,0),
ifnull(fast_forward,0),
ifnull(backend,0),
ifnull(frontend,0),
ifnull(max_connections,0)
FROM mysql_users
LIMIT %d
OFFSET %d`
/*update a users*/
StmtUpdateOneUser = `
UPDATE
mysql_users
SET
password=%q,
active=%d,
use_ssl=%d,
default_hostgroup=%d,
default_schema=%q,
schema_locked=%d,
transaction_persistent=%d,
fast_forward=%d,
backend=%d,
frontend=%d,
max_connections=%d
WHERE
username = %q
AND
backend = %d
AND
frontend = %d
`
)
//list all users.
func FindAllUserInfo(db *sql.DB, limit uint64, skip uint64) ([]Users, error) {
var alluser []Users
Query := fmt.Sprintf(StmtFindAllUserInfo, limit, skip)
rows, err := db.Query(Query)
if err != nil {
return []Users{}, errors.Trace(err)
}
defer rows.Close()
for rows.Next() {
var tmpusr Users
err = rows.Scan(
&tmpusr.Username,
&tmpusr.Password,
&tmpusr.Active,
&tmpusr.UseSsl,
&tmpusr.DefaultHostgroup,
&tmpusr.DefaultSchema,
&tmpusr.SchemaLocked,
&tmpusr.TransactionPersistent,
&tmpusr.FastForward,
&tmpusr.Backend,
&tmpusr.Frontend,
&tmpusr.MaxConnections,
)
if err != nil {
continue
}
alluser = append(alluser, tmpusr)
}
return alluser, nil
}
func NewUser(username string, password string, default_hostgroup uint64, default_schema string) (*Users, error) {
newuser := new(Users)
newuser.Username = username
newuser.Password = password
newuser.DefaultHostgroup = default_hostgroup
newuser.DefaultSchema = default_schema
newuser.Active = 1
newuser.UseSsl = 0
newuser.SchemaLocked = 0
newuser.TransactionPersistent = 0
newuser.FastForward = 0
newuser.Backend = 1
newuser.Frontend = 1
newuser.MaxConnections = 10000
return newuser, nil
}
// set fast_forward
func (users *Users) SetFastForward(fast_forward uint64) {
if fast_forward >= 1 {
users.FastForward = 1
} else {
users.FastForward = 0
}
}
// set max_connections
func (users *Users) SetMaxConnections(max_connections uint64) {
switch {
case max_connections >= 10000:
users.MaxConnections = 10000
case max_connections <= 1:
users.MaxConnections = 1
default:
users.MaxConnections = max_connections
}
}
// set backend
func (users *Users) SetBackend(backend uint64) {
if backend >= 1 {
users.Backend = 1
} else {
users.Backend = 0
}
}
// set fronted
func (users *Users) SetFrontend(frontend uint64) {
if frontend >= 1 {
users.Frontend = 1
} else {
users.Frontend = 0
}
}
// set user active/disactive
func (users *Users) SetUserActive(active uint64) {
if active >= 1 {
users.Active = 1
} else {
users.Active = 0
}
}
// Set users UseSSL
func (users *Users) SetUseSSL(use_ssl uint64) {
if use_ssl >= 1 {
users.UseSsl = 1
} else {
users.UseSsl = 0
}
}
// set users SchemaLocked
func (users *Users) SetSchemaLocked(schema_locked uint64) {
if schema_locked >= 1 {
users.SchemaLocked = 1
} else {
users.SchemaLocked = 0
}
}
// set users transaction_persistent
func (users *Users) SetTransactionPersistent(transaction_persistent uint64) {
if transaction_persistent >= 1 {
users.TransactionPersistent = 1
} else {
users.TransactionPersistent = 0
}
}
//add a new user.
func (users *Users) AddOneUser(db *sql.DB) error {
Query := fmt.Sprintf(StmtAddOneUser, users.Username, users.Password, users.DefaultHostgroup, users.DefaultSchema)
_, err := db.Exec(Query)
if err != nil {
switch {
case err.(*mysql.MySQLError).Number == 1045:
return errors.NewAlreadyExists(err, users.Username)
default:
return errors.Trace(err) //add user failed
}
}
LoadUserToRuntime(db)
SaveUserToDisk(db)
return nil
}
//delete a user.
func (users *Users) DeleteOneUser(db *sql.DB) error {
Query := fmt.Sprintf(StmtDeleteOneUser, users.Username, users.Backend, users.Frontend)
result, err := db.Exec(Query)
if err != nil {
return errors.Trace(err) //delte failed
}
rowsAffected, _ := result.RowsAffected()
if rowsAffected == 0 {
return errors.NotFoundf(users.Username)
}
LoadUserToRuntime(db)
SaveUserToDisk(db)
return nil //delete success
}
// update a user.
func (users *Users) UpdateOneUserInfo(db *sql.DB) error {
Query := fmt.Sprintf(StmtUpdateOneUser,
users.Password,
users.Active,
users.UseSsl,
users.DefaultHostgroup,
users.DefaultSchema,
users.SchemaLocked,
users.TransactionPersistent,
users.FastForward,
users.Backend,
users.Frontend,
users.MaxConnections,
users.Username,
users.Backend,
users.Frontend)
result, err := db.Exec(Query)
if err != nil {
return errors.Trace(err)
}
rowsAffected, _ := result.RowsAffected()
if rowsAffected == 0 {
return errors.NotFoundf(users.Username)
}
LoadUserToRuntime(db)
SaveUserToDisk(db)
return nil
}