001    /**
002     * Copyright (c) 2000-2012 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.LockMode;
019    import com.liferay.portal.kernel.dao.orm.ORMException;
020    import com.liferay.portal.kernel.dao.orm.Query;
021    import com.liferay.portal.kernel.dao.orm.SQLQuery;
022    import com.liferay.portal.kernel.dao.orm.ScrollableResults;
023    import com.liferay.portal.kernel.dao.orm.Type;
024    import com.liferay.portal.kernel.util.ListUtil;
025    import com.liferay.portal.kernel.util.UnmodifiableList;
026    
027    import java.io.Serializable;
028    
029    import java.sql.Timestamp;
030    
031    import java.util.Arrays;
032    import java.util.Iterator;
033    import java.util.List;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     * @author Shuyang Zhou
038     */
039    public class SQLQueryImpl implements SQLQuery {
040    
041            public SQLQueryImpl(org.hibernate.SQLQuery sqlQuery, boolean strictName) {
042                    _sqlQuery = sqlQuery;
043    
044                    if (!_strictName) {
045                            _names = sqlQuery.getNamedParameters();
046    
047                            Arrays.sort(_names);
048                    }
049            }
050    
051            public SQLQuery addEntity(String alias, Class<?> entityClass) {
052                    _sqlQuery.addEntity(alias, entityClass);
053    
054                    return this;
055            }
056    
057            public SQLQuery addScalar(String columnAlias, Type type) {
058                    _sqlQuery.addScalar(columnAlias, TypeTranslator.translate(type));
059    
060                    return this;
061            }
062    
063            public int executeUpdate() throws ORMException {
064                    try {
065                            return _sqlQuery.executeUpdate();
066                    }
067                    catch (Exception e) {
068                            throw ExceptionTranslator.translate(e);
069                    }
070            }
071    
072            @SuppressWarnings("rawtypes")
073            public Iterator iterate() throws ORMException {
074                    return iterate(true);
075            }
076    
077            @SuppressWarnings("rawtypes")
078            public Iterator iterate(boolean unmodifiable) throws ORMException {
079                    try {
080                            return list(unmodifiable).iterator();
081                    }
082                    catch (Exception e) {
083                            throw ExceptionTranslator.translate(e);
084                    }
085            }
086    
087            public List<?> list() throws ORMException {
088                    return list(false, false);
089            }
090    
091            public List<?> list(boolean unmodifiable) throws ORMException {
092                    return list(true, unmodifiable);
093            }
094    
095            public List<?> list(boolean copy, boolean unmodifiable)
096                    throws ORMException {
097    
098                    try {
099                            List<?> list = _sqlQuery.list();
100    
101                            if (unmodifiable) {
102                                    list = new UnmodifiableList<Object>(list);
103                            }
104                            else if (copy) {
105                                    list = ListUtil.copy(list);
106                            }
107    
108                            return list;
109                    }
110                    catch (Exception e) {
111                            throw ExceptionTranslator.translate(e);
112                    }
113            }
114    
115            public ScrollableResults scroll() throws ORMException {
116                    try {
117                            return new ScrollableResultsImpl(_sqlQuery.scroll());
118                    }
119                    catch (Exception e) {
120                            throw ExceptionTranslator.translate(e);
121                    }
122            }
123    
124            public Query setBoolean(int pos, boolean value) {
125                    _sqlQuery.setBoolean(pos, value);
126    
127                    return this;
128            }
129    
130            public Query setBoolean(String name, boolean value) {
131                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
132                            return this;
133                    }
134    
135                    _sqlQuery.setBoolean(name, value);
136    
137                    return this;
138            }
139    
140            public Query setCacheable(boolean cacheable) {
141                    _sqlQuery.setCacheable(cacheable);
142    
143                    return this;
144            }
145    
146            public Query setCacheMode(CacheMode cacheMode) {
147                    _sqlQuery.setCacheMode(CacheModeTranslator.translate(cacheMode));
148    
149                    return this;
150            }
151    
152            public Query setCacheRegion(String cacheRegion) {
153                    _sqlQuery.setCacheRegion(cacheRegion);
154    
155                    return this;
156            }
157    
158            public Query setDouble(int pos, double value) {
159                    _sqlQuery.setDouble(pos, value);
160    
161                    return this;
162            }
163    
164            public Query setDouble(String name, double value) {
165                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
166                            return this;
167                    }
168    
169                    _sqlQuery.setDouble(name, value);
170    
171                    return this;
172            }
173    
174            public Query setFirstResult(int firstResult) {
175                    _sqlQuery.setFirstResult(firstResult);
176    
177                    return this;
178            }
179    
180            public Query setFloat(int pos, float value) {
181                    _sqlQuery.setFloat(pos, value);
182    
183                    return this;
184            }
185    
186            public Query setFloat(String name, float value) {
187                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
188                            return this;
189                    }
190    
191                    _sqlQuery.setFloat(name, value);
192    
193                    return this;
194            }
195    
196            public Query setInteger(int pos, int value) {
197                    _sqlQuery.setInteger(pos, value);
198    
199                    return this;
200            }
201    
202            public Query setInteger(String name, int value) {
203                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
204                            return this;
205                    }
206    
207                    _sqlQuery.setInteger(name, value);
208    
209                    return this;
210            }
211    
212            public Query setLockMode(String alias, LockMode lockMode) {
213                    _sqlQuery.setLockMode(alias, LockModeTranslator.translate(lockMode));
214    
215                    return this;
216            }
217    
218            public Query setLong(int pos, long value) {
219                    _sqlQuery.setLong(pos, value);
220    
221                    return this;
222            }
223    
224            public Query setLong(String name, long value) {
225                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
226                            return this;
227                    }
228    
229                    _sqlQuery.setLong(name, value);
230    
231                    return this;
232            }
233    
234            public Query setMaxResults(int maxResults) {
235                    _sqlQuery.setMaxResults(maxResults);
236    
237                    return this;
238            }
239    
240            public Query setSerializable(int pos, Serializable value) {
241                    _sqlQuery.setSerializable(pos, value);
242    
243                    return this;
244            }
245    
246            public Query setSerializable(String name, Serializable value) {
247                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
248                            return this;
249                    }
250    
251                    _sqlQuery.setSerializable(name, value);
252    
253                    return this;
254            }
255    
256            public Query setShort(int pos, short value) {
257                    _sqlQuery.setShort(pos, value);
258    
259                    return this;
260            }
261    
262            public Query setShort(String name, short value) {
263                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
264                            return this;
265                    }
266    
267                    _sqlQuery.setShort(name, value);
268    
269                    return this;
270            }
271    
272            public Query setString(int pos, String value) {
273                    _sqlQuery.setString(pos, value);
274    
275                    return this;
276            }
277    
278            public Query setString(String name, String value) {
279                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
280                            return this;
281                    }
282    
283                    _sqlQuery.setString(name, value);
284    
285                    return this;
286            }
287    
288            public Query setTimestamp(int pos, Timestamp value) {
289                    _sqlQuery.setTimestamp(pos, value);
290    
291                    return this;
292            }
293    
294            public Query setTimestamp(String name, Timestamp value) {
295                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
296                            return this;
297                    }
298    
299                    _sqlQuery.setTimestamp(name, value);
300    
301                    return this;
302            }
303    
304            public Object uniqueResult() throws ORMException {
305                    try {
306                            return _sqlQuery.uniqueResult();
307                    }
308                    catch (Exception e) {
309                            throw ExceptionTranslator.translate(e);
310                    }
311            }
312    
313            private String[] _names;
314            private org.hibernate.SQLQuery _sqlQuery;
315            private boolean _strictName;
316    
317    }