KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > expr > fun > FunExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free Software Foundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.amber.expr.fun;
30
31 import com.caucho.amber.expr.AbstractAmberExpr;
32 import com.caucho.amber.expr.AmberExpr;
33 import com.caucho.amber.expr.IdExpr;
34 import com.caucho.amber.expr.KeyColumnExpr;
35 import com.caucho.amber.manager.AmberConnection;
36 import com.caucho.amber.query.FromItem;
37 import com.caucho.amber.query.QueryParser;
38 import com.caucho.util.CharBuffer;
39 import com.caucho.util.L10N;
40
41 import java.sql.ResultSet JavaDoc;
42 import java.sql.SQLException JavaDoc;
43 import java.util.ArrayList JavaDoc;
44
45
46 /**
47  * Function expression
48  */

49 public class FunExpr extends AbstractAmberExpr {
50   private static final L10N L = new L10N(FunExpr.class);
51
52   QueryParser _parser;
53
54   String JavaDoc _id;
55   ArrayList JavaDoc<AmberExpr> _args;
56   boolean _distinct;
57
58   /**
59    * Creates a new function expression
60    */

61   protected FunExpr(QueryParser parser,
62                     String JavaDoc id,
63                     ArrayList JavaDoc<AmberExpr> args,
64                     boolean distinct)
65   {
66     _parser = parser;
67     _id = id;
68     _args = args;
69     _distinct = distinct;
70   }
71
72   public static FunExpr create(QueryParser parser,
73                                String JavaDoc id,
74                                ArrayList JavaDoc<AmberExpr> args,
75                                boolean distinct)
76   {
77     return new FunExpr(parser, id, args, distinct);
78   }
79
80   /**
81    * Binds the expression as a select item.
82    */

83   public AmberExpr bindSelect(QueryParser parser)
84   {
85     for (int i = 0; i < _args.size(); i++) {
86       AmberExpr arg = _args.get(i);
87
88       arg = arg.bindSelect(parser);
89
90       _args.set(i, arg);
91     }
92
93     return this;
94   }
95
96   /**
97    * Returns true if the expression uses the from item.
98    */

99   public boolean usesFrom(FromItem from, int type, boolean isNot)
100   {
101     for (int i = 0; i < _args.size(); i++) {
102       AmberExpr arg = _args.get(i);
103
104       if (arg instanceof IdExpr) {
105         IdExpr id = (IdExpr) arg;
106
107         // jpa/0i18
108
if (id.getFromItem() == from)
109           return true;
110       }
111
112       if (arg instanceof KeyColumnExpr) {
113         KeyColumnExpr key = (KeyColumnExpr) arg;
114
115         // jpa/1123
116
if (key.usesFrom(from, IS_INNER_JOIN, false))
117           return true;
118       }
119
120       if (arg.usesFrom(from, type))
121         return true;
122     }
123
124     return false;
125   }
126
127   /**
128    * Generates the where expression.
129    */

130   public void generateWhere(CharBuffer cb)
131   {
132     generateInternalWhere(cb, true);
133   }
134
135   /**
136    * Generates the (update) where expression.
137    */

138   public void generateUpdateWhere(CharBuffer cb)
139   {
140     generateInternalWhere(cb, false);
141   }
142
143   /**
144    * Generates the having expression.
145    */

146   public void generateHaving(CharBuffer cb)
147   {
148     generateWhere(cb);
149   }
150
151   /**
152    * Returns the object for the expr.
153    */

154   public Object JavaDoc getObject(AmberConnection aConn, ResultSet JavaDoc rs, int index)
155     throws SQLException JavaDoc
156   {
157     if (_id.equalsIgnoreCase("count"))
158       return rs.getLong(index);
159
160     if (_id.equalsIgnoreCase("avg"))
161       return rs.getDouble(index);
162
163     return super.getObject(aConn, rs, index);
164   }
165
166   public String JavaDoc toString()
167   {
168     String JavaDoc str = _id + "(";
169
170     if (_distinct)
171       str += "distinct ";
172
173     for (int i = 0; i < _args.size(); i++) {
174       if (i != 0)
175         str += ',';
176
177       str += _args.get(i);
178     }
179
180     return str + ")";
181   }
182
183   // protected
184

185   /**
186    * Returns the args.
187    */

188   ArrayList JavaDoc<AmberExpr> getArgs()
189   {
190     return _args;
191   }
192
193   /**
194    * Generates the where clause.
195    */

196   private void generateInternalWhere(CharBuffer cb,
197                      boolean select)
198   {
199     cb.append(_id);
200     cb.append('(');
201
202     if (_distinct)
203       cb.append("distinct ");
204
205     for (int i = 0; i < _args.size(); i++) {
206       if (i != 0)
207         cb.append(',');
208
209       if (select)
210         _args.get(i).generateWhere(cb);
211       else
212         _args.get(i).generateUpdateWhere(cb);
213     }
214
215     cb.append(')');
216   }
217 }
218
Popular Tags