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