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