mirror of
https://github.com/gogf/gf.git
synced 2025-04-05 11:18:50 +08:00
contrib/drivers/pgsql: support slice type to insert into array for pgsql (#3645)
This commit is contained in:
parent
74d0945fa1
commit
4abb32e11b
@ -8,6 +8,7 @@ package pgsql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
@ -16,6 +17,23 @@ import (
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
// ConvertValueForField converts value to database acceptable value.
|
||||
func (d *Driver) ConvertValueForField(ctx context.Context, fieldType string, fieldValue interface{}) (interface{}, error) {
|
||||
var (
|
||||
fieldValueKind = reflect.TypeOf(fieldValue).Kind()
|
||||
)
|
||||
|
||||
if fieldValueKind == reflect.Slice {
|
||||
fieldValue = gstr.ReplaceByMap(gconv.String(fieldValue),
|
||||
map[string]string{
|
||||
"[": "{",
|
||||
"]": "}",
|
||||
},
|
||||
)
|
||||
}
|
||||
return d.Core.ConvertValueForField(ctx, fieldType, fieldValue)
|
||||
}
|
||||
|
||||
// CheckLocalTypeForField checks and returns corresponding local golang type for given db type.
|
||||
func (d *Driver) CheckLocalTypeForField(ctx context.Context, fieldType string, fieldValue interface{}) (gdb.LocalType, error) {
|
||||
var typeName string
|
||||
|
@ -8,9 +8,10 @@ package pgsql_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
@ -73,3 +74,32 @@ func Test_Issue3330(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// https://github.com/gogf/gf/issues/3632
|
||||
func Test_Issue3632(t *testing.T) {
|
||||
type Member struct {
|
||||
One []int64 `json:"one" orm:"one"`
|
||||
Two [][]string `json:"two" orm:"two"`
|
||||
}
|
||||
var (
|
||||
sqlText = gtest.DataContent("issues", "issue3632.sql")
|
||||
table = fmt.Sprintf(`%s_%d`, TablePrefix+"issue3632", gtime.TimestampNano())
|
||||
)
|
||||
if _, err := db.Exec(ctx, fmt.Sprintf(sqlText, table)); err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
dao = db.Model(table)
|
||||
member = Member{
|
||||
One: []int64{1, 2, 3},
|
||||
Two: [][]string{{"a", "b"}, {"c", "d"}},
|
||||
}
|
||||
)
|
||||
|
||||
_, err := dao.Ctx(ctx).Data(&member).Insert()
|
||||
t.AssertNil(err)
|
||||
})
|
||||
}
|
||||
|
4
contrib/drivers/pgsql/testdata/issues/issue3632.sql
vendored
Normal file
4
contrib/drivers/pgsql/testdata/issues/issue3632.sql
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
CREATE TABLE "public"."%s" (
|
||||
"one" int8[] NOT NULL,
|
||||
"two" text[][] NOT NULL
|
||||
);
|
Loading…
x
Reference in New Issue
Block a user