Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ExecOptions.ExpectResults #87

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions sqlitex/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ type ExecOptions struct {
// If ResultFunc returns an error then iteration ceases
// and the execution function returns the error value.
ResultFunc func(stmt *sqlite.Stmt) error

// ExpectRows, if enabled, will cause Execute calls to return [ErrNoRows] if the statement returns no rows.
ExpectRows bool
}

// Exec executes an SQLite query.
Expand Down Expand Up @@ -289,6 +292,7 @@ func exec(stmt *sqlite.Stmt, flags uint8, opts *ExecOptions) (err error) {
}
return fmt.Errorf("sqlitex.Exec: missing argument for %s", name)
}
hasResults := false
for {
hasRow, err := stmt.Step()
if err != nil {
Expand All @@ -297,12 +301,16 @@ func exec(stmt *sqlite.Stmt, flags uint8, opts *ExecOptions) (err error) {
if !hasRow {
break
}
hasResults = true
if opts != nil && opts.ResultFunc != nil {
if err := opts.ResultFunc(stmt); err != nil {
return err
}
}
}
if opts != nil && opts.ExpectRows && !hasResults {
return ErrNoResults
}
return nil
}

Expand Down
54 changes: 54 additions & 0 deletions sqlitex/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package sqlitex

import (
"errors"
"fmt"
"reflect"
"testing"
Expand Down Expand Up @@ -296,6 +297,59 @@ INSERT INTO t (a, b) VALUES ('a2', :a2);
})
}

func TestExecuteExpectResults(t *testing.T) {
conn, err := sqlite.OpenConn(":memory:", 0)
if err != nil {
t.Fatal(err)
}
defer conn.Close()

script := `
CREATE TABLE t (a TEXT, b INTEGER);
INSERT INTO t (a, b) VALUES ('a1', 1);
`
err = ExecuteScript(conn, script, &ExecOptions{})
if err != nil {
t.Fatal(err)
}

args := map[string]any{
":a": "a2",
}

var bVal int

err = Execute(conn, `SELECT b FROM t WHERE a==:a`, &ExecOptions{
Named: args,
ResultFunc: func(stmt *sqlite.Stmt) error {
bVal = stmt.ColumnInt(0)
return nil
},
ExpectRows: false,
})
if err != nil {
t.Errorf("err=%v, want nil", err)
}
if bVal != 0 {
t.Errorf("bVal=%d, want 0 - ResultFunc should not have run", bVal)
}

err = Execute(conn, `SELECT b FROM t WHERE a==:a`, &ExecOptions{
Named: args,
ResultFunc: func(stmt *sqlite.Stmt) error {
bVal = stmt.ColumnInt(0)
return nil
},
ExpectRows: true,
})
if !errors.Is(err, ErrNoResults) {
t.Errorf("err=%v, want ErrNoResults", err)
}
if bVal != 0 {
t.Errorf("bVal=%d, want 0 - ResultFunc should not have run", bVal)
}
}

func TestBitsetHasAll(t *testing.T) {
tests := []struct {
bs bitset
Expand Down
10 changes: 6 additions & 4 deletions sqlitex/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"zombiezen.com/go/sqlite"
)

var errNoResults = errors.New("sqlite: statement has no results")
var errMultipleResults = errors.New("sqlite: statement has multiple result rows")
var (
ErrNoResults = errors.New("sqlite: statement has no results")
ErrMultipleResults = errors.New("sqlite: statement has multiple result rows")
)

func resultSetup(stmt *sqlite.Stmt) error {
hasRow, err := stmt.Step()
Expand All @@ -20,7 +22,7 @@ func resultSetup(stmt *sqlite.Stmt) error {
}
if !hasRow {
stmt.Reset()
return errNoResults
return ErrNoResults
}
return nil
}
Expand All @@ -33,7 +35,7 @@ func resultTeardown(stmt *sqlite.Stmt) error {
}
if hasRow {
stmt.Reset()
return errMultipleResults
return ErrMultipleResults
}
return stmt.Reset()
}
Expand Down