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: ResultBytes #86

Merged
merged 5 commits into from
Aug 16, 2024
Merged
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
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) {
zombiezen marked this conversation as resolved.
Show resolved Hide resolved
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