github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/json/json_array_insert.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_ARRAY_INSERT(json_doc, path, val[, path, val] ...) 26 // 27 // JSONArrayInsert Updates a JSON document, inserting into an array within the document and returning the modified 28 // document. Returns NULL if any argument is NULL. An error occurs if the json_doc argument is not a valid JSON document 29 // or any path argument is not a valid path expression or contains a * or ** wildcard or does not end with an array 30 // element identifier. The path-value pairs are evaluated left to right. The document produced by evaluating one pair 31 // becomes the new value against which the next pair is evaluated. Pairs for which the path does not identify any array 32 // in the JSON document are ignored. If a path identifies an array element, the corresponding value is inserted at that 33 // element position, shifting any following values to the right. If a path identifies an array position past the end of 34 // an array, the value is inserted at the end of the array. 35 // 36 // https://dev.mysql.com/doc/refman/8.0/en/json-modification-functions.html#function_json-array-insert 37 type JSONArrayInsert struct { 38 doc sql.Expression 39 pathVals []sql.Expression 40 } 41 42 func (j JSONArrayInsert) Resolved() bool { 43 for _, child := range j.Children() { 44 if child != nil && !child.Resolved() { 45 return false 46 } 47 } 48 return true 49 } 50 51 func (j JSONArrayInsert) String() string { 52 children := j.Children() 53 var parts = make([]string, len(children)) 54 55 for i, c := range children { 56 parts[i] = c.String() 57 } 58 59 return fmt.Sprintf("%s(%s)", j.FunctionName(), strings.Join(parts, ",")) 60 } 61 62 func (j JSONArrayInsert) Type() sql.Type { 63 return types.JSON 64 } 65 66 func (j JSONArrayInsert) IsNullable() bool { 67 for _, arg := range j.pathVals { 68 if arg.IsNullable() { 69 return true 70 } 71 } 72 return j.doc.IsNullable() 73 } 74 75 func (j JSONArrayInsert) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 76 doc, err := getMutableJSONVal(ctx, row, j.doc) 77 if err != nil || doc == nil { 78 return nil, err 79 } 80 81 pairs := make([]pathValPair, 0, len(j.pathVals)/2) 82 for i := 0; i < len(j.pathVals); i += 2 { 83 argPair, err := buildPathValue(ctx, j.pathVals[i], j.pathVals[i+1], row) 84 if argPair == nil || err != nil { 85 return nil, err 86 } 87 pairs = append(pairs, *argPair) 88 } 89 90 // Apply the path-value pairs to the document. 91 for _, pair := range pairs { 92 doc, _, err = doc.ArrayInsert(pair.path, pair.val) 93 if err != nil { 94 return nil, err 95 } 96 } 97 98 return doc, nil 99 } 100 101 func (j JSONArrayInsert) Children() []sql.Expression { 102 return append([]sql.Expression{j.doc}, j.pathVals...) 103 } 104 105 func (j JSONArrayInsert) WithChildren(children ...sql.Expression) (sql.Expression, error) { 106 if len(j.Children()) != len(children) { 107 return nil, fmt.Errorf("json_array_insert did not receive the correct amount of args") 108 } 109 return NewJSONArrayInsert(children...) 110 } 111 112 var _ sql.FunctionExpression = JSONArrayInsert{} 113 114 // NewJSONArrayInsert creates a new JSONArrayInsert function. 115 func NewJSONArrayInsert(args ...sql.Expression) (sql.Expression, error) { 116 if len(args) <= 1 { 117 return nil, sql.ErrInvalidArgumentNumber.New("JSON_ARRAY_INSERT", "more than 1", len(args)) 118 } else if (len(args)-1)%2 == 1 { 119 return nil, sql.ErrInvalidArgumentNumber.New("JSON_ARRAY_INSERT", "even number of path/val", len(args)-1) 120 } 121 122 return JSONArrayInsert{args[0], args[1:]}, nil 123 } 124 125 // FunctionName implements sql.FunctionExpression 126 func (j JSONArrayInsert) FunctionName() string { 127 return "json_array_insert" 128 } 129 130 // Description implements sql.FunctionExpression 131 func (j JSONArrayInsert) Description() string { 132 return "inserts into JSON array." 133 } 134 135 // IsUnsupported implements sql.UnsupportedFunctionStub 136 func (j JSONArrayInsert) IsUnsupported() bool { 137 return false 138 }