| 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.tools.CompressTool; |
| 13 |
import org.h2.value.TypeInfo; |
| 14 |
import org.h2.value.Value; |
| 15 |
import org.h2.value.ValueVarbinary; |
| 16 |
|
| 17 |
/** |
| 18 |
* A COMPRESS or EXPAND function. |
| 19 |
*/ |
| 20 |
public final class CompressFunction extends Function1_2 { |
| 21 |
|
| 22 |
/** |
| 23 |
* COMPRESS() (non-standard). |
| 24 |
*/ |
| 25 |
public static final int COMPRESS = 0; |
| 26 |
|
| 27 |
/** |
| 28 |
* EXPAND() (non-standard). |
| 29 |
*/ |
| 30 |
public static final int EXPAND = COMPRESS + 1; |
| 31 |
|
| 32 |
private static final String[] NAMES = { // |
| 33 |
"COMPRESS", "EXPAND" // |
| 34 |
}; |
| 35 |
|
| 36 |
private final int function; |
| 37 |
|
| 38 |
public CompressFunction(Expression arg1, Expression arg2, int function) { |
| 39 |
super(arg1, arg2); |
| 40 |
this.function = function; |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
public Value getValue(SessionLocal session, Value v1, Value v2) { |
| 45 |
switch (function) { |
| 46 |
case COMPRESS: |
| 47 |
v1 = ValueVarbinary.getNoCopy( |
| 48 |
CompressTool.getInstance().compress(v1.getBytesNoCopy(), v2 != null ? v2.getString() : null)); |
| 49 |
break; |
| 50 |
case EXPAND: |
| 51 |
v1 = ValueVarbinary.getNoCopy(CompressTool.getInstance().expand(v1.getBytesNoCopy())); |
| 52 |
break; |
| 53 |
default: |
| 54 |
throw DbException.getInternalError("function=" + function); |
| 55 |
} |
| 56 |
return v1; |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public Expression optimize(SessionLocal session) { |
| 61 |
left = left.optimize(session); |
| 62 |
if (right != null) { |
| 63 |
right = right.optimize(session); |
| 64 |
} |
| 65 |
type = TypeInfo.TYPE_VARBINARY; |
| 66 |
if (left.isConstant() && (right == null || right.isConstant())) { |
| 67 |
return TypedValueExpression.getTypedIfNull(getValue(session), type); |
| 68 |
} |
| 69 |
return this; |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public String getName() { |
| 74 |
return NAMES[function]; |
| 75 |
} |
| 76 |
|
| 77 |
} |