|
| 1 | +// Copyright 2020-2024 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 | + |
| 20 | + "github.com/dolthub/go-mysql-server/sql" |
| 21 | + "github.com/dolthub/go-mysql-server/sql/types" |
| 22 | +) |
| 23 | + |
| 24 | +// Insert implements the SQL function INSERT() which inserts a substring at a specified position |
| 25 | +type Insert struct { |
| 26 | + str sql.Expression |
| 27 | + pos sql.Expression |
| 28 | + length sql.Expression |
| 29 | + newStr sql.Expression |
| 30 | +} |
| 31 | + |
| 32 | +var _ sql.FunctionExpression = (*Insert)(nil) |
| 33 | +var _ sql.CollationCoercible = (*Insert)(nil) |
| 34 | + |
| 35 | +// NewInsert creates a new Insert expression |
| 36 | +func NewInsert(str, pos, length, newStr sql.Expression) sql.Expression { |
| 37 | + return &Insert{str, pos, length, newStr} |
| 38 | +} |
| 39 | + |
| 40 | +// FunctionName implements sql.FunctionExpression |
| 41 | +func (i *Insert) FunctionName() string { |
| 42 | + return "insert" |
| 43 | +} |
| 44 | + |
| 45 | +// Description implements sql.FunctionExpression |
| 46 | +func (i *Insert) Description() string { |
| 47 | + return "returns the string str, with the substring beginning at position pos and len characters long replaced by the string newstr." |
| 48 | +} |
| 49 | + |
| 50 | +// Children implements the Expression interface |
| 51 | +func (i *Insert) Children() []sql.Expression { |
| 52 | + return []sql.Expression{i.str, i.pos, i.length, i.newStr} |
| 53 | +} |
| 54 | + |
| 55 | +// Resolved implements the Expression interface |
| 56 | +func (i *Insert) Resolved() bool { |
| 57 | + return i.str.Resolved() && i.pos.Resolved() && i.length.Resolved() && i.newStr.Resolved() |
| 58 | +} |
| 59 | + |
| 60 | +// IsNullable implements the Expression interface |
| 61 | +func (i *Insert) IsNullable() bool { |
| 62 | + return i.str.IsNullable() || i.pos.IsNullable() || i.length.IsNullable() || i.newStr.IsNullable() |
| 63 | +} |
| 64 | + |
| 65 | +// Type implements the Expression interface |
| 66 | +func (i *Insert) Type() sql.Type { |
| 67 | + return types.LongText |
| 68 | +} |
| 69 | + |
| 70 | +// CollationCoercibility implements the interface sql.CollationCoercible |
| 71 | +func (i *Insert) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { |
| 72 | + collation, coercibility = sql.GetCoercibility(ctx, i.str) |
| 73 | + otherCollation, otherCoercibility := sql.GetCoercibility(ctx, i.newStr) |
| 74 | + return sql.ResolveCoercibility(collation, coercibility, otherCollation, otherCoercibility) |
| 75 | +} |
| 76 | + |
| 77 | +// String implements the Expression interface |
| 78 | +func (i *Insert) String() string { |
| 79 | + return fmt.Sprintf("insert(%s, %s, %s, %s)", i.str, i.pos, i.length, i.newStr) |
| 80 | +} |
| 81 | + |
| 82 | +// WithChildren implements the Expression interface |
| 83 | +func (i *Insert) WithChildren(children ...sql.Expression) (sql.Expression, error) { |
| 84 | + if len(children) != 4 { |
| 85 | + return nil, sql.ErrInvalidChildrenNumber.New(i, len(children), 4) |
| 86 | + } |
| 87 | + return NewInsert(children[0], children[1], children[2], children[3]), nil |
| 88 | +} |
| 89 | + |
| 90 | +// Eval implements the Expression interface |
| 91 | +func (i *Insert) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { |
| 92 | + str, err := i.str.Eval(ctx, row) |
| 93 | + if err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + if str == nil { |
| 97 | + return nil, nil |
| 98 | + } |
| 99 | + |
| 100 | + pos, err := i.pos.Eval(ctx, row) |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + if pos == nil { |
| 105 | + return nil, nil |
| 106 | + } |
| 107 | + |
| 108 | + length, err := i.length.Eval(ctx, row) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + if length == nil { |
| 113 | + return nil, nil |
| 114 | + } |
| 115 | + |
| 116 | + newStr, err := i.newStr.Eval(ctx, row) |
| 117 | + if err != nil { |
| 118 | + return nil, err |
| 119 | + } |
| 120 | + if newStr == nil { |
| 121 | + return nil, nil |
| 122 | + } |
| 123 | + |
| 124 | + // Convert all arguments to their expected types |
| 125 | + strVal, _, err := types.LongText.Convert(ctx, str) |
| 126 | + if err != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + |
| 130 | + posVal, _, err := types.Int64.Convert(ctx, pos) |
| 131 | + if err != nil { |
| 132 | + return nil, err |
| 133 | + } |
| 134 | + |
| 135 | + lengthVal, _, err := types.Int64.Convert(ctx, length) |
| 136 | + if err != nil { |
| 137 | + return nil, err |
| 138 | + } |
| 139 | + |
| 140 | + newStrVal, _, err := types.LongText.Convert(ctx, newStr) |
| 141 | + if err != nil { |
| 142 | + return nil, err |
| 143 | + } |
| 144 | + |
| 145 | + s := strVal.(string) |
| 146 | + p := posVal.(int64) |
| 147 | + l := lengthVal.(int64) |
| 148 | + n := newStrVal.(string) |
| 149 | + |
| 150 | + // MySQL uses 1-based indexing for position |
| 151 | + // Handle negative position - return original string |
| 152 | + if p < 1 { |
| 153 | + return s, nil |
| 154 | + } |
| 155 | + |
| 156 | + // Convert to 0-based indexing |
| 157 | + startIdx := p - 1 |
| 158 | + |
| 159 | + // Handle case where position is beyond string length |
| 160 | + if startIdx >= int64(len(s)) { |
| 161 | + return s, nil |
| 162 | + } |
| 163 | + |
| 164 | + // Calculate end index |
| 165 | + // For negative length, replace from position to end of string |
| 166 | + var endIdx int64 |
| 167 | + if l < 0 { |
| 168 | + endIdx = int64(len(s)) |
| 169 | + } else { |
| 170 | + endIdx = startIdx + l |
| 171 | + if endIdx > int64(len(s)) { |
| 172 | + endIdx = int64(len(s)) |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + // Build the result string |
| 177 | + result := s[:startIdx] + n + s[endIdx:] |
| 178 | + return result, nil |
| 179 | +} |
0 commit comments