From f9e867be609dd6ba6b112ff8adeae38ea626607b Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Fri, 4 Oct 2024 16:39:34 +0100 Subject: [PATCH] Time fix. --- driver/driver.go | 7 +++++-- ext/statement/stmt.go | 2 +- tests/time_test.go | 1 + time.go | 3 +++ 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/driver/driver.go b/driver/driver.go index 086a87e..f2568e8 100644 --- a/driver/driver.go +++ b/driver/driver.go @@ -40,14 +40,14 @@ // When using a custom time struct, you'll have to implement // [database/sql/driver.Valuer] and [database/sql.Scanner]. // -// The Value method should ideally serialise to a time [format] supported by SQLite. +// The Value method should ideally encode to a time [format] supported by SQLite. // This ensures SQL date and time functions work as they should, // and that your schema works with other SQLite tools. // [sqlite3.TimeFormat.Encode] may help. // // The Scan method needs to take into account that the value it receives can be of differing types. // It can already be a [time.Time], if the driver decoded the value according to "_timefmt" rules. -// Or it can be a: string, int64, float64, []byte, nil, +// Or it can be a: string, int64, float64, []byte, or nil, // depending on the column type and what whoever wrote the value. // [sqlite3.TimeFormat.Decode] may help. // @@ -680,6 +680,9 @@ func (r *rows) decodeTime(i int, v any) (_ time.Time, ok bool) { case int64, float64: // could be a time value case string: + if r.tmWrite != "" && r.tmWrite != time.RFC3339 && r.tmWrite != time.RFC3339Nano { + break + } t, ok := maybeTime(v) if ok { return t, true diff --git a/ext/statement/stmt.go b/ext/statement/stmt.go index 9e08a6d..9016cd5 100644 --- a/ext/statement/stmt.go +++ b/ext/statement/stmt.go @@ -34,7 +34,7 @@ func declare(db *sqlite3.Conn, _, _, _ string, arg ...string) (*table, error) { sql := "SELECT * FROM\n" + arg[0] - stmt, _, err := db.Prepare(sql) + stmt, _, err := db.PrepareFlags(sql, sqlite3.PREPARE_PERSISTENT) if err != nil { return nil, err } diff --git a/tests/time_test.go b/tests/time_test.go index 2482ec6..611a6f5 100644 --- a/tests/time_test.go +++ b/tests/time_test.go @@ -111,6 +111,7 @@ func TestTimeFormat_Decode(t *testing.T) { {sqlite3.TimeFormatDefault, "2013-10-07T04:23:19.12-04:00", reference, 0, offset, false}, {sqlite3.TimeFormatDefault, "2013-10-07T08:23:19.12Z", reference, 0, 0, false}, + {sqlite3.TimeFormatDefault, reference, reference, 0, offset, false}, {sqlite3.TimeFormatDefault, false, time.Time{}, 0, 0, true}, } diff --git a/time.go b/time.go index 0164a30..d9c516c 100644 --- a/time.go +++ b/time.go @@ -138,6 +138,9 @@ func (f TimeFormat) Encode(t time.Time) any { // // https://sqlite.org/lang_datefunc.html func (f TimeFormat) Decode(v any) (time.Time, error) { + if t, ok := v.(time.Time); ok { + return t, nil + } switch f { // Numeric formats. case TimeFormatJulianDay: