bugfix: after executing statement table lock was not cleared
When we execute a statement, we step once and return. If the query returned more than one row, we would not have consumed everything so the underlying table would still be locked even when the execution had finished.
This change resets the statement after execution so that the locks can be cleared.
Apart from the included test, the following go program serves to reproduce the issue:
package main
import (
"context"
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
func main() {
ctx := context.Background()
sqldb, err := sql.Open("sqlite3", "file:test.db?cache=shared&mode=memory")
stmt, err := sqldb.PrepareContext(ctx, "CREATE TABLE person (id number, name text);")
stmt.Exec()
stmt, err = sqldb.PrepareContext(ctx, "INSERT INTO person VALUES (1, 'Fred'), (2, 'Susan');")
stmt.Exec()
stmt, err = sqldb.PrepareContext(ctx, "SELECT name FROM person;")
_, err = stmt.ExecContext(ctx)
stmt, err = sqldb.Prepare("DROP TABLE person;")
_, err = stmt.ExecContext(ctx)
if err != nil {
panic(err) // panic: database table is locked
}
}
@mattn Can you take a look to see if this PR makes sense? Should I create an issue along with it?
As far as I look this changes, looks good to me. @rittneje could you please take a look?
@rittneje can you take a look?