Compare commits
5 Commits
87f10c73d6
...
v0.11.1
| Author | SHA1 | Date | |
|---|---|---|---|
| 7daf1915a5 | |||
| 029cf6ce01 | |||
| 278d7ed497 | |||
| dd6be6b9b6 | |||
| 12a87a8762 |
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")
|
||||||
@@ -42,7 +42,7 @@ func (d *Db) MigrateDb(filesystem ReadDirFileFS, directory string) error {
|
|||||||
log.Printf("Current version is %d, max migration version is %d", currentVersion, latestVersion)
|
log.Printf("Current version is %d, max migration version is %d", currentVersion, latestVersion)
|
||||||
|
|
||||||
// Create a backup if we're not on the latest version
|
// Create a backup if we're not on the latest version
|
||||||
if currentVersion != latestVersion && d.source != ":memory:" {
|
if currentVersion != 0 && currentVersion != latestVersion && d.source != ":memory:" {
|
||||||
target := d.source + ".backup." + strconv.Itoa(currentVersion)
|
target := d.source + ".backup." + strconv.Itoa(currentVersion)
|
||||||
log.Printf("Creating backup of database to %s", target)
|
log.Printf("Creating backup of database to %s", target)
|
||||||
data, err := d.Db.Serialize("main")
|
data, err := d.Db.Serialize("main")
|
||||||
|
|||||||
16
query.go
16
query.go
@@ -44,6 +44,18 @@ func (q *Query) bindInto(into *int, args ...any) *Query {
|
|||||||
}
|
}
|
||||||
for i, arg := range args {
|
for i, arg := range args {
|
||||||
*into++
|
*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 {
|
if asString, ok := arg.(string); ok {
|
||||||
q.stmt.BindText(*into, asString)
|
q.stmt.BindText(*into, asString)
|
||||||
} else if asInt, ok := arg.(int); ok {
|
} 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)
|
q.stmt.BindBool(*into, asBool)
|
||||||
} else {
|
} else {
|
||||||
// Check if the argument is a slice or array of any type
|
// 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 {
|
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
|
||||||
*into--
|
*into--
|
||||||
for i := 0; i < v.Len(); i++ {
|
for i := 0; i < v.Len(); i++ {
|
||||||
@@ -116,7 +128,7 @@ func (q *Query) ScanSingle(results ...any) (rerr error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !hasResult {
|
if !hasResult {
|
||||||
return fmt.Errorf("did not return any rows")
|
return ErrNoRows
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scan its columns
|
// Scan its columns
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package mysqlite
|
package mysqlite
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@@ -19,6 +20,14 @@ func TestSimpleQuery(t *testing.T) {
|
|||||||
require.Equal(t, 1, count, "expected empty count")
|
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) {
|
func TestSimpleQueryWithArgs(t *testing.T) {
|
||||||
db := openTestDb(t)
|
db := openTestDb(t)
|
||||||
var value string
|
var value string
|
||||||
@@ -85,12 +94,70 @@ func TestUpdateQuery(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateQueryWithWrongArguments(t *testing.T) {
|
func TestUpdateQueryWithWrongArguments(t *testing.T) {
|
||||||
|
type S struct {
|
||||||
|
Field string
|
||||||
|
}
|
||||||
db := openTestDb(t)
|
db := openTestDb(t)
|
||||||
value := "ipsum"
|
abc := S{
|
||||||
err := db.Query("insert into mytable(key, value) values ('lorem', ?)").Bind(&value).Exec()
|
Field: "ipsum",
|
||||||
|
}
|
||||||
|
err := db.Query("insert into mytable(key, value) values ('lorem', ?)").Bind(abc).Exec()
|
||||||
require.Error(t, err)
|
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) {
|
func TestQueryWithPointerStringArguments(t *testing.T) {
|
||||||
db := openTestDb(t)
|
db := openTestDb(t)
|
||||||
var result *string
|
var result *string
|
||||||
@@ -102,7 +169,7 @@ func TestQueryWithPointerStringArguments(t *testing.T) {
|
|||||||
|
|
||||||
func TestQueryWithPointerStringArgumentsCanSetToNull(t *testing.T) {
|
func TestQueryWithPointerStringArgumentsCanSetToNull(t *testing.T) {
|
||||||
db := openTestDb(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"
|
myString := "some string"
|
||||||
var result *string
|
var result *string
|
||||||
result = &myString
|
result = &myString
|
||||||
|
|||||||
Reference in New Issue
Block a user