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.ORMException;
020 import com.liferay.portal.kernel.dao.orm.Query;
021 import com.liferay.portal.kernel.dao.orm.ScrollableResults;
022 import com.liferay.portal.kernel.util.CharPool;
023 import com.liferay.portal.kernel.util.ListUtil;
024 import com.liferay.portal.kernel.util.StringBundler;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.UnmodifiableList;
027
028 import java.io.Serializable;
029
030 import java.sql.Timestamp;
031
032 import java.util.Date;
033 import java.util.HashMap;
034 import java.util.Iterator;
035 import java.util.List;
036 import java.util.Map;
037
038 import javax.persistence.FlushModeType;
039
040
044 public class QueryImpl implements Query {
045
046 public QueryImpl(SessionImpl sessionImpl, String queryString) {
047 this.sessionImpl = sessionImpl;
048 this.queryString = _hqlTojpql(SQLTransformer.transform(queryString));
049 }
050
051 public int executeUpdate() throws ORMException {
052 try {
053 return sessionImpl.executeUpdate(
054 queryString, parameterMap, firstResult, maxResults,
055 flushModeType, sqlQuery, entityClass);
056 }
057 catch (Exception e) {
058 throw ExceptionTranslator.translate(e);
059 }
060 }
061
062 public Iterator<?> iterate() throws ORMException {
063 return iterate(true);
064 }
065
066 public Iterator<?> iterate(boolean unmodifiable) throws ORMException {
067 try {
068 return list(unmodifiable).iterator();
069 }
070 catch (Exception e) {
071 throw ExceptionTranslator.translate(e);
072 }
073 }
074
075 public List<?> list() throws ORMException {
076 return list(true);
077 }
078
079 public List<?> list(boolean unmodifiable) throws ORMException {
080 try {
081 List<?> list = sessionImpl.list(
082 queryString, parameterMap, firstResult, maxResults,
083 flushModeType, sqlQuery, entityClass);
084
085 if (unmodifiable) {
086 return new UnmodifiableList<Object>(list);
087 }
088 else {
089 return ListUtil.copy(list);
090 }
091 }
092 catch (Exception e) {
093 throw ExceptionTranslator.translate(e);
094 }
095 }
096
097 public ScrollableResults scroll() throws ORMException {
098 try {
099 return new ScrollableResultsImpl(list());
100 }
101 catch (Exception e) {
102 throw ExceptionTranslator.translate(e);
103 }
104 }
105
106 public Query setBoolean(int pos, boolean value) {
107 parameterMap.put(pos, value);
108
109 return this;
110 }
111
112 public Query setCacheable(boolean cacheable) {
113 return this;
114 }
115
116 public Query setCacheMode(CacheMode cacheMode) {
117 return this;
118 }
119
120 public Query setCacheRegion(String cacheRegion) {
121 return this;
122 }
123
124 public Query setDouble(int pos, double value) {
125 parameterMap.put(pos, Double.valueOf(value));
126
127 return this;
128 }
129
130 public Query setFirstResult(int firstResult) {
131 this.firstResult = firstResult;
132
133 return this;
134 }
135
136 public Query setFloat(int pos, float value) {
137 parameterMap.put(pos, Float.valueOf(value));
138
139 return this;
140 }
141
142 public Query setFlushMode(FlushModeType flushModeType) {
143 this.flushModeType = flushModeType;
144
145 return this;
146 }
147
148 public Query setInteger(int pos, int value) {
149 parameterMap.put(pos, Integer.valueOf(value));
150
151 return this;
152 }
153
154 public Query setLong(int pos, long value) {
155 parameterMap.put(pos, Long.valueOf(value));
156
157 return this;
158 }
159
160 public Query setMaxResults(int maxResults) {
161 this.maxResults = maxResults;
162
163 return this;
164 }
165
166 public Query setSerializable(int pos, Serializable value) {
167 parameterMap.put(pos, value);
168
169 return this;
170 }
171
172 public Query setShort(int pos, short value) {
173 parameterMap.put(pos, Short.valueOf(value));
174
175 return this;
176 }
177
178 public Query setString(int pos, String value) {
179 parameterMap.put(pos, value);
180
181 return this;
182 }
183
184 public Query setTimestamp(int pos, Timestamp value) {
185 Date date = null;
186
187 if (value != null) {
188 date = new Date(value.getTime());
189 }
190
191 parameterMap.put(pos, date);
192
193 return this;
194 }
195
196 public Object uniqueResult() throws ORMException {
197 try {
198 return sessionImpl.uniqueResult(
199 queryString, parameterMap, firstResult, maxResults,
200 flushModeType, sqlQuery, entityClass);
201 }
202 catch (Exception e) {
203 throw ExceptionTranslator.translate(e);
204 }
205 }
206
207 private String _hqlTojpql(String queryString) {
208 queryString = _transformPositionalParams(queryString);
209
210 queryString = queryString.replaceAll(_HQL_NOT_EQUALS, _JPQL_NOT_EQUALS);
211 queryString = queryString.replaceAll(
212 _HQL_COMPOSITE_ID_MARKER, _JPQL_DOT_SEPARTOR);
213
214 return queryString;
215 }
216
217 private String _transformPositionalParams(String queryString) {
218 if (queryString.indexOf(CharPool.QUESTION) == -1) {
219 return queryString;
220 }
221
222 StringBundler sb = new StringBundler();
223
224 int i = 1;
225 int from = 0;
226 int to = 0;
227
228 while ((to = queryString.indexOf(CharPool.QUESTION, from)) != -1) {
229 sb.append(queryString.substring(from, to));
230 sb.append(StringPool.QUESTION);
231 sb.append(i++);
232
233 from = to + 1;
234 }
235
236 sb.append(queryString.substring(from, queryString.length()));
237
238 return sb.toString();
239 }
240
241 protected Class<?> entityClass = null;
242 protected int firstResult = -1;
243 protected FlushModeType flushModeType = null;
244 protected int maxResults = -1;
245 protected Map<Integer, Object> parameterMap =
246 new HashMap<Integer, Object>();
247 protected String queryString;
248 protected SessionImpl sessionImpl;
249 protected boolean sqlQuery = false;
250
251 private static final String _HQL_COMPOSITE_ID_MARKER = "\\.id\\.";
252
253 private static final String _HQL_NOT_EQUALS = "!=";
254
255 private static final String _JPQL_DOT_SEPARTOR = ".";
256
257 private static final String _JPQL_NOT_EQUALS = "<>";
258
259 }