github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/concat_ws.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/types" 23 ) 24 25 // ConcatWithSeparator joins several strings together. The first argument is 26 // the separator for the rest of the arguments. The separator is added between 27 // the strings to be concatenated. The separator can be a string, as can the 28 // rest of the arguments. If the separator is NULL, the result is NULL. 29 type ConcatWithSeparator struct { 30 args []sql.Expression 31 } 32 33 var _ sql.FunctionExpression = (*ConcatWithSeparator)(nil) 34 var _ sql.CollationCoercible = (*ConcatWithSeparator)(nil) 35 36 // NewConcatWithSeparator creates a new NewConcatWithSeparator UDF. 37 func NewConcatWithSeparator(args ...sql.Expression) (sql.Expression, error) { 38 if len(args) == 0 { 39 return nil, sql.ErrInvalidArgumentNumber.New("CONCAT_WS", "1 or more", 0) 40 } 41 42 return &ConcatWithSeparator{args}, nil 43 } 44 45 // FunctionName implements sql.FunctionExpression 46 func (f *ConcatWithSeparator) FunctionName() string { 47 return "concat_ws" 48 } 49 50 // Description implements sql.FunctionExpression 51 func (f *ConcatWithSeparator) Description() string { 52 return "concatenates any group of fields into a single string. The first argument is the separator for the rest of the arguments. The separator is added between the strings to be concatenated. The separator can be a string, as can the rest of the arguments. If the separator is NULL, the result is NULL." 53 } 54 55 // Type implements the Expression interface. 56 func (f *ConcatWithSeparator) Type() sql.Type { return types.LongText } 57 58 // CollationCoercibility implements the interface sql.CollationCoercible. 59 func (c *ConcatWithSeparator) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { 60 if len(c.args) == 0 { 61 return sql.Collation_binary, 6 62 } 63 collation, coercibility = sql.GetCoercibility(ctx, c.args[0]) 64 for i := 1; i < len(c.args); i++ { 65 nextCollation, nextCoercibility := sql.GetCoercibility(ctx, c.args[i]) 66 collation, coercibility = sql.ResolveCoercibility(collation, coercibility, nextCollation, nextCoercibility) 67 } 68 return collation, coercibility 69 } 70 71 // IsNullable implements the Expression interface. 72 func (f *ConcatWithSeparator) IsNullable() bool { 73 for _, arg := range f.args { 74 if arg.IsNullable() { 75 return true 76 } 77 } 78 return false 79 } 80 81 func (f *ConcatWithSeparator) String() string { 82 var args = make([]string, len(f.args)) 83 for i, arg := range f.args { 84 args[i] = arg.String() 85 } 86 return fmt.Sprintf("%s(%s)", f.FunctionName(), strings.Join(args, ",")) 87 } 88 89 // WithChildren implements the Expression interface. 90 func (*ConcatWithSeparator) WithChildren(children ...sql.Expression) (sql.Expression, error) { 91 return NewConcatWithSeparator(children...) 92 } 93 94 // Resolved implements the Expression interface. 95 func (f *ConcatWithSeparator) Resolved() bool { 96 for _, arg := range f.args { 97 if !arg.Resolved() { 98 return false 99 } 100 } 101 return true 102 } 103 104 // Children implements the Expression interface. 105 func (f *ConcatWithSeparator) Children() []sql.Expression { return f.args } 106 107 // Eval implements the Expression interface. 108 func (f *ConcatWithSeparator) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 109 var parts []string 110 111 for i, arg := range f.args { 112 val, err := arg.Eval(ctx, row) 113 if err != nil { 114 return nil, err 115 } 116 117 if val == nil && i == 0 { 118 return nil, nil 119 } 120 121 if val == nil { 122 continue 123 } 124 125 val, _, err = types.LongText.Convert(val) 126 if err != nil { 127 return nil, err 128 } 129 130 parts = append(parts, val.(string)) 131 } 132 133 return strings.Join(parts[1:], parts[0]), nil 134 }