Compare commits
4 Commits
850e4a27d8
...
v0.10.0
| Author | SHA1 | Date | |
|---|---|---|---|
| dd6be6b9b6 | |||
| 12a87a8762 | |||
| 87f10c73d6 | |||
| 58d63b6cf3 |
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)
|
||||
|
||||
// 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)
|
||||
log.Printf("Creating backup of database to %s", target)
|
||||
data, err := d.Db.Serialize("main")
|
||||
|
||||
29
query.go
29
query.go
@@ -34,23 +34,39 @@ func (d *Db) query(query string) *Query {
|
||||
}
|
||||
|
||||
func (q *Query) Bind(args ...any) *Query {
|
||||
into := 0
|
||||
return q.bindInto(&into, args...)
|
||||
}
|
||||
|
||||
func (q *Query) bindInto(into *int, args ...any) *Query {
|
||||
if q.err != nil || q.stmt == nil {
|
||||
return q
|
||||
}
|
||||
for i, arg := range args {
|
||||
*into++
|
||||
if asString, ok := arg.(string); ok {
|
||||
q.stmt.BindText(i+1, asString)
|
||||
q.stmt.BindText(*into, asString)
|
||||
} else if asInt, ok := arg.(int); ok {
|
||||
q.stmt.BindInt64(i+1, int64(asInt))
|
||||
q.stmt.BindInt64(*into, int64(asInt))
|
||||
} else if asInt, ok := arg.(int64); ok {
|
||||
q.stmt.BindInt64(i+1, asInt)
|
||||
q.stmt.BindInt64(*into, asInt)
|
||||
} else if asBool, ok := arg.(bool); ok {
|
||||
q.stmt.BindBool(i+1, asBool)
|
||||
q.stmt.BindBool(*into, asBool)
|
||||
} else {
|
||||
// Check if the argument is a slice or array of any type
|
||||
v := reflect.ValueOf(arg)
|
||||
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
|
||||
*into--
|
||||
for i := 0; i < v.Len(); i++ {
|
||||
q.bindInto(into, v.Index(i).Interface())
|
||||
}
|
||||
} else {
|
||||
*into--
|
||||
q.err = fmt.Errorf("unsupported column type %s at index %d", reflect.TypeOf(arg).Name(), i)
|
||||
return q
|
||||
}
|
||||
}
|
||||
}
|
||||
return q
|
||||
}
|
||||
|
||||
@@ -100,7 +116,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
|
||||
@@ -138,6 +154,9 @@ type Rows struct {
|
||||
}
|
||||
|
||||
func (q *Query) ScanMulti() (*Rows, error) {
|
||||
if q.err != nil {
|
||||
return nil, q.err
|
||||
}
|
||||
return &Rows{
|
||||
query: q,
|
||||
}, nil
|
||||
|
||||
@@ -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
|
||||
@@ -102,7 +111,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
|
||||
@@ -133,3 +142,29 @@ func TestTransactionRollback(t *testing.T) {
|
||||
db.Query("select value from mytable where key = 'foo'").MustScanSingle(&value)
|
||||
require.Equal(t, "bar", value, "expected original value after rollback")
|
||||
}
|
||||
|
||||
func TestQueryWithInClause(t *testing.T) {
|
||||
db := openTestDb(t)
|
||||
// Insert additional test rows
|
||||
db.Query("insert into mytable(key, value) values ('key1', 'value1')").MustExec()
|
||||
db.Query("insert into mytable(key, value) values ('key2', 'value2')").MustExec()
|
||||
|
||||
// Execute query with IN clause
|
||||
args := []string{"foo", "key2"}
|
||||
rows, err := db.Query("select key, value from mytable where key in (?, ?)").Bind(args).ScanMulti()
|
||||
require.NoError(t, err)
|
||||
defer rows.MustFinish()
|
||||
|
||||
// Check results
|
||||
results := make(map[string]string)
|
||||
for rows.MustNext() {
|
||||
var key, value string
|
||||
rows.MustScan(&key, &value)
|
||||
results[key] = value
|
||||
}
|
||||
|
||||
// Verify we got exactly the expected results
|
||||
require.Equal(t, 2, len(results), "expected 2 matching rows")
|
||||
require.Equal(t, "bar", results["foo"], "unexpected value for key 'foo'")
|
||||
require.Equal(t, "value2", results["key2"], "unexpected value for key 'key2'")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user