| 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 java.nio.charset.StandardCharsets; |
| 9 |
import java.util.Arrays; |
| 10 |
|
| 11 |
import org.h2.api.ErrorCode; |
| 12 |
import org.h2.engine.Mode; |
| 13 |
import org.h2.engine.Mode.ModeEnum; |
| 14 |
import org.h2.engine.SessionLocal; |
| 15 |
import org.h2.expression.Expression; |
| 16 |
import org.h2.expression.TypedValueExpression; |
| 17 |
import org.h2.message.DbException; |
| 18 |
import org.h2.util.StringUtils; |
| 19 |
import org.h2.value.DataType; |
| 20 |
import org.h2.value.TypeInfo; |
| 21 |
import org.h2.value.Value; |
| 22 |
import org.h2.value.ValueInteger; |
| 23 |
import org.h2.value.ValueNull; |
| 24 |
import org.h2.value.ValueVarbinary; |
| 25 |
import org.h2.value.ValueVarchar; |
| 26 |
|
| 27 |
/** |
| 28 |
* A string function with one argument. |
| 29 |
*/ |
| 30 |
public final class StringFunction1 extends Function1 { |
| 31 |
|
| 32 |
// Fold functions |
| 33 |
|
| 34 |
/** |
| 35 |
* UPPER(). |
| 36 |
*/ |
| 37 |
public static final int UPPER = 0; |
| 38 |
|
| 39 |
/** |
| 40 |
* LOWER(). |
| 41 |
*/ |
| 42 |
public static final int LOWER = UPPER + 1; |
| 43 |
|
| 44 |
// Various non-standard functions |
| 45 |
|
| 46 |
/** |
| 47 |
* ASCII() (non-standard). |
| 48 |
*/ |
| 49 |
public static final int ASCII = LOWER + 1; |
| 50 |
|
| 51 |
/** |
| 52 |
* CHAR() (non-standard). |
| 53 |
*/ |
| 54 |
public static final int CHAR = ASCII + 1; |
| 55 |
|
| 56 |
/** |
| 57 |
* STRINGENCODE() (non-standard). |
| 58 |
*/ |
| 59 |
public static final int STRINGENCODE = CHAR + 1; |
| 60 |
|
| 61 |
/** |
| 62 |
* STRINGDECODE() (non-standard). |
| 63 |
*/ |
| 64 |
public static final int STRINGDECODE = STRINGENCODE + 1; |
| 65 |
|
| 66 |
/** |
| 67 |
* STRINGTOUTF8() (non-standard). |
| 68 |
*/ |
| 69 |
public static final int STRINGTOUTF8 = STRINGDECODE + 1; |
| 70 |
|
| 71 |
/** |
| 72 |
* UTF8TOSTRING() (non-standard). |
| 73 |
*/ |
| 74 |
public static final int UTF8TOSTRING = STRINGTOUTF8 + 1; |
| 75 |
|
| 76 |
/** |
| 77 |
* HEXTORAW() (non-standard). |
| 78 |
*/ |
| 79 |
public static final int HEXTORAW = UTF8TOSTRING + 1; |
| 80 |
|
| 81 |
/** |
| 82 |
* RAWTOHEX() (non-standard). |
| 83 |
*/ |
| 84 |
public static final int RAWTOHEX = HEXTORAW + 1; |
| 85 |
|
| 86 |
/** |
| 87 |
* SPACE() (non-standard). |
| 88 |
*/ |
| 89 |
public static final int SPACE = RAWTOHEX + 1; |
| 90 |
|
| 91 |
/** |
| 92 |
* QUOTE_IDENT() (non-standard). |
| 93 |
*/ |
| 94 |
public static final int QUOTE_IDENT = SPACE + 1; |
| 95 |
|
| 96 |
private static final String[] NAMES = { // |
| 97 |
"UPPER", "LOWER", "ASCII", "CHAR", "STRINGENCODE", "STRINGDECODE", "STRINGTOUTF8", "UTF8TOSTRING", |
| 98 |
"HEXTORAW", "RAWTOHEX", "SPACE", "QUOTE_IDENT" // |
| 99 |
}; |
| 100 |
|
| 101 |
private final int function; |
| 102 |
|
| 103 |
public StringFunction1(Expression arg, int function) { |
| 104 |
super(arg); |
| 105 |
this.function = function; |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public Value getValue(SessionLocal session) { |
| 110 |
Value v = arg.getValue(session); |
| 111 |
if (v == ValueNull.INSTANCE) { |
| 112 |
return ValueNull.INSTANCE; |
| 113 |
} |
| 114 |
switch (function) { |
| 115 |
case UPPER: |
| 116 |
// TODO this is locale specific, need to document or provide a way |
| 117 |
// to set the locale |
| 118 |
v = ValueVarchar.get(v.getString().toUpperCase(), session); |
| 119 |
break; |
| 120 |
case LOWER: |
| 121 |
// TODO this is locale specific, need to document or provide a way |
| 122 |
// to set the locale |
| 123 |
v = ValueVarchar.get(v.getString().toLowerCase(), session); |
| 124 |
break; |
| 125 |
case ASCII: { |
| 126 |
String s = v.getString(); |
| 127 |
v = s.isEmpty() ? ValueNull.INSTANCE : ValueInteger.get(s.charAt(0)); |
| 128 |
break; |
| 129 |
} |
| 130 |
case CHAR: |
| 131 |
v = ValueVarchar.get(String.valueOf((char) v.getInt()), session); |
| 132 |
break; |
| 133 |
case STRINGENCODE: |
| 134 |
v = ValueVarchar.get(StringUtils.javaEncode(v.getString()), session); |
| 135 |
break; |
| 136 |
case STRINGDECODE: |
| 137 |
v = ValueVarchar.get(StringUtils.javaDecode(v.getString()), session); |
| 138 |
break; |
| 139 |
case STRINGTOUTF8: |
| 140 |
v = ValueVarbinary.getNoCopy(v.getString().getBytes(StandardCharsets.UTF_8)); |
| 141 |
break; |
| 142 |
case UTF8TOSTRING: |
| 143 |
v = ValueVarchar.get(new String(v.getBytesNoCopy(), StandardCharsets.UTF_8), session); |
| 144 |
break; |
| 145 |
case HEXTORAW: |
| 146 |
v = hexToRaw(v.getString(), session); |
| 147 |
break; |
| 148 |
case RAWTOHEX: |
| 149 |
v = ValueVarchar.get(rawToHex(v, session.getMode()), session); |
| 150 |
break; |
| 151 |
case SPACE: { |
| 152 |
byte[] chars = new byte[Math.max(0, v.getInt())]; |
| 153 |
Arrays.fill(chars, (byte) ' '); |
| 154 |
v = ValueVarchar.get(new String(chars, StandardCharsets.ISO_8859_1), session); |
| 155 |
break; |
| 156 |
} |
| 157 |
case QUOTE_IDENT: |
| 158 |
v = ValueVarchar.get(StringUtils.quoteIdentifier(v.getString()), session); |
| 159 |
break; |
| 160 |
default: |
| 161 |
throw DbException.getInternalError("function=" + function); |
| 162 |
} |
| 163 |
return v; |
| 164 |
} |
| 165 |
|
| 166 |
private static Value hexToRaw(String s, SessionLocal session) { |
| 167 |
if (session.getMode().getEnum() == ModeEnum.Oracle) { |
| 168 |
return ValueVarbinary.get(StringUtils.convertHexToBytes(s)); |
| 169 |
} |
| 170 |
int len = s.length(); |
| 171 |
if (len % 4 != 0) { |
| 172 |
throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, s); |
| 173 |
} |
| 174 |
StringBuilder builder = new StringBuilder(len / 4); |
| 175 |
for (int i = 0; i < len; i += 4) { |
| 176 |
try { |
| 177 |
builder.append((char) Integer.parseInt(s.substring(i, i + 4), 16)); |
| 178 |
} catch (NumberFormatException e) { |
| 179 |
throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1, s); |
| 180 |
} |
| 181 |
} |
| 182 |
return ValueVarchar.get(builder.toString(), session); |
| 183 |
} |
| 184 |
|
| 185 |
private static String rawToHex(Value v, Mode mode) { |
| 186 |
if (DataType.isBinaryStringOrSpecialBinaryType(v.getValueType())) { |
| 187 |
return StringUtils.convertBytesToHex(v.getBytesNoCopy()); |
| 188 |
} |
| 189 |
String s = v.getString(); |
| 190 |
if (mode.getEnum() == ModeEnum.Oracle) { |
| 191 |
return StringUtils.convertBytesToHex(s.getBytes(StandardCharsets.UTF_8)); |
| 192 |
} |
| 193 |
int length = s.length(); |
| 194 |
StringBuilder buff = new StringBuilder(4 * length); |
| 195 |
for (int i = 0; i < length; i++) { |
| 196 |
String hex = Integer.toHexString(s.charAt(i) & 0xffff); |
| 197 |
for (int j = hex.length(); j < 4; j++) { |
| 198 |
buff.append('0'); |
| 199 |
} |
| 200 |
buff.append(hex); |
| 201 |
} |
| 202 |
return buff.toString(); |
| 203 |
} |
| 204 |
|
| 205 |
@Override |
| 206 |
public Expression optimize(SessionLocal session) { |
| 207 |
arg = arg.optimize(session); |
| 208 |
switch (function) { |
| 209 |
/* |
| 210 |
* UPPER and LOWER may return string of different length for some |
| 211 |
* characters. |
| 212 |
*/ |
| 213 |
case UPPER: |
| 214 |
case LOWER: |
| 215 |
case STRINGENCODE: |
| 216 |
case SPACE: |
| 217 |
case QUOTE_IDENT: |
| 218 |
type = TypeInfo.TYPE_VARCHAR; |
| 219 |
break; |
| 220 |
case ASCII: |
| 221 |
type = TypeInfo.TYPE_INTEGER; |
| 222 |
break; |
| 223 |
case CHAR: |
| 224 |
type = TypeInfo.getTypeInfo(Value.VARCHAR, 1L, 0, null); |
| 225 |
break; |
| 226 |
case STRINGDECODE: { |
| 227 |
TypeInfo t = arg.getType(); |
| 228 |
type = DataType.isCharacterStringType(t.getValueType()) |
| 229 |
? TypeInfo.getTypeInfo(Value.VARCHAR, t.getPrecision(), 0, null) |
| 230 |
: TypeInfo.TYPE_VARCHAR; |
| 231 |
break; |
| 232 |
} |
| 233 |
case STRINGTOUTF8: |
| 234 |
type = TypeInfo.TYPE_VARBINARY; |
| 235 |
break; |
| 236 |
case UTF8TOSTRING: { |
| 237 |
TypeInfo t = arg.getType(); |
| 238 |
type = DataType.isBinaryStringType(t.getValueType()) |
| 239 |
? TypeInfo.getTypeInfo(Value.VARCHAR, t.getPrecision(), 0, null) |
| 240 |
: TypeInfo.TYPE_VARCHAR; |
| 241 |
break; |
| 242 |
} |
| 243 |
case HEXTORAW: { |
| 244 |
TypeInfo t = arg.getType(); |
| 245 |
if (session.getMode().getEnum() == ModeEnum.Oracle) { |
| 246 |
if (DataType.isCharacterStringType(t.getValueType())) { |
| 247 |
type = TypeInfo.getTypeInfo(Value.VARBINARY, t.getPrecision() / 2, 0, null); |
| 248 |
} else { |
| 249 |
type = TypeInfo.TYPE_VARBINARY; |
| 250 |
} |
| 251 |
} else { |
| 252 |
if (DataType.isCharacterStringType(t.getValueType())) { |
| 253 |
type = TypeInfo.getTypeInfo(Value.VARCHAR, t.getPrecision() / 4, 0, null); |
| 254 |
} else { |
| 255 |
type = TypeInfo.TYPE_VARCHAR; |
| 256 |
} |
| 257 |
} |
| 258 |
break; |
| 259 |
} |
| 260 |
case RAWTOHEX: { |
| 261 |
TypeInfo t = arg.getType(); |
| 262 |
long precision = t.getPrecision(); |
| 263 |
int mul = DataType.isBinaryStringOrSpecialBinaryType(t.getValueType()) ? 2 |
| 264 |
: session.getMode().getEnum() == ModeEnum.Oracle ? 6 : 4; |
| 265 |
type = TypeInfo.getTypeInfo(Value.VARCHAR, |
| 266 |
precision <= Long.MAX_VALUE / mul ? precision * mul : Long.MAX_VALUE, 0, null); |
| 267 |
break; |
| 268 |
} |
| 269 |
default: |
| 270 |
throw DbException.getInternalError("function=" + function); |
| 271 |
} |
| 272 |
if (arg.isConstant()) { |
| 273 |
return TypedValueExpression.getTypedIfNull(getValue(session), type); |
| 274 |
} |
| 275 |
return this; |
| 276 |
} |
| 277 |
|
| 278 |
@Override |
| 279 |
public String getName() { |
| 280 |
return NAMES[function]; |
| 281 |
} |
| 282 |
|
| 283 |
} |