Add ErrMissingScan error and support for int64 (#4)
All checks were successful
Build / build (push) Successful in 41s
All checks were successful
Build / build (push) Successful in 41s
Reviewed-on: #4
This commit is contained in:
parent
6880fb5af4
commit
33f1a94fb2
@ -4,3 +4,4 @@ import "errors"
|
|||||||
|
|
||||||
var ErrNoRows = errors.New("mysqlite: no rows returned")
|
var ErrNoRows = errors.New("mysqlite: no rows returned")
|
||||||
var ErrMissingBind = errors.New("mysqlite: missing bind value")
|
var ErrMissingBind = errors.New("mysqlite: missing bind value")
|
||||||
|
var ErrMissingScan = errors.New("mysqlite: missing scan value")
|
||||||
|
6
query.go
6
query.go
@ -66,6 +66,9 @@ func (q *Query) bindInto(into *int, args ...any) *Query {
|
|||||||
} else if asInt, ok := arg.(int); ok {
|
} else if asInt, ok := arg.(int); ok {
|
||||||
q.stmt.BindInt64(*into, int64(asInt))
|
q.stmt.BindInt64(*into, int64(asInt))
|
||||||
q.binds++
|
q.binds++
|
||||||
|
} else if asInt64, ok := arg.(int64); ok {
|
||||||
|
q.stmt.BindInt64(*into, asInt64)
|
||||||
|
q.binds++
|
||||||
} else if asFloat, ok := arg.(float64); ok {
|
} else if asFloat, ok := arg.(float64); ok {
|
||||||
q.stmt.BindFloat(*into, asFloat)
|
q.stmt.BindFloat(*into, asFloat)
|
||||||
q.binds++
|
q.binds++
|
||||||
@ -215,6 +218,9 @@ func (r *Rows) MustNext() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Rows) Scan(results ...any) error {
|
func (r *Rows) Scan(results ...any) error {
|
||||||
|
if r.query.stmt.ColumnCount() != len(results) {
|
||||||
|
return ErrMissingScan
|
||||||
|
}
|
||||||
for i, arg := range results {
|
for i, arg := range results {
|
||||||
err := r.scanArgument(i, arg)
|
err := r.scanArgument(i, arg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -28,6 +28,23 @@ func TestSimpleQueryWithNoResults(t *testing.T) {
|
|||||||
require.True(t, errors.Is(err, ErrNoRows))
|
require.True(t, errors.Is(err, ErrNoRows))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestScanWithMissingValues(t *testing.T) {
|
||||||
|
db := openTestDb(t)
|
||||||
|
var count int
|
||||||
|
err := db.Query("select 1, 2").ScanSingle(&count)
|
||||||
|
require.Equal(t, ErrMissingScan, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBindInt64(t *testing.T) {
|
||||||
|
db := openTestDb(t)
|
||||||
|
var value int64
|
||||||
|
var result int64
|
||||||
|
value = 5
|
||||||
|
err := db.Query("select ?").Bind(&value).ScanSingle(&result)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, int64(5), result)
|
||||||
|
}
|
||||||
|
|
||||||
func TestSimpleQueryWithArgs(t *testing.T) {
|
func TestSimpleQueryWithArgs(t *testing.T) {
|
||||||
db := openTestDb(t)
|
db := openTestDb(t)
|
||||||
var value string
|
var value string
|
||||||
|
Loading…
x
Reference in New Issue
Block a user