| 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.table; |
| 7 |
|
| 8 |
import org.h2.api.ErrorCode; |
| 9 |
import org.h2.engine.SessionLocal; |
| 10 |
import org.h2.expression.Expression; |
| 11 |
import org.h2.message.DbException; |
| 12 |
import org.h2.result.ResultInterface; |
| 13 |
import org.h2.schema.FunctionAlias; |
| 14 |
|
| 15 |
/** |
| 16 |
* This class wraps a user-defined function. |
| 17 |
*/ |
| 18 |
public final class JavaTableFunction extends TableFunction { |
| 19 |
|
| 20 |
private final FunctionAlias functionAlias; |
| 21 |
private final FunctionAlias.JavaMethod javaMethod; |
| 22 |
|
| 23 |
public JavaTableFunction(FunctionAlias functionAlias, Expression[] args) { |
| 24 |
super(args); |
| 25 |
this.functionAlias = functionAlias; |
| 26 |
this.javaMethod = functionAlias.findJavaMethod(args); |
| 27 |
if (javaMethod.getDataType() != null) { |
| 28 |
throw DbException.get(ErrorCode.FUNCTION_MUST_RETURN_RESULT_SET_1, getName()); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
@Override |
| 33 |
public ResultInterface getValue(SessionLocal session) { |
| 34 |
return javaMethod.getTableValue(session, args, false); |
| 35 |
} |
| 36 |
|
| 37 |
@Override |
| 38 |
public ResultInterface getValueTemplate(SessionLocal session) { |
| 39 |
return javaMethod.getTableValue(session, args, true); |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
public void optimize(SessionLocal session) { |
| 44 |
super.optimize(session); |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public StringBuilder getSQL(StringBuilder builder, int sqlFlags) { |
| 49 |
return Expression.writeExpressions(functionAlias.getSQL(builder, sqlFlags).append('('), args, sqlFlags) |
| 50 |
.append(')'); |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public String getName() { |
| 55 |
return functionAlias.getName(); |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
public boolean isDeterministic() { |
| 60 |
return functionAlias.isDeterministic(); |
| 61 |
} |
| 62 |
|
| 63 |
} |