From ebbb969cd7d7b06cede56f1f92ce95d4ddc89be3 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Fri, 15 Dec 2023 00:42:12 +0000 Subject: [PATCH] Tweaks. --- conn.go | 5 ++--- driver/driver.go | 2 +- driver/time.go | 14 +++++++------- driver/time_test.go | 4 ++-- sqlite.go | 8 +++----- 5 files changed, 15 insertions(+), 18 deletions(-) diff --git a/conn.go b/conn.go index 8c3d382..b5eb52d 100644 --- a/conn.go +++ b/conn.go @@ -240,8 +240,8 @@ func (c *Conn) SetInterrupt(ctx context.Context) (old context.Context) { return ctx } - // An uncompleted SQL statement prevents SQLite from ignoring - // an interrupt that comes before any other statements are started. + // A busy SQL statement prevents SQLite from ignoring an interrupt + // that comes before any other statements are started. if c.pending == nil { c.pending, _, _ = c.Prepare(`SELECT 1 UNION ALL SELECT 2`) } else { @@ -250,7 +250,6 @@ func (c *Conn) SetInterrupt(ctx context.Context) (old context.Context) { old = c.interrupt c.interrupt = ctx - // Remove the handler if the context can't be canceled. if ctx == nil || ctx.Done() == nil { return old } diff --git a/driver/driver.go b/driver/driver.go index efd7503..c1d96bb 100644 --- a/driver/driver.go +++ b/driver/driver.go @@ -547,7 +547,7 @@ func (r *rows) Next(dest []driver.Value) error { case sqlite3.BLOB: dest[i] = r.Stmt.ColumnRawBlob(i) case sqlite3.TEXT: - dest[i] = stringOrTime(r.Stmt.ColumnRawText(i)) + dest[i] = stringOrTime(r.Stmt.ColumnText(i)) case sqlite3.NULL: dest[i] = nil default: diff --git a/driver/time.go b/driver/time.go index fa3247d..0fc8238 100644 --- a/driver/time.go +++ b/driver/time.go @@ -9,23 +9,23 @@ import ( // if it roundtrips back to the same string. // This way times can be persisted to, and recovered from, the database, // but if a string is needed, [database/sql] will recover the same string. -func stringOrTime(text []byte) driver.Value { +func stringOrTime(text string) driver.Value { // Weed out (some) values that can't possibly be // [time.RFC3339Nano] timestamps. if len(text) < len("2006-01-02T15:04:05Z") { - return string(text) + return text } if len(text) > len(time.RFC3339Nano) { - return string(text) + return text } if text[4] != '-' || text[10] != 'T' || text[16] != ':' { - return string(text) + return text } // Slow path. - date, err := time.Parse(time.RFC3339Nano, string(text)) - if err == nil && date.Format(time.RFC3339Nano) == string(text) { + date, err := time.Parse(time.RFC3339Nano, text) + if err == nil && date.Format(time.RFC3339Nano) == text { return date } - return string(text) + return text } diff --git a/driver/time_test.go b/driver/time_test.go index 0134629..58e31aa 100644 --- a/driver/time_test.go +++ b/driver/time_test.go @@ -22,7 +22,7 @@ func Fuzz_stringOrTime_1(f *testing.F) { f.Add("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.") f.Fuzz(func(t *testing.T, str string) { - value := stringOrTime([]byte(str)) + value := stringOrTime(str) switch v := value.(type) { case time.Time: @@ -59,7 +59,7 @@ func Fuzz_stringOrTime_2(f *testing.F) { f.Add(int64(-763421161058), int64(222_222_222)) // twosday, year 22222BC checkTime := func(t testing.TB, date time.Time) { - value := stringOrTime([]byte(date.Format(time.RFC3339Nano))) + value := stringOrTime(date.Format(time.RFC3339Nano)) switch v := value.(type) { case time.Time: diff --git a/sqlite.go b/sqlite.go index 7c7101d..15bbf6e 100644 --- a/sqlite.go +++ b/sqlite.go @@ -146,13 +146,11 @@ func (sqlt *sqlite) error(rc uint64, handle uint32, sql ...string) error { func (sqlt *sqlite) getfn(name string) api.Function { c := &sqlt.funcs p := unsafe.StringData(name) - for i, id := range c.id { - if id == p { - f := c.fn[i] + for i := range c.id { + if c.id[i] == p { c.id[i] = nil - c.fn[i] = nil c.mask &^= uint32(1) << i - return f + return c.fn[i] } } return sqlt.mod.ExportedFunction(name)