github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/boolean.go (about) 1 // Copyright 2020-2021 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package expression 16 17 import ( 18 "fmt" 19 20 "github.com/dolthub/go-mysql-server/sql" 21 "github.com/dolthub/go-mysql-server/sql/types" 22 ) 23 24 // Not is a node that negates an expression. 25 type Not struct { 26 UnaryExpression 27 } 28 29 var _ sql.Expression = (*Not)(nil) 30 var _ sql.CollationCoercible = (*Not)(nil) 31 32 // NewNot returns a new Not node. 33 func NewNot(child sql.Expression) *Not { 34 return &Not{UnaryExpression{child}} 35 } 36 37 // Type implements the Expression interface. 38 func (e *Not) Type() sql.Type { 39 return types.Boolean 40 } 41 42 // CollationCoercibility implements the interface sql.CollationCoercible. 43 func (*Not) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { 44 return sql.Collation_binary, 5 45 } 46 47 // Eval implements the Expression interface. 48 func (e *Not) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 49 v, err := e.Child.Eval(ctx, row) 50 if err != nil { 51 return nil, err 52 } 53 if v == nil { 54 return nil, nil 55 } 56 57 b, ok := v.(bool) 58 if !ok { 59 b, err = sql.ConvertToBool(ctx, v) 60 if err != nil { 61 return nil, err 62 } 63 } 64 65 return !b, nil 66 } 67 68 func (e *Not) String() string { 69 return fmt.Sprintf("(NOT(%s))", e.Child) 70 } 71 72 func (e *Not) DebugString() string { 73 pr := sql.NewTreePrinter() 74 _ = pr.WriteNode("NOT") 75 children := []string{sql.DebugString(e.Child)} 76 _ = pr.WriteChildren(children...) 77 return pr.String() 78 } 79 80 // WithChildren implements the Expression interface. 81 func (e *Not) WithChildren(children ...sql.Expression) (sql.Expression, error) { 82 if len(children) != 1 { 83 return nil, sql.ErrInvalidChildrenNumber.New(e, len(children), 1) 84 } 85 return NewNot(children[0]), nil 86 }