github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/wrapper.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  // Wrapper simply acts as a wrapper for another expression. If a nil expression is wrapped, then the wrapper functions
    25  // as a guard against functions that expect non-nil expressions.
    26  type Wrapper struct {
    27  	inner sql.Expression
    28  }
    29  
    30  var _ sql.Expression = (*Wrapper)(nil)
    31  var _ sql.CollationCoercible = (*Wrapper)(nil)
    32  
    33  // WrapExpression takes in an expression and wraps it, returning the resulting Wrapper expression. Useful for when
    34  // an expression is nil.
    35  func WrapExpression(expr sql.Expression) *Wrapper {
    36  	return &Wrapper{expr}
    37  }
    38  
    39  // WrapExpressions takes in a number of expressions and wraps each one, returning the resulting slice. Useful for when
    40  // an expression in a slice may be nil.
    41  func WrapExpressions(exprs ...sql.Expression) []sql.Expression {
    42  	wrappers := make([]sql.Expression, len(exprs))
    43  	for i, expr := range exprs {
    44  		wrappers[i] = WrapExpression(expr)
    45  	}
    46  	return wrappers
    47  }
    48  
    49  // Children implements sql.Expression
    50  func (w *Wrapper) Children() []sql.Expression {
    51  	if w.inner == nil {
    52  		return nil
    53  	}
    54  	return []sql.Expression{w.inner}
    55  }
    56  
    57  // Eval implements sql.Expression
    58  func (w *Wrapper) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
    59  	if w.inner == nil {
    60  		return nil, nil
    61  	}
    62  	return w.inner.Eval(ctx, row)
    63  }
    64  
    65  // IsNullable implements sql.Expression
    66  func (w *Wrapper) IsNullable() bool {
    67  	if w.inner == nil {
    68  		return true
    69  	}
    70  	return w.inner.IsNullable()
    71  }
    72  
    73  // Resolved implements sql.Expression
    74  func (w *Wrapper) Resolved() bool {
    75  	if w.inner == nil {
    76  		return true
    77  	}
    78  	return w.inner.Resolved()
    79  }
    80  
    81  // String implements sql.Expression
    82  func (w *Wrapper) String() string {
    83  	if w.inner == nil {
    84  		return ""
    85  	}
    86  	return fmt.Sprintf("(%s)", w.inner.String())
    87  }
    88  
    89  // DebugString implements sql.DebugStringer
    90  func (w *Wrapper) DebugString() string {
    91  	if w.inner == nil {
    92  		return ""
    93  	}
    94  	return fmt.Sprintf("(%s)", sql.DebugString(w.inner))
    95  }
    96  
    97  // Type implements sql.Expression
    98  func (w *Wrapper) Type() sql.Type {
    99  	if w.inner == nil {
   100  		return types.Null
   101  	}
   102  	return w.inner.Type()
   103  }
   104  
   105  // Unwrap returns the wrapped expression, or nil if no expression was wrapped.
   106  func (w *Wrapper) Unwrap() sql.Expression {
   107  	return w.inner
   108  }
   109  
   110  // WithChildren implements sql.Expression
   111  func (w *Wrapper) WithChildren(children ...sql.Expression) (sql.Expression, error) {
   112  	if len(children) == 0 {
   113  		return WrapExpression(nil), nil
   114  	} else if len(children) != 1 {
   115  		return nil, sql.ErrInvalidChildrenNumber.New(w, len(children), 1)
   116  	}
   117  	return WrapExpression(children[0]), nil
   118  }
   119  
   120  // CollationCoercibility implements the interface sql.CollationCoercible.
   121  func (w *Wrapper) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
   122  	if w.inner == nil {
   123  		return sql.Collation_binary, 6
   124  	}
   125  	return sql.GetCoercibility(ctx, w.inner)
   126  }