forked from src-d/go-mysql-server
/
boolean.go
61 lines (49 loc) · 1.15 KB
/
boolean.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package expression
import (
"fmt"
"reflect"
"github.com/src-d/go-mysql-server/sql"
)
// Not is a node that negates an expression.
type Not struct {
UnaryExpression
}
// NewNot returns a new Not node.
func NewNot(child sql.Expression) *Not {
return &Not{UnaryExpression{child}}
}
// Type implements the Expression interface.
func (e *Not) Type() sql.Type {
return sql.Boolean
}
// Eval implements the Expression interface.
func (e *Not) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
v, err := e.Child.Eval(ctx, row)
if err != nil {
return nil, err
}
if v == nil {
return nil, nil
}
b, ok := v.(bool)
if !ok {
v, _ = e.Type().Convert(v)
if v == nil {
return nil, nil
}
if b, ok = v.(bool); !ok {
return nil, sql.ErrInvalidType.New(reflect.TypeOf(v).String())
}
}
return !b, nil
}
func (e *Not) String() string {
return fmt.Sprintf("NOT(%s)", e.Child)
}
// WithChildren implements the Expression interface.
func (e *Not) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(e, len(children), 1)
}
return NewNot(children[0]), nil
}