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