| 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.OperationN; |
| 11 |
import org.h2.message.DbException; |
| 12 |
import org.h2.value.Value; |
| 13 |
import org.h2.value.ValueNull; |
| 14 |
|
| 15 |
/** |
| 16 |
* Function with many arguments. |
| 17 |
*/ |
| 18 |
public abstract class FunctionN extends OperationN implements NamedExpression { |
| 19 |
|
| 20 |
protected FunctionN(Expression[] args) { |
| 21 |
super(args); |
| 22 |
} |
| 23 |
|
| 24 |
@Override |
| 25 |
public Value getValue(SessionLocal session) { |
| 26 |
Value v1, v2, v3; |
| 27 |
int count = args.length; |
| 28 |
if (count >= 1) { |
| 29 |
v1 = args[0].getValue(session); |
| 30 |
if (v1 == ValueNull.INSTANCE) { |
| 31 |
return ValueNull.INSTANCE; |
| 32 |
} |
| 33 |
if (count >= 2) { |
| 34 |
v2 = args[1].getValue(session); |
| 35 |
if (v2 == ValueNull.INSTANCE) { |
| 36 |
return ValueNull.INSTANCE; |
| 37 |
} |
| 38 |
if (count >= 3) { |
| 39 |
v3 = args[2].getValue(session); |
| 40 |
if (v3 == ValueNull.INSTANCE) { |
| 41 |
return ValueNull.INSTANCE; |
| 42 |
} |
| 43 |
} else { |
| 44 |
v3 = null; |
| 45 |
} |
| 46 |
} else { |
| 47 |
v3 = v2 = null; |
| 48 |
} |
| 49 |
} else { |
| 50 |
v3 = v2 = v1 = null; |
| 51 |
} |
| 52 |
return getValue(session, v1, v2, v3); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Returns the value of this function. |
| 57 |
* |
| 58 |
* @param session |
| 59 |
* the session |
| 60 |
* @param v1 |
| 61 |
* the value of first argument, or {@code null} |
| 62 |
* @param v2 |
| 63 |
* the value of second argument, or {@code null} |
| 64 |
* @param v3 |
| 65 |
* the value of third argument, or {@code null} |
| 66 |
* @return the resulting value |
| 67 |
*/ |
| 68 |
protected Value getValue(SessionLocal session, Value v1, Value v2, Value v3) { |
| 69 |
throw DbException.getInternalError(); |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) { |
| 74 |
return writeExpressions(builder.append(getName()).append('('), args, sqlFlags).append(')'); |
| 75 |
} |
| 76 |
|
| 77 |
} |