github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/json/json_unquote.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 json 16 17 import ( 18 "fmt" 19 "reflect" 20 21 "github.com/dolthub/go-mysql-server/internal/strings" 22 "github.com/dolthub/go-mysql-server/sql" 23 "github.com/dolthub/go-mysql-server/sql/expression" 24 "github.com/dolthub/go-mysql-server/sql/types" 25 ) 26 27 // JSONUnquote unquotes JSON value and returns the result as a utf8mb4 string. 28 // Returns NULL if the argument is NULL. 29 // An error occurs if the value starts and ends with double quotes but is not a valid JSON string literal. 30 type JSONUnquote struct { 31 expression.UnaryExpression 32 } 33 34 var _ sql.FunctionExpression = (*JSONUnquote)(nil) 35 var _ sql.CollationCoercible = (*JSONUnquote)(nil) 36 37 // NewJSONUnquote creates a new JSONUnquote UDF. 38 func NewJSONUnquote(json sql.Expression) sql.Expression { 39 return &JSONUnquote{expression.UnaryExpression{Child: json}} 40 } 41 42 // FunctionName implements sql.FunctionExpression 43 func (js *JSONUnquote) FunctionName() string { 44 return "json_unquote" 45 } 46 47 // Description implements sql.FunctionExpression 48 func (js *JSONUnquote) Description() string { 49 return "unquotes JSON value and returns the result as a utf8mb4 string." 50 } 51 52 // IsUnsupported implements sql.UnsupportedFunctionStub 53 func (js *JSONUnquote) IsUnsupported() bool { 54 return false 55 } 56 57 func (js *JSONUnquote) String() string { 58 return fmt.Sprintf("%s(%s)", js.FunctionName(), js.Child) 59 } 60 61 // Type implements the Expression interface. 62 func (*JSONUnquote) Type() sql.Type { 63 return types.LongText 64 } 65 66 // CollationCoercibility implements the interface sql.CollationCoercible. 67 func (*JSONUnquote) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { 68 return ctx.GetCharacterSet().BinaryCollation(), 4 69 } 70 71 // WithChildren implements the Expression interface. 72 func (js *JSONUnquote) WithChildren(children ...sql.Expression) (sql.Expression, error) { 73 if len(children) != 1 { 74 return nil, sql.ErrInvalidChildrenNumber.New(js, len(children), 1) 75 } 76 return NewJSONUnquote(children[0]), nil 77 } 78 79 // Eval implements the Expression interface. 80 func (js *JSONUnquote) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 81 json, err := js.Child.Eval(ctx, row) 82 if json == nil || err != nil { 83 return json, err 84 } 85 86 ex, _, err := types.LongText.Convert(json) 87 if err != nil { 88 return nil, err 89 } 90 if ex == nil { 91 return nil, nil 92 } 93 str, ok := ex.(string) 94 if !ok { 95 return nil, sql.ErrInvalidType.New(reflect.TypeOf(ex).String()) 96 } 97 98 return strings.Unquote(str) 99 }