Skip to content
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
18 changes: 9 additions & 9 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func Rebind(bindType int, query string) string {
return string(rqb)
}

func asSliceForIn(i interface{}) (v reflect.Value, ok bool) {
func asSliceForIn(i any) (v reflect.Value, ok bool) {
if i == nil {
return reflect.Value{}, false
}
Expand All @@ -119,7 +119,7 @@ func asSliceForIn(i interface{}) (v reflect.Value, ok bool) {
}

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

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

Expand Down Expand Up @@ -181,7 +181,7 @@ func In(query string, args ...interface{}) (string, []interface{}, error) {
return query, args, nil
}

newArgs := make([]interface{}, 0, flatArgsCount)
newArgs := make([]any, 0, flatArgsCount)

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

func appendReflectSlice(args []interface{}, v reflect.Value, vlen int) []interface{} {
func appendReflectSlice(args []any, v reflect.Value, vlen int) []any {
switch val := v.Interface().(type) {
case []interface{}:
case []any:
args = append(args, val...)
case []int:
for i := range val {
Expand All @@ -253,7 +253,7 @@ func appendReflectSlice(args []interface{}, v reflect.Value, vlen int) []interfa
args = append(args, val[i])
}
default:
for si := 0; si < vlen; si++ {
for si := range vlen {
args = append(args, v.Index(si).Interface())
}
}
Expand All @@ -276,7 +276,7 @@ func appendReflectSlice(args []interface{}, v reflect.Value, vlen int) []interfa
func callValuerValue(vr driver.Valuer) (v driver.Value, err error) {
if rv := reflect.ValueOf(vr); rv.Kind() == reflect.Pointer &&
rv.IsNil() &&
rv.Type().Elem().Implements(reflect.TypeOf((*driver.Valuer)(nil)).Elem()) {
rv.Type().Elem().Implements(reflect.TypeFor[driver.Valuer]()) {
return nil, nil
}
return vr.Value()
Expand Down
2 changes: 1 addition & 1 deletion convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import (
)

//go:linkname convertAssign database/sql.convertAssign
func convertAssign(dest, src interface{}) error
func convertAssign(dest, src any) error
70 changes: 35 additions & 35 deletions named.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (n *GenericNamedStmt[T]) Close() error {

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

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

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

// Queryx using this NamedStmt
// Any named placeholder parameters are replaced with fields from arg.
func (n *GenericNamedStmt[T]) Queryx(arg interface{}) (*Rows, error) {
func (n *GenericNamedStmt[T]) Queryx(arg any) (*Rows, error) {
r, err := n.Query(arg)
if err != nil {
return nil, err
Expand All @@ -98,13 +98,13 @@ func (n *GenericNamedStmt[T]) Queryx(arg interface{}) (*Rows, error) {
// QueryRowx this NamedStmt. Because of limitations with QueryRow, this is
// an alias for QueryRow.
// Any named placeholder parameters are replaced with fields from arg.
func (n *GenericNamedStmt[T]) QueryRowx(arg interface{}) *Row {
func (n *GenericNamedStmt[T]) QueryRowx(arg any) *Row {
return n.QueryRow(arg)
}

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

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

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

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

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

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

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

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

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

arglist, err := bindAnyArgs(compiled.names, arg, m)
if err != nil {
return "", []interface{}{}, err
return "", []any{}, err
}

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

// bindArray binds a named parameter query with fields from an array or slice of
// structs argument.
func bindArray(bindType int, query string, arg interface{}, m *reflectx.Mapper) (string, []interface{}, error) {
func bindArray(bindType int, query string, arg any, m *reflectx.Mapper) (string, []any, error) {
// do the initial binding with QUESTION; if bindType is not question,
// we can rebind it at the end.
compiled, err := compileNamedQuery([]byte(query), QUESTION)
if err != nil {
return "", []interface{}{}, err
return "", []any{}, err
}
arrayValue := reflect.ValueOf(arg)
arrayLen := arrayValue.Len()
if arrayLen == 0 {
return "", []interface{}{}, fmt.Errorf("length of array is 0: %#v", arg)
return "", []any{}, fmt.Errorf("length of array is 0: %#v", arg)
}
arglist := make([]interface{}, 0, len(compiled.names)*arrayLen)
for i := 0; i < arrayLen; i++ {
arglist := make([]any, 0, len(compiled.names)*arrayLen)
for i := range arrayLen {
elemArglist, err := bindAnyArgs(compiled.names, arrayValue.Index(i).Interface(), m)
if err != nil {
return "", []interface{}{}, err
return "", []any{}, err
}
arglist = append(arglist, elemArglist...)
}
Expand All @@ -346,10 +346,10 @@ func bindArray(bindType int, query string, arg interface{}, m *reflectx.Mapper)
}

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

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

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

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

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