2 Commits

Author SHA1 Message Date
637797116d Add support for pointer arguments
Some checks failed
Build / build (push) Failing after 41s
2025-05-08 19:42:57 +02:00
dd6be6b9b6 Return a verifiable error if no results were returned
All checks were successful
Build / build (push) Successful in 3m9s
2025-05-08 11:14:41 +02:00
3 changed files with 58 additions and 3 deletions

5
errors.go Normal file
View File

@@ -0,0 +1,5 @@
package mysqlite
import "errors"
var ErrNoRows = errors.New("mysqlite: no rows returned")

View File

@@ -44,6 +44,14 @@ func (q *Query) bindInto(into *int, args ...any) *Query {
}
for i, arg := range args {
*into++
v := reflect.ValueOf(arg)
if v.Kind() == reflect.Ptr {
if v.IsNil() {
q.stmt.BindNull(*into)
continue
}
arg = v.Elem().Interface() // Dereference the pointer
}
if asString, ok := arg.(string); ok {
q.stmt.BindText(*into, asString)
} else if asInt, ok := arg.(int); ok {
@@ -54,7 +62,7 @@ func (q *Query) bindInto(into *int, args ...any) *Query {
q.stmt.BindBool(*into, asBool)
} else {
// Check if the argument is a slice or array of any type
v := reflect.ValueOf(arg)
v = reflect.ValueOf(arg)
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
*into--
for i := 0; i < v.Len(); i++ {
@@ -116,7 +124,7 @@ func (q *Query) ScanSingle(results ...any) (rerr error) {
return err
}
if !hasResult {
return fmt.Errorf("did not return any rows")
return ErrNoRows
}
// Scan its columns

View File

@@ -1,6 +1,7 @@
package mysqlite
import (
"errors"
"github.com/stretchr/testify/require"
"testing"
)
@@ -19,6 +20,14 @@ func TestSimpleQuery(t *testing.T) {
require.Equal(t, 1, count, "expected empty count")
}
func TestSimpleQueryWithNoResults(t *testing.T) {
db := openTestDb(t)
var count int
err := db.Query("select 1 from mytable where key=999").ScanSingle(&count)
require.Equal(t, ErrNoRows, err)
require.True(t, errors.Is(err, ErrNoRows))
}
func TestSimpleQueryWithArgs(t *testing.T) {
db := openTestDb(t)
var value string
@@ -91,6 +100,39 @@ func TestUpdateQueryWithWrongArguments(t *testing.T) {
require.Error(t, err)
}
func TestUpdateQueryWithPointerValue(t *testing.T) {
db := openTestDb(t)
func() {
tx := db.MustBegin()
defer tx.MustRollback()
tx.Query("insert into mytable(key, value) values ('lorem', 'bar')").MustExec()
value := "ipsum"
key := "lorem"
tx.Query("update mytable set value = ? where key = ?").Bind(&value, key).MustExec()
tx.MustCommit()
}()
var value string
db.Query("select value from mytable where key = 'lorem'").MustScanSingle(&value)
require.Equal(t, "ipsum", value)
}
func TestUpdateQueryWithNullValue(t *testing.T) {
db := openTestDb(t)
func() {
tx := db.MustBegin()
defer tx.MustRollback()
tx.Query("insert into mytable(key, value) values ('lorem', 'bar')").MustExec()
key := "lorem"
tx.Query("update mytable set value = ? where key = ?").Bind(nil, key).MustExec()
tx.MustCommit()
}()
var value string
db.Query("select value from mytable where key = 'lorem'").MustScanSingle(&value)
require.Nil(t, value)
}
func TestQueryWithPointerStringArguments(t *testing.T) {
db := openTestDb(t)
var result *string
@@ -102,7 +144,7 @@ func TestQueryWithPointerStringArguments(t *testing.T) {
func TestQueryWithPointerStringArgumentsCanSetToNull(t *testing.T) {
db := openTestDb(t)
db.Query("update mytable set value=NULL where key = 'foo'").MustExec()
db.Query("update mytable set value=null where key = 'foo'").MustExec()
myString := "some string"
var result *string
result = &myString