| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.expression.function; |
| 7 |
|
| 8 |
import org.h2.engine.Mode.ModeEnum; |
| 9 |
import org.h2.engine.SessionLocal; |
| 10 |
import org.h2.expression.Expression; |
| 11 |
import org.h2.expression.TypedValueExpression; |
| 12 |
import org.h2.message.DbException; |
| 13 |
import org.h2.util.StringUtils; |
| 14 |
import org.h2.value.TypeInfo; |
| 15 |
import org.h2.value.Value; |
| 16 |
import org.h2.value.ValueInteger; |
| 17 |
import org.h2.value.ValueNull; |
| 18 |
import org.h2.value.ValueVarchar; |
| 19 |
|
| 20 |
/** |
| 21 |
* An string function with multiple arguments. |
| 22 |
*/ |
| 23 |
public final class StringFunction extends FunctionN { |
| 24 |
|
| 25 |
/** |
| 26 |
* LOCATE() (non-standard). |
| 27 |
*/ |
| 28 |
public static final int LOCATE = 0; |
| 29 |
|
| 30 |
/** |
| 31 |
* INSERT() (non-standard). |
| 32 |
*/ |
| 33 |
public static final int INSERT = LOCATE + 1; |
| 34 |
|
| 35 |
/** |
| 36 |
* REPLACE() (non-standard). |
| 37 |
*/ |
| 38 |
public static final int REPLACE = INSERT + 1; |
| 39 |
|
| 40 |
/** |
| 41 |
* LPAD(). |
| 42 |
*/ |
| 43 |
public static final int LPAD = REPLACE + 1; |
| 44 |
|
| 45 |
/** |
| 46 |
* RPAD(). |
| 47 |
*/ |
| 48 |
public static final int RPAD = LPAD + 1; |
| 49 |
|
| 50 |
/** |
| 51 |
* TRANSLATE() (non-standard). |
| 52 |
*/ |
| 53 |
public static final int TRANSLATE = RPAD + 1; |
| 54 |
|
| 55 |
private static final String[] NAMES = { // |
| 56 |
"LOCATE", "INSERT", "REPLACE", "LPAD", "RPAD", "TRANSLATE" // |
| 57 |
}; |
| 58 |
|
| 59 |
private final int function; |
| 60 |
|
| 61 |
public StringFunction(Expression arg1, Expression arg2, Expression arg3, int function) { |
| 62 |
super(arg3 == null ? new Expression[] { arg1, arg2 } : new Expression[] { arg1, arg2, arg3 }); |
| 63 |
this.function = function; |
| 64 |
} |
| 65 |
|
| 66 |
public StringFunction(Expression arg1, Expression arg2, Expression arg3, Expression arg4, int function) { |
| 67 |
super(new Expression[] { arg1, arg2, arg3, arg4 }); |
| 68 |
this.function = function; |
| 69 |
} |
| 70 |
|
| 71 |
public StringFunction(Expression[] args, int function) { |
| 72 |
super(args); |
| 73 |
this.function = function; |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public Value getValue(SessionLocal session) { |
| 78 |
Value v1 = args[0].getValue(session), v2 = args[1].getValue(session); |
| 79 |
switch (function) { |
| 80 |
case LOCATE: { |
| 81 |
if (v1 == ValueNull.INSTANCE || v2 == ValueNull.INSTANCE) { |
| 82 |
return ValueNull.INSTANCE; |
| 83 |
} |
| 84 |
Value v3 = args.length >= 3 ? args[2].getValue(session) : null; |
| 85 |
if (v3 == ValueNull.INSTANCE) { |
| 86 |
return ValueNull.INSTANCE; |
| 87 |
} |
| 88 |
v1 = ValueInteger.get(locate(v1.getString(), v2.getString(), v3 == null ? 1 : v3.getInt())); |
| 89 |
break; |
| 90 |
} |
| 91 |
case INSERT: { |
| 92 |
Value v3 = args[2].getValue(session), v4 = args[3].getValue(session); |
| 93 |
if (v2 != ValueNull.INSTANCE && v3 != ValueNull.INSTANCE) { |
| 94 |
String s = insert(v1.getString(), v2.getInt(), v3.getInt(), v4.getString()); |
| 95 |
v1 = s != null ? ValueVarchar.get(s, session) : ValueNull.INSTANCE; |
| 96 |
} |
| 97 |
break; |
| 98 |
} |
| 99 |
case REPLACE: { |
| 100 |
if (v1 == ValueNull.INSTANCE || v2 == ValueNull.INSTANCE) { |
| 101 |
return ValueNull.INSTANCE; |
| 102 |
} |
| 103 |
String after; |
| 104 |
if (args.length >= 3) { |
| 105 |
Value v3 = args[2].getValue(session); |
| 106 |
if (v3 == ValueNull.INSTANCE && session.getMode().getEnum() != ModeEnum.Oracle) { |
| 107 |
return ValueNull.INSTANCE; |
| 108 |
} |
| 109 |
after = v3.getString(); |
| 110 |
if (after == null) { |
| 111 |
after = ""; |
| 112 |
} |
| 113 |
} else { |
| 114 |
after = ""; |
| 115 |
} |
| 116 |
v1 = ValueVarchar.get(StringUtils.replaceAll(v1.getString(), v2.getString(), after), session); |
| 117 |
break; |
| 118 |
} |
| 119 |
case LPAD: |
| 120 |
case RPAD: |
| 121 |
if (v1 == ValueNull.INSTANCE || v2 == ValueNull.INSTANCE) { |
| 122 |
return ValueNull.INSTANCE; |
| 123 |
} |
| 124 |
String padding; |
| 125 |
if (args.length >= 3) { |
| 126 |
Value v3 = args[2].getValue(session); |
| 127 |
if (v3 == ValueNull.INSTANCE) { |
| 128 |
return ValueNull.INSTANCE; |
| 129 |
} |
| 130 |
padding = v3.getString(); |
| 131 |
} else { |
| 132 |
padding = null; |
| 133 |
} |
| 134 |
v1 = ValueVarchar.get(StringUtils.pad(v1.getString(), v2.getInt(), padding, function == RPAD), session); |
| 135 |
break; |
| 136 |
case TRANSLATE: { |
| 137 |
if (v1 == ValueNull.INSTANCE || v2 == ValueNull.INSTANCE) { |
| 138 |
return ValueNull.INSTANCE; |
| 139 |
} |
| 140 |
Value v3 = args[2].getValue(session); |
| 141 |
if (v3 == ValueNull.INSTANCE) { |
| 142 |
return ValueNull.INSTANCE; |
| 143 |
} |
| 144 |
String matching = v2.getString(); |
| 145 |
String replacement = v3.getString(); |
| 146 |
if (session.getMode().getEnum() == ModeEnum.DB2) { |
| 147 |
String t = matching; |
| 148 |
matching = replacement; |
| 149 |
replacement = t; |
| 150 |
} |
| 151 |
v1 = ValueVarchar.get(translate(v1.getString(), matching, replacement), session); |
| 152 |
break; |
| 153 |
} |
| 154 |
default: |
| 155 |
throw DbException.getInternalError("function=" + function); |
| 156 |
} |
| 157 |
return v1; |
| 158 |
} |
| 159 |
|
| 160 |
private static int locate(String search, String s, int start) { |
| 161 |
if (start < 0) { |
| 162 |
return s.lastIndexOf(search, s.length() + start) + 1; |
| 163 |
} |
| 164 |
return s.indexOf(search, start == 0 ? 0 : start - 1) + 1; |
| 165 |
} |
| 166 |
|
| 167 |
private static String insert(String s1, int start, int length, String s2) { |
| 168 |
if (s1 == null) { |
| 169 |
return s2; |
| 170 |
} |
| 171 |
if (s2 == null) { |
| 172 |
return s1; |
| 173 |
} |
| 174 |
int len1 = s1.length(); |
| 175 |
int len2 = s2.length(); |
| 176 |
start--; |
| 177 |
if (start < 0 || length <= 0 || len2 == 0 || start > len1) { |
| 178 |
return s1; |
| 179 |
} |
| 180 |
if (start + length > len1) { |
| 181 |
length = len1 - start; |
| 182 |
} |
| 183 |
return s1.substring(0, start) + s2 + s1.substring(start + length); |
| 184 |
} |
| 185 |
|
| 186 |
private static String translate(String original, String findChars, String replaceChars) { |
| 187 |
if (StringUtils.isNullOrEmpty(original) || StringUtils.isNullOrEmpty(findChars)) { |
| 188 |
return original; |
| 189 |
} |
| 190 |
// if it stays null, then no replacements have been made |
| 191 |
StringBuilder builder = null; |
| 192 |
// if shorter than findChars, then characters are removed |
| 193 |
// (if null, we don't access replaceChars at all) |
| 194 |
int replaceSize = replaceChars == null ? 0 : replaceChars.length(); |
| 195 |
for (int i = 0, size = original.length(); i < size; i++) { |
| 196 |
char ch = original.charAt(i); |
| 197 |
int index = findChars.indexOf(ch); |
| 198 |
if (index >= 0) { |
| 199 |
if (builder == null) { |
| 200 |
builder = new StringBuilder(size); |
| 201 |
if (i > 0) { |
| 202 |
builder.append(original, 0, i); |
| 203 |
} |
| 204 |
} |
| 205 |
if (index < replaceSize) { |
| 206 |
ch = replaceChars.charAt(index); |
| 207 |
} |
| 208 |
} |
| 209 |
if (builder != null) { |
| 210 |
builder.append(ch); |
| 211 |
} |
| 212 |
} |
| 213 |
return builder == null ? original : builder.toString(); |
| 214 |
} |
| 215 |
|
| 216 |
@Override |
| 217 |
public Expression optimize(SessionLocal session) { |
| 218 |
boolean allConst = optimizeArguments(session, true); |
| 219 |
switch (function) { |
| 220 |
case LOCATE: |
| 221 |
type = TypeInfo.TYPE_INTEGER; |
| 222 |
break; |
| 223 |
case INSERT: |
| 224 |
case REPLACE: |
| 225 |
case LPAD: |
| 226 |
case RPAD: |
| 227 |
case TRANSLATE: |
| 228 |
type = TypeInfo.TYPE_VARCHAR; |
| 229 |
break; |
| 230 |
default: |
| 231 |
throw DbException.getInternalError("function=" + function); |
| 232 |
} |
| 233 |
if (allConst) { |
| 234 |
return TypedValueExpression.getTypedIfNull(getValue(session), type); |
| 235 |
} |
| 236 |
return this; |
| 237 |
} |
| 238 |
|
| 239 |
@Override |
| 240 |
public String getName() { |
| 241 |
return NAMES[function]; |
| 242 |
} |
| 243 |
|
| 244 |
} |