Skip to content

Commit

Permalink
fix: 修复pluck
Browse files Browse the repository at this point in the history
Change-Id: I5cd51ac3055b85ebcf8b13d2b9c6e931955df1f5
  • Loading branch information
Pacific73 committed Oct 30, 2023
1 parent 1bae144 commit 07cc465
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
10 changes: 9 additions & 1 deletion cache/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ func getPrimaryKeysFromExpr(expr clause.Expr, ttype string) []string {
func getObjectsAfterLoad(db *gorm.DB) (primaryKeys []string, objects []interface{}) {
primaryKeys = make([]string, 0)
values := make([]reflect.Value, 0)
isPluck := false

destValue := reflect.Indirect(reflect.ValueOf(db.Statement.Dest))
switch destValue.Kind() {
Expand All @@ -225,6 +226,9 @@ func getObjectsAfterLoad(db *gorm.DB) (primaryKeys []string, objects []interface
elem := destValue.Index(i)
values = append(values, elem)
}
if isBasicType(destValue.Type().Elem().Kind()) {
isPluck = true
}
case reflect.Struct:
values = append(values, destValue)
}
Expand All @@ -241,7 +245,7 @@ func getObjectsAfterLoad(db *gorm.DB) (primaryKeys []string, objects []interface

objects = make([]interface{}, 0, len(values))
for _, elemValue := range values {
if valueOf != nil {
if valueOf != nil && !isPluck {
primaryKey, isZero := valueOf(context.Background(), elemValue)
if isZero {
continue
Expand All @@ -253,6 +257,10 @@ func getObjectsAfterLoad(db *gorm.DB) (primaryKeys []string, objects []interface
return primaryKeys, objects
}

func isBasicType(k reflect.Kind) bool {
return (k > 0 && k < reflect.Array) || (k == reflect.String)
}

func uniqueStringSlice(slice []string) []string {
retSlice := make([]string, 0)
mmap := make(map[string]struct{})
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.15
require (
github.com/go-redis/redis/v8 v8.11.5
github.com/karlseguin/ccache/v2 v2.0.8
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.19.0 // indirect
github.com/smartystreets/goconvey v1.7.2
gorm.io/driver/mysql v1.3.3
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cu
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg=
github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
Expand Down
2 changes: 2 additions & 0 deletions testkit/functionality_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func TestSearchCacheFunctionality(t *testing.T) {

testFind(searchCache, searchDB)

testPluck(searchCache, searchDB)

testSearchFind(searchCache, searchDB)

testSearchCreate(searchCache, searchDB)
Expand Down
17 changes: 17 additions & 0 deletions testkit/test_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ func testFind(cache *cache.Gorm2Cache, db *gorm.DB) {
So(models[1].Value1, ShouldEqual, 2)
}

func testPluck(cache *cache.Gorm2Cache, db *gorm.DB) {
err := cache.ResetCache()
So(err, ShouldBeNil)
So(cache.GetHitCount(), ShouldEqual, 0)

rawInts := make([]int64, 0)
result := db.Model(&TestModel{}).Where("id IN (?)", []int{1, 2}).Pluck("value1", &rawInts)
So(result.Error, ShouldBeNil)
So(cache.GetHitCount(), ShouldEqual, 0)

cacheInts := make([]int64, 0)
result = db.Model(&TestModel{}).Where("id IN (?)", []int{1, 2}).Pluck("value1", &cacheInts)
So(result.Error, ShouldBeNil)
So(cache.GetHitCount(), ShouldEqual, 1)
So(cacheInts, ShouldResemble, rawInts)
}

func testPtrFind(cache *cache.Gorm2Cache, db *gorm.DB) {
err := cache.ResetCache()
So(err, ShouldBeNil)
Expand Down

0 comments on commit 07cc465

Please sign in to comment.