1 56 57 package org.objectstyle.cayenne.query; 58 59 import java.util.Collection ; 60 import java.util.Collections ; 61 import java.util.HashMap ; 62 import java.util.Iterator ; 63 import java.util.Map ; 64 65 import org.apache.commons.collections.IteratorUtils; 66 import org.apache.commons.collections.Transformer; 67 import org.objectstyle.cayenne.map.DataMap; 68 import org.objectstyle.cayenne.map.DbEntity; 69 import org.objectstyle.cayenne.map.ObjEntity; 70 import org.objectstyle.cayenne.map.Procedure; 71 import org.objectstyle.cayenne.map.QueryBuilder; 72 import org.objectstyle.cayenne.util.XMLEncoder; 73 import org.objectstyle.cayenne.util.XMLSerializable; 74 75 116 public class SQLTemplate extends AbstractQuery implements GenericSelectQuery, 117 ParameterizedQuery, XMLSerializable { 118 119 private static final Transformer nullMapTransformer = new Transformer() { 120 121 public Object transform(Object input) { 122 return (input != null) ? input : Collections.EMPTY_MAP; 123 } 124 }; 125 126 protected SelectExecutionProperties selectProperties = new SelectExecutionProperties(); 127 protected String defaultTemplate; 128 protected Map templates; 129 protected Map [] parameters; 130 protected boolean selecting; 131 132 135 public SQLTemplate(boolean selecting) { 136 setSelecting(selecting); 137 } 138 139 public SQLTemplate(DataMap rootMap, String defaultTemplate, boolean selecting) { 140 setDefaultTemplate(defaultTemplate); 141 setSelecting(selecting); 142 setRoot(rootMap); 143 } 144 145 public SQLTemplate(ObjEntity rootEntity, String defaultTemplate, boolean selecting) { 146 setDefaultTemplate(defaultTemplate); 147 setSelecting(selecting); 148 setRoot(rootEntity); 149 } 150 151 public SQLTemplate(Class rootClass, String defaultTemplate, boolean selecting) { 152 setDefaultTemplate(defaultTemplate); 153 setSelecting(selecting); 154 setRoot(rootClass); 155 } 156 157 public SQLTemplate(DbEntity rootEntity, String defaultTemplate, boolean selecting) { 158 setDefaultTemplate(defaultTemplate); 159 setSelecting(selecting); 160 setRoot(rootEntity); 161 } 162 163 public SQLTemplate(String objEntityName, String defaultTemplate, boolean selecting) { 164 setSelecting(selecting); 165 setRoot(objEntityName); 166 setDefaultTemplate(defaultTemplate); 167 } 168 169 174 public SQLAction createSQLAction(SQLActionVisitor visitor) { 175 return visitor.sqlAction(this); 176 } 177 178 183 public void encodeAsXML(XMLEncoder encoder) { 184 encoder.print("<query name=\""); 185 encoder.print(getName()); 186 encoder.print("\" factory=\""); 187 encoder.print("org.objectstyle.cayenne.map.SQLTemplateBuilder"); 188 189 String rootString = null; 190 String rootType = null; 191 192 if (root instanceof String ) { 193 rootType = QueryBuilder.OBJ_ENTITY_ROOT; 194 rootString = root.toString(); 195 } 196 else if (root instanceof ObjEntity) { 197 rootType = QueryBuilder.OBJ_ENTITY_ROOT; 198 rootString = ((ObjEntity) root).getName(); 199 } 200 else if (root instanceof DbEntity) { 201 rootType = QueryBuilder.DB_ENTITY_ROOT; 202 rootString = ((DbEntity) root).getName(); 203 } 204 else if (root instanceof Procedure) { 205 rootType = QueryBuilder.PROCEDURE_ROOT; 206 rootString = ((Procedure) root).getName(); 207 } 208 else if (root instanceof Class ) { 209 rootType = QueryBuilder.JAVA_CLASS_ROOT; 210 rootString = ((Class ) root).getName(); 211 } 212 else if (root instanceof DataMap) { 213 rootType = QueryBuilder.DATA_MAP_ROOT; 214 rootString = ((DataMap) root).getName(); 215 } 216 217 if (rootType != null) { 218 encoder.print("\" root=\""); 219 encoder.print(rootType); 220 encoder.print("\" root-name=\""); 221 encoder.print(rootString); 222 } 223 224 if (!selecting) { 225 encoder.print("\" selecting=\"false"); 226 } 227 228 encoder.println("\">"); 229 230 encoder.indent(1); 231 232 selectProperties.encodeAsXML(encoder); 233 234 if (defaultTemplate != null) { 236 encoder.print("<sql><![CDATA["); 237 encoder.print(defaultTemplate); 238 encoder.println("]]></sql>"); 239 } 240 241 if (templates != null && !templates.isEmpty()) { 243 Iterator it = templates.entrySet().iterator(); 244 while (it.hasNext()) { 245 Map.Entry entry = (Map.Entry ) it.next(); 246 Object key = entry.getKey(); 247 Object value = entry.getValue(); 248 249 if (key != null && value != null) { 250 String sql = value.toString().trim(); 251 if (sql.length() > 0) { 252 encoder.print("<sql adapter-class=\""); 253 encoder.print(key.toString()); 254 encoder.print("\"><![CDATA["); 255 encoder.print(sql); 256 encoder.println("]]></sql>"); 257 } 258 } 259 } 260 } 261 262 264 encoder.indent(-1); 265 encoder.println("</query>"); 266 } 267 268 273 public void initWithProperties(Map properties) { 274 275 if (properties == null) { 277 properties = Collections.EMPTY_MAP; 278 } 279 280 selectProperties.initWithProperties(properties); 281 } 282 283 287 public Iterator parametersIterator() { 288 return (parameters == null || parameters.length == 0) ? IteratorUtils 289 .emptyIterator() : IteratorUtils.transformedIterator(IteratorUtils 290 .arrayIterator(parameters), nullMapTransformer); 291 } 292 293 296 public int parametersSize() { 297 return (parameters != null) ? parameters.length : 0; 298 } 299 300 304 public SQLTemplate queryWithParameters(Map parameters) { 305 return queryWithParameters(new Map [] { 306 parameters 307 }); 308 } 309 310 314 public SQLTemplate queryWithParameters(Map [] parameters) { 315 SQLTemplate query = new SQLTemplate(isSelecting()); 317 318 query.setLoggingLevel(logLevel); 319 query.setRoot(root); 320 query.setDefaultTemplate(getDefaultTemplate()); 321 322 if (templates != null) { 323 query.templates = new HashMap (templates); 324 } 325 326 selectProperties.copyToProperties(query.selectProperties); 327 query.setParameters(parameters); 328 329 333 return query; 334 } 335 336 342 public Query createQuery(Map parameters) { 343 return queryWithParameters(parameters); 344 } 345 346 public String getCachePolicy() { 347 return selectProperties.getCachePolicy(); 348 } 349 350 public void setCachePolicy(String policy) { 351 this.selectProperties.setCachePolicy(policy); 352 } 353 354 public int getFetchLimit() { 355 return selectProperties.getFetchLimit(); 356 } 357 358 public void setFetchLimit(int fetchLimit) { 359 this.selectProperties.setFetchLimit(fetchLimit); 360 } 361 362 public int getPageSize() { 363 return selectProperties.getPageSize(); 364 } 365 366 public void setPageSize(int pageSize) { 367 selectProperties.setPageSize(pageSize); 368 } 369 370 public void setFetchingDataRows(boolean flag) { 371 selectProperties.setFetchingDataRows(flag); 372 } 373 374 public boolean isFetchingDataRows() { 375 return selectProperties.isFetchingDataRows(); 376 } 377 378 public boolean isRefreshingObjects() { 379 return selectProperties.isRefreshingObjects(); 380 } 381 382 public void setRefreshingObjects(boolean flag) { 383 selectProperties.setRefreshingObjects(flag); 384 } 385 386 public boolean isResolvingInherited() { 387 return selectProperties.isResolvingInherited(); 388 } 389 390 public void setResolvingInherited(boolean b) { 391 selectProperties.setResolvingInherited(b); 392 } 393 394 397 public String getDefaultTemplate() { 398 return defaultTemplate; 399 } 400 401 404 public void setDefaultTemplate(String string) { 405 defaultTemplate = string; 406 } 407 408 412 public synchronized String getTemplate(String key) { 413 if (templates == null) { 414 return defaultTemplate; 415 } 416 417 String template = (String ) templates.get(key); 418 return (template != null) ? template : defaultTemplate; 419 } 420 421 426 public synchronized String getCustomTemplate(String key) { 427 return (templates != null) ? (String ) templates.get(key) : null; 428 } 429 430 435 public synchronized void setTemplate(String key, String template) { 436 if (templates == null) { 437 templates = new HashMap (); 438 } 439 440 templates.put(key, template); 441 } 442 443 public synchronized void removeTemplate(String key) { 444 if (templates != null) { 445 templates.remove(key); 446 } 447 } 448 449 452 public synchronized Collection getTemplateKeys() { 453 return (templates != null) ? Collections.unmodifiableCollection(templates 454 .keySet()) : Collections.EMPTY_LIST; 455 } 456 457 461 public Map getParameters() { 462 Map map = (parameters != null && parameters.length > 0) ? parameters[0] : null; 463 return (map != null) ? map : Collections.EMPTY_MAP; 464 } 465 466 471 public void setParameters(Map map) { 472 setParameters(map != null ? new Map [] { 473 map 474 } : null); 475 } 476 477 public void setParameters(Map [] parameters) { 478 this.parameters = parameters; 479 } 480 481 484 public boolean isSelecting() { 485 return selecting; 486 } 487 488 491 public void setSelecting(boolean b) { 492 selecting = b; 493 } 494 495 500 public Collection getJointPrefetches() { 501 return selectProperties.getJointPrefetches(); 502 } 503 504 509 public void addJointPrefetch(String relationshipPath) { 510 selectProperties.addJointPrefetch(relationshipPath); 511 } 512 513 518 public void addJointPrefetches(Collection relationshipPaths) { 519 selectProperties.addJointPrefetches(relationshipPaths); 520 } 521 522 527 public void clearJointPrefetches() { 528 selectProperties.clearJointPrefetches(); 529 } 530 531 536 public void removeJointPrefetch(String relationshipPath) { 537 selectProperties.removeJointPrefetch(relationshipPath); 538 } 539 } | Popular Tags |