001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029: package com.caucho.amber.ejb3;
030:
031: import com.caucho.amber.manager.AmberConnection;
032: import com.caucho.amber.query.AbstractQuery;
033: import com.caucho.amber.query.UserQuery;
034: import com.caucho.ejb.EJBExceptionWrapper;
035: import com.caucho.util.L10N;
036:
037: import javax.persistence.FlushModeType;
038: import javax.persistence.Query;
039: import javax.persistence.TemporalType;
040: import java.sql.ResultSet;
041: import java.sql.SQLException;
042: import java.util.ArrayList;
043: import java.util.Calendar;
044: import java.util.Date;
045: import java.util.List;
046:
047: /**
048: * The EJB query
049: */
050: public class QueryImpl implements Query {
051: private static final L10N L = new L10N(QueryImpl.class);
052:
053: private AbstractQuery _query;
054: private UserQuery _userQuery;
055:
056: private AmberConnection _aConn;
057: private int _firstResult;
058: private int _maxResults = Integer.MAX_VALUE / 2;
059:
060: /**
061: * Creates a manager instance.
062: */
063: QueryImpl(AbstractQuery query, AmberConnection aConn) {
064: _query = query;
065: _aConn = aConn;
066:
067: _userQuery = new UserQuery(query);
068: _userQuery.setSession(_aConn);
069: }
070:
071: /**
072: * Execute the query and return as a List.
073: */
074: public List getResultList() {
075: try {
076: ArrayList results = new ArrayList();
077:
078: ResultSet rs = executeQuery();
079:
080: while (rs.next())
081: results.add(rs.getObject(1));
082:
083: rs.close();
084:
085: return results;
086: } catch (Exception e) {
087: throw EJBExceptionWrapper.createRuntime(e);
088: }
089: }
090:
091: /**
092: * Returns a single result.
093: */
094: public Object getSingleResult() {
095: try {
096: ResultSet rs = executeQuery();
097:
098: Object value = null;
099:
100: if (rs.next())
101: value = rs.getObject(1);
102:
103: rs.close();
104:
105: return value;
106: } catch (Exception e) {
107: throw EJBExceptionWrapper.createRuntime(e);
108: }
109: }
110:
111: /**
112: * Execute an update or delete.
113: */
114: public int executeUpdate() {
115: try {
116: return _userQuery.executeUpdate();
117: } catch (Exception e) {
118: throw EJBExceptionWrapper.createRuntime(e);
119: }
120: }
121:
122: /**
123: * Executes the query returning a result set.
124: */
125: protected ResultSet executeQuery() throws SQLException {
126: return _userQuery.executeQuery();
127: }
128:
129: /**
130: * Sets the maximum result returned.
131: */
132: public Query setMaxResults(int maxResult) {
133: return this ;
134: }
135:
136: /**
137: * Sets the position of the first result.
138: */
139: public Query setFirstResult(int startPosition) {
140: return this ;
141: }
142:
143: /**
144: * Sets a hint.
145: */
146: public Query setHint(String hintName, Object value) {
147: return this ;
148: }
149:
150: /**
151: * Sets a named parameter.
152: */
153: public Query setParameter(String name, Object value) {
154: return this ;
155: }
156:
157: /**
158: * Sets a date parameter.
159: */
160: public Query setParameter(String name, Date value, TemporalType type) {
161: return this ;
162: }
163:
164: /**
165: * Sets a calendar parameter.
166: */
167: public Query setParameter(String name, Calendar value,
168: TemporalType type) {
169: return this ;
170: }
171:
172: /**
173: * Sets an indexed parameter.
174: */
175: public Query setParameter(int index, Object value) {
176: _userQuery.setObject(index, value);
177:
178: return this ;
179: }
180:
181: /**
182: * Sets a date parameter.
183: */
184: public Query setParameter(int index, Date value, TemporalType type) {
185: return this ;
186: }
187:
188: /**
189: * Sets a calendar parameter.
190: */
191: public Query setParameter(int index, Calendar value,
192: TemporalType type) {
193: return this ;
194: }
195:
196: /**
197: * Sets the flush mode type.
198: */
199: public Query setFlushMode(FlushModeType mode) {
200: return this ;
201: }
202:
203: //
204: // extensions
205:
206: /**
207: * Sets an indexed parameter.
208: */
209: public Query setDouble(int index, double value) {
210: _userQuery.setDouble(index, value);
211:
212: return this;
213: }
214: }
|