github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/function.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 function 16 17 import ( 18 "fmt" 19 "strings" 20 21 "github.com/dolthub/go-mysql-server/sql" 22 "github.com/dolthub/go-mysql-server/sql/expression" 23 ) 24 25 type UnaryFunc struct { 26 expression.UnaryExpression 27 // Name is the name of the function 28 Name string 29 // The type returned by the function 30 RetType sql.Type 31 } 32 33 func NewUnaryFunc(arg sql.Expression, name string, returnType sql.Type) *UnaryFunc { 34 return &UnaryFunc{ 35 UnaryExpression: expression.UnaryExpression{Child: arg}, 36 Name: name, 37 RetType: returnType, 38 } 39 } 40 41 // FunctionName implements sql.FunctionExpression 42 func (uf *UnaryFunc) FunctionName() string { 43 return strings.ToLower(uf.Name) 44 } 45 46 // EvalChild is a convenience function for safely evaluating a child expression 47 func (uf *UnaryFunc) EvalChild(ctx *sql.Context, row sql.Row) (interface{}, error) { 48 if uf.Child == nil { 49 return nil, nil 50 } 51 52 return uf.Child.Eval(ctx, row) 53 } 54 55 // String implements the fmt.Stringer interface. 56 func (uf *UnaryFunc) String() string { 57 return fmt.Sprintf("%s(%s)", uf.FunctionName(), uf.Child.String()) 58 } 59 60 // Type implements the Expression interface. 61 func (uf *UnaryFunc) Type() sql.Type { 62 return uf.RetType 63 }