Commit 0750ff30 authored by gwenn's avatar gwenn

Introduce Stmt.ExplainQueryPlan.

parent ed327e64
...@@ -27,6 +27,7 @@ import "C" ...@@ -27,6 +27,7 @@ import "C"
import ( import (
"fmt" "fmt"
"io"
"time" "time"
"unsafe" "unsafe"
) )
...@@ -431,3 +432,28 @@ func ConfigUri(b bool) error { ...@@ -431,3 +432,28 @@ func ConfigUri(b bool) error {
} }
return Errno(rv) return Errno(rv)
} }
func (s *Stmt) ExplainQueryPlan(w io.Writer) error {
sql := s.SQL()
if len(sql) == 0 {
return s.specificError("empty statement")
}
explain := Mprintf("EXPLAIN QUERY PLAN %s", s.SQL())
sExplain, err := s.Conn().prepare(explain)
if err != nil {
return err
}
defer sExplain.finalize()
var selectid, order, from int
var detail string
err = sExplain.Select(func(s *Stmt) error {
if err := s.Scan(&selectid, &order, &from, &detail); err != nil {
return err
}
fmt.Fprintf(w, "%d\t%d\t%d\t%s\n", selectid, order, from, detail)
return nil
})
return err
}
...@@ -6,6 +6,7 @@ package sqlite_test ...@@ -6,6 +6,7 @@ package sqlite_test
import ( import (
"fmt" "fmt"
"os"
"testing" "testing"
"time" "time"
...@@ -119,7 +120,7 @@ func TestTrace(t *testing.T) { ...@@ -119,7 +120,7 @@ func TestTrace(t *testing.T) {
defer checkClose(db, t) defer checkClose(db, t)
db.Trace(trace, t) db.Trace(trace, t)
err := db.SetAuthorizer(authorizer, t) err := db.SetAuthorizer(authorizer, t)
checkNoError(t, err, "couldn't set an authorizer") checkNoError(t, err, "couldn't set an authorizer: %s")
db.Profile(profile, t) db.Profile(profile, t)
db.ProgressHandler(progressHandler, 1, t) db.ProgressHandler(progressHandler, 1, t)
db.CommitHook(commitHook, t) db.CommitHook(commitHook, t)
...@@ -140,3 +141,16 @@ func TestMemory(t *testing.T) { ...@@ -140,3 +141,16 @@ func TestMemory(t *testing.T) {
limit := SoftHeapLimit() limit := SoftHeapLimit()
assert.T(t, limit >= 0, "soft heap limit positive") assert.T(t, limit >= 0, "soft heap limit positive")
} }
func TestExplainQueryPlan(t *testing.T) {
db := open(t)
defer checkClose(db, t)
createTable(db, t)
s, err := db.Prepare("SELECT * FROM test WHERE a_string like ?")
checkNoError(t, err, "error while preparing stmt: %s")
defer checkFinalize(s, t)
w, err := os.Open(os.DevNull)
checkNoError(t, err, "couldn't open /dev/null: %s")
err = s.ExplainQueryPlan(w)
checkNoError(t, err, "error while explaining query plan: %s")
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment