fix(contrib/drivers/pgsql): #3671 fix invalid pgsql insert json type (#3742)

This commit is contained in:
Haowen_G 2024-09-09 14:32:14 +08:00 committed by GitHub
parent 26d2a98fb1
commit 467a5c1093
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 56 additions and 6 deletions

View File

@ -24,12 +24,15 @@ func (d *Driver) ConvertValueForField(ctx context.Context, fieldType string, fie
)
if fieldValueKind == reflect.Slice {
fieldValue = gstr.ReplaceByMap(gconv.String(fieldValue),
map[string]string{
"[": "{",
"]": "}",
},
)
// For pgsql, json or jsonb require '[]'
if !gstr.Contains(fieldType, "json") {
fieldValue = gstr.ReplaceByMap(gconv.String(fieldValue),
map[string]string{
"[": "{",
"]": "}",
},
)
}
}
return d.Core.ConvertValueForField(ctx, fieldType, fieldValue)
}

View File

@ -103,3 +103,42 @@ func Test_Issue3632(t *testing.T) {
t.AssertNil(err)
})
}
// https://github.com/gogf/gf/issues/3671
func Test_Issue3671(t *testing.T) {
type SubMember struct {
Seven string
Eight int64
}
type Member struct {
One []int64 `json:"one" orm:"one"`
Two [][]string `json:"two" orm:"two"`
Three []string `json:"three" orm:"three"`
Four []int64 `json:"four" orm:"four"`
Five []SubMember `json:"five" orm:"five"`
}
var (
sqlText = gtest.DataContent("issues", "issue3671.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"}},
Three: []string{"x", "y", "z"},
Four: []int64{1, 2, 3},
Five: []SubMember{{Seven: "1", Eight: 2}, {Seven: "3", Eight: 4}},
}
)
_, err := dao.Ctx(ctx).Data(&member).Insert()
t.AssertNil(err)
})
}

View File

@ -0,0 +1,8 @@
CREATE TABLE "public"."%s"
(
"one" int8[] NOT NULL,
"two" text[][] NOT NULL,
"three" jsonb,
"four" json,
"five" jsonb
);