From 605d9d08513fd3936c3d4a8631abd1db14136074 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Sun, 6 Nov 2016 20:43:53 +0900 Subject: [PATCH] cancel --- sqlite3.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) 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() }