| 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; |
| 7 |
|
| 8 |
import org.h2.engine.SessionLocal; |
| 9 |
import org.h2.value.TypeInfo; |
| 10 |
import org.h2.value.Value; |
| 11 |
import org.h2.value.ValueNull; |
| 12 |
|
| 13 |
/** |
| 14 |
* A searched case. |
| 15 |
*/ |
| 16 |
public final class SearchedCase extends OperationN { |
| 17 |
|
| 18 |
public SearchedCase() { |
| 19 |
super(new Expression[4]); |
| 20 |
} |
| 21 |
|
| 22 |
public SearchedCase(Expression[] args) { |
| 23 |
super(args); |
| 24 |
} |
| 25 |
|
| 26 |
@Override |
| 27 |
public Value getValue(SessionLocal session) { |
| 28 |
int len = args.length - 1; |
| 29 |
for (int i = 0; i < len; i += 2) { |
| 30 |
if (args[i].getBooleanValue(session)) { |
| 31 |
return args[i + 1].getValue(session).convertTo(type, session); |
| 32 |
} |
| 33 |
} |
| 34 |
if ((len & 1) == 0) { |
| 35 |
return args[len].getValue(session).convertTo(type, session); |
| 36 |
} |
| 37 |
return ValueNull.INSTANCE; |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
public Expression optimize(SessionLocal session) { |
| 42 |
TypeInfo typeInfo = TypeInfo.TYPE_UNKNOWN; |
| 43 |
int len = args.length - 1; |
| 44 |
boolean allConst = true; |
| 45 |
for (int i = 0; i < len; i += 2) { |
| 46 |
Expression condition = args[i].optimize(session); |
| 47 |
Expression result = args[i + 1].optimize(session); |
| 48 |
if (allConst) { |
| 49 |
if (condition.isConstant()) { |
| 50 |
if (condition.getBooleanValue(session)) { |
| 51 |
return result; |
| 52 |
} |
| 53 |
} else { |
| 54 |
allConst = false; |
| 55 |
} |
| 56 |
} |
| 57 |
args[i] = condition; |
| 58 |
args[i + 1] = result; |
| 59 |
typeInfo = SimpleCase.combineTypes(typeInfo, result); |
| 60 |
} |
| 61 |
if ((len & 1) == 0) { |
| 62 |
Expression result = args[len].optimize(session); |
| 63 |
if (allConst) { |
| 64 |
return result; |
| 65 |
} |
| 66 |
args[len] = result; |
| 67 |
typeInfo = SimpleCase.combineTypes(typeInfo, result); |
| 68 |
} else if (allConst) { |
| 69 |
return ValueExpression.NULL; |
| 70 |
} |
| 71 |
if (typeInfo.getValueType() == Value.UNKNOWN) { |
| 72 |
typeInfo = TypeInfo.TYPE_VARCHAR; |
| 73 |
} |
| 74 |
type = typeInfo; |
| 75 |
return this; |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 80 |
builder.append("CASE"); |
| 81 |
int len = args.length - 1; |
| 82 |
for (int i = 0; i < len; i += 2) { |
| 83 |
builder.append(" WHEN "); |
| 84 |
args[i].getUnenclosedSQL(builder, sqlFlags); |
| 85 |
builder.append(" THEN "); |
| 86 |
args[i + 1].getUnenclosedSQL(builder, sqlFlags); |
| 87 |
} |
| 88 |
if ((len & 1) == 0) { |
| 89 |
builder.append(" ELSE "); |
| 90 |
args[len].getUnenclosedSQL(builder, sqlFlags); |
| 91 |
} |
| 92 |
return builder.append(" END"); |
| 93 |
} |
| 94 |
|
| 95 |
} |