Debian Codegraph @

src/main/org/h2/expression/function/JavaFunction.java

raw
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.api.ErrorCode;
9 import org.h2.engine.SessionLocal;
10 import org.h2.expression.Expression;
11 import org.h2.expression.ExpressionVisitor;
12 import org.h2.expression.ValueExpression;
13 import org.h2.message.DbException;
14 import org.h2.schema.FunctionAlias;
15 import org.h2.table.ColumnResolver;
16 import org.h2.table.TableFilter;
17 import org.h2.value.TypeInfo;
18 import org.h2.value.Value;
19
20 /**
21 * This class wraps a user-defined function.
22 */
23 public final class JavaFunction extends Expression implements NamedExpression {
24
25 private final FunctionAlias functionAlias;
26 private final FunctionAlias.JavaMethod javaMethod;
27 private final Expression[] args;
28
29 public JavaFunction(FunctionAlias functionAlias, Expression[] args) {
30 this.functionAlias = functionAlias;
31 this.javaMethod = functionAlias.findJavaMethod(args);
32 if (javaMethod.getDataType() == null) {
33 throw DbException.get(ErrorCode.FUNCTION_NOT_FOUND_1, getName());
34 }
35 this.args = args;
36 }
37
38 @Override
39 public Value getValue(SessionLocal session) {
40 return javaMethod.getValue(session, args, false);
41 }
42
43 @Override
44 public TypeInfo getType() {
45 return javaMethod.getDataType();
46 }
47
48 @Override
49 public void mapColumns(ColumnResolver resolver, int level, int state) {
50 for (Expression e : args) {
51 e.mapColumns(resolver, level, state);
52 }
53 }
54
55 @Override
56 public Expression optimize(SessionLocal session) {
57 boolean allConst = functionAlias.isDeterministic();
58 for (int i = 0, len = args.length; i < len; i++) {
59 Expression e = args[i].optimize(session);
60 args[i] = e;
61 allConst &= e.isConstant();
62 }
63 if (allConst) {
64 return ValueExpression.get(getValue(session));
65 }
66 return this;
67 }
68
69 @Override
70 public void setEvaluatable(TableFilter tableFilter, boolean b) {
71 for (Expression e : args) {
72 if (e != null) {
73 e.setEvaluatable(tableFilter, b);
74 }
75 }
76 }
77
78 @Override
79 public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) {
80 return writeExpressions(functionAlias.getSQL(builder, sqlFlags).append('('), args, sqlFlags).append(')');
81 }
82
83 @Override
84 public void updateAggregate(SessionLocal session, int stage) {
85 for (Expression e : args) {
86 if (e != null) {
87 e.updateAggregate(session, stage);
88 }
89 }
90 }
91
92 @Override
93 public String getName() {
94 return functionAlias.getName();
95 }
96
97 @Override
98 public boolean isEverything(ExpressionVisitor visitor) {
99 switch (visitor.getType()) {
100 case ExpressionVisitor.DETERMINISTIC:
101 case ExpressionVisitor.READONLY:
102 case ExpressionVisitor.QUERY_COMPARABLE:
103 if (!functionAlias.isDeterministic()) {
104 return false;
105 }
106 // only if all parameters are deterministic as well
107 break;
108 case ExpressionVisitor.GET_DEPENDENCIES:
109 visitor.addDependency(functionAlias);
110 break;
111 default:
112 }
113 for (Expression e : args) {
114 if (e != null && !e.isEverything(visitor)) {
115 return false;
116 }
117 }
118 return true;
119 }
120
121 @Override
122 public int getCost() {
123 int cost = javaMethod.hasConnectionParam() ? 25 : 5;
124 for (Expression e : args) {
125 cost += e.getCost();
126 }
127 return cost;
128 }
129
130 @Override
131 public int getSubexpressionCount() {
132 return args.length;
133 }
134
135 @Override
136 public Expression getSubexpression(int index) {
137 return args[index];
138 }
139
140 }