@@ -16,6 +16,8 @@ import (
16
16
)
17
17
18
18
var nameRegex = regexp .MustCompile (`@([a-zA-Z0-9_]+)` )
19
+
20
+ // dbmapper grabs struct 'db' tags.
19
21
var dbmapper = reflectx .NewMapper ("db" )
20
22
var sqlValuer = reflect .TypeOf ((* driver .Valuer )(nil )).Elem ()
21
23
@@ -26,7 +28,7 @@ var sqlValuer = reflect.TypeOf((*driver.Valuer)(nil)).Elem()
26
28
//
27
29
// 1. SQLx does not reuse arguments, so "@arg, @arg" will result in two arguments
28
30
// "$1, $2" instead of "$1, $1".
29
- // 2. SQLx does not handle generic array types
31
+ // 2. SQLx does not handle uuid arrays.
30
32
// 3. SQLx only supports ":name" style arguments and breaks "::" type casting.
31
33
func bindNamed (query string , arg interface {}) (newQuery string , args []interface {}, err error ) {
32
34
// We do not need to implement a sql parser to extract and replace the variable names.
@@ -39,13 +41,14 @@ func bindNamed(query string, arg interface{}) (newQuery string, args []interface
39
41
for i , name := range names {
40
42
rpl := fmt .Sprintf ("$%d" , i + 1 )
41
43
query = strings .ReplaceAll (query , name , rpl )
44
+ // Remove the "@" prefix to match to the "db" struct tag.
42
45
names [i ] = strings .TrimPrefix (name , "@" )
43
46
}
44
47
45
48
arglist := make ([]interface {}, 0 , len (names ))
46
49
47
- // This comes straight from SQLx
48
- // grab the indirected value of arg
50
+ // This comes straight from SQLx's implementation to get the values
51
+ // of the struct fields.
49
52
v := reflect .ValueOf (arg )
50
53
for v = reflect .ValueOf (arg ); v .Kind () == reflect .Ptr ; {
51
54
v = v .Elem ()
@@ -60,6 +63,7 @@ func bindNamed(query string, arg interface{}) (newQuery string, args []interface
60
63
61
64
// Handle some custom types to make arguments easier to use.
62
65
switch val .Interface ().(type ) {
66
+ // Feel free to add more types here as needed.
63
67
case []uuid.UUID :
64
68
arglist = append (arglist , pq .Array (val .Interface ()))
65
69
default :
@@ -74,15 +78,3 @@ func bindNamed(query string, arg interface{}) (newQuery string, args []interface
74
78
75
79
return query , arglist , nil
76
80
}
77
-
78
- type UUIDs []uuid.UUID
79
-
80
- func (ids UUIDs ) Value () (driver.Value , error ) {
81
- v := pq .Array (ids )
82
- return v .Value ()
83
- }
84
-
85
- func (ids * UUIDs ) Scan (src interface {}) error {
86
- v := pq .Array (ids )
87
- return v .Scan (src )
88
- }
0 commit comments