File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\expression\JavaFunction.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.expression;
8
9 import org.h2.command.Parser;
10 import org.h2.engine.Constants;
11 import org.h2.engine.FunctionAlias;
12 import org.h2.engine.Session;
13 import org.h2.table.ColumnResolver;
14 import org.h2.table.TableFilter;
15 import org.h2.util.StatementBuilder;
16 import org.h2.value.DataType;
17 import org.h2.value.Value;
18 import org.h2.value.ValueArray;
19 import org.h2.value.ValueNull;
20 import org.h2.value.ValueResultSet;
21
22 /**
23 * This class wraps a user-defined function.
24 */
25 public class JavaFunction extends Expression implements FunctionCall {
26
27 private final FunctionAlias functionAlias;
28 private final FunctionAlias.JavaMethod javaMethod;
29 private final Expression[] args;
30
31 public JavaFunction(FunctionAlias functionAlias, Expression[] args) {
32 this.functionAlias = functionAlias;
33 this.javaMethod = functionAlias.findJavaMethod(args);
34 this.args = args;
35 }
36
37 public Value getValue(Session session) {
38 return javaMethod.getValue(session, args, false);
39 }
40
41 public int getType() {
42 return javaMethod.getDataType();
43 }
44
45 public void mapColumns(ColumnResolver resolver, int level) {
46 for (Expression e : args) {
47 e.mapColumns(resolver, level);
48 }
49 }
50
51 public Expression optimize(Session session) {
52 boolean allConst = isDeterministic();
53 for (int i = 0, len = args.length; i < len; i++) {
54 Expression e = args[i].optimize(session);
55 args[i] = e;
56 allConst &= e.isConstant();
57 }
58 if (allConst) {
59 return ValueExpression.get(getValue(session));
60 }
61 return this;
62 }
63
64 public void setEvaluatable(TableFilter tableFilter, boolean b) {
65 for (Expression e : args) {
66 if (e != null) {
67 e.setEvaluatable(tableFilter, b);
68 }
69 }
70 }
71
72 public int getScale() {
73 return DataType.getDataType(getType()).defaultScale;
74 }
75
76 public long getPrecision() {
77 return Integer.MAX_VALUE;
78 }
79
80 public int getDisplaySize() {
81 return Integer.MAX_VALUE;
82 }
83
84 public String getSQL() {
85 StatementBuilder buff = new StatementBuilder();
86 // TODO always append the schema once FUNCTIONS_IN_SCHEMA is enabled
87 if (functionAlias.getDatabase().getSettings().functionsInSchema ||
88 !functionAlias.getSchema().getName().equals(Constants.SCHEMA_MAIN)) {
89 buff.append(Parser.quoteIdentifier(functionAlias.getSchema().getName())).append('.');
90 }
91 buff.append(Parser.quoteIdentifier(functionAlias.getName())).append('(');
92 for (Expression e : args) {
93 buff.appendExceptFirst(", ");
94 buff.append(e.getSQL());
95 }
96 return buff.append(')').toString();
97 }
98
99 public void updateAggregate(Session session) {
100 for (Expression e : args) {
101 if (e != null) {
102 e.updateAggregate(session);
103 }
104 }
105 }
106
107 public String getName() {
108 return functionAlias.getName();
109 }
110
111 public int getParameterCount() {
112 return javaMethod.getParameterCount();
113 }
114
115 public ValueResultSet getValueForColumnList(Session session, Expression[] argList) {
116 Value v = javaMethod.getValue(session, argList, true);
117 return v == ValueNull.INSTANCE ? null : (ValueResultSet) v;
118 }
119
120 public Expression[] getArgs() {
121 return args;
122 }
123
124 public boolean isEverything(ExpressionVisitor visitor) {
125 switch(visitor.getType()) {
126 case ExpressionVisitor.DETERMINISTIC:
127 if (!isDeterministic()) {
128 return false;
129 }
130 // only if all parameters are deterministic as well
131 break;
132 case ExpressionVisitor.GET_DEPENDENCIES:
133 visitor.addDependency(functionAlias);
134 break;
135 default:
136 }
137 for (Expression e : args) {
138 if (e != null && !e.isEverything(visitor)) {
139 return false;
140 }
141 }
142 return true;
143 }
144
145 public int getCost() {
146 int cost = javaMethod.hasConnectionParam() ? 25 : 5;
147 for (Expression e : args) {
148 cost += e.getCost();
149 }
150 return cost;
151 }
152
153 public boolean isDeterministic() {
154 return functionAlias.isDeterministic();
155 }
156
157 public Expression[] getExpressionColumns(Session session) {
158 switch (getType()) {
159 case Value.RESULT_SET:
160 ValueResultSet rs = getValueForColumnList(session, getArgs());
161 return getExpressionColumns(session, rs.getResultSet());
162 case Value.ARRAY:
163 return getExpressionColumns(session, (ValueArray) getValue(session));
164 }
165 return super.getExpressionColumns(session);
166 }
167
168 public boolean isFast() {
169 return false;
170 }
171
172 }
173