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      public Iterator iterate() throws ORMException {
74          return iterate(true);
75      }
76  
77      public Iterator iterate(boolean unmodifiable) throws ORMException {
78          try {
79              return list(unmodifiable).iterator();
80          }
81          catch (Exception e) {
82              throw ExceptionTranslator.translate(e);
83          }
84      }
85  
86      public List list() throws ORMException {
87          return list(true);
88      }
89  
90      public List list(boolean unmodifiable) throws ORMException {
91          try {
92              List list = _sqlQuery.list();
93  
94              if (unmodifiable) {
95                  return new UnmodifiableList(list);
96              }
97              else {
98                  return ListUtil.copy(list);
99              }
100         }
101         catch (Exception e) {
102             throw ExceptionTranslator.translate(e);
103         }
104     }
105 
106     public ScrollableResults scroll() throws ORMException {
107         try {
108             return new ScrollableResultsImpl(_sqlQuery.scroll());
109         }
110         catch (Exception e) {
111             throw ExceptionTranslator.translate(e);
112         }
113     }
114 
115     public Query setBoolean(int pos, boolean value) {
116         _sqlQuery.setBoolean(pos, value);
117 
118         return this;
119     }
120 
121     public Query setCacheable(boolean cacheable) {
122         _sqlQuery.setCacheable(cacheable);
123 
124         return this;
125     }
126 
127     public Query setCacheMode(CacheMode cacheMode) {
128         _sqlQuery.setCacheMode(CacheModeTranslator.translate(cacheMode));
129 
130         return this;
131     }
132 
133     public Query setCacheRegion(String cacheRegion) {
134         _sqlQuery.setCacheRegion(cacheRegion);
135 
136         return this;
137     }
138 
139     public Query setDouble(int pos, double value) {
140         _sqlQuery.setDouble(pos, value);
141 
142         return this;
143     }
144 
145     public Query setFirstResult(int firstResult) {
146         _sqlQuery.setFirstResult(firstResult);
147 
148         return this;
149     }
150 
151     public Query setFloat(int pos, float value) {
152         _sqlQuery.setFloat(pos, value);
153 
154         return this;
155     }
156 
157     public Query setInteger(int pos, int value) {
158         _sqlQuery.setInteger(pos, value);
159 
160         return this;
161     }
162 
163     public Query setLong(int pos, long value) {
164         _sqlQuery.setLong(pos, value);
165 
166         return this;
167     }
168 
169     public Query setMaxResults(int maxResults) {
170         _sqlQuery.setMaxResults(maxResults);
171 
172         return this;
173     }
174 
175     public Query setSerializable(int pos, Serializable value) {
176         _sqlQuery.setSerializable(pos, value);
177 
178         return this;
179     }
180 
181     public Query setShort(int pos, short value) {
182         _sqlQuery.setShort(pos, value);
183 
184         return this;
185     }
186 
187     public Query setString(int pos, String value) {
188         _sqlQuery.setString(pos, value);
189 
190         return this;
191     }
192 
193     public Query setTimestamp(int pos, Timestamp value) {
194         _sqlQuery.setTimestamp(pos, value);
195 
196         return this;
197     }
198 
199     public Object uniqueResult() throws ORMException {
200         try {
201             return _sqlQuery.uniqueResult();
202         }
203         catch (Exception e) {
204             throw ExceptionTranslator.translate(e);
205         }
206     }
207 
208     private org.hibernate.SQLQuery _sqlQuery;
209 
210 }