001    /**
002     * Copyright (c) 2000-2013 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.security.pacl.DoPrivileged;
025    import com.liferay.portal.kernel.security.pacl.NotPrivileged;
026    import com.liferay.portal.kernel.util.ListUtil;
027    import com.liferay.portal.kernel.util.UnmodifiableList;
028    
029    import java.io.Serializable;
030    
031    import java.sql.Timestamp;
032    
033    import java.util.Arrays;
034    import java.util.Iterator;
035    import java.util.List;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Shuyang Zhou
040     */
041    @DoPrivileged
042    public class SQLQueryImpl implements SQLQuery {
043    
044            public SQLQueryImpl(org.hibernate.SQLQuery sqlQuery, boolean strictName) {
045                    _sqlQuery = sqlQuery;
046                    _strictName = strictName;
047    
048                    if (!_strictName) {
049                            _names = sqlQuery.getNamedParameters();
050    
051                            Arrays.sort(_names);
052                    }
053            }
054    
055            @Override
056            public SQLQuery addEntity(String alias, Class<?> entityClass) {
057                    _sqlQuery.addEntity(alias, entityClass);
058    
059                    return this;
060            }
061    
062            @Override
063            public SQLQuery addScalar(String columnAlias, Type type) {
064                    _sqlQuery.addScalar(columnAlias, TypeTranslator.translate(type));
065    
066                    return this;
067            }
068    
069            @Override
070            public SQLQuery addSynchronizedEntityClass(Class<?> entityClass) {
071                    _sqlQuery.addSynchronizedEntityClass(entityClass);
072    
073                    return this;
074            }
075    
076            @Override
077            public SQLQuery addSynchronizedEntityClasses(Class<?>... entityClasses) {
078                    for (Class<?> entityClass : entityClasses) {
079                            _sqlQuery.addSynchronizedEntityClass(entityClass);
080                    }
081    
082                    return this;
083            }
084    
085            @Override
086            public SQLQuery addSynchronizedEntityName(String entityName) {
087                    _sqlQuery.addSynchronizedEntityName(entityName);
088    
089                    return this;
090            }
091    
092            @Override
093            public SQLQuery addSynchronizedEntityNames(String... entityNames) {
094                    for (String entityName : entityNames) {
095                            _sqlQuery.addSynchronizedEntityName(entityName);
096                    }
097    
098                    return this;
099            }
100    
101            @Override
102            public SQLQuery addSynchronizedQuerySpace(String querySpace) {
103                    _sqlQuery.addSynchronizedQuerySpace(querySpace);
104    
105                    return this;
106            }
107    
108            @Override
109            public SQLQuery addSynchronizedQuerySpaces(String... querySpaces) {
110                    for (String querySpace : querySpaces) {
111                            _sqlQuery.addSynchronizedQuerySpace(querySpace);
112                    }
113    
114                    return this;
115            }
116    
117            @NotPrivileged
118            @Override
119            public int executeUpdate() throws ORMException {
120                    try {
121                            return _sqlQuery.executeUpdate();
122                    }
123                    catch (Exception e) {
124                            throw ExceptionTranslator.translate(e);
125                    }
126            }
127    
128            @NotPrivileged
129            @Override
130            @SuppressWarnings("rawtypes")
131            public Iterator iterate() throws ORMException {
132                    return iterate(true);
133            }
134    
135            @NotPrivileged
136            @Override
137            @SuppressWarnings("rawtypes")
138            public Iterator iterate(boolean unmodifiable) throws ORMException {
139                    try {
140                            return list(unmodifiable).iterator();
141                    }
142                    catch (Exception e) {
143                            throw ExceptionTranslator.translate(e);
144                    }
145            }
146    
147            @NotPrivileged
148            @Override
149            public Object iterateNext() throws ORMException {
150                    Iterator<?> iterator = iterate(false);
151    
152                    if (iterator.hasNext()) {
153                            return iterator.next();
154                    }
155    
156                    return null;
157            }
158    
159            @NotPrivileged
160            @Override
161            public List<?> list() throws ORMException {
162                    return list(false, false);
163            }
164    
165            @NotPrivileged
166            @Override
167            public List<?> list(boolean unmodifiable) throws ORMException {
168                    return list(true, unmodifiable);
169            }
170    
171            @NotPrivileged
172            @Override
173            public List<?> list(boolean copy, boolean unmodifiable)
174                    throws ORMException {
175    
176                    try {
177                            List<?> list = _sqlQuery.list();
178    
179                            if (unmodifiable) {
180                                    list = new UnmodifiableList<Object>(list);
181                            }
182                            else if (copy) {
183                                    list = ListUtil.copy(list);
184                            }
185    
186                            return list;
187                    }
188                    catch (Exception e) {
189                            throw ExceptionTranslator.translate(e);
190                    }
191            }
192    
193            @NotPrivileged
194            @Override
195            public ScrollableResults scroll() throws ORMException {
196                    try {
197                            return new ScrollableResultsImpl(_sqlQuery.scroll());
198                    }
199                    catch (Exception e) {
200                            throw ExceptionTranslator.translate(e);
201                    }
202            }
203    
204            @Override
205            public Query setBoolean(int pos, boolean value) {
206                    _sqlQuery.setBoolean(pos, value);
207    
208                    return this;
209            }
210    
211            @Override
212            public Query setBoolean(String name, boolean value) {
213                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
214                            return this;
215                    }
216    
217                    _sqlQuery.setBoolean(name, value);
218    
219                    return this;
220            }
221    
222            @Override
223            public Query setCacheable(boolean cacheable) {
224                    _sqlQuery.setCacheable(cacheable);
225    
226                    return this;
227            }
228    
229            @Override
230            public Query setCacheMode(CacheMode cacheMode) {
231                    _sqlQuery.setCacheMode(CacheModeTranslator.translate(cacheMode));
232    
233                    return this;
234            }
235    
236            @Override
237            public Query setCacheRegion(String cacheRegion) {
238                    _sqlQuery.setCacheRegion(cacheRegion);
239    
240                    return this;
241            }
242    
243            @Override
244            public Query setDouble(int pos, double value) {
245                    _sqlQuery.setDouble(pos, value);
246    
247                    return this;
248            }
249    
250            @Override
251            public Query setDouble(String name, double value) {
252                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
253                            return this;
254                    }
255    
256                    _sqlQuery.setDouble(name, value);
257    
258                    return this;
259            }
260    
261            @Override
262            public Query setFirstResult(int firstResult) {
263                    _sqlQuery.setFirstResult(firstResult);
264    
265                    return this;
266            }
267    
268            @Override
269            public Query setFloat(int pos, float value) {
270                    _sqlQuery.setFloat(pos, value);
271    
272                    return this;
273            }
274    
275            @Override
276            public Query setFloat(String name, float value) {
277                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
278                            return this;
279                    }
280    
281                    _sqlQuery.setFloat(name, value);
282    
283                    return this;
284            }
285    
286            @Override
287            public Query setInteger(int pos, int value) {
288                    _sqlQuery.setInteger(pos, value);
289    
290                    return this;
291            }
292    
293            @Override
294            public Query setInteger(String name, int value) {
295                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
296                            return this;
297                    }
298    
299                    _sqlQuery.setInteger(name, value);
300    
301                    return this;
302            }
303    
304            @Override
305            public Query setLockMode(String alias, LockMode lockMode) {
306                    _sqlQuery.setLockMode(alias, LockModeTranslator.translate(lockMode));
307    
308                    return this;
309            }
310    
311            @Override
312            public Query setLong(int pos, long value) {
313                    _sqlQuery.setLong(pos, value);
314    
315                    return this;
316            }
317    
318            @Override
319            public Query setLong(String name, long value) {
320                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
321                            return this;
322                    }
323    
324                    _sqlQuery.setLong(name, value);
325    
326                    return this;
327            }
328    
329            @Override
330            public Query setMaxResults(int maxResults) {
331                    _sqlQuery.setMaxResults(maxResults);
332    
333                    return this;
334            }
335    
336            @Override
337            public Query setSerializable(int pos, Serializable value) {
338                    _sqlQuery.setSerializable(pos, value);
339    
340                    return this;
341            }
342    
343            @Override
344            public Query setSerializable(String name, Serializable value) {
345                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
346                            return this;
347                    }
348    
349                    _sqlQuery.setSerializable(name, value);
350    
351                    return this;
352            }
353    
354            @Override
355            public Query setShort(int pos, short value) {
356                    _sqlQuery.setShort(pos, value);
357    
358                    return this;
359            }
360    
361            @Override
362            public Query setShort(String name, short value) {
363                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
364                            return this;
365                    }
366    
367                    _sqlQuery.setShort(name, value);
368    
369                    return this;
370            }
371    
372            @Override
373            public Query setString(int pos, String value) {
374                    _sqlQuery.setString(pos, value);
375    
376                    return this;
377            }
378    
379            @Override
380            public Query setString(String name, String value) {
381                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
382                            return this;
383                    }
384    
385                    _sqlQuery.setString(name, value);
386    
387                    return this;
388            }
389    
390            @Override
391            public Query setTimestamp(int pos, Timestamp value) {
392                    _sqlQuery.setTimestamp(pos, value);
393    
394                    return this;
395            }
396    
397            @Override
398            public Query setTimestamp(String name, Timestamp value) {
399                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
400                            return this;
401                    }
402    
403                    _sqlQuery.setTimestamp(name, value);
404    
405                    return this;
406            }
407    
408            @NotPrivileged
409            @Override
410            public Object uniqueResult() throws ORMException {
411                    try {
412                            return _sqlQuery.uniqueResult();
413                    }
414                    catch (Exception e) {
415                            throw ExceptionTranslator.translate(e);
416                    }
417            }
418    
419            private String[] _names;
420            private org.hibernate.SQLQuery _sqlQuery;
421            private boolean _strictName;
422    
423    }