1 23 24 29 30 package com.sun.jdo.spi.persistence.support.sqlstore.query; 31 32 import java.util.ArrayList ; 33 import java.util.Collection ; 34 import java.util.Map ; 35 import java.util.ResourceBundle ; 36 37 import com.sun.jdo.api.persistence.support.Query; 38 import com.sun.jdo.api.persistence.support.Transaction; 39 import com.sun.jdo.api.persistence.support.JDOException; 40 import com.sun.jdo.api.persistence.support.JDOQueryException; 41 import com.sun.jdo.api.persistence.support.JDOUnsupportedOptionException; 42 import com.sun.jdo.spi.persistence.support.sqlstore.PersistenceManager; 43 import com.sun.jdo.spi.persistence.support.sqlstore.RetrieveDesc; 44 import com.sun.jdo.spi.persistence.utility.I18NHelper; 45 import com.sun.jdo.spi.persistence.utility.logging.Logger; 46 import com.sun.jdo.spi.persistence.support.sqlstore.query.jqlc.JQLC; 47 import com.sun.jdo.spi.persistence.support.sqlstore.query.jqlc.ParameterTable; 48 import com.sun.jdo.spi.persistence.support.sqlstore.ValueFetcher; 49 50 55 public class QueryImpl 56 implements Query 57 { 58 61 private Class candidateClass; 62 63 66 private String filterExpression; 67 68 71 private String importDeclarations; 72 73 76 private String parameterDeclarations; 77 78 81 private String variableDeclarations; 82 83 86 private String orderingSpecification; 87 88 91 private String resultExpression; 92 93 96 private boolean compiled = false; 97 98 101 private transient PersistenceManager pm; 102 103 106 private transient Collection candidateCollection; 107 108 111 private transient boolean ignoreCache; 112 113 116 private transient boolean prefetchEnabled = true; 117 118 121 private transient JQLC jqlc; 122 123 126 private transient ParameterTable paramtab; 127 128 131 private transient boolean createdBySerialization = false; 132 133 136 protected final static ResourceBundle messages = 137 I18NHelper.loadBundle(QueryImpl.class); 138 139 140 private static Logger logger = LogHelperQueryExecute.getLogger(); 141 142 145 public QueryImpl(PersistenceManager pm) 146 { 147 if (logger.isLoggable(Logger.FINER)) 148 logger.finer("LOG_CreateNewQuery", identity()); this.pm = pm; 150 this.paramtab = new ParameterTable(); 151 this.ignoreCache = pm.getPersistenceManagerFactory().getIgnoreCache(); 152 } 153 154 160 public QueryImpl (PersistenceManager pm, Object compiled) 161 { 162 if (logger.isLoggable(Logger.FINER)) 163 logger.finer("LOG_CreateNewQueryFromCompiled", identity(), compiled); this.pm = pm; 165 if (compiled == null) 166 { 167 JDOException ex = new JDOQueryException(I18NHelper.getMessage( 168 messages, "query.queryimpl.init.compiledquery.isnull")); logger.throwing("query.QueryImpl", "<init>", ex); throw ex; 171 } 172 173 if (!(compiled instanceof QueryImpl)) 174 { 175 JDOException ex = new JDOQueryException(I18NHelper.getMessage( 176 messages, "query.queryimpl.init.compiledquery.invalidtype", compiled.getClass().getName())); 178 logger.throwing("query.QueryImpl", "<init>", ex); throw ex; 180 } 181 182 QueryImpl other = (QueryImpl)compiled; 183 this.candidateClass = other.candidateClass; 184 this.filterExpression = other.filterExpression; 185 this.importDeclarations = other.importDeclarations; 186 this.parameterDeclarations = other.parameterDeclarations; 187 this.variableDeclarations = other.variableDeclarations; 188 this.orderingSpecification = other.orderingSpecification; 189 this.resultExpression = other.resultExpression; 190 this.ignoreCache = other.ignoreCache; 191 this.prefetchEnabled = other.prefetchEnabled; 192 this.candidateCollection = null; 193 194 if (other.paramtab != null) 196 { 197 this.jqlc = other.jqlc; 198 this.paramtab = new ParameterTable(other.paramtab); 199 this.compiled = other.compiled; 200 } 201 else 202 { 203 this.jqlc = null; 207 this.paramtab = new ParameterTable(); 208 this.compiled = false; 209 } 210 } 211 212 216 public QueryImpl(PersistenceManager pm, Class candidateClass) 217 { 218 this(pm); 219 setClass(candidateClass); 220 } 221 222 228 public QueryImpl(PersistenceManager pm, Class candidateClass, Collection candidateCollection) 229 { 230 this(pm); 231 setClass(candidateClass); 232 setCandidates(candidateCollection); 233 } 234 235 241 public QueryImpl(PersistenceManager pm, Class candidateClass, String filter) 242 { 243 this(pm); 244 setClass(candidateClass); 245 setFilter(filter); 246 } 247 248 255 public QueryImpl(PersistenceManager pm, Class candidateClass, Collection candidateCollection, String filter) 256 { 257 this(pm); 258 setClass(candidateClass); 259 setCandidates(candidateCollection); 260 setFilter(filter); 261 } 262 263 271 public void setClass(Class candidateClass) 272 { 273 synchronized (this.paramtab) 274 { 275 this.candidateClass = candidateClass; 276 this.compiled = false; 277 } 278 } 279 280 285 public void setCandidates(Collection candidateCollection) 286 { 287 synchronized (this.paramtab) 288 { 289 this.candidateCollection = candidateCollection; 290 } 293 } 294 295 303 public void setFilter(String filter) 304 { 305 synchronized (this.paramtab) 306 { 307 this.filterExpression = filter; 308 this.compiled = false; 309 } 310 } 311 312 324 public void declareImports(String imports) 325 { 326 synchronized (this.paramtab) 327 { 328 this.importDeclarations = imports; 329 this.compiled = false; 330 } 331 } 332 333 346 public void declareParameters(String parameters) 347 { 348 synchronized (this.paramtab) 349 { 350 this.parameterDeclarations = parameters; 351 this.compiled = false; 352 } 353 } 354 355 367 public void declareVariables(String variables) 368 { 369 synchronized (this.paramtab) 370 { 371 this.variableDeclarations = variables; 372 this.compiled = false; 373 } 374 } 375 376 382 public void setOrdering(String ordering) 383 { 384 synchronized (this.paramtab) 385 { 386 this.orderingSpecification = ordering; 387 this.compiled = false; 388 } 389 } 390 391 400 public void setResult(String result) 401 { 402 synchronized (this.paramtab) 403 { 404 this.resultExpression = result; 405 this.compiled = false; 406 } 407 } 408 409 416 public void setIgnoreCache(boolean ignoreCache) 417 { 418 synchronized (this.paramtab) 419 { 420 this.ignoreCache = ignoreCache; 421 } 422 } 423 424 429 public boolean getIgnoreCache() 430 { 431 return ignoreCache; 432 } 433 434 444 public void setPrefetchEnabled(boolean prefetchEnabled) 445 { 446 synchronized (this.paramtab) 447 { 448 this.prefetchEnabled = prefetchEnabled; 449 this.compiled = false; 450 } 451 } 452 453 457 public void compile() 458 { 459 synchronized (this.paramtab) 460 { 461 if (!this.compiled) 462 { 463 if (logger.isLoggable(Logger.FINER)) 464 logger.finer("LOG_CompileQuery", this); jqlc = new JQLC(); 467 jqlc.setClass(candidateClass); 469 jqlc.declareImports(importDeclarations); 470 jqlc.declareParameters(parameterDeclarations); 471 jqlc.declareVariables(variableDeclarations); 472 jqlc.setOrdering(orderingSpecification); 473 jqlc.setResult(resultExpression); 474 jqlc.setFilter(filterExpression); 475 jqlc.setPrefetchEnabled(prefetchEnabled); 476 477 jqlc.semanticCheck(paramtab); 479 this.compiled = true; 480 } 481 } 482 } 483 484 489 public Object execute() 490 { 491 synchronized (this.paramtab) 492 { 493 compile(); 494 ParameterTable params = new ParameterTable(paramtab); 495 params.initValueHandling(); 496 params.checkUnboundParams(); 497 return doExecute(params); 498 } 499 } 500 501 507 public Object execute(Object p1) 508 { 509 Object [] params = new Object [1]; 510 params[0] = p1; 511 return executeWithArray(params); 512 } 513 514 521 public Object execute(Object p1, Object p2) 522 { 523 Object [] params = new Object [2]; 524 params[0] = p1; 525 params[1] = p2; 526 return executeWithArray(params); 527 } 528 529 537 public Object execute(Object p1, Object p2, Object p3) 538 { 539 Object [] params = new Object [3]; 540 params[0] = p1; 541 params[1] = p2; 542 params[2] = p3; 543 return executeWithArray(params); 544 } 545 546 552 public Object executeWithMap (Map parameters) 553 { 554 synchronized (this.paramtab) 555 { 556 compile(); 557 ParameterTable params = new ParameterTable(paramtab); 558 params.initValueHandling(); 559 params.setValues(parameters); 560 params.checkUnboundParams(); 561 return doExecute(params); 562 } 563 } 564 565 585 public Object executeWithArray (Object [] parameters) 586 { 587 synchronized (this.paramtab) 588 { 589 compile(); 590 ParameterTable params = new ParameterTable(paramtab); 591 params.initValueHandling(); 592 params.setValues(parameters); 593 params.checkUnboundParams(); 594 return doExecute(params); 595 } 596 597 } 598 599 605 public com.sun.jdo.api.persistence.support.PersistenceManager getPersistenceManager() 606 { 607 return (pm == null)? null : pm.getCurrentWrapper(); 608 } 609 610 619 public void clearPersistenceManager() 620 { 621 this.pm = null; 622 this.candidateCollection = null; 623 } 624 625 632 private Object doExecute(ParameterTable params) 633 { 634 Object result = null; 635 RetrieveDesc rd = null; 636 637 try 638 { 639 646 pm.acquireExclusiveLock(); 651 } 652 catch (NullPointerException npe) 653 { 654 String key = (createdBySerialization ? 656 "query.queryimpl.doexecute.notboundtopm" : "query.queryimpl.doexecute.pmclosed"); JDOException ex = new JDOQueryException( 659 I18NHelper.getMessage(messages, key)); 660 logger.throwing("query.QueryImpl", "compile", ex); throw ex; 662 } 663 664 try 665 { 666 checkCandidates(); 667 rd = jqlc.codeGen(pm, params); 669 flush(); 671 if (logger.isLoggable(Logger.FINER)) 672 logger.finer("LOG_ExecuteQuery", this, params.getValues()); result = (rd != null) ? pm.retrieve(rd, params.getValueFetcher()) : new ArrayList (); 677 } 678 finally 679 { 680 pm.releaseExclusiveLock(); 684 } 685 686 return result; 687 } 688 689 692 private void checkCandidates() 693 { 694 if ((candidateCollection == null) && (candidateClass != null)) 695 { 696 candidateCollection = pm.getExtent(candidateClass, false); 701 } 702 else { 703 jqlc.checkCandidates(candidateClass, candidateCollection); 704 } 705 } 706 707 710 private void flush() 711 { 712 Transaction tx = pm.currentTransaction(); 713 if ((tx != null) && tx.isActive() && 718 !tx.getOptimistic() && !this.ignoreCache) 719 { 720 pm.internalFlush(); 721 } 722 } 723 724 725 public String toString() 726 { 727 StringBuffer repr = new StringBuffer (); 728 repr.append("QueryImpl("); repr.append("candidateClass: "); repr.append(candidateClass); 731 if (importDeclarations != null) { 732 repr.append(", imports: "); repr.append(importDeclarations); 734 } 735 if (parameterDeclarations != null) { 736 repr.append(", parameters: "); repr.append(parameterDeclarations); 738 } 739 if (variableDeclarations != null) { 740 repr.append(", variables: "); repr.append(variableDeclarations); 742 } 743 if (filterExpression != null) { 744 repr.append(", filter: "); repr.append(filterExpression); 746 } 747 if (orderingSpecification != null) { 748 repr.append(", ordering: "); repr.append(orderingSpecification); 750 } 751 if (resultExpression != null) { 752 repr.append(", result: "); repr.append(resultExpression); 754 } 755 repr.append(", prefetchEnabled: "); repr.append(prefetchEnabled); 757 repr.append(", identity: "); repr.append(identity()); 759 repr.append(")"); return repr.toString(); 761 } 762 763 764 private String identity() 765 { 766 return "QueryImpl@" + System.identityHashCode(this); } 768 769 770 774 private void readObject(java.io.ObjectInputStream in) 775 throws java.io.IOException , ClassNotFoundException 776 { 777 in.defaultReadObject(); 778 this.paramtab = new ParameterTable(); 779 this.createdBySerialization = true; 780 } 781 782 } 783 | Popular Tags |