001
014
015 package com.liferay.portal.dao.orm.jpa;
016
017 import com.liferay.portal.dao.orm.common.SQLTransformer;
018 import com.liferay.portal.kernel.dao.orm.CacheMode;
019 import com.liferay.portal.kernel.dao.orm.LockMode;
020 import com.liferay.portal.kernel.dao.orm.ORMException;
021 import com.liferay.portal.kernel.dao.orm.Query;
022 import com.liferay.portal.kernel.dao.orm.ScrollableResults;
023 import com.liferay.portal.kernel.util.ListUtil;
024 import com.liferay.portal.kernel.util.UnmodifiableList;
025
026 import java.io.Serializable;
027
028 import java.sql.Timestamp;
029
030 import java.util.Date;
031 import java.util.HashMap;
032 import java.util.Iterator;
033 import java.util.List;
034 import java.util.Map;
035
036 import javax.persistence.FlushModeType;
037 import javax.persistence.LockModeType;
038
039
044 public class QueryImpl implements Query {
045
046 public QueryImpl(
047 SessionImpl sessionImpl, String queryString, boolean strictName) {
048
049 this.sessionImpl = sessionImpl;
050 this.queryString = SQLTransformer.transformFromHqlToJpql(queryString);
051 this.strictName = strictName;
052 }
053
054 public int executeUpdate() throws ORMException {
055 try {
056 return sessionImpl.executeUpdate(
057 queryString, positionalParameterMap, namedParameterMap,
058 strictName, firstResult, maxResults, flushModeType,
059 lockModeType, sqlQuery, entityClass);
060 }
061 catch (Exception e) {
062 throw ExceptionTranslator.translate(e);
063 }
064 }
065
066 public Iterator<?> iterate() throws ORMException {
067 return iterate(true);
068 }
069
070 public Iterator<?> iterate(boolean unmodifiable) throws ORMException {
071 try {
072 return list(unmodifiable).iterator();
073 }
074 catch (Exception e) {
075 throw ExceptionTranslator.translate(e);
076 }
077 }
078
079 public List<?> list() throws ORMException {
080 return list(false, false);
081 }
082
083 public List<?> list(boolean unmodifiable) throws ORMException {
084 return list(true, unmodifiable);
085 }
086
087 public List<?> list(boolean copy, boolean unmodifiable)
088 throws ORMException {
089
090 try {
091 List<?> list = sessionImpl.list(
092 queryString, positionalParameterMap, namedParameterMap,
093 strictName, firstResult, maxResults, flushModeType,
094 lockModeType, sqlQuery, entityClass);
095
096 if (unmodifiable) {
097 list = new UnmodifiableList<Object>(list);
098 }
099 else if (copy) {
100 list = ListUtil.copy(list);
101 }
102
103 return list;
104 }
105 catch (Exception e) {
106 throw ExceptionTranslator.translate(e);
107 }
108 }
109
110 public ScrollableResults scroll() throws ORMException {
111 try {
112 return new ScrollableResultsImpl(list());
113 }
114 catch (Exception e) {
115 throw ExceptionTranslator.translate(e);
116 }
117 }
118
119 public Query setBoolean(int pos, boolean value) {
120 positionalParameterMap.put(pos, value);
121
122 return this;
123 }
124
125 public Query setBoolean(String name, boolean value) {
126 namedParameterMap.put(name, value);
127
128 return this;
129 }
130
131 public Query setCacheable(boolean cacheable) {
132 return this;
133 }
134
135 public Query setCacheMode(CacheMode cacheMode) {
136 return this;
137 }
138
139 public Query setCacheRegion(String cacheRegion) {
140 return this;
141 }
142
143 public Query setDouble(int pos, double value) {
144 positionalParameterMap.put(pos, Double.valueOf(value));
145
146 return this;
147 }
148
149 public Query setDouble(String name, double value) {
150 namedParameterMap.put(name, Double.valueOf(value));
151
152 return this;
153 }
154
155 public Query setFirstResult(int firstResult) {
156 this.firstResult = firstResult;
157
158 return this;
159 }
160
161 public Query setFloat(int pos, float value) {
162 positionalParameterMap.put(pos, Float.valueOf(value));
163
164 return this;
165 }
166
167 public Query setFloat(String name, float value) {
168 namedParameterMap.put(name, Float.valueOf(value));
169
170 return this;
171 }
172
173 public Query setFlushMode(FlushModeType flushModeType) {
174 this.flushModeType = flushModeType;
175
176 return this;
177 }
178
179 public Query setInteger(int pos, int value) {
180 positionalParameterMap.put(pos, Integer.valueOf(value));
181
182 return this;
183 }
184
185 public Query setInteger(String name, int value) {
186 namedParameterMap.put(name, Integer.valueOf(value));
187
188 return this;
189 }
190
191 public Query setLockMode(String alias, LockMode lockMode) {
192 lockModeType = LockModeTranslator.translate(lockMode);
193
194 return this;
195 }
196
197 public Query setLong(int pos, long value) {
198 positionalParameterMap.put(pos, Long.valueOf(value));
199
200 return this;
201 }
202
203 public Query setLong(String name, long value) {
204 namedParameterMap.put(name, Long.valueOf(value));
205
206 return this;
207 }
208
209 public Query setMaxResults(int maxResults) {
210 this.maxResults = maxResults;
211
212 return this;
213 }
214
215 public Query setSerializable(int pos, Serializable value) {
216 positionalParameterMap.put(pos, value);
217
218 return this;
219 }
220
221 public Query setSerializable(String name, Serializable value) {
222 namedParameterMap.put(name, value);
223
224 return this;
225 }
226
227 public Query setShort(int pos, short value) {
228 positionalParameterMap.put(pos, Short.valueOf(value));
229
230 return this;
231 }
232
233 public Query setShort(String name, short value) {
234 namedParameterMap.put(name, Short.valueOf(value));
235
236 return this;
237 }
238
239 public Query setString(int pos, String value) {
240 positionalParameterMap.put(pos, value);
241
242 return this;
243 }
244
245 public Query setString(String name, String value) {
246 namedParameterMap.put(name, value);
247
248 return this;
249 }
250
251 public Query setTimestamp(int pos, Timestamp value) {
252 Date date = null;
253
254 if (value != null) {
255 date = new Date(value.getTime());
256 }
257
258 positionalParameterMap.put(pos, date);
259
260 return this;
261 }
262
263 public Query setTimestamp(String name, Timestamp value) {
264 Date date = null;
265
266 if (value != null) {
267 date = new Date(value.getTime());
268 }
269
270 namedParameterMap.put(name, date);
271
272 return this;
273 }
274
275 public Object uniqueResult() throws ORMException {
276 try {
277 return sessionImpl.uniqueResult(
278 queryString, positionalParameterMap, namedParameterMap,
279 strictName, firstResult, maxResults, flushModeType,
280 lockModeType, sqlQuery, entityClass);
281 }
282 catch (Exception e) {
283 throw ExceptionTranslator.translate(e);
284 }
285 }
286
287 protected Class<?> entityClass;
288 protected int firstResult = -1;
289 protected FlushModeType flushModeType;
290 protected LockModeType lockModeType;
291 protected int maxResults = -1;
292 protected Map<String, Object> namedParameterMap =
293 new HashMap<String, Object>();
294 protected Map<Integer, Object> positionalParameterMap =
295 new HashMap<Integer, Object>();
296 protected String queryString;
297 protected SessionImpl sessionImpl;
298 protected boolean sqlQuery;
299 protected boolean strictName = true;
300
301 }