Skip to content

Commit

Permalink
fix: 4086
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn committed Feb 27, 2025
1 parent 4f069ef commit 67bb75a
Show file tree
Hide file tree
Showing 19 changed files with 101 additions and 639 deletions.
3 changes: 2 additions & 1 deletion contrib/drivers/mysql/mysql_z_unit_feature_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"fmt"
"testing"

"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/test/gtest"
Expand All @@ -29,7 +30,7 @@ func Test_Model_Hook_Select(t *testing.T) {
return
}
for i, record := range result {
record["test"] = gdb.NewValue(100 + record["id"].Int())
record["test"] = gvar.New(100 + record["id"].Int())
result[i] = record
}
return
Expand Down
3 changes: 2 additions & 1 deletion contrib/drivers/mysql/mysql_z_unit_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"testing"
"time"

"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
Expand Down Expand Up @@ -1508,7 +1509,7 @@ func Test_Issue3968(t *testing.T) {
}
if result != nil {
for i, _ := range result {
result[i]["location"] = gdb.NewValue("ny")
result[i]["location"] = gvar.New("ny")
}
}
return
Expand Down
125 changes: 63 additions & 62 deletions contrib/drivers/mysql/mysql_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"time"

"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/encoding/gjson"
"github.com/gogf/gf/v2/frame/g"
Expand Down Expand Up @@ -3366,14 +3367,14 @@ func Test_ScanList_NoRecreate_PtrAttribute(t *testing.T) {
)
r1 := gdb.Result{
gdb.Record{
"id": gdb.NewValue(1),
"name": gdb.NewValue("john"),
"age": gdb.NewValue(16),
"id": gvar.New(1),
"name": gvar.New("john"),
"age": gvar.New(16),
},
gdb.Record{
"id": gdb.NewValue(2),
"name": gdb.NewValue("smith"),
"age": gdb.NewValue(18),
"id": gvar.New(2),
"name": gvar.New("smith"),
"age": gvar.New(18),
},
}
err = r1.ScanList(&s, "One")
Expand All @@ -3386,12 +3387,12 @@ func Test_ScanList_NoRecreate_PtrAttribute(t *testing.T) {

r2 := gdb.Result{
gdb.Record{
"id": gdb.NewValue(1),
"age": gdb.NewValue(20),
"id": gvar.New(1),
"age": gvar.New(20),
},
gdb.Record{
"id": gdb.NewValue(2),
"age": gdb.NewValue(21),
"id": gvar.New(2),
"age": gvar.New(21),
},
}
err = r2.ScanList(&s, "One", "One", "id:Id")
Expand Down Expand Up @@ -3422,14 +3423,14 @@ func Test_ScanList_NoRecreate_StructAttribute(t *testing.T) {
)
r1 := gdb.Result{
gdb.Record{
"id": gdb.NewValue(1),
"name": gdb.NewValue("john"),
"age": gdb.NewValue(16),
"id": gvar.New(1),
"name": gvar.New("john"),
"age": gvar.New(16),
},
gdb.Record{
"id": gdb.NewValue(2),
"name": gdb.NewValue("smith"),
"age": gdb.NewValue(18),
"id": gvar.New(2),
"name": gvar.New("smith"),
"age": gvar.New(18),
},
}
err = r1.ScanList(&s, "One")
Expand All @@ -3442,12 +3443,12 @@ func Test_ScanList_NoRecreate_StructAttribute(t *testing.T) {

r2 := gdb.Result{
gdb.Record{
"id": gdb.NewValue(1),
"age": gdb.NewValue(20),
"id": gvar.New(1),
"age": gvar.New(20),
},
gdb.Record{
"id": gdb.NewValue(2),
"age": gdb.NewValue(21),
"id": gvar.New(2),
"age": gvar.New(21),
},
}
err = r2.ScanList(&s, "One", "One", "id:Id")
Expand Down Expand Up @@ -3486,14 +3487,14 @@ func Test_ScanList_NoRecreate_SliceAttribute_Ptr(t *testing.T) {
)
r1 := gdb.Result{
gdb.Record{
"id": gdb.NewValue(1),
"name": gdb.NewValue("john"),
"age": gdb.NewValue(16),
"id": gvar.New(1),
"name": gvar.New("john"),
"age": gvar.New(16),
},
gdb.Record{
"id": gdb.NewValue(2),
"name": gdb.NewValue("smith"),
"age": gdb.NewValue(18),
"id": gvar.New(2),
"name": gvar.New("smith"),
"age": gvar.New(18),
},
}
err = r1.ScanList(&s, "One")
Expand All @@ -3506,16 +3507,16 @@ func Test_ScanList_NoRecreate_SliceAttribute_Ptr(t *testing.T) {

r2 := gdb.Result{
gdb.Record{
"id": gdb.NewValue(100),
"pid": gdb.NewValue(1),
"age": gdb.NewValue(30),
"name": gdb.NewValue("john"),
"id": gvar.New(100),
"pid": gvar.New(1),
"age": gvar.New(30),
"name": gvar.New("john"),
},
gdb.Record{
"id": gdb.NewValue(200),
"pid": gdb.NewValue(1),
"age": gdb.NewValue(31),
"name": gdb.NewValue("smith"),
"id": gvar.New(200),
"pid": gvar.New(1),
"age": gvar.New(31),
"name": gvar.New("smith"),
},
}
err = r2.ScanList(&s, "Many", "One", "pid:Id")
Expand All @@ -3536,14 +3537,14 @@ func Test_ScanList_NoRecreate_SliceAttribute_Ptr(t *testing.T) {

r3 := gdb.Result{
gdb.Record{
"id": gdb.NewValue(100),
"pid": gdb.NewValue(1),
"age": gdb.NewValue(40),
"id": gvar.New(100),
"pid": gvar.New(1),
"age": gvar.New(40),
},
gdb.Record{
"id": gdb.NewValue(200),
"pid": gdb.NewValue(1),
"age": gdb.NewValue(41),
"id": gvar.New(200),
"pid": gvar.New(1),
"age": gvar.New(41),
},
}
err = r3.ScanList(&s, "Many", "One", "pid:Id")
Expand Down Expand Up @@ -3590,14 +3591,14 @@ func Test_ScanList_NoRecreate_SliceAttribute_Struct(t *testing.T) {
)
r1 := gdb.Result{
gdb.Record{
"id": gdb.NewValue(1),
"name": gdb.NewValue("john"),
"age": gdb.NewValue(16),
"id": gvar.New(1),
"name": gvar.New("john"),
"age": gvar.New(16),
},
gdb.Record{
"id": gdb.NewValue(2),
"name": gdb.NewValue("smith"),
"age": gdb.NewValue(18),
"id": gvar.New(2),
"name": gvar.New("smith"),
"age": gvar.New(18),
},
}
err = r1.ScanList(&s, "One")
Expand All @@ -3610,16 +3611,16 @@ func Test_ScanList_NoRecreate_SliceAttribute_Struct(t *testing.T) {

r2 := gdb.Result{
gdb.Record{
"id": gdb.NewValue(100),
"pid": gdb.NewValue(1),
"age": gdb.NewValue(30),
"name": gdb.NewValue("john"),
"id": gvar.New(100),
"pid": gvar.New(1),
"age": gvar.New(30),
"name": gvar.New("john"),
},
gdb.Record{
"id": gdb.NewValue(200),
"pid": gdb.NewValue(1),
"age": gdb.NewValue(31),
"name": gdb.NewValue("smith"),
"id": gvar.New(200),
"pid": gvar.New(1),
"age": gvar.New(31),
"name": gvar.New("smith"),
},
}
err = r2.ScanList(&s, "Many", "One", "pid:Id")
Expand All @@ -3640,14 +3641,14 @@ func Test_ScanList_NoRecreate_SliceAttribute_Struct(t *testing.T) {

r3 := gdb.Result{
gdb.Record{
"id": gdb.NewValue(100),
"pid": gdb.NewValue(1),
"age": gdb.NewValue(40),
"id": gvar.New(100),
"pid": gvar.New(1),
"age": gvar.New(40),
},
gdb.Record{
"id": gdb.NewValue(200),
"pid": gdb.NewValue(1),
"age": gdb.NewValue(41),
"id": gvar.New(200),
"pid": gvar.New(1),
"age": gvar.New(41),
},
}
err = r3.ScanList(&s, "Many", "One", "pid:Id")
Expand Down Expand Up @@ -3678,8 +3679,8 @@ func TestResult_Structs1(t *testing.T) {
}
gtest.C(t, func(t *gtest.T) {
r := gdb.Result{
gdb.Record{"id": gdb.NewValue(nil), "name": gdb.NewValue("john")},
gdb.Record{"id": gdb.NewValue(1), "name": gdb.NewValue("smith")},
gdb.Record{"id": gvar.New(nil), "name": gvar.New("john")},
gdb.Record{"id": gvar.New(1), "name": gvar.New("smith")},
}
array := make([]*B, 2)
err := r.Structs(&array)
Expand Down
51 changes: 26 additions & 25 deletions database/gdb/gdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import (
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/container/gtype"
"github.com/gogf/gf/v2/database/gdb/internal/defines"
"github.com/gogf/gf/v2/database/gdb/internal/fieldvar"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/os/gcache"
Expand Down Expand Up @@ -669,7 +668,7 @@ type (
Raw string

// Value is the field value type.
Value = *fieldvar.Var
Value = *gvar.Var

// Record is the row record of the table.
Record map[string]Value
Expand Down Expand Up @@ -770,30 +769,32 @@ const (
SqlTypeStmtQueryRowContext SqlType = "DB.Statement.QueryRowContext"
)

type LocalType = defines.LocalType
// LocalType is a type that defines the local storage type of a field value.
// It is used to specify how the field value should be processed locally.
type LocalType string

const (
LocalTypeUndefined = defines.LocalTypeUndefined
LocalTypeString = defines.LocalTypeString
LocalTypeTime = defines.LocalTypeTime
LocalTypeDate = defines.LocalTypeDate
LocalTypeDatetime = defines.LocalTypeDatetime
LocalTypeInt = defines.LocalTypeInt
LocalTypeUint = defines.LocalTypeUint
LocalTypeInt64 = defines.LocalTypeInt64
LocalTypeUint64 = defines.LocalTypeUint64
LocalTypeIntSlice = defines.LocalTypeIntSlice
LocalTypeInt64Slice = defines.LocalTypeInt64Slice
LocalTypeUint64Slice = defines.LocalTypeUint64Slice
LocalTypeStringSlice = defines.LocalTypeStringSlice
LocalTypeInt64Bytes = defines.LocalTypeInt64Bytes
LocalTypeUint64Bytes = defines.LocalTypeUint64Bytes
LocalTypeFloat32 = defines.LocalTypeFloat32
LocalTypeFloat64 = defines.LocalTypeFloat64
LocalTypeBytes = defines.LocalTypeBytes
LocalTypeBool = defines.LocalTypeBool
LocalTypeJson = defines.LocalTypeJson
LocalTypeJsonb = defines.LocalTypeJsonb
LocalTypeUndefined LocalType = ""
LocalTypeString LocalType = "string"
LocalTypeTime LocalType = "time"
LocalTypeDate LocalType = "date"
LocalTypeDatetime LocalType = "datetime"
LocalTypeInt LocalType = "int"
LocalTypeUint LocalType = "uint"
LocalTypeInt64 LocalType = "int64"
LocalTypeUint64 LocalType = "uint64"
LocalTypeIntSlice LocalType = "[]int"
LocalTypeInt64Slice LocalType = "[]int64"
LocalTypeUint64Slice LocalType = "[]uint64"
LocalTypeStringSlice LocalType = "[]string"
LocalTypeInt64Bytes LocalType = "int64-bytes"
LocalTypeUint64Bytes LocalType = "uint64-bytes"
LocalTypeFloat32 LocalType = "float32"
LocalTypeFloat64 LocalType = "float64"
LocalTypeBytes LocalType = "[]byte"
LocalTypeBool LocalType = "bool"
LocalTypeJson LocalType = "json"
LocalTypeJsonb LocalType = "jsonb"
)

const (
Expand Down
5 changes: 3 additions & 2 deletions database/gdb/gdb_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/gogf/gf/v2/container/gmap"
"github.com/gogf/gf/v2/container/gset"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/intlog"
Expand Down Expand Up @@ -239,12 +240,12 @@ func (c *Core) GetScan(ctx context.Context, pointer interface{}, sql string, arg
func (c *Core) GetValue(ctx context.Context, sql string, args ...interface{}) (Value, error) {
one, err := c.db.GetOne(ctx, sql, args...)
if err != nil {
return NewValue(nil), err
return gvar.New(nil), err
}
for _, v := range one {
return v, nil
}
return NewValue(nil), nil
return gvar.New(nil), nil
}

// GetCount queries and returns the count from database.
Expand Down
8 changes: 2 additions & 6 deletions database/gdb/gdb_core_underlying.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"fmt"
"reflect"

"github.com/gogf/gf/v2/container/gvar"

Check failure on line 16 in database/gdb/gdb_core_underlying.go

View workflow job for this annotation

GitHub Actions / golang-ci-lint (stable)

File is not properly formatted (gci)
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"

Check failure on line 19 in database/gdb/gdb_core_underlying.go

View workflow job for this annotation

GitHub Actions / golang-ci-lint (stable)

File is not properly formatted (goimports)
Expand Down Expand Up @@ -480,16 +481,11 @@ func (c *Core) RowsToResult(ctx context.Context, rows *sql.Rows) (Result, error)
var (
convertedValue interface{}
columnType = columnTypes[i]
localType = LocalTypeUndefined
)
if convertedValue, err = c.columnValueToLocalValue(ctx, value, columnType); err != nil {
return nil, err
}
localType, err = c.getLocalTypeForFieldWithCache(ctx, columnType.DatabaseTypeName(), convertedValue)
if err != nil {
return nil, err
}
record[columnTypes[i].Name()] = NewValueWithType(convertedValue, localType)
record[columnTypes[i].Name()] = gvar.New(convertedValue)
}
}
result = append(result, record)
Expand Down
Loading

0 comments on commit 67bb75a

Please sign in to comment.