diff --git a/sqlite3.go b/sqlite3.go index b018918..c1c42f0 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -190,6 +190,7 @@ type SQLiteRows struct { cols []string decltype []string cls bool + done chan struct{} } type functionInfo struct { @@ -766,7 +767,26 @@ func (s *SQLiteStmt) query(ctx context.Context, args []namedValue) (driver.Rows, if err := s.bind(args); err != nil { return nil, err } - return &SQLiteRows{s, int(C.sqlite3_column_count(s.s)), nil, nil, s.cls}, nil + + rows := &SQLiteRows{ + s: s, + nc: int(C.sqlite3_column_count(s.s)), + cols: nil, + decltype: nil, + cls: s.cls, + done: make(chan struct{}), + } + + go func() { + select { + case <-ctx.Done(): + C.sqlite3_interrupt(s.c.db) + rows.Close() + case <-rows.done: + } + }() + + return rows, nil } // LastInsertId teturn last inserted ID. @@ -813,6 +833,10 @@ func (rc *SQLiteRows) Close() error { if rc.s.closed { return nil } + if rc.done != nil { + close(rc.done) + rc.done = nil + } if rc.cls { return rc.s.Close() }