mirror of
https://github.com/chaisql/chai.git
synced 2025-10-15 12:10:47 +08:00

All new error handling code now rely on internal/errors package which provides a compilation time toggle that enables to capture stacktraces for easier debugging while developing. It also comes with a new testutil/assert package which replaces the require package when it comes to checking or comparing errors and printing the stack traces if needed. Finally, the test target of the Makefile uses the debug build tag by default. A testnodebug target is also provided for convenience and to make sure no tests are broken due to not having used the internal/errors or testutil/assert package. See #431 for more details
57 lines
1.4 KiB
Go
57 lines
1.4 KiB
Go
package statement
|
|
|
|
import (
|
|
"github.com/genjidb/genji/internal/errors"
|
|
"github.com/genjidb/genji/internal/expr"
|
|
"github.com/genjidb/genji/internal/stream"
|
|
"github.com/genjidb/genji/types"
|
|
)
|
|
|
|
// ExplainStmt is a Statement that
|
|
// displays information about how a statement
|
|
// is going to be executed, without executing it.
|
|
type ExplainStmt struct {
|
|
Statement Statement
|
|
}
|
|
|
|
// Run analyses the inner statement and displays its execution plan.
|
|
// If the statement is a stream, Optimize will be called prior to
|
|
// displaying all the operations.
|
|
// Explain currently only works on SELECT, UPDATE, INSERT and DELETE statements.
|
|
func (stmt *ExplainStmt) Run(ctx *Context) (Result, error) {
|
|
st, ok := stmt.Statement.(*StreamStmt)
|
|
if !ok {
|
|
return Result{}, errors.New("EXPLAIN only works on INSERT, SELECT, UPDATE AND DELETE statements")
|
|
}
|
|
|
|
err := st.Prepare(ctx)
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
|
|
var plan string
|
|
if st.PreparedStream != nil {
|
|
plan = st.PreparedStream.String()
|
|
} else {
|
|
plan = "<no exec>"
|
|
}
|
|
|
|
newStatement := StreamStmt{
|
|
PreparedStream: &stream.Stream{
|
|
Op: stream.Project(
|
|
&expr.NamedExpr{
|
|
ExprName: "plan",
|
|
Expr: expr.LiteralValue{Value: types.NewTextValue(plan)},
|
|
}),
|
|
},
|
|
ReadOnly: true,
|
|
}
|
|
return newStatement.Run(ctx)
|
|
}
|
|
|
|
// IsReadOnly indicates that this statement doesn't write anything into
|
|
// the database.
|
|
func (s *ExplainStmt) IsReadOnly() bool {
|
|
return true
|
|
}
|