Skip to content

Commit 2cdb3a2

Browse files
committed
fixbug-user
1 parent 1bcf403 commit 2cdb3a2

File tree

2 files changed

+56
-20
lines changed

2 files changed

+56
-20
lines changed

store/postgresql/user.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -387,35 +387,37 @@ func (u *userStore) listUsers(filters map[string]string, offset uint32, limit ui
387387
getSql += " AND user_type != 0 "
388388
}
389389

390-
args := make([]interface{}, 0)
391-
var index = 1
390+
var (
391+
args = make([]interface{}, 0)
392+
idx = 1
393+
)
392394

393395
if len(filters) != 0 {
394396
for k, v := range filters {
395397
getSql += " AND "
396398
countSql += " AND "
397399
if k == NameAttribute {
398400
if utils.IsPrefixWildName(v) {
399-
getSql += " " + k + fmt.Sprintf(" like $%d ", index)
400-
countSql += " " + k + fmt.Sprintf(" like $%d ", index)
401+
getSql += " " + k + fmt.Sprintf(" like $%d ", idx)
402+
countSql += " " + k + fmt.Sprintf(" like $%d ", idx)
401403
args = append(args, "%"+v[:len(v)-1]+"%")
402404
} else {
403-
getSql += " " + k + fmt.Sprintf(" = $%d ", index)
404-
countSql += " " + k + fmt.Sprintf(" = $%d ", index)
405+
getSql += " " + k + fmt.Sprintf(" = $%d ", idx)
406+
countSql += " " + k + fmt.Sprintf(" = $%d ", idx)
405407
args = append(args, v)
406408
}
407409
} else if k == OwnerAttribute {
408-
getSql += fmt.Sprintf(" (id = $%d OR owner = $%d) ", index, index+1)
409-
countSql += fmt.Sprintf(" (id = $%d OR owner = $%d) ", index, index+1)
410-
index += 1
410+
getSql += fmt.Sprintf(" (id = $%d OR owner = $%d) ", idx, idx+1)
411+
countSql += fmt.Sprintf(" (id = $%d OR owner = $%d) ", idx, idx+1)
412+
idx += 2
411413
args = append(args, v, v)
412414
continue
413415
} else {
414-
getSql += " " + k + fmt.Sprintf(" = $%d ", index)
415-
countSql += " " + k + fmt.Sprintf(" = $%d ", index)
416+
getSql += " " + k + fmt.Sprintf(" = $%d ", idx)
417+
countSql += " " + k + fmt.Sprintf(" = $%d ", idx)
416418
args = append(args, v)
417419
}
418-
index++
420+
idx++
419421
}
420422
}
421423

@@ -424,7 +426,7 @@ func (u *userStore) listUsers(filters map[string]string, offset uint32, limit ui
424426
return 0, nil, store.Error(err)
425427
}
426428

427-
getSql += fmt.Sprintf(" ORDER BY mtime LIMIT $%d OFFSET $%d", index, index+1)
429+
getSql += fmt.Sprintf(" ORDER BY mtime LIMIT $%d OFFSET $%d", idx, idx+1)
428430
getArgs := append(args, limit, offset)
429431

430432
users, err := u.collectUsers(u.master.Query, getSql, getArgs)
@@ -456,7 +458,7 @@ func (u *userStore) listGroupUsers(filters map[string]string, offset uint32, lim
456458
querySql += " AND u.user_type != 0 "
457459
}
458460

459-
var index = 1
461+
var idx = 1
460462

461463
for k, v := range filters {
462464
if newK, ok := userLinkGroupAttributeMapping[k]; ok {
@@ -468,24 +470,24 @@ func (u *userStore) listGroupUsers(filters map[string]string, offset uint32, lim
468470
}
469471

470472
if utils.IsPrefixWildName(v) {
471-
querySql += " AND " + k + fmt.Sprintf(" like $%d", index)
472-
countSql += " AND " + k + fmt.Sprintf(" like $%d", index)
473+
querySql += " AND " + k + fmt.Sprintf(" like $%d", idx)
474+
countSql += " AND " + k + fmt.Sprintf(" like $%d", idx)
473475
args = append(args, v[:len(v)-1]+"%")
474476
} else {
475-
querySql += " AND " + k + fmt.Sprintf(" = $%d", index)
476-
countSql += " AND " + k + fmt.Sprintf(" = $%d", index)
477+
querySql += " AND " + k + fmt.Sprintf(" = $%d", idx)
478+
countSql += " AND " + k + fmt.Sprintf(" = $%d", idx)
477479
args = append(args, v)
478480
}
479481

480-
index++
482+
idx++
481483
}
482484

483485
count, err := queryEntryCount(u.slave, countSql, args)
484486
if err != nil {
485487
return 0, nil, err
486488
}
487489

488-
querySql += fmt.Sprintf(" ORDER BY u.mtime LIMIT $%d OFFSET $%d", index, index+1)
490+
querySql += fmt.Sprintf(" ORDER BY u.mtime LIMIT $%d OFFSET $%d", idx, idx+1)
489491
args = append(args, limit, offset)
490492

491493
users, err := u.collectUsers(u.master.Query, querySql, args)

store/postgresql/user_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Tencent is pleased to support the open source community by making Polaris available.
3+
*
4+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package postgresql
19+
20+
import (
21+
"fmt"
22+
"testing"
23+
)
24+
25+
func TestGetUsers(t *testing.T) {
26+
obj := initConf()
27+
filters := map[string]string{
28+
"name": "polaris",
29+
"hide_admin": "true",
30+
"owner": "",
31+
}
32+
cnt, ret, err := obj.userStore.GetUsers(filters, 0, 10)
33+
fmt.Printf("cnt: %+v, ret: %+v, err: %+v\n", cnt, ret, err)
34+
}

0 commit comments

Comments
 (0)