1 23 24 29 30 package com.sun.jdo.spi.persistence.support.sqlstore.query.jqlc; 31 32 import java.io.Reader ; 33 import java.io.StringReader ; 34 import java.util.*; 35 36 import persistence.antlr.TokenBuffer; 37 import persistence.antlr.ANTLRException; 38 39 import com.sun.jdo.api.persistence.support.JDOQueryException; 40 import com.sun.jdo.api.persistence.support.JDOUnsupportedOptionException; 41 import com.sun.jdo.api.persistence.support.JDOFatalInternalException; 42 import com.sun.jdo.spi.persistence.utility.I18NHelper; 43 import com.sun.jdo.spi.persistence.utility.StringHelper; 44 import com.sun.jdo.spi.persistence.support.sqlstore.PersistenceManager; 45 import com.sun.jdo.spi.persistence.support.sqlstore.RetrieveDesc; 46 import com.sun.jdo.spi.persistence.support.sqlstore.ExtentCollection; 47 import com.sun.jdo.spi.persistence.utility.logging.Logger; 48 49 import com.sun.jdo.spi.persistence.support.sqlstore.query.util.type.TypeTable; 50 51 62 public class JQLC 63 { 64 65 protected TypeTable typetab; 66 67 68 protected ErrorMsg errorMsg; 69 70 71 protected Class candidateClass; 72 73 74 protected JQLAST filterAST = null; 75 76 77 protected JQLAST importsAST = null; 78 79 80 protected JQLAST varsAST = null; 81 82 83 protected JQLAST paramsAST = null; 84 85 86 protected JQLAST orderingAST = null; 87 88 89 protected JQLAST resultAST = null; 90 91 92 protected JQLAST queryAST = null; 93 94 95 private boolean prefetchEnabled; 96 97 103 protected Map retrieveDescCache = new HashMap(); 104 105 106 protected final static ResourceBundle messages = 107 I18NHelper.loadBundle(JQLC.class); 108 109 110 private static Logger logger = LogHelperQueryCompilerJDO.getLogger(); 111 112 115 public JQLC() 116 { 117 this.errorMsg = new ErrorMsg(); 118 } 119 120 123 public void setClass(Class candidateClass) 124 { 125 if (candidateClass == null) 127 { 128 JDOQueryException ex = new JDOQueryException(I18NHelper.getMessage( 129 messages, "jqlc.jqlc.generic.nocandidateclass")); logger.throwing("jqlc.JQLC", "setClass", ex); throw ex; 132 } 133 this.candidateClass = candidateClass; 134 } 135 136 139 public void declareImports(String imports) 140 { 141 if (imports == null) 142 { 143 importsAST = null; 144 return; 145 } 146 147 try 148 { 149 JQLParser parser = createStringParser(imports); 150 parser.parseImports(); 151 importsAST = (JQLAST)parser.getAST(); 152 } 153 catch (ANTLRException ex) 154 { 155 JQLParser.handleANTLRException(ex, errorMsg); 156 } 157 } 158 159 162 public void declareParameters(String parameters) 163 { 164 if (parameters == null) 165 { 166 paramsAST = null; 167 return; 168 } 169 170 try 171 { 172 JQLParser parser = createStringParser(parameters); 173 parser.parseParameters(); 174 paramsAST = (JQLAST)parser.getAST(); 175 } 176 catch (ANTLRException ex) 177 { 178 JQLParser.handleANTLRException(ex, errorMsg); 179 } 180 } 181 182 185 public void declareVariables(String variables) 186 { 187 if (variables == null) 188 { 189 varsAST = null; 190 return; 191 } 192 193 try 194 { 195 JQLParser parser = createStringParser(variables); 196 parser.parseVariables(); 197 varsAST = (JQLAST)parser.getAST(); 198 } 199 catch (ANTLRException ex) 200 { 201 JQLParser.handleANTLRException(ex, errorMsg); 202 } 203 } 204 205 208 public void setOrdering(String ordering) 209 { 210 if (ordering == null) 211 { 212 orderingAST = null; 213 return; 214 } 215 216 try 217 { 218 JQLParser parser = createStringParser(ordering); 219 parser.parseOrdering(); 220 orderingAST = (JQLAST)parser.getAST(); 221 } 222 catch (ANTLRException ex) 223 { 224 JQLParser.handleANTLRException(ex, errorMsg); 225 } 226 } 227 228 231 public void setResult(String result) 232 { 233 if (result == null) 234 { 235 resultAST = null; 236 return; 237 } 238 239 try 240 { 241 JQLParser parser = createStringParser(result); 242 parser.parseResult(); 243 resultAST = (JQLAST)parser.getAST(); 244 } 245 catch (ANTLRException ex) 246 { 247 JQLParser.handleANTLRException(ex, errorMsg); 248 } 249 } 250 251 254 public void setFilter(String filter) 255 { 256 if (StringHelper.isEmpty(filter)) 257 { 258 filter = "true"; } 266 267 try 268 { 269 JQLParser parser = createStringParser(filter); 270 parser.parseFilter(); 271 filterAST = (JQLAST)parser.getAST(); 272 } 273 catch (ANTLRException ex) 274 { 275 JQLParser.handleANTLRException(ex, errorMsg); 276 } 277 } 278 279 282 public void setPrefetchEnabled(boolean prefetchEnabled) 283 { 284 this.prefetchEnabled = prefetchEnabled; 285 } 286 287 290 public void semanticCheck(ParameterTable paramtab) 291 { 292 boolean finer = logger.isLoggable(Logger.FINER); 293 boolean finest = logger.isLoggable(Logger.FINEST); 294 this.typetab = TypeTable.getInstance(candidateClass.getClassLoader()); 295 paramtab.init(); 296 Semantic semantic = new Semantic(); 297 semantic.init(typetab, paramtab, errorMsg); 298 semantic.setASTFactory(JQLAST.Factory.getInstance()); 299 300 JQLAST classAST = semantic.checkCandidateClass(candidateClass); 302 queryAST = semantic.createQueryAST(classAST, importsAST, paramsAST, varsAST, 303 orderingAST, resultAST, filterAST); 304 305 if (finest) logger.finest("LOG_JQLCDumpTree", queryAST.getTreeRepr("(AST)")); 307 try 309 { 310 if (finer) logger.finer("LOG_JQLCStartPass", "semantic analysis"); semantic.query(queryAST); 312 queryAST = (JQLAST)semantic.getAST(); 313 if (finest) logger.finest("LOG_JQLCDumpTree", queryAST.getTreeRepr("(typed AST)")); } 315 catch (ANTLRException ex) 316 { 317 errorMsg.fatal("JQLC.semanticCheck unexpected exception", ex); } 319 } 320 321 324 public RetrieveDesc codeGen(PersistenceManager pm, ParameterTable paramtab) 325 { 326 boolean finer = logger.isLoggable(Logger.FINER); 327 boolean finest = logger.isLoggable(Logger.FINEST); 328 RetrieveDesc rd = null; 329 330 String key = paramtab.getKeyForRetrieveDescCache(); 333 334 synchronized(retrieveDescCache) 335 { 336 if (key != null) 337 rd = (RetrieveDesc)retrieveDescCache.get(key); 338 339 if (rd == null) { 340 Optimizer optimizer = new Optimizer(); 341 optimizer.init(typetab, paramtab, errorMsg); 342 optimizer.setASTFactory(JQLAST.Factory.getInstance()); 343 344 CodeGeneration codeGen = new CodeGeneration(); 345 codeGen.init(pm, typetab, paramtab, errorMsg, prefetchEnabled); 346 codeGen.setASTFactory(JQLAST.Factory.getInstance()); 347 348 try 349 { 350 JQLAST ast = queryAST; 351 352 if (finer) logger.finer("LOG_JQLCStartPass", "optimizer"); optimizer.query(ast); 358 ast = (JQLAST)optimizer.getAST(); 362 if (finest) logger.finest("LOG_JQLCDumpTree", ast.getTreeRepr("(optimized AST)")); 364 if (finer) logger.finer("LOG_JQLCStartPass", "code generation"); codeGen.query(ast); 366 rd = codeGen.getRetrieveDesc(); 367 if (key != null) 370 retrieveDescCache.put(key, rd); 371 } 372 catch (ANTLRException ex) 373 { 374 errorMsg.fatal("JQLC.codeGen unexpected exception", ex); } 376 } 377 else { 378 if (finer) logger.finest("LOG_JQLCReuseRetrieveDesc"); } 380 } 381 382 return rd; 383 } 384 385 388 public void checkCandidates(Class candidateClass, Collection candidateCollection) 389 { 390 if (candidateClass == null) 391 throw new JDOQueryException( 392 I18NHelper.getMessage(messages, "jqlc.jqlc.generic.nocandidateclass")); if (!(candidateCollection instanceof ExtentCollection)) 394 throw new JDOUnsupportedOptionException( 395 I18NHelper.getMessage(messages, "jqlc.jqlc.checkcandidates.memorycollection")); 397 Class candidatePCClass = ((ExtentCollection)candidateCollection).getPersistenceCapableClass(); 398 if (candidatePCClass == null) 399 throw new JDOFatalInternalException( 400 I18NHelper.getMessage(messages, "jqlc.jqlc.checkcandidates.nullpc")); 402 if (!candidateClass.getName().equals(candidatePCClass.getName())) 403 throw new JDOQueryException( 404 I18NHelper.getMessage(messages, "jqlc.jqlc.checkcandidates.mismatch", candidateClass.getName())); } 406 407 408 private JQLParser createStringParser(String text) 409 { 410 return createStringParser(text, errorMsg); 411 } 412 413 416 public static JQLParser createStringParser(String text, ErrorMsg errorMsg) 417 { 418 Reader in = new StringReader (text); 419 JQLLexer lexer = new JQLLexer(in); 420 lexer.init(errorMsg); 421 TokenBuffer buffer = new TokenBuffer(lexer); 422 JQLParser parser = new JQLParser(buffer); 423 parser.init(errorMsg); 424 parser.setASTFactory(JQLAST.Factory.getInstance()); 425 return parser; 426 } 427 428 } 429 | Popular Tags |