Files
sqlite3/driver/driver.go

587 lines
13 KiB
Go
Raw Normal View History

2023-02-14 18:21:18 +00:00
// Package driver provides a database/sql driver for SQLite.
2023-02-28 14:50:15 +00:00
//
// Importing package driver registers a [database/sql] driver named "sqlite3".
// You may also need to import package embed.
//
// import _ "github.com/ncruces/go-sqlite3/driver"
// import _ "github.com/ncruces/go-sqlite3/embed"
//
// The data source name for "sqlite3" databases can be a filename or a "file:" [URI].
//
// The [TRANSACTION] mode can be specified using "_txlock":
//
// sql.Open("sqlite3", "file:demo.db?_txlock=immediate")
//
2023-12-07 03:09:37 -08:00
// Possible values are: "deferred", "immediate", "exclusive".
// A [read-only] transaction is always "deferred", regardless of "_txlock".
//
// The time encoding/decoding format can be specified using "_timefmt":
//
// sql.Open("sqlite3", "file:demo.db?_timefmt=sqlite")
//
// Possible values are: "auto" (the default), "sqlite", "rfc3339";
// "auto" encodes as RFC 3339 and decodes any [format] supported by SQLite;
// "sqlite" encodes as SQLite and decodes any [format] supported by SQLite;
// "rfc3339" encodes and decodes RFC 3339 only.
//
2023-02-28 14:50:15 +00:00
// [PRAGMA] statements can be specified using "_pragma":
//
2023-06-24 02:18:56 +01:00
// sql.Open("sqlite3", "file:demo.db?_pragma=busy_timeout(10000)")
2023-02-28 14:50:15 +00:00
//
2023-06-24 02:18:56 +01:00
// If no PRAGMAs are specified, a busy timeout of 1 minute is set.
2023-02-28 14:50:15 +00:00
//
2023-05-25 11:14:18 +01:00
// Order matters:
// busy timeout and locking mode should be the first PRAGMAs set, in that order.
//
2023-11-09 16:35:45 +00:00
// [URI]: https://sqlite.org/uri.html
// [PRAGMA]: https://sqlite.org/pragma.html
2023-12-07 03:09:37 -08:00
// [format]: https://sqlite.org/lang_datefunc.html#time_values
2023-11-09 16:35:45 +00:00
// [TRANSACTION]: https://sqlite.org/lang_transaction.html#deferred_immediate_and_exclusive_transactions
2023-12-07 03:09:37 -08:00
// [read-only]: https://pkg.go.dev/database/sql#TxOptions
2023-02-14 18:21:18 +00:00
package driver
import (
2023-02-18 02:16:11 +00:00
"context"
2023-02-14 18:21:18 +00:00
"database/sql"
"database/sql/driver"
2023-10-25 12:56:52 +01:00
"errors"
2023-02-20 13:30:01 +00:00
"fmt"
2023-02-17 02:21:07 +00:00
"io"
2023-02-18 12:20:42 +00:00
"net/url"
"strings"
2023-02-17 02:21:07 +00:00
"time"
2024-01-16 15:18:14 +00:00
"unsafe"
2023-02-14 18:21:18 +00:00
"github.com/ncruces/go-sqlite3"
2023-03-29 15:01:25 +01:00
"github.com/ncruces/go-sqlite3/internal/util"
2023-02-14 18:21:18 +00:00
)
2023-09-20 02:41:09 +01:00
// This variable can be replaced with -ldflags:
//
2023-12-07 03:09:37 -08:00
// go build -ldflags="-X github.com/ncruces/go-sqlite3/driver.driverName=sqlite"
2023-09-20 02:41:09 +01:00
var driverName = "sqlite3"
2023-02-14 18:21:18 +00:00
func init() {
2023-09-20 02:41:09 +01:00
if driverName != "" {
sql.Register(driverName, &SQLite{})
2023-09-20 02:41:09 +01:00
}
}
// Open opens the SQLite database specified by dataSourceName as a [database/sql.DB].
//
// The init function is called by the driver on new connections.
2024-04-22 15:28:19 +01:00
// The [sqlite3.Conn] can be used to execute queries, register functions, etc.
// Any error returned closes the connection and is returned to [database/sql].
2023-10-13 18:53:37 +01:00
func Open(dataSourceName string, init func(*sqlite3.Conn) error) (*sql.DB, error) {
c, err := (&SQLite{Init: init}).OpenConnector(dataSourceName)
2023-09-20 02:41:09 +01:00
if err != nil {
return nil, err
}
return sql.OpenDB(c), nil
2023-02-14 18:21:18 +00:00
}
2024-04-22 15:28:19 +01:00
// SQLite implements [database/sql/driver.Driver].
type SQLite struct {
2024-04-22 15:28:19 +01:00
// Init function is called by the driver on new connections.
// The [sqlite3.Conn] can be used to execute queries, register functions, etc.
// Any error returned closes the connection and is returned to [database/sql].
Init func(*sqlite3.Conn) error
}
2023-02-14 18:21:18 +00:00
2024-04-22 15:28:19 +01:00
// Open implements [database/sql/driver.Driver].
func (d *SQLite) Open(name string) (driver.Conn, error) {
c, err := d.newConnector(name)
2023-08-10 13:18:13 +01:00
if err != nil {
return nil, err
}
return c.Connect(context.Background())
}
2024-04-22 15:28:19 +01:00
// OpenConnector implements [database/sql/driver.DriverContext].
func (d *SQLite) OpenConnector(name string) (driver.Connector, error) {
return d.newConnector(name)
2023-09-20 02:41:09 +01:00
}
func (d *SQLite) newConnector(name string) (*connector, error) {
c := connector{driver: d, name: name}
2023-12-07 03:09:37 -08:00
var txlock, timefmt string
2023-08-10 13:18:13 +01:00
if strings.HasPrefix(name, "file:") {
if _, after, ok := strings.Cut(name, "?"); ok {
query, err := url.ParseQuery(after)
if err != nil {
return nil, err
}
2023-12-07 03:09:37 -08:00
txlock = query.Get("_txlock")
timefmt = query.Get("_timefmt")
c.pragmas = query.Has("_pragma")
2023-08-10 13:18:13 +01:00
}
}
2023-12-07 03:09:37 -08:00
switch txlock {
case "":
c.txBegin = "BEGIN"
case "deferred", "immediate", "exclusive":
c.txBegin = "BEGIN " + txlock
default:
return nil, fmt.Errorf("sqlite3: invalid _txlock: %s", txlock)
}
switch timefmt {
case "":
c.tmRead = sqlite3.TimeFormatAuto
c.tmWrite = sqlite3.TimeFormatDefault
case "sqlite":
c.tmRead = sqlite3.TimeFormatAuto
c.tmWrite = sqlite3.TimeFormat3
case "rfc3339":
c.tmRead = sqlite3.TimeFormatDefault
c.tmWrite = sqlite3.TimeFormatDefault
default:
c.tmRead = sqlite3.TimeFormat(timefmt)
c.tmWrite = sqlite3.TimeFormat(timefmt)
}
2023-08-10 13:18:13 +01:00
return &c, nil
}
type connector struct {
driver *SQLite
2023-08-10 13:18:13 +01:00
name string
2023-12-07 03:09:37 -08:00
txBegin string
tmRead sqlite3.TimeFormat
tmWrite sqlite3.TimeFormat
2023-08-10 13:18:13 +01:00
pragmas bool
}
func (n *connector) Driver() driver.Driver {
return n.driver
2023-08-10 13:18:13 +01:00
}
func (n *connector) Connect(ctx context.Context) (_ driver.Conn, err error) {
2023-12-07 03:09:37 -08:00
c := &conn{
txBegin: n.txBegin,
tmRead: n.tmRead,
tmWrite: n.tmWrite,
}
2023-08-10 13:18:13 +01:00
c.Conn, err = sqlite3.Open(n.name)
2023-02-17 16:19:55 +00:00
if err != nil {
return nil, err
}
2023-07-13 11:07:54 +01:00
defer func() {
if err != nil {
c.Close()
}
}()
2023-02-19 16:16:13 +00:00
2023-08-10 13:18:13 +01:00
old := c.Conn.SetInterrupt(ctx)
defer c.Conn.SetInterrupt(old)
2023-02-28 16:03:31 +00:00
2023-08-10 13:18:13 +01:00
if !n.pragmas {
2024-02-02 23:41:34 +00:00
err = c.Conn.BusyTimeout(60 * time.Second)
2023-04-17 00:29:20 +01:00
if err != nil {
return nil, err
}
2023-10-13 18:53:37 +01:00
}
if n.driver.Init != nil {
err = n.driver.Init(c.Conn)
2023-02-28 16:03:31 +00:00
if err != nil {
return nil, err
}
2023-10-13 18:53:37 +01:00
}
if n.pragmas || n.driver.Init != nil {
2023-10-13 18:53:37 +01:00
s, _, err := c.Conn.Prepare(`PRAGMA query_only`)
2023-04-17 00:29:20 +01:00
if err != nil {
return nil, err
}
2023-10-13 18:53:37 +01:00
if s.Step() && s.ColumnBool(0) {
c.readOnly = '1'
} else {
c.readOnly = '0'
}
err = s.Close()
2023-09-20 02:41:09 +01:00
if err != nil {
return nil, err
}
}
2023-12-07 03:09:37 -08:00
return c, nil
2023-02-14 18:21:18 +00:00
}
2023-02-18 12:20:42 +00:00
type conn struct {
2023-05-30 13:39:34 +01:00
*sqlite3.Conn
2023-03-08 18:05:18 +00:00
txBegin string
txCommit string
txRollback string
2023-12-07 03:09:37 -08:00
tmRead sqlite3.TimeFormat
tmWrite sqlite3.TimeFormat
2023-04-17 00:29:20 +01:00
readOnly byte
2023-02-18 12:20:42 +00:00
}
2023-02-17 02:21:07 +00:00
var (
2023-02-17 16:19:55 +00:00
// Ensure these interfaces are implemented:
2023-09-20 02:41:09 +01:00
_ driver.ConnPrepareContext = &conn{}
_ driver.ExecerContext = &conn{}
_ driver.ConnBeginTx = &conn{}
_ sqlite3.DriverConn = &conn{}
2023-02-17 02:21:07 +00:00
)
2023-09-20 02:41:09 +01:00
func (c *conn) Raw() *sqlite3.Conn {
return c.Conn
}
2024-06-21 13:01:55 +01:00
// Deprecated: use BeginTx instead.
2023-05-30 13:39:34 +01:00
func (c *conn) Begin() (driver.Tx, error) {
2024-07-10 15:41:28 +01:00
// notest
2023-02-19 16:16:13 +00:00
return c.BeginTx(context.Background(), driver.TxOptions{})
}
2023-05-30 13:39:34 +01:00
func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
2023-02-19 16:16:13 +00:00
txBegin := c.txBegin
2023-02-25 15:34:24 +00:00
c.txCommit = `COMMIT`
2023-03-08 18:05:18 +00:00
c.txRollback = `ROLLBACK`
2023-02-19 16:16:13 +00:00
if opts.ReadOnly {
txBegin = `
2023-02-20 13:30:01 +00:00
BEGIN deferred;
2023-02-25 15:34:24 +00:00
PRAGMA query_only=on`
2023-10-13 18:53:37 +01:00
c.txRollback = `
2023-03-08 18:05:18 +00:00
ROLLBACK;
2023-04-17 00:29:20 +01:00
PRAGMA query_only=` + string(c.readOnly)
2023-10-13 18:53:37 +01:00
c.txCommit = c.txRollback
2023-03-08 18:05:18 +00:00
}
switch opts.Isolation {
default:
2023-03-29 15:01:25 +01:00
return nil, util.IsolationErr
2023-03-08 18:05:18 +00:00
case
driver.IsolationLevel(sql.LevelDefault),
driver.IsolationLevel(sql.LevelSerializable):
break
2023-02-19 16:16:13 +00:00
}
2023-05-30 13:39:34 +01:00
old := c.Conn.SetInterrupt(ctx)
defer c.Conn.SetInterrupt(old)
err := c.Conn.Exec(txBegin)
2023-02-17 02:21:07 +00:00
if err != nil {
return nil, err
}
return c, nil
}
2023-05-30 13:39:34 +01:00
func (c *conn) Commit() error {
err := c.Conn.Exec(c.txCommit)
2023-09-20 02:41:09 +01:00
if err != nil && !c.Conn.GetAutocommit() {
2023-02-17 02:21:07 +00:00
c.Rollback()
}
return err
}
2023-02-14 18:21:18 +00:00
2023-05-30 13:39:34 +01:00
func (c *conn) Rollback() error {
2023-10-25 12:56:52 +01:00
err := c.Conn.Exec(c.txRollback)
if errors.Is(err, sqlite3.INTERRUPT) {
old := c.Conn.SetInterrupt(context.Background())
defer c.Conn.SetInterrupt(old)
err = c.Conn.Exec(c.txRollback)
}
return err
2023-02-17 02:21:07 +00:00
}
2023-02-14 18:21:18 +00:00
2023-05-30 13:39:34 +01:00
func (c *conn) Prepare(query string) (driver.Stmt, error) {
2024-07-10 15:41:28 +01:00
// notest
2023-05-30 13:39:34 +01:00
return c.PrepareContext(context.Background(), query)
}
func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
old := c.Conn.SetInterrupt(ctx)
defer c.Conn.SetInterrupt(old)
s, tail, err := c.Conn.Prepare(query)
2023-02-17 02:21:07 +00:00
if err != nil {
return nil, err
}
2023-02-18 02:57:47 +00:00
if tail != "" {
2023-11-30 12:26:15 +00:00
s.Close()
return nil, util.TailErr
2023-02-18 02:57:47 +00:00
}
2024-06-19 13:54:58 +01:00
return &stmt{Stmt: s, tmRead: c.tmRead, tmWrite: c.tmWrite, inputs: -2}, nil
2023-02-17 02:21:07 +00:00
}
2023-05-30 13:39:34 +01:00
func (c *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
2023-02-18 02:57:47 +00:00
if len(args) != 0 {
// Slow path.
return nil, driver.ErrSkip
}
2023-11-07 15:19:40 +00:00
if savept, ok := ctx.(*saveptCtx); ok {
// Called from driver.Savepoint.
2023-12-07 03:09:37 -08:00
savept.Savepoint = c.Conn.Savepoint()
2023-11-08 07:28:48 +00:00
return resultRowsAffected(0), nil
2023-11-07 15:19:40 +00:00
}
2023-05-30 13:39:34 +01:00
old := c.Conn.SetInterrupt(ctx)
defer c.Conn.SetInterrupt(old)
2023-02-18 02:57:47 +00:00
2023-05-30 13:39:34 +01:00
err := c.Conn.Exec(query)
2023-02-18 02:57:47 +00:00
if err != nil {
return nil, err
}
2023-05-30 13:39:34 +01:00
return newResult(c.Conn), nil
2023-05-17 14:29:59 +01:00
}
2024-04-28 10:33:39 +01:00
func (c *conn) CheckNamedValue(arg *driver.NamedValue) error {
2023-10-18 23:14:46 +01:00
return nil
}
2023-02-17 02:21:07 +00:00
type stmt struct {
2023-12-04 12:37:53 +00:00
*sqlite3.Stmt
2023-12-07 03:09:37 -08:00
tmWrite sqlite3.TimeFormat
tmRead sqlite3.TimeFormat
2024-06-19 13:54:58 +01:00
inputs int
2023-02-17 02:21:07 +00:00
}
2023-02-17 16:19:55 +00:00
var (
// Ensure these interfaces are implemented:
2023-05-30 13:39:34 +01:00
_ driver.StmtExecContext = &stmt{}
_ driver.StmtQueryContext = &stmt{}
_ driver.NamedValueChecker = &stmt{}
2023-02-17 16:19:55 +00:00
)
2023-05-30 13:39:34 +01:00
func (s *stmt) NumInput() int {
2024-06-19 13:54:58 +01:00
if s.inputs >= -1 {
return s.inputs
}
2023-05-30 13:39:34 +01:00
n := s.Stmt.BindCount()
2023-02-20 13:30:01 +00:00
for i := 1; i <= n; i++ {
2023-05-30 13:39:34 +01:00
if s.Stmt.BindName(i) != "" {
2024-06-19 13:54:58 +01:00
s.inputs = -1
2023-02-20 13:30:01 +00:00
return -1
}
}
2024-06-19 13:54:58 +01:00
s.inputs = n
2023-02-20 13:30:01 +00:00
return n
2023-02-17 02:21:07 +00:00
}
2023-02-18 02:16:11 +00:00
// Deprecated: use ExecContext instead.
2023-05-30 13:39:34 +01:00
func (s *stmt) Exec(args []driver.Value) (driver.Result, error) {
2024-07-10 15:41:28 +01:00
// notest
2023-02-18 02:16:11 +00:00
return s.ExecContext(context.Background(), namedValues(args))
}
// Deprecated: use QueryContext instead.
2023-05-30 13:39:34 +01:00
func (s *stmt) Query(args []driver.Value) (driver.Rows, error) {
2024-07-10 15:41:28 +01:00
// notest
2023-02-18 02:16:11 +00:00
return s.QueryContext(context.Background(), namedValues(args))
}
2023-05-30 13:39:34 +01:00
func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
2023-08-10 13:18:13 +01:00
err := s.setupBindings(args)
2023-02-14 18:21:18 +00:00
if err != nil {
return nil, err
}
2023-02-17 02:21:07 +00:00
2023-12-04 12:37:53 +00:00
old := s.Stmt.Conn().SetInterrupt(ctx)
defer s.Stmt.Conn().SetInterrupt(old)
2023-08-10 13:18:13 +01:00
2023-05-30 13:39:34 +01:00
err = s.Stmt.Exec()
2023-02-17 02:21:07 +00:00
if err != nil {
return nil, err
}
2023-12-04 12:37:53 +00:00
return newResult(s.Stmt.Conn()), nil
2023-02-17 02:21:07 +00:00
}
2023-05-30 13:39:34 +01:00
func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
2023-08-10 13:18:13 +01:00
err := s.setupBindings(args)
2023-02-18 02:16:11 +00:00
if err != nil {
return nil, err
}
2023-12-14 14:02:41 +00:00
return &rows{ctx: ctx, stmt: s}, nil
2023-08-04 14:12:36 +01:00
}
2024-06-19 13:54:58 +01:00
func (s *stmt) setupBindings(args []driver.NamedValue) (err error) {
2023-02-18 02:16:11 +00:00
var ids [3]int
for _, arg := range args {
ids := ids[:0]
if arg.Name == "" {
ids = append(ids, arg.Ordinal)
} else {
for _, prefix := range []string{":", "@", "$"} {
2023-05-30 13:39:34 +01:00
if id := s.Stmt.BindIndex(prefix + arg.Name); id != 0 {
2023-02-18 02:16:11 +00:00
ids = append(ids, id)
}
}
}
for _, id := range ids {
switch a := arg.Value.(type) {
case bool:
2023-05-30 13:39:34 +01:00
err = s.Stmt.BindBool(id, a)
2023-02-22 14:19:56 +00:00
case int:
2023-05-30 13:39:34 +01:00
err = s.Stmt.BindInt(id, a)
2023-02-18 02:16:11 +00:00
case int64:
2023-05-30 13:39:34 +01:00
err = s.Stmt.BindInt64(id, a)
2023-02-18 02:16:11 +00:00
case float64:
2023-05-30 13:39:34 +01:00
err = s.Stmt.BindFloat(id, a)
2023-02-18 02:16:11 +00:00
case string:
2023-05-30 13:39:34 +01:00
err = s.Stmt.BindText(id, a)
2023-02-18 02:16:11 +00:00
case []byte:
2023-05-30 13:39:34 +01:00
err = s.Stmt.BindBlob(id, a)
2023-02-22 14:19:56 +00:00
case sqlite3.ZeroBlob:
2023-05-30 13:39:34 +01:00
err = s.Stmt.BindZeroBlob(id, int64(a))
2023-02-18 02:16:11 +00:00
case time.Time:
2023-12-07 03:09:37 -08:00
err = s.Stmt.BindTime(id, a, s.tmWrite)
2023-12-29 11:37:50 +00:00
case util.JSON:
err = s.Stmt.BindJSON(id, a.Value)
case util.PointerUnwrap:
err = s.Stmt.BindPointer(id, util.UnwrapPointer(a))
2023-02-18 02:16:11 +00:00
case nil:
2023-05-30 13:39:34 +01:00
err = s.Stmt.BindNull(id)
2023-02-18 02:16:11 +00:00
default:
2023-03-29 15:01:25 +01:00
panic(util.AssertErr())
2023-02-18 02:16:11 +00:00
}
2023-02-17 02:21:07 +00:00
}
if err != nil {
2023-08-04 14:12:36 +01:00
return err
2023-02-17 02:21:07 +00:00
}
}
2023-08-04 14:12:36 +01:00
return nil
2023-02-17 02:21:07 +00:00
}
2023-05-30 13:39:34 +01:00
func (s *stmt) CheckNamedValue(arg *driver.NamedValue) error {
2023-02-22 14:19:56 +00:00
switch arg.Value.(type) {
case bool, int, int64, float64, string, []byte,
2023-12-29 11:37:50 +00:00
time.Time, sqlite3.ZeroBlob,
util.JSON, util.PointerUnwrap,
2023-11-09 16:16:48 +00:00
nil:
2023-02-22 14:19:56 +00:00
return nil
default:
return driver.ErrSkip
}
}
2023-05-08 10:34:33 +01:00
func newResult(c *sqlite3.Conn) driver.Result {
rows := c.Changes()
if rows != 0 {
id := c.LastInsertRowID()
if id != 0 {
return result{id, rows}
}
}
return resultRowsAffected(rows)
}
2023-02-17 02:21:07 +00:00
type result struct{ lastInsertId, rowsAffected int64 }
func (r result) LastInsertId() (int64, error) {
return r.lastInsertId, nil
}
func (r result) RowsAffected() (int64, error) {
return r.rowsAffected, nil
}
2023-05-08 10:34:33 +01:00
type resultRowsAffected int64
func (r resultRowsAffected) LastInsertId() (int64, error) {
return 0, nil
}
func (r resultRowsAffected) RowsAffected() (int64, error) {
return int64(r), nil
}
2023-02-18 02:16:11 +00:00
type rows struct {
2023-12-07 03:09:37 -08:00
ctx context.Context
2023-12-14 14:02:41 +00:00
*stmt
names []string
types []string
2023-02-18 02:16:11 +00:00
}
2023-02-17 02:21:07 +00:00
2023-05-30 13:39:34 +01:00
func (r *rows) Close() error {
2023-12-04 12:37:53 +00:00
r.Stmt.ClearBindings()
2023-05-30 13:39:34 +01:00
return r.Stmt.Reset()
2023-02-17 02:21:07 +00:00
}
2023-05-30 13:39:34 +01:00
func (r *rows) Columns() []string {
2023-12-14 14:02:41 +00:00
if r.names == nil {
count := r.Stmt.ColumnCount()
r.names = make([]string, count)
for i := range r.names {
r.names[i] = r.Stmt.ColumnName(i)
}
}
return r.names
}
func (r *rows) declType(index int) string {
if r.types == nil {
count := r.Stmt.ColumnCount()
r.types = make([]string, count)
for i := range r.types {
r.types[i] = strings.ToUpper(r.Stmt.ColumnDeclType(i))
}
2023-02-17 02:21:07 +00:00
}
2023-12-14 14:02:41 +00:00
return r.types[index]
2023-02-14 18:21:18 +00:00
}
2023-12-01 02:38:56 +00:00
func (r *rows) ColumnTypeDatabaseTypeName(index int) string {
2023-12-14 14:02:41 +00:00
decltype := r.declType(index)
2023-12-01 02:38:56 +00:00
if len := len(decltype); len > 0 && decltype[len-1] == ')' {
if i := strings.LastIndexByte(decltype, '('); i >= 0 {
decltype = decltype[:i]
}
}
2023-12-14 14:02:41 +00:00
return strings.TrimSpace(decltype)
2023-12-01 02:38:56 +00:00
}
2023-05-30 13:39:34 +01:00
func (r *rows) Next(dest []driver.Value) error {
2023-12-04 12:37:53 +00:00
old := r.Stmt.Conn().SetInterrupt(r.ctx)
defer r.Stmt.Conn().SetInterrupt(old)
2023-02-18 02:16:11 +00:00
2023-05-30 13:39:34 +01:00
if !r.Stmt.Step() {
if err := r.Stmt.Err(); err != nil {
2023-02-17 12:30:07 +00:00
return err
2023-02-17 02:21:07 +00:00
}
2023-02-17 12:30:07 +00:00
return io.EOF
2023-02-17 02:21:07 +00:00
}
2023-02-14 18:21:18 +00:00
2024-01-16 15:18:14 +00:00
data := unsafe.Slice((*any)(unsafe.SliceData(dest)), len(dest))
err := r.Stmt.Columns(data)
2023-02-17 02:21:07 +00:00
for i := range dest {
2024-01-16 15:18:14 +00:00
if t, ok := r.decodeTime(i, dest[i]); ok {
dest[i] = t
2024-01-23 17:50:11 +00:00
continue
}
if s, ok := dest[i].(string); ok {
t, ok := maybeTime(s)
if ok {
dest[i] = t
}
2023-02-17 02:21:07 +00:00
}
}
2024-01-16 15:18:14 +00:00
return err
2023-02-17 02:21:07 +00:00
}
2023-12-07 03:09:37 -08:00
2024-06-21 13:01:55 +01:00
func (r *rows) decodeTime(i int, v any) (_ time.Time, ok bool) {
2023-12-14 14:02:41 +00:00
if r.tmRead == sqlite3.TimeFormatDefault {
2024-06-21 13:01:55 +01:00
// handled by maybeTime
2023-12-07 03:09:37 -08:00
return
}
2024-06-21 13:01:55 +01:00
switch v.(type) {
case int64, float64, string:
// could be a time value
2023-12-07 03:09:37 -08:00
default:
return
}
2024-06-21 13:01:55 +01:00
switch r.declType(i) {
case "DATE", "TIME", "DATETIME", "TIMESTAMP":
// could be a time value
2023-12-07 03:09:37 -08:00
default:
return
}
2024-01-16 15:18:14 +00:00
t, err := r.tmRead.Decode(v)
return t, err == nil
2023-12-07 03:09:37 -08:00
}