1
14
15 package com.liferay.portal.dao.orm.jpa;
16
17 import com.liferay.portal.dao.orm.common.SQLTransformer;
18 import com.liferay.portal.kernel.dao.orm.CacheMode;
19 import com.liferay.portal.kernel.dao.orm.ORMException;
20 import com.liferay.portal.kernel.dao.orm.Query;
21 import com.liferay.portal.kernel.dao.orm.ScrollableResults;
22 import com.liferay.portal.kernel.util.ListUtil;
23 import com.liferay.portal.kernel.util.StringBundler;
24 import com.liferay.portal.kernel.util.StringPool;
25 import com.liferay.portal.kernel.util.UnmodifiableList;
26
27 import java.io.Serializable;
28
29 import java.sql.Timestamp;
30
31 import java.util.Date;
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Map;
36
37 import javax.persistence.FlushModeType;
38
39
45 public class QueryImpl implements Query {
46
47 public QueryImpl(SessionImpl sessionImpl, String queryString) {
48 this.sessionImpl = sessionImpl;
49 this.queryString = _hqlTojpql(SQLTransformer.transform(queryString));
50 }
51
52 public int executeUpdate() throws ORMException {
53 try {
54 return sessionImpl.executeUpdate(
55 queryString, parameterMap, firstResult, maxResults,
56 flushModeType, sqlQuery, entityClass);
57 }
58 catch (Exception e) {
59 throw ExceptionTranslator.translate(e);
60 }
61 }
62
63 public Iterator<?> iterate() throws ORMException {
64 return iterate(true);
65 }
66
67 public Iterator<?> iterate(boolean unmodifiable) throws ORMException {
68 try {
69 return list(unmodifiable).iterator();
70 }
71 catch (Exception e) {
72 throw ExceptionTranslator.translate(e);
73 }
74 }
75
76 public List<?> list() throws ORMException {
77 return list(true);
78 }
79
80 public List<?> list(boolean unmodifiable) throws ORMException {
81 try {
82 List<?> list = sessionImpl.list(
83 queryString, parameterMap, firstResult, maxResults,
84 flushModeType, sqlQuery, entityClass);
85
86 if (unmodifiable) {
87 return new UnmodifiableList<Object>(list);
88 }
89 else {
90 return ListUtil.copy(list);
91 }
92 }
93 catch (Exception e) {
94 throw ExceptionTranslator.translate(e);
95 }
96 }
97
98 public ScrollableResults scroll() throws ORMException {
99 try {
100 return new ScrollableResultsImpl(list());
101 }
102 catch (Exception e) {
103 throw ExceptionTranslator.translate(e);
104 }
105 }
106
107 public Query setBoolean(int pos, boolean value) {
108 parameterMap.put(pos, value);
109
110 return this;
111 }
112
113 public Query setCacheable(boolean cacheable) {
114 return this;
115 }
116
117 public Query setCacheMode(CacheMode cacheMode) {
118 return this;
119 }
120
121 public Query setCacheRegion(String cacheRegion) {
122 return this;
123 }
124
125 public Query setDouble(int pos, double value) {
126 parameterMap.put(pos, Double.valueOf(value));
127
128 return this;
129 }
130
131 public Query setFirstResult(int firstResult) {
132 this.firstResult = firstResult;
133
134 return this;
135 }
136
137 public Query setFloat(int pos, float value) {
138 parameterMap.put(pos, Float.valueOf(value));
139
140 return this;
141 }
142
143 public Query setFlushMode(FlushModeType flushModeType) {
144 this.flushModeType = flushModeType;
145
146 return this;
147 }
148
149 public Query setInteger(int pos, int value) {
150 parameterMap.put(pos, Integer.valueOf(value));
151
152 return this;
153 }
154
155 public Query setLong(int pos, long value) {
156 parameterMap.put(pos, Long.valueOf(value));
157
158 return this;
159 }
160
161 public Query setMaxResults(int maxResults) {
162 this.maxResults = maxResults;
163
164 return this;
165 }
166
167 public Query setSerializable(int pos, Serializable value) {
168 parameterMap.put(pos, value);
169
170 return this;
171 }
172
173 public Query setShort(int pos, short value) {
174 parameterMap.put(pos, Short.valueOf(value));
175
176 return this;
177 }
178
179 public Query setString(int pos, String value) {
180 parameterMap.put(pos, value);
181
182 return this;
183 }
184
185 public Query setTimestamp(int pos, Timestamp value) {
186 Date date = null;
187
188 if (value != null) {
189 date = new Date(value.getTime());
190 }
191
192 parameterMap.put(pos, date);
193
194 return this;
195 }
196
197 public Object uniqueResult() throws ORMException {
198 try {
199 return sessionImpl.uniqueResult(
200 queryString, parameterMap, firstResult, maxResults,
201 flushModeType, sqlQuery, entityClass);
202 }
203 catch (Exception e) {
204 throw ExceptionTranslator.translate(e);
205 }
206 }
207
208 private String _hqlTojpql(String queryString) {
209 queryString = _transformPositionalParams(queryString);
210
211 queryString = queryString.replaceAll(_HQL_NOT_EQUALS, _JPQL_NOT_EQUALS);
212 queryString = queryString.replaceAll(
213 _HQL_COMPOSITE_ID_MARKER, _JPQL_DOT_SEPARTOR);
214
215 return queryString;
216 }
217
218 private String _transformPositionalParams(String queryString) {
219 if (queryString.indexOf(StringPool.QUESTION) == -1) {
220 return queryString;
221 }
222
223 StringBundler sb = new StringBundler();
224
225 int i = 1;
226 int from = 0;
227 int to = 0;
228
229 while ((to = queryString.indexOf(StringPool.QUESTION, from)) != -1) {
230 sb.append(queryString.substring(from, to));
231 sb.append(StringPool.QUESTION);
232 sb.append(i++);
233
234 from = to + 1;
235 }
236
237 sb.append(queryString.substring(from, queryString.length()));
238
239 return sb.toString();
240 }
241
242 protected Class<?> entityClass = null;
243 protected int firstResult = -1;
244 protected FlushModeType flushModeType = null;
245 protected int maxResults = -1;
246 protected Map<Integer, Object> parameterMap =
247 new HashMap<Integer, Object>();
248 protected String queryString;
249 protected SessionImpl sessionImpl;
250 protected boolean sqlQuery = false;
251
252 private static final String _HQL_COMPOSITE_ID_MARKER = "\\.id\\.";
253
254 private static final String _HQL_NOT_EQUALS = "!=";
255
256 private static final String _JPQL_DOT_SEPARTOR = ".";
257
258 private static final String _JPQL_NOT_EQUALS = "<>";
259
260 }