1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.dao.orm.hibernate;
24  
25  import com.liferay.portal.kernel.dao.orm.CacheMode;
26  import com.liferay.portal.kernel.dao.orm.ORMException;
27  import com.liferay.portal.kernel.dao.orm.Query;
28  import com.liferay.portal.kernel.dao.orm.SQLQuery;
29  import com.liferay.portal.kernel.dao.orm.ScrollableResults;
30  import com.liferay.portal.kernel.dao.orm.Type;
31  import com.liferay.portal.kernel.util.ListUtil;
32  import com.liferay.portal.kernel.util.UnmodifiableList;
33  
34  import java.io.Serializable;
35  
36  import java.sql.Timestamp;
37  
38  import java.util.Iterator;
39  import java.util.List;
40  
41  /**
42   * <a href="SQLQueryImpl.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   */
46  public class SQLQueryImpl implements SQLQuery {
47  
48      public SQLQueryImpl(org.hibernate.SQLQuery sqlQuery) {
49          _sqlQuery = sqlQuery;
50      }
51  
52      public SQLQuery addEntity(String alias, Class<?> entityClass) {
53          _sqlQuery.addEntity(alias, entityClass);
54  
55          return this;
56      }
57  
58      public SQLQuery addScalar(String columnAlias, Type type) {
59          _sqlQuery.addScalar(columnAlias, TypeTranslator.translate(type));
60  
61          return this;
62      }
63  
64      public int executeUpdate() throws ORMException {
65          try {
66              return _sqlQuery.executeUpdate();
67          }
68          catch (Exception e) {
69              throw ExceptionTranslator.translate(e);
70          }
71      }
72  
73      @SuppressWarnings("unchecked")
74      public Iterator iterate() throws ORMException {
75          return iterate(true);
76      }
77  
78      @SuppressWarnings("unchecked")
79      public Iterator iterate(boolean unmodifiable) throws ORMException {
80          try {
81              return list(unmodifiable).iterator();
82          }
83          catch (Exception e) {
84              throw ExceptionTranslator.translate(e);
85          }
86      }
87  
88      @SuppressWarnings("unchecked")
89      public List list() throws ORMException {
90          return list(true);
91      }
92  
93      @SuppressWarnings("unchecked")
94      public List list(boolean unmodifiable) throws ORMException {
95          try {
96              List list = _sqlQuery.list();
97  
98              if (unmodifiable) {
99                  return new UnmodifiableList(list);
100             }
101             else {
102                 return ListUtil.copy(list);
103             }
104         }
105         catch (Exception e) {
106             throw ExceptionTranslator.translate(e);
107         }
108     }
109 
110     public ScrollableResults scroll() throws ORMException {
111         try {
112             return new ScrollableResultsImpl(_sqlQuery.scroll());
113         }
114         catch (Exception e) {
115             throw ExceptionTranslator.translate(e);
116         }
117     }
118 
119     public Query setBoolean(int pos, boolean value) {
120         _sqlQuery.setBoolean(pos, value);
121 
122         return this;
123     }
124 
125     public Query setCacheable(boolean cacheable) {
126         _sqlQuery.setCacheable(cacheable);
127 
128         return this;
129     }
130 
131     public Query setCacheMode(CacheMode cacheMode) {
132         _sqlQuery.setCacheMode(CacheModeTranslator.translate(cacheMode));
133 
134         return this;
135     }
136 
137     public Query setCacheRegion(String cacheRegion) {
138         _sqlQuery.setCacheRegion(cacheRegion);
139 
140         return this;
141     }
142 
143     public Query setDouble(int pos, double value) {
144         _sqlQuery.setDouble(pos, value);
145 
146         return this;
147     }
148 
149     public Query setFirstResult(int firstResult) {
150         _sqlQuery.setFirstResult(firstResult);
151 
152         return this;
153     }
154 
155     public Query setFloat(int pos, float value) {
156         _sqlQuery.setFloat(pos, value);
157 
158         return this;
159     }
160 
161     public Query setInteger(int pos, int value) {
162         _sqlQuery.setInteger(pos, value);
163 
164         return this;
165     }
166 
167     public Query setLong(int pos, long value) {
168         _sqlQuery.setLong(pos, value);
169 
170         return this;
171     }
172 
173     public Query setMaxResults(int maxResults) {
174         _sqlQuery.setMaxResults(maxResults);
175 
176         return this;
177     }
178 
179     public Query setSerializable(int pos, Serializable value) {
180         _sqlQuery.setSerializable(pos, value);
181 
182         return this;
183     }
184 
185     public Query setShort(int pos, short value) {
186         _sqlQuery.setShort(pos, value);
187 
188         return this;
189     }
190 
191     public Query setString(int pos, String value) {
192         _sqlQuery.setString(pos, value);
193 
194         return this;
195     }
196 
197     public Query setTimestamp(int pos, Timestamp value) {
198         _sqlQuery.setTimestamp(pos, value);
199 
200         return this;
201     }
202 
203     public Object uniqueResult() throws ORMException {
204         try {
205             return _sqlQuery.uniqueResult();
206         }
207         catch (Exception e) {
208             throw ExceptionTranslator.translate(e);
209         }
210     }
211 
212     private org.hibernate.SQLQuery _sqlQuery;
213 
214 }