Compare commits
No commits in common. "93844c04b2a6cd5308b6e725fef72067ea961b65" and "7daf1915a5b2d71d97dc18e3bcd186a953edd4a9" have entirely different histories.
93844c04b2
...
7daf1915a5
@ -3,4 +3,3 @@ package mysqlite
|
||||
import "errors"
|
||||
|
||||
var ErrNoRows = errors.New("mysqlite: no rows returned")
|
||||
var ErrMissingBind = errors.New("mysqlite: missing bind value")
|
||||
|
20
query.go
20
query.go
@ -12,8 +12,6 @@ type Query struct {
|
||||
// Reference to the database. If set, it is assumed that a lock was taken
|
||||
// by the query that should be freed by the query.
|
||||
db *Db
|
||||
// The number of bound arguments
|
||||
binds int
|
||||
err error
|
||||
}
|
||||
|
||||
@ -48,30 +46,24 @@ func (q *Query) bindInto(into *int, args ...any) *Query {
|
||||
*into++
|
||||
if arg == nil {
|
||||
q.stmt.BindNull(*into)
|
||||
q.binds++
|
||||
continue
|
||||
}
|
||||
v := reflect.ValueOf(arg)
|
||||
if v.Kind() == reflect.Ptr {
|
||||
if v.IsNil() {
|
||||
q.stmt.BindNull(*into)
|
||||
q.binds++
|
||||
continue
|
||||
}
|
||||
arg = v.Elem().Interface()
|
||||
}
|
||||
if asString, ok := arg.(string); ok {
|
||||
q.stmt.BindText(*into, asString)
|
||||
q.binds++
|
||||
} else if asInt, ok := arg.(int); ok {
|
||||
q.stmt.BindInt64(*into, int64(asInt))
|
||||
q.binds++
|
||||
} else if asFloat, ok := arg.(float64); ok {
|
||||
q.stmt.BindFloat(*into, asFloat)
|
||||
q.binds++
|
||||
} else if asInt, ok := arg.(int64); ok {
|
||||
q.stmt.BindInt64(*into, asInt)
|
||||
} else if asBool, ok := arg.(bool); ok {
|
||||
q.stmt.BindBool(*into, asBool)
|
||||
q.binds++
|
||||
} else {
|
||||
// Check if the argument is a slice or array of any type
|
||||
v = reflect.ValueOf(arg)
|
||||
@ -174,10 +166,6 @@ type Rows struct {
|
||||
}
|
||||
|
||||
func (q *Query) ScanMulti() (*Rows, error) {
|
||||
if q.binds != q.stmt.BindParamCount() {
|
||||
return nil, ErrMissingBind
|
||||
}
|
||||
|
||||
if q.err != nil {
|
||||
return nil, q.err
|
||||
}
|
||||
@ -229,10 +217,6 @@ func (r *Rows) scanArgument(i int, arg any) error {
|
||||
*asString = r.query.stmt.ColumnText(i)
|
||||
} else if asInt, ok := arg.(*int); ok {
|
||||
*asInt = r.query.stmt.ColumnInt(i)
|
||||
} else if asInt, ok := arg.(*int64); ok {
|
||||
*asInt = r.query.stmt.ColumnInt64(i)
|
||||
} else if asFloat, ok := arg.(*float64); ok {
|
||||
*asFloat = r.query.stmt.ColumnFloat(i)
|
||||
} else if asBool, ok := arg.(*bool); ok {
|
||||
*asBool = r.query.stmt.ColumnBool(i)
|
||||
} else if reflect.TypeOf(arg).Kind() == reflect.Ptr && reflect.TypeOf(arg).Elem().Kind() == reflect.Ptr {
|
||||
|
@ -167,31 +167,6 @@ func TestQueryWithPointerStringArguments(t *testing.T) {
|
||||
require.Equal(t, "bar", *result)
|
||||
}
|
||||
|
||||
func TestQueryWithInt64Scan(t *testing.T) {
|
||||
db := openTestDb(t)
|
||||
var result int64
|
||||
err := db.Query("select 2").ScanSingle(&result)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.Equal(t, int64(2), result)
|
||||
}
|
||||
|
||||
func TestQueryWithFloat64Scan(t *testing.T) {
|
||||
db := openTestDb(t)
|
||||
var result float64
|
||||
err := db.Query("select 2.5").ScanSingle(&result)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.InDelta(t, 2.5, result, 0.001)
|
||||
}
|
||||
|
||||
func TestQueryWithMissingBinds(t *testing.T) {
|
||||
db := openTestDb(t)
|
||||
var result float64
|
||||
err := db.Query("select ?").ScanSingle(&result)
|
||||
require.ErrorIs(t, err, ErrMissingBind)
|
||||
}
|
||||
|
||||
func TestQueryWithPointerStringArgumentsCanSetToNull(t *testing.T) {
|
||||
db := openTestDb(t)
|
||||
db.Query("update mytable set value=null where key = 'foo'").MustExec()
|
||||
|
Loading…
x
Reference in New Issue
Block a user