KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > entity2 > EJBQueryImpl


1 /*
2  * Copyright (c) 1998-2004 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 SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28 package com.caucho.ejb.entity2;
29
30 import java.util.List;
31 import java.util.ArrayList;
32 import java.util.Date;
33 import java.util.Calendar;
34
35 import java.sql.Connection;
36 import java.sql.PreparedStatement;
37 import java.sql.ResultSet;
38 import java.sql.SQLException;
39
40 import javax.ejb.Query;
41
42 import com.caucho.amber.connection.AmberConnectionImpl;
43
44 import com.caucho.amber.query.AbstractQuery;
45 import com.caucho.amber.query.UserQuery;
46
47 import com.caucho.util.L10N;
48
49 import com.caucho.ejb.EJBExceptionWrapper;
50
51 /**
52  * The EJB query
53  */

54 public class EJBQueryImpl implements Query {
55   private static final L10N L = new L10N(EJBQueryImpl.class);
56   
57   private AbstractQuery _query;
58   private UserQuery _userQuery;
59     
60   private AmberConnectionImpl _aConn;
61   private int _firstResult;
62   private int _maxResults = Integer.MAX_VALUE / 2;
63
64   /**
65    * Creates a manager instance.
66    */

67   EJBQueryImpl(AbstractQuery query, AmberConnectionImpl aConn)
68   {
69     _query = query;
70     _aConn = aConn;
71
72     _userQuery = new UserQuery(query);
73     _userQuery.setSession(_aConn);
74   }
75
76   /**
77    * Execute the query and return as a List.
78    */

79   public List listResults()
80   {
81     try {
82       ArrayList results = new ArrayList();
83       
84       ResultSet rs = executeQuery();
85
86       while (rs.next())
87     results.add(rs.getObject(1));
88
89       rs.close();
90
91       return results;
92     } catch (Exception e) {
93       throw EJBExceptionWrapper.createRuntime(e);
94     }
95   }
96
97   /**
98    * Returns a single result.
99    */

100   public Object getSingleResult()
101   {
102     try {
103       ResultSet rs = executeQuery();
104
105       Object value = null;
106
107       if (rs.next())
108     value = rs.getObject(1);
109
110       rs.close();
111
112       return value;
113     } catch (Exception e) {
114       throw EJBExceptionWrapper.createRuntime(e);
115     }
116   }
117
118   /**
119    * Execute an update or delete.
120    */

121   public int executeUpdate()
122   {
123     try {
124       return _userQuery.executeUpdate();
125     } catch (Exception e) {
126       throw EJBExceptionWrapper.createRuntime(e);
127     }
128   }
129
130   /**
131    * Executes the query returning a result set.
132    */

133   protected ResultSet executeQuery()
134     throws SQLException
135   {
136     return _userQuery.executeQuery();
137   }
138
139   /**
140    * Sets the maximum result returned.
141    */

142   public Query setMaxResults(int maxResult)
143   {
144     return this;
145   }
146
147   /**
148    * Sets the position of the first result.
149    */

150   public Query setFirstResult(int startPosition)
151   {
152     return this;
153   }
154
155   /**
156    * Sets a hint.
157    */

158   public Query setHint(String hintName, Object value)
159   {
160     return this;
161   }
162
163   /**
164    * Sets a named parameter.
165    */

166   public Query setParameter(String name, Object value)
167   {
168     return this;
169   }
170
171   /**
172    * Sets a date parameter.
173    */

174   public Query setParameter(String name, Date value, TemporalType type)
175   {
176     return this;
177   }
178
179   /**
180    * Sets a calendar parameter.
181    */

182   public Query setParameter(String name, Calendar value, TemporalType type)
183   {
184     return this;
185   }
186
187   /**
188    * Sets an indexed parameter.
189    */

190   public Query setParameter(int index, Object value)
191   {
192     _userQuery.setObject(index, value);
193     
194     return this;
195   }
196
197   /**
198    * Sets a date parameter.
199    */

200   public Query setParameter(int index, Date value, TemporalType type)
201   {
202     return this;
203   }
204
205   /**
206    * Sets a calendar parameter.
207    */

208   public Query setParameter(int index, Calendar value, TemporalType type)
209   {
210     return this;
211   }
212 }
213
Popular Tags