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

Previously, expressions and params were evaluated during the planning phase. This change builds the query plan without evaluating params and expressions which are then evaluated only during the execution phase.
55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package statement
|
|
|
|
import (
|
|
"github.com/genjidb/genji/document"
|
|
"github.com/genjidb/genji/internal/expr"
|
|
"github.com/genjidb/genji/internal/stream"
|
|
)
|
|
|
|
// UpdateConfig holds UPDATE configuration.
|
|
type UpdateStmt struct {
|
|
TableName string
|
|
|
|
// SetPairs is used along with the Set clause. It holds
|
|
// each path with its corresponding value that
|
|
// should be set in the document.
|
|
SetPairs []UpdateSetPair
|
|
|
|
// UnsetFields is used along with the Unset clause. It holds
|
|
// each path that should be unset from the document.
|
|
UnsetFields []string
|
|
|
|
WhereExpr expr.Expr
|
|
}
|
|
|
|
type UpdateSetPair struct {
|
|
Path document.Path
|
|
E expr.Expr
|
|
}
|
|
|
|
// ToTree turns the statement into a stream.
|
|
func (stmt *UpdateStmt) ToStream() *StreamStmt {
|
|
s := stream.New(stream.SeqScan(stmt.TableName))
|
|
|
|
if stmt.WhereExpr != nil {
|
|
s = s.Pipe(stream.Filter(stmt.WhereExpr))
|
|
}
|
|
|
|
if stmt.SetPairs != nil {
|
|
for _, pair := range stmt.SetPairs {
|
|
s = s.Pipe(stream.Set(pair.Path, pair.E))
|
|
}
|
|
} else if stmt.UnsetFields != nil {
|
|
for _, name := range stmt.UnsetFields {
|
|
s = s.Pipe(stream.Unset(name))
|
|
}
|
|
}
|
|
|
|
s = s.Pipe(stream.TableReplace(stmt.TableName))
|
|
|
|
return &StreamStmt{
|
|
Stream: s,
|
|
ReadOnly: false,
|
|
}
|
|
}
|