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