Skip to content

Commit c29a657

Browse files
Add sqlitex.ResultBytes function (#86)
- Add ResultBytes to sqlitex to support one-result BLOB queries - Add unit tests for Result* functions Co-authored-by: Roxy Light <[email protected]>
1 parent 26b464f commit c29a657

File tree

3 files changed

+384
-2
lines changed

3 files changed

+384
-2
lines changed

Diff for: CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
## [Unreleased][]
1111

12+
### Added
13+
14+
- New function `sqlitex.ResultBytes`.
15+
([#86](https://github.com/zombiezen/go-sqlite/pull/86))
16+
1217
### Changed
1318

1419
- `Conn.Close` returns an error if the connection has already been closed

Diff for: sqlitex/query.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import (
99
"zombiezen.com/go/sqlite"
1010
)
1111

12-
var errNoResults = errors.New("sqlite: statement has no results")
13-
var errMultipleResults = errors.New("sqlite: statement has multiple result rows")
12+
var (
13+
errNoResults = errors.New("sqlite: statement has no results")
14+
errMultipleResults = errors.New("sqlite: statement has multiple result rows")
15+
)
1416

1517
func resultSetup(stmt *sqlite.Stmt) error {
1618
hasRow, err := stmt.Step()
@@ -100,3 +102,18 @@ func ResultFloat(stmt *sqlite.Stmt) (float64, error) {
100102
}
101103
return res, nil
102104
}
105+
106+
// ResultBytes reads the first column of the first and only row
107+
// produced by running stmt into buf,
108+
// returning the number of bytes read.
109+
// It returns an error if there is not exactly one result row.
110+
func ResultBytes(stmt *sqlite.Stmt, buf []byte) (int, error) {
111+
if err := resultSetup(stmt); err != nil {
112+
return 0, err
113+
}
114+
read := stmt.ColumnBytes(0, buf)
115+
if err := resultTeardown(stmt); err != nil {
116+
return 0, err
117+
}
118+
return read, nil
119+
}

0 commit comments

Comments
 (0)