1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
// Copyright 2021 Roxy Light
// SPDX-License-Identifier: ISC
package sqlitex
import (
"errors"
"zombiezen.com/go/sqlite"
)
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()
if err != nil {
stmt.Reset()
return err
}
if !hasRow {
stmt.Reset()
return errNoResults
}
return nil
}
func resultTeardown(stmt *sqlite.Stmt) error {
hasRow, err := stmt.Step()
if err != nil {
stmt.Reset()
return err
}
if hasRow {
stmt.Reset()
return errMultipleResults
}
return stmt.Reset()
}
// ResultBool reports whether the first column of the first and only row
// produced by running stmt
// is non-zero.
// It returns an error if there is not exactly one result row.
func ResultBool(stmt *sqlite.Stmt) (bool, error) {
res, err := ResultInt64(stmt)
return res != 0, err
}
// ResultInt returns the first column of the first and only row
// produced by running stmt
// as an integer.
// It returns an error if there is not exactly one result row.
func ResultInt(stmt *sqlite.Stmt) (int, error) {
res, err := ResultInt64(stmt)
return int(res), err
}
// ResultInt64 returns the first column of the first and only row
// produced by running stmt
// as an integer.
// It returns an error if there is not exactly one result row.
func ResultInt64(stmt *sqlite.Stmt) (int64, error) {
if err := resultSetup(stmt); err != nil {
return 0, err
}
res := stmt.ColumnInt64(0)
if err := resultTeardown(stmt); err != nil {
return 0, err
}
return res, nil
}
// ResultText returns the first column of the first and only row
// produced by running stmt
// as text.
// It returns an error if there is not exactly one result row.
func ResultText(stmt *sqlite.Stmt) (string, error) {
if err := resultSetup(stmt); err != nil {
return "", err
}
res := stmt.ColumnText(0)
if err := resultTeardown(stmt); err != nil {
return "", err
}
return res, nil
}
// ResultFloat returns the first column of the first and only row
// produced by running stmt
// as a real number.
// It returns an error if there is not exactly one result row.
func ResultFloat(stmt *sqlite.Stmt) (float64, error) {
if err := resultSetup(stmt); err != nil {
return 0, err
}
res := stmt.ColumnFloat(0)
if err := resultTeardown(stmt); err != nil {
return 0, err
}
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
}
|