| 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.ExpressionVisitor; |
| 11 |
import org.h2.table.ColumnResolver; |
| 12 |
import org.h2.table.TableFilter; |
| 13 |
import org.h2.value.TypeInfo; |
| 14 |
|
| 15 |
/** |
| 16 |
* Function with one optional argument. |
| 17 |
*/ |
| 18 |
public abstract class Function0_1 extends Expression implements NamedExpression { |
| 19 |
|
| 20 |
/** |
| 21 |
* The argument of the operation. |
| 22 |
*/ |
| 23 |
protected Expression arg; |
| 24 |
|
| 25 |
/** |
| 26 |
* The type of the result. |
| 27 |
*/ |
| 28 |
protected TypeInfo type; |
| 29 |
|
| 30 |
protected Function0_1(Expression arg) { |
| 31 |
this.arg = arg; |
| 32 |
} |
| 33 |
|
| 34 |
@Override |
| 35 |
public TypeInfo getType() { |
| 36 |
return type; |
| 37 |
} |
| 38 |
|
| 39 |
@Override |
| 40 |
public void mapColumns(ColumnResolver resolver, int level, int state) { |
| 41 |
if (arg != null) { |
| 42 |
arg.mapColumns(resolver, level, state); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
@Override |
| 47 |
public void setEvaluatable(TableFilter tableFilter, boolean value) { |
| 48 |
if (arg != null) { |
| 49 |
arg.setEvaluatable(tableFilter, value); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public void updateAggregate(SessionLocal session, int stage) { |
| 55 |
if (arg != null) { |
| 56 |
arg.updateAggregate(session, stage); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public boolean isEverything(ExpressionVisitor visitor) { |
| 62 |
return arg == null || arg.isEverything(visitor); |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public int getCost() { |
| 67 |
int cost = 1; |
| 68 |
if (arg != null) { |
| 69 |
cost += arg.getCost(); |
| 70 |
} |
| 71 |
return cost; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public int getSubexpressionCount() { |
| 76 |
return arg != null ? 1 : 0; |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public Expression getSubexpression(int index) { |
| 81 |
if (index == 0 && arg != null) { |
| 82 |
return arg; |
| 83 |
} |
| 84 |
throw new IndexOutOfBoundsException(); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 89 |
builder.append(getName()).append('('); |
| 90 |
if (arg != null) { |
| 91 |
arg.getUnenclosedSQL(builder, sqlFlags); |
| 92 |
} |
| 93 |
return builder.append(')'); |
| 94 |
} |
| 95 |
|
| 96 |
} |