Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions contrib/drivers/pgsql/pgsql_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func (d *Driver) CheckLocalTypeForField(ctx context.Context, fieldType string, f
case
"_varchar", "_text":
return gdb.LocalTypeStringSlice, nil
case "_numeric", "_decimal":
return gdb.LocalTypeFloat64Slice, nil

default:
return d.Core.CheckLocalTypeForField(ctx, fieldType, fieldValue)
Expand Down Expand Up @@ -130,6 +132,13 @@ func (d *Driver) ConvertValueForLocal(ctx context.Context, fieldType string, fie
}
return []string(result), nil

// Float64 slice.
case "_numeric", "_decimal":
var result pq.Float64Array
if err := result.Scan(fieldValue); err != nil {
return nil, err
}
return []float64(result), nil
default:
return d.Core.ConvertValueForLocal(ctx, fieldType, fieldValue)
}
Expand Down
2 changes: 2 additions & 0 deletions contrib/drivers/pgsql/pgsql_z_unit_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ func createTableWithDb(db gdb.DB, table ...string) (name string) {
create_time timestamp NOT NULL,
favorite_movie varchar[],
favorite_music text[],
numeric_values numeric[],
decimal_values decimal[],
PRIMARY KEY (id)
) ;`, name,
)); err != nil {
Expand Down
65 changes: 65 additions & 0 deletions contrib/drivers/pgsql/pgsql_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,3 +692,68 @@ func Test_ConvertSliceString(t *testing.T) {
t.Assert(len(user2.FavoriteMovie), 0)
})
}

func Test_ConvertSliceFloat64(t *testing.T) {
table := createTable()
defer dropTable(table)

type Args struct {
NumericValues []float64 `orm:"numeric_values"`
DecimalValues []float64 `orm:"decimal_values"`
}
type User struct {
Id int `orm:"id"`
Passport string `orm:"passport"`
Password string `json:"password"`
NickName string `json:"nickname"`
CreateTime *gtime.Time `json:"create_time"`
Args
}

tests := []struct {
name string
args Args
}{
{
name: "nil",
args: Args{
NumericValues: nil,
DecimalValues: nil,
},
},
{
name: "not nil",
args: Args{
NumericValues: []float64{1.1, 2.2, 3.3},
DecimalValues: []float64{1.1, 2.2, 3.3},
},
},
{
name: "not empty",
args: Args{
NumericValues: []float64{},
DecimalValues: []float64{},
},
},
}
now := gtime.New(CreateTime)
for i, tt := range tests {
gtest.C(t, func(t *gtest.T) {
user := User{
Id: i + 1,
Passport: "",
Password: "",
NickName: "",
CreateTime: now,
Args: tt.args,
}

_, err := db.Model(table).OmitNilData().Insert(user)
t.AssertNil(err)
var got Args
err = db.Model(table).Where("id", user.Id).Limit(1).Scan(&got)
t.AssertNil(err)
t.AssertEQ(tt.args, got)
})
}
}
45 changes: 23 additions & 22 deletions database/gdb/gdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,28 +787,29 @@ const (
type LocalType string

const (
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"
LocalTypeBigInt LocalType = "bigint"
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"
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"
LocalTypeBigInt LocalType = "bigint"
LocalTypeIntSlice LocalType = "[]int"
LocalTypeInt64Slice LocalType = "[]int64"
LocalTypeUint64Slice LocalType = "[]uint64"
LocalTypeStringSlice LocalType = "[]string"
LocalTypeFloat64Slice LocalType = "[]float64"
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
Loading