Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Dec 5, 2024
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
13 changes: 13 additions & 0 deletions contrib/drivers/pgsql/pgsql_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package pgsql

import (
"context"
"github.com/lib/pq"
"reflect"
"strings"

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
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 @@ -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 {
Expand Down
59 changes: 59 additions & 0 deletions contrib/drivers/pgsql/pgsql_z_unit_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
1 change: 1 addition & 0 deletions database/gdb/gdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading