1
0
mirror of https://github.com/gogf/gf.git synced 2025-04-05 03:05:05 +08:00

feat(contrib/drivers/pgsql): add array type varchar[] and text[] converting to Go []string support (#4000)

This commit is contained in:
ninjashixuan 2024-12-05 15:50:59 +08:00 committed by GitHub
parent 5fa33411fc
commit 4ad061faff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 75 additions and 0 deletions

View File

@ -8,6 +8,7 @@ package pgsql
import (
"context"
"github.com/lib/pq"
"reflect"
"strings"
@ -72,6 +73,10 @@ func (d *Driver) CheckLocalTypeForField(ctx context.Context, fieldType string, f
"_int8":
return gdb.LocalTypeInt64Slice, nil
case
"_varchar", "_text":
return gdb.LocalTypeStringSlice, nil
default:
return d.Core.CheckLocalTypeForField(ctx, fieldType, fieldValue)
}
@ -116,6 +121,14 @@ func (d *Driver) ConvertValueForLocal(ctx context.Context, fieldType string, fie
),
), nil
// String slice.
case "_varchar", "_text":
var result pq.StringArray
if err := result.Scan(fieldValue); err != nil {
return nil, err
}
return result, nil
default:
return d.Core.ConvertValueForLocal(ctx, fieldType, fieldValue)
}

View File

@ -83,6 +83,8 @@ func createTableWithDb(db gdb.DB, table ...string) (name string) {
password varchar(32) NOT NULL,
nickname varchar(45) NOT NULL,
create_time timestamp NOT NULL,
favorite_movie varchar[],
favorite_music text[],
PRIMARY KEY (id)
) ;`, name,
)); err != nil {

View File

@ -611,3 +611,62 @@ func Test_OrderRandom(t *testing.T) {
t.Assert(len(result), TableSize)
})
}
func Test_ConvertSliceString(t *testing.T) {
table := createTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
type User struct {
Id int
Passport string
Password string
NickName string
CreateTime *gtime.Time
FavoriteMovie []string
FavoriteMusic []string
}
var (
user User
user2 User
err error
)
// slice string not null
_, err = db.Model(table).Data(g.Map{
"id": 1,
"passport": "p1",
"password": "pw1",
"nickname": "n1",
"create_time": CreateTime,
"favorite_movie": g.Slice{"Iron-Man", "Spider-Man"},
"favorite_music": g.Slice{"Hey jude", "Let it be"},
}).Insert()
t.AssertNil(err)
err = db.Model(table).Where("id", 1).Scan(&user)
t.AssertNil(err)
t.Assert(len(user.FavoriteMusic), 2)
t.Assert(user.FavoriteMusic[0], "Hey jude")
t.Assert(user.FavoriteMusic[1], "Let it be")
t.Assert(len(user.FavoriteMovie), 2)
t.Assert(user.FavoriteMovie[0], "Iron-Man")
t.Assert(user.FavoriteMovie[1], "Spider-Man")
// slice string null
_, err = db.Model(table).Data(g.Map{
"id": 2,
"passport": "p1",
"password": "pw1",
"nickname": "n1",
"create_time": CreateTime,
}).Insert()
t.AssertNil(err)
err = db.Model(table).Where("id", 2).Scan(&user2)
t.AssertNil(err)
t.Assert(user2.FavoriteMusic, nil)
t.Assert(len(user2.FavoriteMovie), 0)
})
}

View File

@ -458,6 +458,7 @@ const (
LocalTypeIntSlice LocalType = "[]int"
LocalTypeInt64Slice LocalType = "[]int64"
LocalTypeUint64Slice LocalType = "[]uint64"
LocalTypeStringSlice LocalType = "[]string"
LocalTypeInt64Bytes LocalType = "int64-bytes"
LocalTypeUint64Bytes LocalType = "uint64-bytes"
LocalTypeFloat32 LocalType = "float32"