github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/json/json_set.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 "strings" 20 21 "github.com/dolthub/go-mysql-server/sql" 22 "github.com/dolthub/go-mysql-server/sql/types" 23 ) 24 25 // JSON_SET(json_doc, path, val[, path, val] ...) 26 // 27 // JSONSet Inserts or updates data in a JSON document and returns the result. Returns NULL if any argument is NULL or 28 // path, if given, does not locate an object. An error occurs if the json_doc argument is not a valid JSON document or 29 // any path argument is not a valid path expression or contains a * or ** wildcard. The path-value pairs are evaluated 30 // left to right. The document produced by evaluating one pair becomes the new value against which the next pair is 31 // evaluated. A path-value pair for an existing path in the document overwrites the existing document value with the 32 // new value. A path-value pair for a non-existing path in the document adds the value to the document if the path 33 // identifies one of these types of values: 34 // - A member not present in an existing object. The member is added to the object and associated with the new value. 35 // - A position past the end of an existing array. The array is extended with the new value. If the existing value is 36 // not an array, it is auto-wrapped as an array, then extended with the new value. 37 // 38 // Otherwise, a path-value pair for a non-existing path in the document is ignored and has no effect. 39 // 40 // https://dev.mysql.com/doc/refman/8.0/en/json-modification-functions.html#function_json-set 41 42 type JSONSet struct { 43 JSONDoc sql.Expression 44 PathAndVals []sql.Expression 45 } 46 47 var _ sql.FunctionExpression = (*JSONContains)(nil) 48 49 // NewJSONSet creates a new JSONSet function. 50 func NewJSONSet(args ...sql.Expression) (sql.Expression, error) { 51 if len(args) <= 1 { 52 return nil, sql.ErrInvalidArgumentNumber.New("JSON_SET", "more than 1", len(args)) 53 } else if (len(args)-1)%2 == 1 { 54 return nil, sql.ErrInvalidArgumentNumber.New("JSON_SET", "even number of path/val", len(args)-1) 55 } 56 57 return &JSONSet{args[0], args[1:]}, nil 58 } 59 60 // FunctionName implements sql.FunctionExpression 61 func (j *JSONSet) FunctionName() string { 62 return "json_set" 63 } 64 65 // Description implements sql.FunctionExpression 66 func (j *JSONSet) Description() string { 67 return "inserts data into JSON document." 68 } 69 70 func (j *JSONSet) Resolved() bool { 71 for _, child := range j.Children() { 72 if child != nil && !child.Resolved() { 73 return false 74 } 75 } 76 77 return true 78 } 79 80 func (j *JSONSet) Children() []sql.Expression { 81 return append([]sql.Expression{j.JSONDoc}, j.PathAndVals...) 82 } 83 84 func (j *JSONSet) WithChildren(children ...sql.Expression) (sql.Expression, error) { 85 if len(j.Children()) != len(children) { 86 return nil, fmt.Errorf("json_set did not receive the correct amount of args") 87 } 88 89 return NewJSONSet(children...) 90 } 91 92 func (j *JSONSet) String() string { 93 children := j.Children() 94 var parts = make([]string, len(children)) 95 96 for i, c := range children { 97 parts[i] = c.String() 98 } 99 100 return fmt.Sprintf("%s(%s)", j.FunctionName(), strings.Join(parts, ",")) 101 } 102 103 func (j *JSONSet) Type() sql.Type { 104 return types.JSON 105 } 106 107 func (j *JSONSet) IsNullable() bool { 108 for _, pv := range j.PathAndVals { 109 if pv.IsNullable() { 110 return true 111 } 112 } 113 return j.JSONDoc.IsNullable() 114 } 115 116 func (j *JSONSet) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 117 doc, err := getMutableJSONVal(ctx, row, j.JSONDoc) 118 if err != nil || doc == nil { 119 return nil, err 120 } 121 122 pairs := make([]pathValPair, 0, len(j.PathAndVals)/2) 123 for i := 0; i < len(j.PathAndVals); i += 2 { 124 argPair, err := buildPathValue(ctx, j.PathAndVals[i], j.PathAndVals[i+1], row) 125 if argPair == nil || err != nil { 126 return nil, err 127 } 128 pairs = append(pairs, *argPair) 129 } 130 131 // Apply the path-value pairs to the document. 132 for _, pair := range pairs { 133 doc, _, err = doc.Set(pair.path, pair.val) 134 if err != nil { 135 return nil, err 136 } 137 } 138 139 return doc, nil 140 } 141 142 // IsUnsupported implements sql.UnsupportedFunctionStub 143 func (j JSONSet) IsUnsupported() bool { 144 return false 145 }