| 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.SessionLocal; |
| 9 |
import org.h2.expression.Expression; |
| 10 |
import org.h2.expression.TypedValueExpression; |
| 11 |
import org.h2.message.DbException; |
| 12 |
import org.h2.value.TypeInfo; |
| 13 |
import org.h2.value.Value; |
| 14 |
import org.h2.value.ValueVarchar; |
| 15 |
|
| 16 |
/** |
| 17 |
* A string function with two arguments. |
| 18 |
*/ |
| 19 |
public final class StringFunction2 extends Function2 { |
| 20 |
|
| 21 |
/** |
| 22 |
* LEFT() (non-standard). |
| 23 |
*/ |
| 24 |
public static final int LEFT = 0; |
| 25 |
|
| 26 |
/** |
| 27 |
* RIGHT() (non-standard). |
| 28 |
*/ |
| 29 |
public static final int RIGHT = LEFT + 1; |
| 30 |
|
| 31 |
/** |
| 32 |
* REPEAT() (non-standard). |
| 33 |
*/ |
| 34 |
public static final int REPEAT = RIGHT + 1; |
| 35 |
|
| 36 |
private static final String[] NAMES = { // |
| 37 |
"LEFT", "RIGHT", "REPEAT" // |
| 38 |
}; |
| 39 |
|
| 40 |
private final int function; |
| 41 |
|
| 42 |
public StringFunction2(Expression arg1, Expression arg2, int function) { |
| 43 |
super(arg1, arg2); |
| 44 |
this.function = function; |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public Value getValue(SessionLocal session, Value v1, Value v2) { |
| 49 |
String s = v1.getString(); |
| 50 |
int count = v2.getInt(); |
| 51 |
if (count <= 0) { |
| 52 |
return ValueVarchar.get("", session); |
| 53 |
} |
| 54 |
int length = s.length(); |
| 55 |
switch (function) { |
| 56 |
case LEFT: |
| 57 |
if (count > length) { |
| 58 |
count = length; |
| 59 |
} |
| 60 |
s = s.substring(0, count); |
| 61 |
break; |
| 62 |
case RIGHT: |
| 63 |
if (count > length) { |
| 64 |
count = length; |
| 65 |
} |
| 66 |
s = s.substring(length - count); |
| 67 |
break; |
| 68 |
case REPEAT: { |
| 69 |
StringBuilder builder = new StringBuilder(length * count); |
| 70 |
while (count-- > 0) { |
| 71 |
builder.append(s); |
| 72 |
} |
| 73 |
s = builder.toString(); |
| 74 |
break; |
| 75 |
} |
| 76 |
default: |
| 77 |
throw DbException.getInternalError("function=" + function); |
| 78 |
} |
| 79 |
return ValueVarchar.get(s, session); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public Expression optimize(SessionLocal session) { |
| 84 |
left = left.optimize(session); |
| 85 |
right = right.optimize(session); |
| 86 |
switch (function) { |
| 87 |
case LEFT: |
| 88 |
case RIGHT: |
| 89 |
type = TypeInfo.getTypeInfo(Value.VARCHAR, left.getType().getPrecision(), 0, null); |
| 90 |
break; |
| 91 |
case REPEAT: |
| 92 |
type = TypeInfo.TYPE_VARCHAR; |
| 93 |
break; |
| 94 |
default: |
| 95 |
throw DbException.getInternalError("function=" + function); |
| 96 |
} |
| 97 |
if (left.isConstant() && right.isConstant()) { |
| 98 |
return TypedValueExpression.getTypedIfNull(getValue(session), type); |
| 99 |
} |
| 100 |
return this; |
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
public String getName() { |
| 105 |
return NAMES[function]; |
| 106 |
} |
| 107 |
|
| 108 |
} |