KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > expr > ArgExpr


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;
30
31 import com.caucho.amber.query.QueryParser;
32 import com.caucho.amber.type.*;
33 import com.caucho.util.CharBuffer;
34
35 import java.sql.PreparedStatement JavaDoc;
36 import java.sql.SQLException JavaDoc;
37
38 /**
39  * Parameter argument expression.
40  */

41 public class ArgExpr extends AbstractAmberExpr {
42   private QueryParser _parser;
43
44   // argument index
45
private int _index;
46
47   private int _sqlIndex;
48
49   private String JavaDoc _name;
50
51   private Type _type;
52
53   /**
54    * Creates a new argument expression.
55    *
56    * @param index the argument index
57    */

58   public ArgExpr(QueryParser parser, int index)
59   {
60     _parser = parser;
61
62     _index = index;
63
64     _sqlIndex = -1;
65   }
66
67   /**
68    * Creates a new named argument expression.
69    *
70    * @param String the argument name
71    */

72   public ArgExpr(QueryParser parser, String JavaDoc name, int index)
73   {
74     _parser = parser;
75
76     _name = name;
77
78     _index = index;
79
80     _sqlIndex = -1;
81   }
82
83   /*
84    * Returns a new ArgExpr with the same state,
85    * except its _sqlIndex will be the next one
86    * when generated.
87    *
88   public ArgExpr getNextClone()
89   {
90     // jpa/114e
91     ArgExpr arg = new ArgExpr(_parser, _name, _index);
92
93     arg.setType(_type);
94
95     return arg;
96   }
97   */

98
99   /**
100    * Returns the index value
101    */

102   int getIndex()
103   {
104     return _index;
105   }
106
107   /**
108    * Returns the expr type.
109    */

110   public Type getType()
111   {
112     return _type;
113   }
114
115   /**
116    * Sets the expr type.
117    */

118   public void setType(Type type)
119   {
120     _type = type;
121   }
122
123   /**
124    * Binds the expression as a select item.
125    */

126   public AmberExpr bindSelect(QueryParser parser)
127   {
128     parser.addArg(this);
129
130     return this;
131   }
132
133   /**
134    * Returns true if the expression must exist
135    */

136   @Override JavaDoc
137   public boolean exists()
138   {
139     // ejb/0h1k
140
return true;
141   }
142
143   /**
144    * Generates the literal.
145    */

146   public void generateWhere(CharBuffer cb)
147   {
148     generateInternalWhere(cb, true);
149   }
150
151   /**
152    * Generates the (update) literal.
153    */

154   public void generateUpdateWhere(CharBuffer cb)
155   {
156     generateInternalWhere(cb, false);
157   }
158
159   /**
160    * Generates the having expression.
161    */

162   public void generateHaving(CharBuffer cb)
163   {
164     generateWhere(cb);
165   }
166
167   /**
168    * Returns the argument name, or null
169    * if it is a positional parameter.
170    */

171   public String JavaDoc getName()
172   {
173     return _name;
174   }
175
176   /**
177    * Sets the parameter.
178    */

179   public void setParameter(PreparedStatement JavaDoc pstmt, int i,
180                            Type []argTypes, Object JavaDoc []argValues)
181     throws SQLException JavaDoc
182   {
183     if (_name == null) {
184
185       // jpa/141d (enum type)
186
if (getType() != null) {
187         if (! ((getType() instanceof UtilDateType) ||
188                (getType() instanceof CalendarType))) {
189           argTypes[_index - 1] = getType();
190         }
191       }
192
193       if (argTypes[_index - 1] != null) {
194
195         argTypes[_index - 1].setParameter(pstmt, _sqlIndex + 1,
196                                           argValues[_index - 1]);
197         // jpa/141e
198
}
199       else
200         pstmt.setString(_sqlIndex + 1, null);
201     }
202     else {
203       // jpa/141d (enum type)
204
if (getType() != null) {
205         // jpa/1410, jpa/1413
206
if (! ((getType() instanceof UtilDateType) ||
207                (getType() instanceof CalendarType))) {
208           argTypes[i - 1] = getType();
209         }
210       }
211
212       if (argTypes[i - 1] != null) {
213         // jpa/141g
214

215         // jpa/1217 argTypes[i - 1].setParameter(pstmt, _sqlIndex + 1, argValues[i - 1]);
216
argTypes[i - 1].setParameter(pstmt, i, argValues[i - 1]);
217       }
218       else
219         pstmt.setString(_sqlIndex + 1, null);
220     }
221
222   }
223
224   public String JavaDoc toString()
225   {
226     if (_name == null)
227       return "?" + _index;
228     else
229       return ":" + _name;
230   }
231
232   //
233
// private
234

235   private void generateInternalWhere(CharBuffer cb,
236                                      boolean select)
237   {
238     if (_sqlIndex < 0)
239       _sqlIndex = _parser.generateSQLArg();
240
241     cb.append("?");
242   }
243 }
244
Popular Tags