KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > expression > JavaFunction


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.expression;
6
7 import java.sql.SQLException JavaDoc;
8
9 import org.h2.command.Parser;
10 import org.h2.engine.FunctionAlias;
11 import org.h2.engine.Session;
12 import org.h2.table.ColumnResolver;
13 import org.h2.table.TableFilter;
14 import org.h2.value.Value;
15 import org.h2.value.ValueNull;
16 import org.h2.value.ValueResultSet;
17
18 public class JavaFunction extends Expression implements FunctionCall {
19
20     private FunctionAlias functionAlias;
21     private Expression[] args;
22
23     public JavaFunction(FunctionAlias functionAlias, Expression[] args) {
24         this.functionAlias = functionAlias;
25         this.args = args;
26     }
27     
28     public Value getValue(Session session) throws SQLException JavaDoc {
29         return functionAlias.getValue(session, args);
30     }
31
32     public int getType() {
33         return functionAlias.getDataType();
34     }
35     
36     public void mapColumns(ColumnResolver resolver, int level) throws SQLException JavaDoc {
37         for (int i = 0; i < args.length; i++) {
38             args[i].mapColumns(resolver, level);
39         }
40     }
41
42     public Expression optimize(Session session) throws SQLException JavaDoc {
43         for(int i=0; i<args.length; i++) {
44             Expression e = args[i].optimize(session);
45             args[i] = e;
46         }
47         return this;
48     }
49
50     public void setEvaluatable(TableFilter tableFilter, boolean b) {
51         for (int i = 0; i < args.length; i++) {
52             Expression e = args[i];
53             if (e != null) {
54                 e.setEvaluatable(tableFilter, b);
55             }
56         }
57     }
58
59     public int getScale() {
60         return 0;
61     }
62
63     public long getPrecision() {
64         return 0;
65     }
66
67     public String JavaDoc getSQL() {
68         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
69         buff.append(Parser.quoteIdentifier(functionAlias.getName()));
70         buff.append('(');
71         for (int i = 0; i < args.length; i++) {
72             if (i > 0) {
73                 buff.append(", ");
74             }
75             Expression e = args[i];
76             buff.append(e.getSQL());
77         }
78         buff.append(')');
79         return buff.toString();
80     }
81
82     public void updateAggregate(Session session) throws SQLException JavaDoc {
83         for (int i = 0; i < args.length; i++) {
84             Expression e = args[i];
85             if (e != null) {
86                 e.updateAggregate(session);
87             }
88         }
89     }
90
91     public FunctionAlias getFunctionAlias() {
92         return functionAlias;
93     }
94
95     public String JavaDoc getName() {
96         return functionAlias.getName();
97     }
98
99     public int getParameterCount() {
100         return functionAlias.getParameterCount();
101     }
102
103     public ValueResultSet getValueForColumnList(Session session, Expression[] args) throws SQLException JavaDoc {
104         Value v = functionAlias.getValue(session, args, true);
105         return v == ValueNull.INSTANCE ? null : (ValueResultSet) v;
106     }
107
108     public Expression[] getArgs() {
109         return args;
110     }
111     
112     public boolean isEverything(ExpressionVisitor visitor) {
113         if(visitor.type == ExpressionVisitor.DETERMINISTIC) {
114             // TODO optimization: some functions are deterministic, but we don't know (no setting for that)
115
return false;
116         }
117         for (int i = 0; i < args.length; i++) {
118             Expression e = args[i];
119             if (e != null && !e.isEverything(visitor)) {
120                 return false;
121             }
122         }
123         return true;
124     }
125     
126     public int getCost() {
127         int cost = functionAlias.hasConnectionParam() ? 25 : 5;
128         for(int i=0; i<args.length; i++) {
129             cost += args[i].getCost();
130         }
131         return cost;
132     }
133
134 }
135
Popular Tags