@@ -17,6 +17,7 @@ import (
17
17
"github.com/uptrace/bun"
18
18
"github.com/uptrace/bun/dialect/pgdialect"
19
19
"github.com/uptrace/bun/driver/pgdriver"
20
+ "github.com/uptrace/bun/schema"
20
21
)
21
22
22
23
func TestPostgresArray (t * testing.T ) {
@@ -25,16 +26,20 @@ func TestPostgresArray(t *testing.T) {
25
26
Array1 []string `bun:",array"`
26
27
Array2 * []string `bun:",array"`
27
28
Array3 * []string `bun:",array"`
29
+ Array4 []* string `bun:",array"`
28
30
}
29
31
30
32
db := pg (t )
31
33
t .Cleanup (func () { db .Close () })
32
34
mustResetModel (t , ctx , db , (* Model )(nil ))
33
35
36
+ str1 := "hello"
37
+ str2 := "world"
34
38
model1 := & Model {
35
39
ID : 123 ,
36
40
Array1 : []string {"one" , "two" , "three" },
37
41
Array2 : & []string {"hello" , "world" },
42
+ Array4 : []* string {& str1 , & str2 },
38
43
}
39
44
_ , err := db .NewInsert ().Model (model1 ).Exec (ctx )
40
45
require .NoError (t , err )
@@ -56,6 +61,12 @@ func TestPostgresArray(t *testing.T) {
56
61
Scan (ctx , pgdialect .Array (& strs ))
57
62
require .NoError (t , err )
58
63
require .Nil (t , strs )
64
+
65
+ err = db .NewSelect ().Model ((* Model )(nil )).
66
+ Column ("array4" ).
67
+ Scan (ctx , pgdialect .Array (& strs ))
68
+ require .NoError (t , err )
69
+ require .Equal (t , []string {"hello" , "world" }, strs )
59
70
}
60
71
61
72
func TestPostgresArrayQuote (t * testing.T ) {
@@ -877,3 +888,78 @@ func TestPostgresMultiRange(t *testing.T) {
877
888
err = db .NewSelect ().Model (out ).Scan (ctx )
878
889
require .NoError (t , err )
879
890
}
891
+
892
+ type UserID struct {
893
+ ID string
894
+ }
895
+
896
+ func (u UserID ) AppendQuery (fmter schema.Formatter , b []byte ) ([]byte , error ) {
897
+ v := []byte (`"` + u .ID + `"` )
898
+ return append (b , v ... ), nil
899
+ }
900
+
901
+ var _ schema.QueryAppender = (* UserID )(nil )
902
+
903
+ func (r * UserID ) Scan (anySrc any ) (err error ) {
904
+ src , ok := anySrc .([]byte )
905
+ if ! ok {
906
+ return fmt .Errorf ("pgdialect: Range can't scan %T" , anySrc )
907
+ }
908
+
909
+ r .ID = string (src )
910
+ return nil
911
+ }
912
+
913
+ var _ sql.Scanner = (* UserID )(nil )
914
+
915
+ func TestPostgresJSONB (t * testing.T ) {
916
+ type Item struct {
917
+ Name string `json:"name"`
918
+ }
919
+ type Model struct {
920
+ ID int64 `bun:",pk,autoincrement"`
921
+ Item Item `bun:",type:jsonb"`
922
+ ItemPtr * Item `bun:",type:jsonb"`
923
+ Items []Item `bun:",type:jsonb"`
924
+ ItemsP []* Item `bun:",type:jsonb"`
925
+ TextItemA []UserID `bun:"type:text[]"`
926
+ }
927
+
928
+ db := pg (t )
929
+ t .Cleanup (func () { db .Close () })
930
+ mustResetModel (t , ctx , db , (* Model )(nil ))
931
+
932
+ item1 := Item {Name : "one" }
933
+ item2 := Item {Name : "two" }
934
+ uid1 := UserID {ID : "1" }
935
+ uid2 := UserID {ID : "2" }
936
+ model1 := & Model {
937
+ ID : 123 ,
938
+ Item : item1 ,
939
+ ItemPtr : & item2 ,
940
+ Items : []Item {item1 , item2 },
941
+ ItemsP : []* Item {& item1 , & item2 },
942
+ TextItemA : []UserID {uid1 , uid2 },
943
+ }
944
+ _ , err := db .NewInsert ().Model (model1 ).Exec (ctx )
945
+ require .NoError (t , err )
946
+
947
+ model2 := new (Model )
948
+ err = db .NewSelect ().Model (model2 ).Scan (ctx )
949
+ require .NoError (t , err )
950
+ require .Equal (t , model1 , model2 )
951
+
952
+ var items []Item
953
+ err = db .NewSelect ().Model ((* Model )(nil )).
954
+ Column ("items" ).
955
+ Scan (ctx , pgdialect .Array (& items ))
956
+ require .NoError (t , err )
957
+ require .Equal (t , []Item {item1 , item2 }, items )
958
+
959
+ err = db .NewSelect ().Model ((* Model )(nil )).
960
+ Column ("itemsp" ).
961
+ Scan (ctx , pgdialect .Array (& items ))
962
+ require .NoError (t , err )
963
+ require .Equal (t , []Item {item1 , item2 }, items )
964
+
965
+ }
0 commit comments