1 56 57 package org.objectstyle.cayenne.query; 58 59 import java.util.ArrayList ; 60 import java.util.Collection ; 61 import java.util.Collections ; 62 import java.util.HashMap ; 63 import java.util.List ; 64 import java.util.Map ; 65 66 import org.objectstyle.cayenne.CayenneRuntimeException; 67 import org.objectstyle.cayenne.access.jdbc.ColumnDescriptor; 68 import org.objectstyle.cayenne.access.jdbc.RowDescriptor; 69 import org.objectstyle.cayenne.map.Procedure; 70 import org.objectstyle.cayenne.map.QueryBuilder; 71 import org.objectstyle.cayenne.util.XMLEncoder; 72 import org.objectstyle.cayenne.util.XMLSerializable; 73 74 100 public class ProcedureQuery extends AbstractQuery implements GenericSelectQuery, 101 ParameterizedQuery, XMLSerializable { 102 103 108 protected String resultClassName; 109 110 protected Map parameters = new HashMap (); 111 protected SelectExecutionProperties selectProperties = new SelectExecutionProperties(); 112 protected boolean selecting; 113 114 119 protected List resultDescriptors; 120 121 124 public ProcedureQuery() { 125 selectProperties.setFetchingDataRows(true); 127 } 128 129 132 public ProcedureQuery(Procedure procedure) { 133 selectProperties.setFetchingDataRows(true); 135 setRoot(procedure); 136 } 137 138 151 public ProcedureQuery(String procedureName) { 152 selectProperties.setFetchingDataRows(true); 154 155 setRoot(procedureName); 156 } 157 158 161 public ProcedureQuery(Procedure procedure, Class resultType) { 162 setRoot(procedure); 163 setResultClassName(resultType != null ? resultType.getName() : null); 164 } 165 166 177 public ProcedureQuery(String procedureName, Class resultType) { 178 setRoot(procedureName); 179 setResultClassName(resultType != null ? resultType.getName() : null); 180 } 181 182 193 public List getResultDescriptors() { 194 return resultDescriptors != null ? resultDescriptors : Collections.EMPTY_LIST; 195 } 196 197 204 public synchronized void addResultDescriptor(ColumnDescriptor[] descriptor) { 205 if (resultDescriptors == null) { 206 resultDescriptors = new ArrayList (2); 207 } 208 209 resultDescriptors.add(descriptor); 210 } 211 212 217 public void removeResultDescriptor(ColumnDescriptor[] descriptor) { 218 if (resultDescriptors != null) { 219 resultDescriptors.remove(descriptor); 220 } 221 } 222 223 228 public SQLAction createSQLAction(SQLActionVisitor visitor) { 229 return visitor.procedureAction(this); 230 } 231 232 237 public void initWithProperties(Map properties) { 238 239 if (properties == null) { 241 properties = Collections.EMPTY_MAP; 242 } 243 244 selectProperties.initWithProperties(properties); 245 } 246 247 252 public void encodeAsXML(XMLEncoder encoder) { 253 encoder.print("<query name=\""); 254 encoder.print(getName()); 255 encoder.print("\" factory=\""); 256 encoder.print("org.objectstyle.cayenne.map.ProcedureQueryBuilder"); 257 258 encoder.print("\" root=\""); 259 encoder.print(QueryBuilder.PROCEDURE_ROOT); 260 261 String rootString = null; 262 263 if (root instanceof String ) { 264 rootString = root.toString(); 265 } 266 else if (root instanceof Procedure) { 267 rootString = ((Procedure) root).getName(); 268 } 269 270 if (rootString != null) { 271 encoder.print("\" root-name=\""); 272 encoder.print(rootString); 273 } 274 275 if (resultClassName != null) { 276 encoder.print("\" result-type=\""); 277 encoder.print(resultClassName); 278 } 279 280 if (!selecting) { 281 encoder.print("\" selecting=\"false"); 282 } 283 284 encoder.println("\">"); 285 286 encoder.indent(1); 287 288 selectProperties.encodeAsXML(encoder); 289 290 encoder.indent(-1); 291 encoder.println("</query>"); 292 } 293 294 300 public Query createQuery(Map parameters) { 301 ProcedureQuery query = new ProcedureQuery(); 303 304 if (root != null) { 305 query.setRoot(root); 306 } 307 308 query.setLoggingLevel(logLevel); 309 query.setResultClassName(resultClassName); 310 311 selectProperties.copyToProperties(query.selectProperties); 312 query.setParameters(parameters); 313 314 return query; 318 } 319 320 public String getCachePolicy() { 321 return selectProperties.getCachePolicy(); 322 } 323 324 public void setCachePolicy(String policy) { 325 this.selectProperties.setCachePolicy(policy); 326 } 327 328 public int getFetchLimit() { 329 return selectProperties.getFetchLimit(); 330 } 331 332 public void setFetchLimit(int fetchLimit) { 333 this.selectProperties.setFetchLimit(fetchLimit); 334 } 335 336 public int getPageSize() { 337 return selectProperties.getPageSize(); 338 } 339 340 public void setPageSize(int pageSize) { 341 selectProperties.setPageSize(pageSize); 342 } 343 344 public void setFetchingDataRows(boolean flag) { 345 selectProperties.setFetchingDataRows(flag); 346 } 347 348 public boolean isFetchingDataRows() { 349 return selectProperties.isFetchingDataRows(); 350 } 351 352 public boolean isRefreshingObjects() { 353 return selectProperties.isRefreshingObjects(); 354 } 355 356 public void setRefreshingObjects(boolean flag) { 357 selectProperties.setRefreshingObjects(flag); 358 } 359 360 public boolean isResolvingInherited() { 361 return selectProperties.isResolvingInherited(); 362 } 363 364 public void setResolvingInherited(boolean b) { 365 selectProperties.setResolvingInherited(b); 366 } 367 368 373 public synchronized void addParameter(String name, Object value) { 374 parameters.put(name, value); 375 } 376 377 380 public synchronized void removeParameter(String name) { 381 parameters.remove(name); 382 } 383 384 389 public Map getParameters() { 390 return parameters; 391 } 392 393 398 public synchronized void setParameters(Map parameters) { 399 this.parameters.clear(); 400 401 if (parameters != null) { 402 this.parameters.putAll(parameters); 403 } 404 } 405 406 411 public synchronized void clearParameters() { 412 this.parameters.clear(); 413 } 414 415 420 public String getResultClassName() { 421 return resultClassName; 422 } 423 424 430 public Class getResultClass(ClassLoader classLoader) { 431 if (this.getResultClassName() == null) { 432 return null; 433 } 434 435 try { 436 if (classLoader == null) { 438 return Class.forName(this.getResultClassName()); 439 } 440 else { 441 return classLoader.loadClass(this.getResultClassName()); 442 } 443 } 444 catch (ClassNotFoundException e) { 445 throw new CayenneRuntimeException("Failed to load class for name '" 446 + this.getResultClassName() 447 + "': " 448 + e.getMessage(), e); 449 } 450 } 451 452 458 public void setResultClassName(String resultClassName) { 459 this.resultClassName = resultClassName; 460 } 461 462 467 public boolean isSelecting() { 468 return selecting; 469 } 470 471 476 public void setSelecting(boolean b) { 477 selecting = b; 478 } 479 480 485 public Collection getJointPrefetches() { 486 return selectProperties.getJointPrefetches(); 487 } 488 489 494 public void addJointPrefetch(String relationshipPath) { 495 selectProperties.addJointPrefetch(relationshipPath); 496 } 497 498 503 public void addJointPrefetches(Collection relationshipPaths) { 504 selectProperties.addJointPrefetches(relationshipPaths); 505 } 506 507 512 public void clearJointPrefetches() { 513 selectProperties.clearJointPrefetches(); 514 } 515 516 521 public void removeJointPrefetch(String relationshipPath) { 522 selectProperties.removeJointPrefetch(relationshipPath); 523 } 524 } | Popular Tags |