001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.dao.orm.hibernate;
016    
017    import com.liferay.portal.kernel.dao.orm.CacheMode;
018    import com.liferay.portal.kernel.dao.orm.ORMException;
019    import com.liferay.portal.kernel.dao.orm.Query;
020    import com.liferay.portal.kernel.dao.orm.SQLQuery;
021    import com.liferay.portal.kernel.dao.orm.ScrollableResults;
022    import com.liferay.portal.kernel.dao.orm.Type;
023    import com.liferay.portal.kernel.util.ListUtil;
024    import com.liferay.portal.kernel.util.UnmodifiableList;
025    
026    import java.io.Serializable;
027    
028    import java.sql.Timestamp;
029    
030    import java.util.Iterator;
031    import java.util.List;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class SQLQueryImpl implements SQLQuery {
037    
038            public SQLQueryImpl(org.hibernate.SQLQuery sqlQuery) {
039                    _sqlQuery = sqlQuery;
040            }
041    
042            public SQLQuery addEntity(String alias, Class<?> entityClass) {
043                    _sqlQuery.addEntity(alias, entityClass);
044    
045                    return this;
046            }
047    
048            public SQLQuery addScalar(String columnAlias, Type type) {
049                    _sqlQuery.addScalar(columnAlias, TypeTranslator.translate(type));
050    
051                    return this;
052            }
053    
054            public int executeUpdate() throws ORMException {
055                    try {
056                            return _sqlQuery.executeUpdate();
057                    }
058                    catch (Exception e) {
059                            throw ExceptionTranslator.translate(e);
060                    }
061            }
062    
063            @SuppressWarnings("rawtypes")
064            public Iterator iterate() throws ORMException {
065                    return iterate(true);
066            }
067    
068            @SuppressWarnings("rawtypes")
069            public Iterator iterate(boolean unmodifiable) throws ORMException {
070                    try {
071                            return list(unmodifiable).iterator();
072                    }
073                    catch (Exception e) {
074                            throw ExceptionTranslator.translate(e);
075                    }
076            }
077    
078            @SuppressWarnings("rawtypes")
079            public List list() throws ORMException {
080                    return list(true);
081            }
082    
083            @SuppressWarnings("rawtypes")
084            public List list(boolean unmodifiable) throws ORMException {
085                    try {
086                            List list = _sqlQuery.list();
087    
088                            if (unmodifiable) {
089                                    return new UnmodifiableList(list);
090                            }
091                            else {
092                                    return ListUtil.copy(list);
093                            }
094                    }
095                    catch (Exception e) {
096                            throw ExceptionTranslator.translate(e);
097                    }
098            }
099    
100            public ScrollableResults scroll() throws ORMException {
101                    try {
102                            return new ScrollableResultsImpl(_sqlQuery.scroll());
103                    }
104                    catch (Exception e) {
105                            throw ExceptionTranslator.translate(e);
106                    }
107            }
108    
109            public Query setBoolean(int pos, boolean value) {
110                    _sqlQuery.setBoolean(pos, value);
111    
112                    return this;
113            }
114    
115            public Query setCacheable(boolean cacheable) {
116                    _sqlQuery.setCacheable(cacheable);
117    
118                    return this;
119            }
120    
121            public Query setCacheMode(CacheMode cacheMode) {
122                    _sqlQuery.setCacheMode(CacheModeTranslator.translate(cacheMode));
123    
124                    return this;
125            }
126    
127            public Query setCacheRegion(String cacheRegion) {
128                    _sqlQuery.setCacheRegion(cacheRegion);
129    
130                    return this;
131            }
132    
133            public Query setDouble(int pos, double value) {
134                    _sqlQuery.setDouble(pos, value);
135    
136                    return this;
137            }
138    
139            public Query setFirstResult(int firstResult) {
140                    _sqlQuery.setFirstResult(firstResult);
141    
142                    return this;
143            }
144    
145            public Query setFloat(int pos, float value) {
146                    _sqlQuery.setFloat(pos, value);
147    
148                    return this;
149            }
150    
151            public Query setInteger(int pos, int value) {
152                    _sqlQuery.setInteger(pos, value);
153    
154                    return this;
155            }
156    
157            public Query setLong(int pos, long value) {
158                    _sqlQuery.setLong(pos, value);
159    
160                    return this;
161            }
162    
163            public Query setMaxResults(int maxResults) {
164                    _sqlQuery.setMaxResults(maxResults);
165    
166                    return this;
167            }
168    
169            public Query setSerializable(int pos, Serializable value) {
170                    _sqlQuery.setSerializable(pos, value);
171    
172                    return this;
173            }
174    
175            public Query setShort(int pos, short value) {
176                    _sqlQuery.setShort(pos, value);
177    
178                    return this;
179            }
180    
181            public Query setString(int pos, String value) {
182                    _sqlQuery.setString(pos, value);
183    
184                    return this;
185            }
186    
187            public Query setTimestamp(int pos, Timestamp value) {
188                    _sqlQuery.setTimestamp(pos, value);
189    
190                    return this;
191            }
192    
193            public Object uniqueResult() throws ORMException {
194                    try {
195                            return _sqlQuery.uniqueResult();
196                    }
197                    catch (Exception e) {
198                            throw ExceptionTranslator.translate(e);
199                    }
200            }
201    
202            private org.hibernate.SQLQuery _sqlQuery;
203    
204    }