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