diff --git a/conn.go b/conn.go index 4357202..9f9251e 100644 --- a/conn.go +++ b/conn.go @@ -3,6 +3,7 @@ package sqlite3 import ( "context" "fmt" + "iter" "math" "math/rand" "net/url" @@ -503,10 +504,16 @@ func (c *Conn) error(rc res_t, sql ...string) error { return c.sqlite.error(rc, c.handle, sql...) } -func (c *Conn) stmtsIter(yield func(*Stmt) bool) { - for _, s := range c.stmts { - if !yield(s) { - break +// Stmts returns an iterator for the prepared statements +// associated with the database connection. +// +// https://sqlite.org/c3ref/next_stmt.html +func (c *Conn) Stmts() iter.Seq[*Stmt] { + return func(yield func(*Stmt) bool) { + for _, s := range c.stmts { + if !yield(s) { + break + } } } } diff --git a/conn_iter.go b/conn_iter.go deleted file mode 100644 index 470f0ad..0000000 --- a/conn_iter.go +++ /dev/null @@ -1,11 +0,0 @@ -//go:build go1.23 - -package sqlite3 - -import "iter" - -// Stmts returns an iterator for the prepared statements -// associated with the database connection. -// -// https://sqlite.org/c3ref/next_stmt.html -func (c *Conn) Stmts() iter.Seq[*Stmt] { return c.stmtsIter } diff --git a/conn_old.go b/conn_old.go deleted file mode 100644 index 4663fa0..0000000 --- a/conn_old.go +++ /dev/null @@ -1,9 +0,0 @@ -//go:build !go1.23 - -package sqlite3 - -// Stmts returns an iterator for the prepared statements -// associated with the database connection. -// -// https://sqlite.org/c3ref/next_stmt.html -func (c *Conn) Stmts() func(func(*Stmt) bool) { return c.stmtsIter } diff --git a/embed/bcw2/go.mod b/embed/bcw2/go.mod index cdf45b1..71c81ff 100644 --- a/embed/bcw2/go.mod +++ b/embed/bcw2/go.mod @@ -1,6 +1,6 @@ module github.com/ncruces/go-sqlite3/embed/bcw2 -go 1.22.0 +go 1.23.0 toolchain go1.24.0 diff --git a/ext/fileio/coro.go b/ext/fileio/coro.go deleted file mode 100644 index c563237..0000000 --- a/ext/fileio/coro.go +++ /dev/null @@ -1,70 +0,0 @@ -//go:build !go1.23 - -package fileio - -import ( - "fmt" - - "github.com/ncruces/go-sqlite3/internal/util" -) - -// Adapted from: https://research.swtch.com/coro - -const errCoroCanceled = util.ErrorString("coroutine canceled") - -func coroNew[In, Out any](f func(In, func(Out) In) Out) (resume func(In) (Out, bool), cancel func()) { - type msg[T any] struct { - panic any - val T - } - - cin := make(chan msg[In]) - cout := make(chan msg[Out]) - running := true - resume = func(in In) (out Out, ok bool) { - if !running { - return - } - cin <- msg[In]{val: in} - m := <-cout - if m.panic != nil { - panic(m.panic) - } - return m.val, running - } - cancel = func() { - if !running { - return - } - e := fmt.Errorf("%w", errCoroCanceled) - cin <- msg[In]{panic: e} - m := <-cout - if m.panic != nil && m.panic != e { - panic(m.panic) - } - } - yield := func(out Out) In { - cout <- msg[Out]{val: out} - m := <-cin - if m.panic != nil { - panic(m.panic) - } - return m.val - } - go func() { - defer func() { - if running { - running = false - cout <- msg[Out]{panic: recover()} - } - }() - var out Out - m := <-cin - if m.panic == nil { - out = f(m.val, yield) - } - running = false - cout <- msg[Out]{val: out} - }() - return resume, cancel -} diff --git a/ext/fileio/fsdir.go b/ext/fileio/fsdir.go index a8881b8..dc22774 100644 --- a/ext/fileio/fsdir.go +++ b/ext/fileio/fsdir.go @@ -2,6 +2,7 @@ package fileio import ( "io/fs" + "iter" "os" "path" "path/filepath" @@ -63,7 +64,7 @@ func (d fsdir) Open() (sqlite3.VTabCursor, error) { type cursor struct { fsdir base string - resume resume + resume func() (entry, bool) cancel func() curr entry eof bool @@ -101,14 +102,26 @@ func (c *cursor) Filter(idxNum int, idxStr string, arg ...sqlite3.Value) error { c.base = base } - c.resume, c.cancel = pull(c, root) + c.resume, c.cancel = iter.Pull(func(yield func(entry) bool) { + walkDir := func(path string, d fs.DirEntry, err error) error { + if yield(entry{d, err, path}) { + return nil + } + return fs.SkipAll + } + if c.fsys != nil { + fs.WalkDir(c.fsys, root, walkDir) + } else { + filepath.WalkDir(root, walkDir) + } + }) c.eof = false c.rowID = 0 return c.Next() } func (c *cursor) Next() error { - curr, ok := next(c) + curr, ok := c.resume() c.curr = curr c.eof = !ok c.rowID++ diff --git a/ext/fileio/fsdir_coro.go b/ext/fileio/fsdir_coro.go deleted file mode 100644 index bbc0b86..0000000 --- a/ext/fileio/fsdir_coro.go +++ /dev/null @@ -1,29 +0,0 @@ -//go:build !go1.23 - -package fileio - -import ( - "io/fs" - "path/filepath" -) - -type resume = func(struct{}) (entry, bool) - -func next(c *cursor) (entry, bool) { - return c.resume(struct{}{}) -} - -func pull(c *cursor, root string) (resume, func()) { - return coroNew(func(_ struct{}, yield func(entry) struct{}) entry { - walkDir := func(path string, d fs.DirEntry, err error) error { - yield(entry{d, err, path}) - return nil - } - if c.fsys != nil { - fs.WalkDir(c.fsys, root, walkDir) - } else { - filepath.WalkDir(root, walkDir) - } - return entry{} - }) -} diff --git a/ext/fileio/fsdir_iter.go b/ext/fileio/fsdir_iter.go deleted file mode 100644 index ce1f479..0000000 --- a/ext/fileio/fsdir_iter.go +++ /dev/null @@ -1,31 +0,0 @@ -//go:build go1.23 - -package fileio - -import ( - "io/fs" - "iter" - "path/filepath" -) - -type resume = func() (entry, bool) - -func next(c *cursor) (entry, bool) { - return c.resume() -} - -func pull(c *cursor, root string) (resume, func()) { - return iter.Pull(func(yield func(entry) bool) { - walkDir := func(path string, d fs.DirEntry, err error) error { - if yield(entry{d, err, path}) { - return nil - } - return fs.SkipAll - } - if c.fsys != nil { - fs.WalkDir(c.fsys, root, walkDir) - } else { - filepath.WalkDir(root, walkDir) - } - }) -} diff --git a/go.mod b/go.mod index 4c542ca..e135a68 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/ncruces/go-sqlite3 -go 1.22.0 +go 1.23.0 toolchain go1.24.0 diff --git a/gormlite/go.mod b/gormlite/go.mod index e911e40..562fa41 100644 --- a/gormlite/go.mod +++ b/gormlite/go.mod @@ -1,6 +1,6 @@ module github.com/ncruces/go-sqlite3/gormlite -go 1.22.0 +go 1.23.0 toolchain go1.24.0