Files
chaisql/internal/expr/like.go
Asdine El Hrychy f966172cee Introduce Value interface (#422)
This replaces the Value struct by an interface to allow us to override some
values behavior in the future.
It also introduces a new package types, which contains type definitions, comparison,
and arithmetics.
Concerning encoding, Genji now only uses on type of encoding for values. This simplifies
indexing logic as well as table access in general.
2021-07-21 22:05:44 +04:00

54 lines
1.4 KiB
Go

package expr
import (
"github.com/genjidb/genji/internal/environment"
"github.com/genjidb/genji/internal/expr/glob"
"github.com/genjidb/genji/internal/sql/scanner"
"github.com/genjidb/genji/internal/stringutil"
"github.com/genjidb/genji/types"
)
func like(pattern, text string) bool {
return glob.MatchLike(pattern, text)
}
type LikeOperator struct {
*simpleOperator
}
// Like creates an expression that evaluates to the result of a LIKE b.
func Like(a, b Expr) Expr {
return &LikeOperator{&simpleOperator{a, b, scanner.LIKE}}
}
func (op *LikeOperator) Eval(env *environment.Environment) (types.Value, error) {
return op.simpleOperator.eval(env, func(a, b types.Value) (types.Value, error) {
if a.Type() != types.TextValue || b.Type() != types.TextValue {
return NullLiteral, nil
}
if like(b.V().(string), a.V().(string)) {
return TrueLiteral, nil
}
return FalseLiteral, nil
})
}
type NotLikeOperator struct {
LikeOperator
}
// NotLike creates an expression that evaluates to the result of a NOT LIKE b.
func NotLike(a, b Expr) Expr {
return &NotLikeOperator{LikeOperator{&simpleOperator{a, b, scanner.LIKE}}}
}
func (op *NotLikeOperator) Eval(env *environment.Environment) (types.Value, error) {
return invertBoolResult(op.LikeOperator.Eval)(env)
}
func (op *NotLikeOperator) String() string {
return stringutil.Sprintf("%v NOT LIKE %v", op.a, op.b)
}