KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > ejb3 > QueryImpl


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29 package com.caucho.amber.ejb3;
30
31 import com.caucho.amber.manager.AmberConnection;
32 import com.caucho.amber.query.AbstractQuery;
33 import com.caucho.amber.query.UserQuery;
34 import com.caucho.ejb.EJBExceptionWrapper;
35 import com.caucho.util.L10N;
36
37 import javax.persistence.FlushModeType;
38 import javax.persistence.Query;
39 import javax.persistence.TemporalType;
40 import java.sql.ResultSet JavaDoc;
41 import java.sql.SQLException JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.Calendar JavaDoc;
44 import java.util.Date JavaDoc;
45 import java.util.List JavaDoc;
46
47 /**
48  * The EJB query
49  */

50 public class QueryImpl implements Query {
51   private static final L10N L = new L10N(QueryImpl.class);
52   
53   private AbstractQuery _query;
54   private UserQuery _userQuery;
55     
56   private AmberConnection _aConn;
57   private int _firstResult;
58   private int _maxResults = Integer.MAX_VALUE / 2;
59
60   /**
61    * Creates a manager instance.
62    */

63   QueryImpl(AbstractQuery query, AmberConnection aConn)
64   {
65     _query = query;
66     _aConn = aConn;
67
68     _userQuery = new UserQuery(query);
69     _userQuery.setSession(_aConn);
70   }
71
72   /**
73    * Execute the query and return as a List.
74    */

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

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

117   public int executeUpdate()
118   {
119     try {
120       return _userQuery.executeUpdate();
121     } catch (Exception JavaDoc e) {
122       throw EJBExceptionWrapper.createRuntime(e);
123     }
124   }
125
126   /**
127    * Executes the query returning a result set.
128    */

129   protected ResultSet JavaDoc executeQuery()
130     throws SQLException JavaDoc
131   {
132     return _userQuery.executeQuery();
133   }
134
135   /**
136    * Sets the maximum result returned.
137    */

138   public Query setMaxResults(int maxResult)
139   {
140     return this;
141   }
142
143   /**
144    * Sets the position of the first result.
145    */

146   public Query setFirstResult(int startPosition)
147   {
148     return this;
149   }
150
151   /**
152    * Sets a hint.
153    */

154   public Query setHint(String JavaDoc hintName, Object JavaDoc value)
155   {
156     return this;
157   }
158
159   /**
160    * Sets a named parameter.
161    */

162   public Query setParameter(String JavaDoc name, Object JavaDoc value)
163   {
164     return this;
165   }
166
167   /**
168    * Sets a date parameter.
169    */

170   public Query setParameter(String JavaDoc name, Date JavaDoc value, TemporalType type)
171   {
172     return this;
173   }
174
175   /**
176    * Sets a calendar parameter.
177    */

178   public Query setParameter(String JavaDoc name, Calendar JavaDoc value, TemporalType type)
179   {
180     return this;
181   }
182
183   /**
184    * Sets an indexed parameter.
185    */

186   public Query setParameter(int index, Object JavaDoc value)
187   {
188     _userQuery.setObject(index, value);
189     
190     return this;
191   }
192
193   /**
194    * Sets a date parameter.
195    */

196   public Query setParameter(int index, Date JavaDoc value, TemporalType type)
197   {
198     return this;
199   }
200
201   /**
202    * Sets a calendar parameter.
203    */

204   public Query setParameter(int index, Calendar JavaDoc value, TemporalType type)
205   {
206     return this;
207   }
208
209   /**
210    * Sets the flush mode type.
211    */

212   public Query setFlushMode(FlushModeType mode)
213   {
214     return this;
215   }
216
217   //
218
// extensions
219

220   /**
221    * Sets an indexed parameter.
222    */

223   public Query setDouble(int index, double value)
224   {
225     _userQuery.setDouble(index, value);
226     
227     return this;
228   }
229 }
230
Popular Tags