Skip to content

Commit 7bc6070

Browse files
go modernize
1 parent e1999f8 commit 7bc6070

13 files changed

Lines changed: 264 additions & 279 deletions

bind.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func Rebind(bindType int, query string) string {
105105
return string(rqb)
106106
}
107107

108-
func asSliceForIn(i interface{}) (v reflect.Value, ok bool) {
108+
func asSliceForIn(i any) (v reflect.Value, ok bool) {
109109
if i == nil {
110110
return reflect.Value{}, false
111111
}
@@ -119,7 +119,7 @@ func asSliceForIn(i interface{}) (v reflect.Value, ok bool) {
119119
}
120120

121121
// []byte is a driver.Value type so it should not be expanded
122-
if t == reflect.TypeOf([]byte{}) {
122+
if t == reflect.TypeFor[[]byte]() {
123123
return reflect.Value{}, false
124124
}
125125

@@ -129,12 +129,12 @@ func asSliceForIn(i interface{}) (v reflect.Value, ok bool) {
129129
// In expands slice values in args, returning the modified query string
130130
// and a new arg list that can be executed by a database. The `query` should
131131
// use the `?` bindVar. The return value uses the `?` bindVar.
132-
func In(query string, args ...interface{}) (string, []interface{}, error) {
132+
func In(query string, args ...any) (string, []any, error) {
133133
// argMeta stores reflect.Value and length for slices and
134134
// the value itself for non-slice arguments
135135
type argMeta struct {
136136
v reflect.Value
137-
i interface{}
137+
i any
138138
length int
139139
}
140140

@@ -181,7 +181,7 @@ func In(query string, args ...interface{}) (string, []interface{}, error) {
181181
return query, args, nil
182182
}
183183

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

186186
var buf strings.Builder
187187
buf.Grow(len(query) + len(", ?")*flatArgsCount)
@@ -240,9 +240,9 @@ func In(query string, args ...interface{}) (string, []interface{}, error) {
240240
return buf.String(), newArgs, nil
241241
}
242242

243-
func appendReflectSlice(args []interface{}, v reflect.Value, vlen int) []interface{} {
243+
func appendReflectSlice(args []any, v reflect.Value, vlen int) []any {
244244
switch val := v.Interface().(type) {
245-
case []interface{}:
245+
case []any:
246246
args = append(args, val...)
247247
case []int:
248248
for i := range val {
@@ -253,7 +253,7 @@ func appendReflectSlice(args []interface{}, v reflect.Value, vlen int) []interfa
253253
args = append(args, val[i])
254254
}
255255
default:
256-
for si := 0; si < vlen; si++ {
256+
for si := range vlen {
257257
args = append(args, v.Index(si).Interface())
258258
}
259259
}
@@ -276,7 +276,7 @@ func appendReflectSlice(args []interface{}, v reflect.Value, vlen int) []interfa
276276
func callValuerValue(vr driver.Valuer) (v driver.Value, err error) {
277277
if rv := reflect.ValueOf(vr); rv.Kind() == reflect.Pointer &&
278278
rv.IsNil() &&
279-
rv.Type().Elem().Implements(reflect.TypeOf((*driver.Valuer)(nil)).Elem()) {
279+
rv.Type().Elem().Implements(reflect.TypeFor[driver.Valuer]()) {
280280
return nil, nil
281281
}
282282
return vr.Value()

convert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ import (
55
)
66

77
//go:linkname convertAssign database/sql.convertAssign
8-
func convertAssign(dest, src interface{}) error
8+
func convertAssign(dest, src any) error

named.go

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (n *GenericNamedStmt[T]) Close() error {
4545

4646
// Exec executes a named statement using the struct passed.
4747
// Any named placeholder parameters are replaced with fields from arg.
48-
func (n *GenericNamedStmt[T]) Exec(arg interface{}) (sql.Result, error) {
48+
func (n *GenericNamedStmt[T]) Exec(arg any) (sql.Result, error) {
4949
args, err := bindAnyArgs(n.Params, arg, n.Stmt.Mapper)
5050
if err != nil {
5151
return *new(sql.Result), err
@@ -55,7 +55,7 @@ func (n *GenericNamedStmt[T]) Exec(arg interface{}) (sql.Result, error) {
5555

5656
// Query executes a named statement using the struct argument, returning rows.
5757
// Any named placeholder parameters are replaced with fields from arg.
58-
func (n *GenericNamedStmt[T]) Query(arg interface{}) (*sql.Rows, error) {
58+
func (n *GenericNamedStmt[T]) Query(arg any) (*sql.Rows, error) {
5959
args, err := bindAnyArgs(n.Params, arg, n.Stmt.Mapper)
6060
if err != nil {
6161
return nil, err
@@ -67,7 +67,7 @@ func (n *GenericNamedStmt[T]) Query(arg interface{}) (*sql.Rows, error) {
6767
// create a *sql.Row with an error condition pre-set for binding errors, sqlx
6868
// returns a *sqlx.Row instead.
6969
// Any named placeholder parameters are replaced with fields from arg.
70-
func (n *GenericNamedStmt[T]) QueryRow(arg interface{}) *Row {
70+
func (n *GenericNamedStmt[T]) QueryRow(arg any) *Row {
7171
args, err := bindAnyArgs(n.Params, arg, n.Stmt.Mapper)
7272
if err != nil {
7373
return &Row{err: err}
@@ -77,7 +77,7 @@ func (n *GenericNamedStmt[T]) QueryRow(arg interface{}) *Row {
7777

7878
// MustExec execs a NamedStmt, panicing on error
7979
// Any named placeholder parameters are replaced with fields from arg.
80-
func (n *GenericNamedStmt[T]) MustExec(arg interface{}) sql.Result {
80+
func (n *GenericNamedStmt[T]) MustExec(arg any) sql.Result {
8181
res, err := n.Exec(arg)
8282
if err != nil {
8383
panic(err)
@@ -87,7 +87,7 @@ func (n *GenericNamedStmt[T]) MustExec(arg interface{}) sql.Result {
8787

8888
// Queryx using this NamedStmt
8989
// Any named placeholder parameters are replaced with fields from arg.
90-
func (n *GenericNamedStmt[T]) Queryx(arg interface{}) (*Rows, error) {
90+
func (n *GenericNamedStmt[T]) Queryx(arg any) (*Rows, error) {
9191
r, err := n.Query(arg)
9292
if err != nil {
9393
return nil, err
@@ -98,13 +98,13 @@ func (n *GenericNamedStmt[T]) Queryx(arg interface{}) (*Rows, error) {
9898
// QueryRowx this NamedStmt. Because of limitations with QueryRow, this is
9999
// an alias for QueryRow.
100100
// Any named placeholder parameters are replaced with fields from arg.
101-
func (n *GenericNamedStmt[T]) QueryRowx(arg interface{}) *Row {
101+
func (n *GenericNamedStmt[T]) QueryRowx(arg any) *Row {
102102
return n.QueryRow(arg)
103103
}
104104

105105
// Select using this NamedStmt
106106
// Any named placeholder parameters are replaced with fields from arg.
107-
func (n *GenericNamedStmt[T]) Select(dest interface{}, arg interface{}) error {
107+
func (n *GenericNamedStmt[T]) Select(dest any, arg any) error {
108108
rows, err := n.Queryx(arg)
109109
if err != nil {
110110
return err
@@ -115,30 +115,30 @@ func (n *GenericNamedStmt[T]) Select(dest interface{}, arg interface{}) error {
115115
}
116116

117117
// List performs a query using the statement and returns all rows as a slice of T.
118-
func (n *GenericNamedStmt[T]) List(arg interface{}) ([]T, error) {
118+
func (n *GenericNamedStmt[T]) List(arg any) ([]T, error) {
119119
var dests []T
120120
err := n.Select(&dests, arg)
121121
return dests, err
122122
}
123123

124124
// Get using this NamedStmt
125125
// Any named placeholder parameters are replaced with fields from arg.
126-
func (n *GenericNamedStmt[T]) Get(dest interface{}, arg interface{}) error {
126+
func (n *GenericNamedStmt[T]) Get(dest any, arg any) error {
127127
r := n.QueryRowx(arg)
128128
return r.scanAny(dest, false)
129129
}
130130

131131
// One get a single row using this NamedStmt
132132
// Any named placeholder parameters are replaced with fields from arg.
133-
func (n *GenericNamedStmt[T]) One(arg interface{}) (T, error) {
133+
func (n *GenericNamedStmt[T]) One(arg any) (T, error) {
134134
r := n.QueryRowx(arg)
135135
var dest T
136136
err := r.scanAny(&dest, false)
137137
return dest, err
138138
}
139139

140140
// All performs a query using the GenericNamedStmt and returns all rows for use with range.
141-
func (n *GenericNamedStmt[T]) All(arg interface{}) iter.Seq2[T, error] {
141+
func (n *GenericNamedStmt[T]) All(arg any) iter.Seq2[T, error] {
142142
rows, err := n.Queryx(arg)
143143
if err != nil {
144144
panic(err)
@@ -219,17 +219,17 @@ func PrepareNamed[T any](p namedPreparer, query string) (*GenericNamedStmt[T], e
219219
// convertMapStringInterface attempts to convert v to map[string]interface{}.
220220
// Unlike v.(map[string]interface{}), this function works on named types that
221221
// are convertible to map[string]interface{} as well.
222-
func convertMapStringInterface(v interface{}) (map[string]interface{}, bool) {
223-
var m map[string]interface{}
222+
func convertMapStringInterface(v any) (map[string]any, bool) {
223+
var m map[string]any
224224
mtype := reflect.TypeOf(m)
225225
t := reflect.TypeOf(v)
226226
if !t.ConvertibleTo(mtype) {
227227
return nil, false
228228
}
229-
return reflect.ValueOf(v).Convert(mtype).Interface().(map[string]interface{}), true
229+
return reflect.ValueOf(v).Convert(mtype).Interface().(map[string]any), true
230230
}
231231

232-
func bindAnyArgs(names []string, arg interface{}, m *reflectx.Mapper) ([]interface{}, error) {
232+
func bindAnyArgs(names []string, arg any, m *reflectx.Mapper) ([]any, error) {
233233
if maparg, ok := convertMapStringInterface(arg); ok {
234234
return bindMapArgs(names, maparg)
235235
}
@@ -239,8 +239,8 @@ func bindAnyArgs(names []string, arg interface{}, m *reflectx.Mapper) ([]interfa
239239
// private interface to generate a list of interfaces from a given struct
240240
// type, given a list of names to pull out of the struct. Used by public
241241
// BindStruct interface.
242-
func bindArgs(names []string, arg interface{}, m *reflectx.Mapper) ([]interface{}, error) {
243-
arglist := make([]interface{}, 0, len(names))
242+
func bindArgs(names []string, arg any, m *reflectx.Mapper) ([]any, error) {
243+
arglist := make([]any, 0, len(names))
244244

245245
// grab the indirected value of arg
246246
var v reflect.Value
@@ -263,8 +263,8 @@ func bindArgs(names []string, arg interface{}, m *reflectx.Mapper) ([]interface{
263263
}
264264

265265
// like bindArgs, but for maps.
266-
func bindMapArgs(names []string, arg map[string]interface{}) ([]interface{}, error) {
267-
arglist := make([]interface{}, 0, len(names))
266+
func bindMapArgs(names []string, arg map[string]any) ([]any, error) {
267+
arglist := make([]any, 0, len(names))
268268

269269
for _, name := range names {
270270
val, ok := arg[name]
@@ -279,15 +279,15 @@ func bindMapArgs(names []string, arg map[string]interface{}) ([]interface{}, err
279279
// bindStruct binds a named parameter query with fields from a struct argument.
280280
// The rules for binding field names to parameter names follow the same
281281
// conventions as for StructScan, including obeying the `db` struct tags.
282-
func bindStruct(bindType int, query string, arg interface{}, m *reflectx.Mapper) (string, []interface{}, error) {
282+
func bindStruct(bindType int, query string, arg any, m *reflectx.Mapper) (string, []any, error) {
283283
compiled, err := compileNamedQuery([]byte(query), bindType)
284284
if err != nil {
285-
return "", []interface{}{}, err
285+
return "", []any{}, err
286286
}
287287

288288
arglist, err := bindAnyArgs(compiled.names, arg, m)
289289
if err != nil {
290-
return "", []interface{}{}, err
290+
return "", []any{}, err
291291
}
292292

293293
return compiled.query, arglist, nil
@@ -314,23 +314,23 @@ func fixBound(cq *compiledQueryResult, loop int) {
314314

315315
// bindArray binds a named parameter query with fields from an array or slice of
316316
// structs argument.
317-
func bindArray(bindType int, query string, arg interface{}, m *reflectx.Mapper) (string, []interface{}, error) {
317+
func bindArray(bindType int, query string, arg any, m *reflectx.Mapper) (string, []any, error) {
318318
// do the initial binding with QUESTION; if bindType is not question,
319319
// we can rebind it at the end.
320320
compiled, err := compileNamedQuery([]byte(query), QUESTION)
321321
if err != nil {
322-
return "", []interface{}{}, err
322+
return "", []any{}, err
323323
}
324324
arrayValue := reflect.ValueOf(arg)
325325
arrayLen := arrayValue.Len()
326326
if arrayLen == 0 {
327-
return "", []interface{}{}, fmt.Errorf("length of array is 0: %#v", arg)
327+
return "", []any{}, fmt.Errorf("length of array is 0: %#v", arg)
328328
}
329-
arglist := make([]interface{}, 0, len(compiled.names)*arrayLen)
330-
for i := 0; i < arrayLen; i++ {
329+
arglist := make([]any, 0, len(compiled.names)*arrayLen)
330+
for i := range arrayLen {
331331
elemArglist, err := bindAnyArgs(compiled.names, arrayValue.Index(i).Interface(), m)
332332
if err != nil {
333-
return "", []interface{}{}, err
333+
return "", []any{}, err
334334
}
335335
arglist = append(arglist, elemArglist...)
336336
}
@@ -346,10 +346,10 @@ func bindArray(bindType int, query string, arg interface{}, m *reflectx.Mapper)
346346
}
347347

348348
// bindMap binds a named parameter query with a map of arguments.
349-
func bindMap(bindType int, query string, args map[string]interface{}) (string, []interface{}, error) {
349+
func bindMap(bindType int, query string, args map[string]any) (string, []any, error) {
350350
compiled, err := compileNamedQuery([]byte(query), bindType)
351351
if err != nil {
352-
return "", []interface{}{}, err
352+
return "", []any{}, err
353353
}
354354

355355
arglist, err := bindMapArgs(compiled.names, args)
@@ -483,18 +483,18 @@ func compileNamedQuery(qs []byte, bindType int) (compiledQueryResult, error) {
483483

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

490490
// Named takes a query using named parameters and an argument and
491491
// returns a new query with a list of args that can be executed by
492492
// a database. The return value uses the `?` bindvar.
493-
func Named(query string, arg interface{}) (string, []interface{}, error) {
493+
func Named(query string, arg any) (string, []any, error) {
494494
return bindNamedMapper(QUESTION, query, arg, mapper())
495495
}
496496

497-
func bindNamedMapper(bindType int, query string, arg interface{}, m *reflectx.Mapper) (string, []interface{}, error) {
497+
func bindNamedMapper(bindType int, query string, arg any, m *reflectx.Mapper) (string, []any, error) {
498498
t := reflect.TypeOf(arg)
499499
k := t.Kind()
500500
switch {
@@ -514,7 +514,7 @@ func bindNamedMapper(bindType int, query string, arg interface{}, m *reflectx.Ma
514514
// NamedQuery binds a named query and then runs Query on the result using the
515515
// provided Ext (sqlx.Tx, sqlx.Db). It works with both structs and with
516516
// map[string]interface{} types.
517-
func NamedQuery(e Ext, query string, arg interface{}) (*Rows, error) {
517+
func NamedQuery(e Ext, query string, arg any) (*Rows, error) {
518518
q, args, err := bindNamedMapper(BindType(e.DriverName()), query, arg, mapperFor(e))
519519
if err != nil {
520520
return nil, err
@@ -525,7 +525,7 @@ func NamedQuery(e Ext, query string, arg interface{}) (*Rows, error) {
525525
// NamedExec uses BindStruct to get a query executable by the driver and
526526
// then runs Exec on the result. Returns an error from the binding
527527
// or the query execution itself.
528-
func NamedExec(e Ext, query string, arg interface{}) (sql.Result, error) {
528+
func NamedExec(e Ext, query string, arg any) (sql.Result, error) {
529529
q, args, err := bindNamedMapper(BindType(e.DriverName()), query, arg, mapperFor(e))
530530
if err != nil {
531531
return nil, err

0 commit comments

Comments
 (0)