1 19 package org.apache.cayenne.jpa; 20 21 import java.util.Calendar ; 22 import java.util.Date ; 23 import java.util.HashMap ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 import javax.persistence.FlushModeType; 28 import javax.persistence.NoResultException; 29 import javax.persistence.NonUniqueResultException; 30 import javax.persistence.Query; 31 import javax.persistence.TemporalType; 32 import javax.persistence.TransactionRequiredException; 33 34 import org.apache.cayenne.ObjectContext; 35 import org.apache.cayenne.QueryResponse; 36 import org.apache.cayenne.query.ParameterizedQuery; 37 import org.apache.cayenne.query.ProcedureQuery; 38 import org.apache.cayenne.query.SQLTemplate; 39 import org.apache.cayenne.query.SelectQuery; 40 41 44 public class JpaQuery implements Query { 45 46 protected Map <String , Object > parameters = new HashMap <String , Object >(); 47 protected org.apache.cayenne.query.Query cayenneQuery; 48 protected ObjectContext context; 49 50 public JpaQuery(ObjectContext ctxt) { 51 this.context = ctxt; 52 } 53 54 57 public JpaQuery(ObjectContext context, String name) { 58 this(context); 59 60 org.apache.cayenne.query.Query q = context.getEntityResolver().lookupQuery(name); 61 62 if (q == null) { 63 throw new IllegalArgumentException ("Non-existing query: " + name); 64 } 65 66 setQuery(q); 67 } 68 69 protected void setQuery(org.apache.cayenne.query.Query q) { 70 this.cayenneQuery = q; 71 } 72 73 protected org.apache.cayenne.query.Query getQuery() { 74 return cayenneQuery; 75 } 76 77 80 private org.apache.cayenne.query.Query queryWithParameters() { 81 if (parameters.size() == 0) { 82 return cayenneQuery; 83 } 84 85 return ((ParameterizedQuery) cayenneQuery).createQuery(parameters); 86 } 87 88 94 public List getResultList() { 95 return context.performQuery(queryWithParameters()); 96 } 97 98 105 public int executeUpdate() { 106 108 QueryResponse response = context.performGenericQuery(queryWithParameters()); 109 int[] res = response.firstUpdateCount(); 110 111 if (res == null) { 112 return -1; 113 } 114 115 int num = 0; 116 for (int i = 0; i < res.length; i++) { 117 num = num + res[i]; 118 } 119 return num; 120 } 121 122 130 public Object getSingleResult() { 131 List rows = getResultList(); 132 if (rows.size() == 0) { 133 throw new NoResultException(); 134 } 135 if (rows.size() > 1) { 136 throw new NonUniqueResultException(); 137 } 138 139 return rows.get(0); 140 } 141 142 149 public Query setMaxResults(int maxResult) { 150 if (maxResult < 0) { 151 throw new IllegalArgumentException ("Invalid max results value: " + maxResult); 152 } 153 154 if (getQuery() instanceof SelectQuery) { 156 ((SelectQuery) getQuery()).setFetchLimit(maxResult); 157 } 158 else if (getQuery() instanceof SQLTemplate) { 159 ((SQLTemplate) getQuery()).setFetchLimit(maxResult); 160 } 161 else if (getQuery() instanceof ProcedureQuery) { 162 ((ProcedureQuery) getQuery()).setFetchLimit(maxResult); 163 } 164 165 throw new IllegalArgumentException ("query does not support maxResult"); 166 } 167 168 public Query setFlushMode(FlushModeType flushModeType) { 169 return this; 170 } 171 172 182 public Query setHint(String hintName, Object value) { 183 return this; 184 } 185 186 193 public Query setFirstResult(int startPosition) { 194 if (startPosition < 0) { 195 throw new IllegalArgumentException ("Invalid first result value: " 196 + startPosition); 197 } 198 throw new UnsupportedOperationException ("TODO"); 201 } 202 203 212 public Query setParameter(String name, Object value) { 213 if (!(cayenneQuery instanceof ParameterizedQuery)) { 214 throw new IllegalArgumentException ("query does not accept parameters"); 215 } 216 217 220 parameters.put(name, value); 221 return this; 222 } 223 224 234 public Query setParameter(String name, Date value, TemporalType temporalType) { 235 return setParameter(name, value); 237 } 238 239 249 public Query setParameter(String name, Calendar value, TemporalType temporalType) { 250 return setParameter(name, value); 252 } 253 254 263 public Query setParameter(int position, Object value) { 264 throw new UnsupportedOperationException ("TODO"); 266 } 267 268 278 public Query setParameter(int position, Date value, TemporalType temporalType) { 279 return setParameter(position, value); 281 } 282 283 293 public Query setParameter(int position, Calendar value, TemporalType temporalType) { 294 return setParameter(position, value); 296 } 297 298 } 299 | Popular Tags |