Compare commits
	
		
			5 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| e9b12dc3f6 | |||
| 7daf1915a5 | |||
| 029cf6ce01 | |||
| 278d7ed497 | |||
| dd6be6b9b6 | 
							
								
								
									
										5
									
								
								errors.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								errors.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,5 @@
 | 
			
		||||
package mysqlite
 | 
			
		||||
 | 
			
		||||
import "errors"
 | 
			
		||||
 | 
			
		||||
var ErrNoRows = errors.New("mysqlite: no rows returned")
 | 
			
		||||
							
								
								
									
										18
									
								
								query.go
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								query.go
									
									
									
									
									
								
							@@ -44,6 +44,18 @@ func (q *Query) bindInto(into *int, args ...any) *Query {
 | 
			
		||||
	}
 | 
			
		||||
	for i, arg := range args {
 | 
			
		||||
		*into++
 | 
			
		||||
		if arg == nil {
 | 
			
		||||
			q.stmt.BindNull(*into)
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		v := reflect.ValueOf(arg)
 | 
			
		||||
		if v.Kind() == reflect.Ptr {
 | 
			
		||||
			if v.IsNil() {
 | 
			
		||||
				q.stmt.BindNull(*into)
 | 
			
		||||
				continue
 | 
			
		||||
			}
 | 
			
		||||
			arg = v.Elem().Interface()
 | 
			
		||||
		}
 | 
			
		||||
		if asString, ok := arg.(string); ok {
 | 
			
		||||
			q.stmt.BindText(*into, asString)
 | 
			
		||||
		} else if asInt, ok := arg.(int); ok {
 | 
			
		||||
@@ -54,7 +66,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 +128,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
 | 
			
		||||
@@ -205,6 +217,8 @@ 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 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 {
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
@@ -85,12 +94,70 @@ func TestUpdateQuery(t *testing.T) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestUpdateQueryWithWrongArguments(t *testing.T) {
 | 
			
		||||
	type S struct {
 | 
			
		||||
		Field string
 | 
			
		||||
	}
 | 
			
		||||
	db := openTestDb(t)
 | 
			
		||||
	value := "ipsum"
 | 
			
		||||
	err := db.Query("insert into mytable(key, value) values ('lorem', ?)").Bind(&value).Exec()
 | 
			
		||||
	abc := S{
 | 
			
		||||
		Field: "ipsum",
 | 
			
		||||
	}
 | 
			
		||||
	err := db.Query("insert into mytable(key, value) values ('lorem', ?)").Bind(abc).Exec()
 | 
			
		||||
	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 TestUpdateQueryWithSetPointerValue(t *testing.T) {
 | 
			
		||||
	type S struct {
 | 
			
		||||
		value *string
 | 
			
		||||
	}
 | 
			
		||||
	db := openTestDb(t)
 | 
			
		||||
	func() {
 | 
			
		||||
		tx := db.MustBegin()
 | 
			
		||||
		defer tx.MustRollback()
 | 
			
		||||
		tx.Query("insert into mytable(key, value) values ('lorem', 'bar')").MustExec()
 | 
			
		||||
		s := S{nil}
 | 
			
		||||
		key := "lorem"
 | 
			
		||||
		tx.Query("update mytable set value = ? where key = ?").Bind(s.value, key).MustExec()
 | 
			
		||||
		tx.MustCommit()
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	var value *string
 | 
			
		||||
	db.Query("select value from mytable where key = 'lorem'").MustScanSingle(&value)
 | 
			
		||||
	require.Equal(t, (*string)(nil), 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
 | 
			
		||||
@@ -100,9 +167,18 @@ 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 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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user