Skip to content

Commit 15b90b7

Browse files
committed
convert all interface{} type to any type
1 parent 1b0a74a commit 15b90b7

12 files changed

+220
-220
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ explains how to use `database/sql` along with sqlx.
2424

2525
* `sqlx.DB.Connx(context.Context) *sqlx.Conn`
2626
* `sqlx.BindDriver(driverName, bindType)`
27-
* support for `[]map[string]interface{}` to do "batch" insertions
27+
* support for `[]map[string]any` to do "batch" insertions
2828
* allocation & perf improvements for `sqlx.In`
2929

3030
DB.Connx returns an `sqlx.Conn`, which is an `sql.Conn`-alike consistent with
@@ -127,7 +127,7 @@ func main() {
127127
tx.NamedExec("INSERT INTO person (first_name, last_name, email) VALUES (:first_name, :last_name, :email)", &Person{"Jane", "Citizen", "[email protected]"})
128128
tx.Commit()
129129

130-
// Query the database, storing results in a []Person (wrapped in []interface{})
130+
// Query the database, storing results in a []Person (wrapped in []any)
131131
people := []Person{}
132132
db.Select(&people, "SELECT * FROM person ORDER BY first_name ASC")
133133
jason, john := people[0], people[1]
@@ -173,14 +173,14 @@ func main() {
173173
// Named queries, using `:name` as the bindvar. Automatic bindvar support
174174
// which takes into account the dbtype based on the driverName on sqlx.Open/Connect
175175
_, err = db.NamedExec(`INSERT INTO person (first_name,last_name,email) VALUES (:first,:last,:email)`,
176-
map[string]interface{}{
176+
map[string]any{
177177
"first": "Bin",
178178
"last": "Smuth",
179179
"email": "[email protected]",
180180
})
181181

182182
// Selects Mr. Smith from the database
183-
rows, err = db.NamedQuery(`SELECT * FROM person WHERE first_name=:fn`, map[string]interface{}{"fn": "Bin"})
183+
rows, err = db.NamedQuery(`SELECT * FROM person WHERE first_name=:fn`, map[string]any{"fn": "Bin"})
184184

185185
// Named queries can also use structs. Their bind names follow the same rules
186186
// as the name -> db mapping, so struct fields are lowercased and the `db` tag
@@ -201,7 +201,7 @@ func main() {
201201
VALUES (:first_name, :last_name, :email)`, personStructs)
202202

203203
// batch insert with maps
204-
personMaps := []map[string]interface{}{
204+
personMaps := []map[string]any{
205205
{"first_name": "Ardie", "last_name": "Savea", "email": "[email protected]"},
206206
{"first_name": "Sonny Bill", "last_name": "Williams", "email": "[email protected]"},
207207
{"first_name": "Ngani", "last_name": "Laumape", "email": "[email protected]"},

bind.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func rebindBuff(bindType int, query string) string {
114114
return rqb.String()
115115
}
116116

117-
func asSliceForIn(i interface{}) (v reflect.Value, ok bool) {
117+
func asSliceForIn(i any) (v reflect.Value, ok bool) {
118118
if i == nil {
119119
return reflect.Value{}, false
120120
}
@@ -139,12 +139,12 @@ func asSliceForIn(i interface{}) (v reflect.Value, ok bool) {
139139
// In expands slice values in args, returning the modified query string
140140
// and a new arg list that can be executed by a database. The `query` should
141141
// use the `?` bindVar. The return value uses the `?` bindVar.
142-
func In(query string, args ...interface{}) (string, []interface{}, error) {
142+
func In(query string, args ...any) (string, []any, error) {
143143
// argMeta stores reflect.Value and length for slices and
144144
// the value itself for non-slice arguments
145145
type argMeta struct {
146146
v reflect.Value
147-
i interface{}
147+
i any
148148
length int
149149
}
150150

@@ -191,7 +191,7 @@ func In(query string, args ...interface{}) (string, []interface{}, error) {
191191
return query, args, nil
192192
}
193193

194-
newArgs := make([]interface{}, 0, flatArgsCount)
194+
newArgs := make([]any, 0, flatArgsCount)
195195

196196
var buf strings.Builder
197197
buf.Grow(len(query) + len(", ?")*flatArgsCount)
@@ -243,9 +243,9 @@ func In(query string, args ...interface{}) (string, []interface{}, error) {
243243
return buf.String(), newArgs, nil
244244
}
245245

246-
func appendReflectSlice(args []interface{}, v reflect.Value, vlen int) []interface{} {
246+
func appendReflectSlice(args []any, v reflect.Value, vlen int) []any {
247247
switch val := v.Interface().(type) {
248-
case []interface{}:
248+
case []any:
249249
args = append(args, val...)
250250
case []int:
251251
for i := range val {

named.go

+35-35
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (n *NamedStmt) Close() error {
3939

4040
// Exec executes a named statement using the struct passed.
4141
// Any named placeholder parameters are replaced with fields from arg.
42-
func (n *NamedStmt) Exec(arg interface{}) (sql.Result, error) {
42+
func (n *NamedStmt) Exec(arg any) (sql.Result, error) {
4343
args, err := bindAnyArgs(n.Params, arg, n.Stmt.Mapper)
4444
if err != nil {
4545
return *new(sql.Result), err
@@ -49,7 +49,7 @@ func (n *NamedStmt) Exec(arg interface{}) (sql.Result, error) {
4949

5050
// Query executes a named statement using the struct argument, returning rows.
5151
// Any named placeholder parameters are replaced with fields from arg.
52-
func (n *NamedStmt) Query(arg interface{}) (*sql.Rows, error) {
52+
func (n *NamedStmt) Query(arg any) (*sql.Rows, error) {
5353
args, err := bindAnyArgs(n.Params, arg, n.Stmt.Mapper)
5454
if err != nil {
5555
return nil, err
@@ -61,7 +61,7 @@ func (n *NamedStmt) Query(arg interface{}) (*sql.Rows, error) {
6161
// create a *sql.Row with an error condition pre-set for binding errors, sqlx
6262
// returns a *sqlx.Row instead.
6363
// Any named placeholder parameters are replaced with fields from arg.
64-
func (n *NamedStmt) QueryRow(arg interface{}) *Row {
64+
func (n *NamedStmt) QueryRow(arg any) *Row {
6565
args, err := bindAnyArgs(n.Params, arg, n.Stmt.Mapper)
6666
if err != nil {
6767
return &Row{err: err}
@@ -71,7 +71,7 @@ func (n *NamedStmt) QueryRow(arg interface{}) *Row {
7171

7272
// MustExec execs a NamedStmt, panicing on error
7373
// Any named placeholder parameters are replaced with fields from arg.
74-
func (n *NamedStmt) MustExec(arg interface{}) sql.Result {
74+
func (n *NamedStmt) MustExec(arg any) sql.Result {
7575
res, err := n.Exec(arg)
7676
if err != nil {
7777
panic(err)
@@ -81,7 +81,7 @@ func (n *NamedStmt) MustExec(arg interface{}) sql.Result {
8181

8282
// Queryx using this NamedStmt
8383
// Any named placeholder parameters are replaced with fields from arg.
84-
func (n *NamedStmt) Queryx(arg interface{}) (*Rows, error) {
84+
func (n *NamedStmt) Queryx(arg any) (*Rows, error) {
8585
r, err := n.Query(arg)
8686
if err != nil {
8787
return nil, err
@@ -92,13 +92,13 @@ func (n *NamedStmt) Queryx(arg interface{}) (*Rows, error) {
9292
// QueryRowx this NamedStmt. Because of limitations with QueryRow, this is
9393
// an alias for QueryRow.
9494
// Any named placeholder parameters are replaced with fields from arg.
95-
func (n *NamedStmt) QueryRowx(arg interface{}) *Row {
95+
func (n *NamedStmt) QueryRowx(arg any) *Row {
9696
return n.QueryRow(arg)
9797
}
9898

9999
// Select using this NamedStmt
100100
// Any named placeholder parameters are replaced with fields from arg.
101-
func (n *NamedStmt) Select(dest interface{}, arg interface{}) error {
101+
func (n *NamedStmt) Select(dest any, arg any) error {
102102
rows, err := n.Queryx(arg)
103103
if err != nil {
104104
return err
@@ -110,7 +110,7 @@ func (n *NamedStmt) Select(dest interface{}, arg interface{}) error {
110110

111111
// Get using this NamedStmt
112112
// Any named placeholder parameters are replaced with fields from arg.
113-
func (n *NamedStmt) Get(dest interface{}, arg interface{}) error {
113+
func (n *NamedStmt) Get(dest any, arg any) error {
114114
r := n.QueryRowx(arg)
115115
return r.scanAny(dest, false)
116116
}
@@ -146,21 +146,21 @@ func prepareNamed(p namedPreparer, query string) (*NamedStmt, error) {
146146
}, nil
147147
}
148148

149-
// convertMapStringInterface attempts to convert v to map[string]interface{}.
150-
// Unlike v.(map[string]interface{}), this function works on named types that
151-
// are convertible to map[string]interface{} as well.
152-
func convertMapStringInterface(v interface{}) (map[string]interface{}, bool) {
153-
var m map[string]interface{}
149+
// convertMapStringInterface attempts to convert v to map[string]any.
150+
// Unlike v.(map[string]any), this function works on named types that
151+
// are convertible to map[string]any as well.
152+
func convertMapStringInterface(v any) (map[string]any, bool) {
153+
var m map[string]any
154154
mtype := reflect.TypeOf(m)
155155
t := reflect.TypeOf(v)
156156
if !t.ConvertibleTo(mtype) {
157157
return nil, false
158158
}
159-
return reflect.ValueOf(v).Convert(mtype).Interface().(map[string]interface{}), true
159+
return reflect.ValueOf(v).Convert(mtype).Interface().(map[string]any), true
160160

161161
}
162162

163-
func bindAnyArgs(names []string, arg interface{}, m *reflectx.Mapper) ([]interface{}, error) {
163+
func bindAnyArgs(names []string, arg any, m *reflectx.Mapper) ([]any, error) {
164164
if maparg, ok := convertMapStringInterface(arg); ok {
165165
return bindMapArgs(names, maparg)
166166
}
@@ -170,8 +170,8 @@ func bindAnyArgs(names []string, arg interface{}, m *reflectx.Mapper) ([]interfa
170170
// private interface to generate a list of interfaces from a given struct
171171
// type, given a list of names to pull out of the struct. Used by public
172172
// BindStruct interface.
173-
func bindArgs(names []string, arg interface{}, m *reflectx.Mapper) ([]interface{}, error) {
174-
arglist := make([]interface{}, 0, len(names))
173+
func bindArgs(names []string, arg any, m *reflectx.Mapper) ([]any, error) {
174+
arglist := make([]any, 0, len(names))
175175

176176
// grab the indirected value of arg
177177
v := reflect.ValueOf(arg)
@@ -194,8 +194,8 @@ func bindArgs(names []string, arg interface{}, m *reflectx.Mapper) ([]interface{
194194
}
195195

196196
// like bindArgs, but for maps.
197-
func bindMapArgs(names []string, arg map[string]interface{}) ([]interface{}, error) {
198-
arglist := make([]interface{}, 0, len(names))
197+
func bindMapArgs(names []string, arg map[string]any) ([]any, error) {
198+
arglist := make([]any, 0, len(names))
199199

200200
for _, name := range names {
201201
val, ok := arg[name]
@@ -210,15 +210,15 @@ func bindMapArgs(names []string, arg map[string]interface{}) ([]interface{}, err
210210
// bindStruct binds a named parameter query with fields from a struct argument.
211211
// The rules for binding field names to parameter names follow the same
212212
// conventions as for StructScan, including obeying the `db` struct tags.
213-
func bindStruct(bindType int, query string, arg interface{}, m *reflectx.Mapper) (string, []interface{}, error) {
213+
func bindStruct(bindType int, query string, arg any, m *reflectx.Mapper) (string, []any, error) {
214214
bound, names, err := compileNamedQuery([]byte(query), bindType)
215215
if err != nil {
216-
return "", []interface{}{}, err
216+
return "", []any{}, err
217217
}
218218

219219
arglist, err := bindAnyArgs(names, arg, m)
220220
if err != nil {
221-
return "", []interface{}{}, err
221+
return "", []any{}, err
222222
}
223223

224224
return bound, arglist, nil
@@ -270,23 +270,23 @@ func fixBound(bound string, loop int) string {
270270

271271
// bindArray binds a named parameter query with fields from an array or slice of
272272
// structs argument.
273-
func bindArray(bindType int, query string, arg interface{}, m *reflectx.Mapper) (string, []interface{}, error) {
273+
func bindArray(bindType int, query string, arg any, m *reflectx.Mapper) (string, []any, error) {
274274
// do the initial binding with QUESTION; if bindType is not question,
275275
// we can rebind it at the end.
276276
bound, names, err := compileNamedQuery([]byte(query), QUESTION)
277277
if err != nil {
278-
return "", []interface{}{}, err
278+
return "", []any{}, err
279279
}
280280
arrayValue := reflect.ValueOf(arg)
281281
arrayLen := arrayValue.Len()
282282
if arrayLen == 0 {
283-
return "", []interface{}{}, fmt.Errorf("length of array is 0: %#v", arg)
283+
return "", []any{}, fmt.Errorf("length of array is 0: %#v", arg)
284284
}
285-
var arglist = make([]interface{}, 0, len(names)*arrayLen)
285+
var arglist = make([]any, 0, len(names)*arrayLen)
286286
for i := 0; i < arrayLen; i++ {
287287
elemArglist, err := bindAnyArgs(names, arrayValue.Index(i).Interface(), m)
288288
if err != nil {
289-
return "", []interface{}{}, err
289+
return "", []any{}, err
290290
}
291291
arglist = append(arglist, elemArglist...)
292292
}
@@ -301,10 +301,10 @@ func bindArray(bindType int, query string, arg interface{}, m *reflectx.Mapper)
301301
}
302302

303303
// bindMap binds a named parameter query with a map of arguments.
304-
func bindMap(bindType int, query string, args map[string]interface{}) (string, []interface{}, error) {
304+
func bindMap(bindType int, query string, args map[string]any) (string, []any, error) {
305305
bound, names, err := compileNamedQuery([]byte(query), bindType)
306306
if err != nil {
307-
return "", []interface{}{}, err
307+
return "", []any{}, err
308308
}
309309

310310
arglist, err := bindMapArgs(names, args)
@@ -407,18 +407,18 @@ func compileNamedQuery(qs []byte, bindType int) (query string, names []string, e
407407

408408
// BindNamed binds a struct or a map to a query with named parameters.
409409
// DEPRECATED: use sqlx.Named` instead of this, it may be removed in future.
410-
func BindNamed(bindType int, query string, arg interface{}) (string, []interface{}, error) {
410+
func BindNamed(bindType int, query string, arg any) (string, []any, error) {
411411
return bindNamedMapper(bindType, query, arg, mapper())
412412
}
413413

414414
// Named takes a query using named parameters and an argument and
415415
// returns a new query with a list of args that can be executed by
416416
// a database. The return value uses the `?` bindvar.
417-
func Named(query string, arg interface{}) (string, []interface{}, error) {
417+
func Named(query string, arg any) (string, []any, error) {
418418
return bindNamedMapper(QUESTION, query, arg, mapper())
419419
}
420420

421-
func bindNamedMapper(bindType int, query string, arg interface{}, m *reflectx.Mapper) (string, []interface{}, error) {
421+
func bindNamedMapper(bindType int, query string, arg any, m *reflectx.Mapper) (string, []any, error) {
422422
t := reflect.TypeOf(arg)
423423
k := t.Kind()
424424
switch {
@@ -437,8 +437,8 @@ func bindNamedMapper(bindType int, query string, arg interface{}, m *reflectx.Ma
437437

438438
// NamedQuery binds a named query and then runs Query on the result using the
439439
// provided Ext (sqlx.Tx, sqlx.Db). It works with both structs and with
440-
// map[string]interface{} types.
441-
func NamedQuery(e Ext, query string, arg interface{}) (*Rows, error) {
440+
// map[string]any types.
441+
func NamedQuery(e Ext, query string, arg any) (*Rows, error) {
442442
q, args, err := bindNamedMapper(BindType(e.DriverName()), query, arg, mapperFor(e))
443443
if err != nil {
444444
return nil, err
@@ -449,7 +449,7 @@ func NamedQuery(e Ext, query string, arg interface{}) (*Rows, error) {
449449
// NamedExec uses BindStruct to get a query executable by the driver and
450450
// then runs Exec on the result. Returns an error from the binding
451451
// or the query execution itself.
452-
func NamedExec(e Ext, query string, arg interface{}) (sql.Result, error) {
452+
func NamedExec(e Ext, query string, arg any) (sql.Result, error) {
453453
q, args, err := bindNamedMapper(BindType(e.DriverName()), query, arg, mapperFor(e))
454454
if err != nil {
455455
return nil, err

0 commit comments

Comments
 (0)