github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/json/json_common.go (about) 1 // Copyright 2023 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 20 "github.com/dolthub/go-mysql-server/sql" 21 "github.com/dolthub/go-mysql-server/sql/types" 22 ) 23 24 var ErrInvalidPath = fmt.Errorf("Invalid JSON path expression") 25 var ErrPathWildcard = fmt.Errorf("Path expressions may not contain the * and ** tokens") 26 27 // getMutableJSONVal returns a JSONValue from the given row and expression. The underling value is deeply copied so that 28 // you are free to use the mutation functions on the returned value. 29 // nil will be returned only if the inputs are nil. This will not return an error, so callers must check. 30 func getMutableJSONVal(ctx *sql.Context, row sql.Row, json sql.Expression) (types.MutableJSON, error) { 31 doc, err := getJSONDocumentFromRow(ctx, row, json) 32 if err != nil || doc == nil || doc.Val == nil { 33 return nil, err 34 } 35 36 mutable := types.DeepCopyJson(doc.Val) 37 return types.JSONDocument{Val: mutable}, nil 38 } 39 40 // getSearchableJSONVal returns a SearchableJSONValue from the given row and expression. The underling value is not copied 41 // so it is intended to be used for read-only operations. 42 // nil will be returned only if the inputs are nil. This will not return an error, so callers must check. 43 func getSearchableJSONVal(ctx *sql.Context, row sql.Row, json sql.Expression) (sql.JSONWrapper, error) { 44 doc, err := getJSONDocumentFromRow(ctx, row, json) 45 if err != nil || doc == nil || doc.Val == nil { 46 return nil, err 47 } 48 49 return doc, nil 50 } 51 52 // getJSONDocumentFromRow returns a JSONDocument from the given row and expression. Helper function only intended to be 53 // used by functions in this file. 54 func getJSONDocumentFromRow(ctx *sql.Context, row sql.Row, json sql.Expression) (*types.JSONDocument, error) { 55 js, err := json.Eval(ctx, row) 56 if err != nil || js == nil { 57 return nil, err 58 } 59 60 var converted interface{} 61 switch js.(type) { 62 case string, []interface{}, map[string]interface{}, sql.JSONWrapper: 63 converted, _, err = types.JSON.Convert(js) 64 if err != nil { 65 return nil, sql.ErrInvalidJSONText.New(js) 66 } 67 default: 68 return nil, sql.ErrInvalidArgument.New(fmt.Sprintf("%v", js)) 69 } 70 71 doc, ok := converted.(types.JSONDocument) 72 if !ok { 73 // This should never happen, but just in case. 74 doc = types.JSONDocument{Val: js.(sql.JSONWrapper).ToInterface()} 75 } 76 77 return &doc, nil 78 } 79 80 // pathValPair is a helper struct for use by functions which take json paths paired with a json value. eg. JSON_SET, JSON_INSERT, etc. 81 type pathValPair struct { 82 path string 83 val sql.JSONWrapper 84 } 85 86 // buildPath builds a path from the given row and expression 87 func buildPath(ctx *sql.Context, pathExp sql.Expression, row sql.Row) (interface{}, error) { 88 path, err := pathExp.Eval(ctx, row) 89 if err != nil { 90 return nil, err 91 } 92 if path == nil { 93 return nil, nil 94 } 95 if _, ok := path.(string); !ok { 96 return "", ErrInvalidPath 97 } 98 return path.(string), nil 99 } 100 101 // buildPathValue builds a pathValPair from the given row and expressions. This is a common pattern in json methods to have 102 // pairs of arguments, and this ensures they are of the right type, non-nil, and they wrapped in a struct as a unit. 103 func buildPathValue(ctx *sql.Context, pathExp sql.Expression, valExp sql.Expression, row sql.Row) (*pathValPair, error) { 104 path, err := buildPath(ctx, pathExp, row) 105 if err != nil { 106 return nil, err 107 } 108 if path == nil { 109 return nil, nil 110 } 111 112 val, err := valExp.Eval(ctx, row) 113 if err != nil { 114 return nil, err 115 } 116 jsonVal, ok := val.(sql.JSONWrapper) 117 if !ok { 118 jsonVal = types.JSONDocument{Val: val} 119 } 120 121 return &pathValPair{path.(string), jsonVal}, nil 122 }