Skip to content

Commit

Permalink
Add sqlitex.ResultBytes function (#86)
Browse files Browse the repository at this point in the history
- Add ResultBytes to sqlitex to support one-result BLOB queries
- Add unit tests for Result* functions

Co-authored-by: Roxy Light <[email protected]>
  • Loading branch information
flowchartsman and zombiezen authored Aug 16, 2024
1 parent 26b464f commit c29a657
Show file tree
Hide file tree
Showing 3 changed files with 384 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased][]

### Added

- New function `sqlitex.ResultBytes`.
([#86](https://github.com/zombiezen/go-sqlite/pull/86))

### Changed

- `Conn.Close` returns an error if the connection has already been closed
Expand Down
21 changes: 19 additions & 2 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 Down Expand Up @@ -100,3 +102,18 @@ func ResultFloat(stmt *sqlite.Stmt) (float64, error) {
}
return res, nil
}

// ResultBytes reads the first column of the first and only row
// produced by running stmt into buf,
// returning the number of bytes read.
// It returns an error if there is not exactly one result row.
func ResultBytes(stmt *sqlite.Stmt, buf []byte) (int, error) {
if err := resultSetup(stmt); err != nil {
return 0, err
}
read := stmt.ColumnBytes(0, buf)
if err := resultTeardown(stmt); err != nil {
return 0, err
}
return read, nil
}
Loading

0 comments on commit c29a657

Please sign in to comment.