1 22 23 package org.xquark.mediator.runtime; 24 25 import java.io.PrintStream ; 26 import java.io.StringReader ; 27 import java.util.*; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.xquark.mediator.algebra.AlgebraManager; 32 import org.xquark.mediator.decomposer.Utils; 33 import org.xquark.mediator.plan.ExecutionPlan; 34 import org.xquark.mediator.plan.OpSource; 35 import org.xquark.xml.xdbc.*; 36 import org.xquark.xquery.metadata.MetaDataImpl; 37 import org.xquark.xquery.metadata.VarCounter; 38 import org.xquark.xquery.normalize.TransformXQueryExpression; 39 import org.xquark.xquery.parser.*; 40 import org.xquark.xquery.typing.TypeVisitor; 41 import org.xquark.xquery.xdbc.XDBCResultSetInterface; 42 43 50 public class MediatorStatement implements XMLStatement { 51 private static final String RCSRevision = "$Revision: 1.14 $"; 55 private static final String RCSName = "$Name: $"; 56 57 private static Log log = LogFactory.getLog(MediatorStatement.class); 58 59 63 private _MediatorConnection connection = null; 64 private boolean qaOnly = false; 65 HashMap preparedStatemements = new HashMap(); 66 XMLResultSet currentResultSet = null; 67 68 private String baseURI = null; 71 protected boolean isClosed = false; 72 73 public MediatorStatement(_MediatorConnection connection) throws XMLDBCException { 77 this.connection = connection; 78 } 79 80 public HashMap getVariables() { 81 return null; 82 } 83 84 88 89 90 91 101 public boolean execute(String query) throws XMLDBCException { 102 if (currentResultSet != null) { 103 currentResultSet.close(); 104 } 105 currentResultSet = executeQuery(query); 106 return true; 107 } 108 109 119 public boolean execute(String query, int queryType) throws XMLDBCException { 120 return execute(query); 121 } 122 123 131 public XMLResultSet executeQuery(String query) throws XMLDBCException { 132 try { 133 ExecutionPlan plan = compileXQuery(query, baseURI); 134 if (!preparedStatemements.isEmpty()) { 135 closePreparedStatements(); 136 } 137 if (currentResultSet != null) { 138 currentResultSet.close(); 139 } 140 currentResultSet = plan.executeQuery(this); 141 return currentResultSet; 142 } catch (XQueryException e) { 143 throw new XMLDBCException("MediatorStatement.executeQuery : " + e.getMessage(), e); 144 } 145 } 146 private PrintStream plnout = null; 148 public XMLResultSet executeQuery(String query, PrintStream plnout) throws XMLDBCException { 149 this.plnout = plnout; 150 return executeQuery(query, XMLConnection.XQUERY_STRING_TYPE); 151 } 152 153 protected ExecutionPlan compileXQuery(String query, String baseURI) throws XMLDBCException { 154 try { 155 XQueryParser ql = new XQueryParser(new StringReader (query)); 156 ql.setBaseURI(baseURI); 157 ql.setFactory("org.xquark.xquery.parser.ParserFactory"); 158 MetaDataImpl metadata = connection.getMetadataAccess(); 159 160 XQueryModule module = ql.Start(metadata, metadata.getSchemaManager(), metadata.getModuleManager(), new VarCounter(), qaOnly); 161 ArrayList expressions = module.getExpressions(); 162 if (expressions == null || expressions.isEmpty()) 163 return null; 164 TypeVisitor typevisitor = ql.getTypeVisitor(); 165 166 168 175 module.normalize(typevisitor, connection.getSourceName()); 176 if (expressions.size() > 1) { 178 throw new XMLDBCException("Fusion only handles modules with a single query (for now...)"); 179 } 180 for (int j = 0; j < expressions.size(); j++) { 181 XQueryExpression exprj = (XQueryExpression) expressions.get(j); 182 Utils.fillSources(exprj, metadata, true); 183 } 184 185 Set sources = module.getSourceNames(); 186 Set urls = module.getUrls(); 187 String xQueryStringImports = null; 188 if (!qaOnly && sources != null && sources.size() == 1 && (urls == null || urls.isEmpty())) { 189 xQueryStringImports = module.toString(false, true); 190 } 191 192 196 ExecutionPlan plan = null; 198 XQueryExpression exprj = (XQueryExpression) expressions.get(0); 200 String xQueryString = module.toString(false, false); 201 202 TransformXQueryExpression txe = exprj.getParentModule().canonize(exprj, typevisitor); 203 plan = new ExecutionPlan(metadata, typevisitor, txe.getIdVarList(), xQueryStringImports, xQueryString, module, baseURI, qaOnly); 204 205 AlgebraManager depmanager = new AlgebraManager(plan, metadata, typevisitor, txe.getQMEM(), txe.getQDB(), txe.getIdVarList()); 206 207 ArrayList qdbs = txe.getQDB(); 208 209 if (log.isDebugEnabled()) { 210 for (int si = 0; si < qdbs.size(); si++) 211 log.debug("QDB[" + si + "]: " + ((Variable) qdbs.get(si)).getExpression()); 212 log.debug("QMEM: " + txe.getQMEM()); 213 } 214 215 XDBCResultSetInterface resultset = null; 216 if (!qdbs.isEmpty()) { 217 depmanager.generate(); 218 depmanager.computeVarPaths(); 219 if (log.isDebugEnabled()) { 220 log.debug("Algebra Tree: " + depmanager); 221 } 222 depmanager.FillPlan(); 223 if (log.isDebugEnabled()) { 224 log.info("Execution Tree: " + plan); 225 } 226 if (plnout != null) { 227 plnout.print(plan.toString()); 228 } 229 } 230 plan.setQMEM(txe.getQMEM()); 231 if (!module.getExternalVariables().isEmpty() && plan.getRoot() != null) 232 plan.getRoot().setPrepared(); 233 234 return plan; 236 } catch (XQueryException xe) { 237 throw new XMLDBCException(xe.getMessage(), xe); 238 } catch (ParseException xe) { 239 throw new XMLDBCException(xe.getMessage(), xe); 240 } 241 242 } 243 253 public XMLResultSet executeQuery(String query, int queryType) throws XMLDBCException { 254 return executeQuery(query); 255 } 256 257 267 public XMLDocumentSet executeDocumentQuery(String query) throws XMLDBCException { 268 throw new XMLDBCNotSupportedException("Not yet implemented."); 269 } 270 271 272 273 274 275 280 public XMLResultSet getResultSet() throws XMLDBCException { 281 throw new XMLDBCNotSupportedException("Not yet implemented."); 282 } 283 284 292 public XMLDocumentSet getDocumentSet() throws XMLDBCException { 293 throw new XMLDBCNotSupportedException("Not yet implemented."); 294 } 295 296 297 298 299 300 304 public XMLConnection getConnection() { 305 return connection.getConnection(); 306 } 307 308 public _MediatorConnection getMediatorConnection() { 309 return connection; 310 } 311 312 317 public void close() throws XMLDBCException { 318 if (!isClosed) { 319 if (currentResultSet != null) { 320 try { 321 currentResultSet.close(); 322 } catch (XMLDBCException xe) { 323 } 324 currentResultSet = null; 325 } 326 connection.statementClosed(this); 327 isClosed = true; 328 } 329 } 330 331 public boolean isClosed() throws XMLDBCException { 332 return isClosed; 333 } 334 335 public void setBaseURI(String baseURI) { 336 this.baseURI = baseURI; 337 } 338 339 public String getBaseURI() { 340 return baseURI; 341 } 342 343 public void setQAOnly(boolean bool) { 344 qaOnly = bool; 345 } 346 347 public void closePreparedStatements() throws XMLDBCException { 348 for (Iterator it = preparedStatemements.values().iterator(); it.hasNext();) { 349 PreparedXMLStatement stat = (PreparedXMLStatement) it.next(); 350 if (stat != null) 351 stat.close(); 352 } 353 preparedStatemements.clear(); 354 } 355 356 public PreparedXMLStatement getPreparedXMLStatement(OpSource opSource, String sourceName, String requestStr) throws XMLDBCException { 357 PreparedXMLStatement stat = (PreparedXMLStatement) preparedStatemements.get(opSource); 359 if (!preparedStatemements.containsKey(opSource)) { 360 XMLConnection xmlConnection = null; 361 try { 362 xmlConnection = connection.getSubConnectionByName(sourceName); 363 } catch (MediatorException me) { 364 throw new XMLDBCException(me.getMessage(), me); 365 } 366 try { 367 stat = xmlConnection.prepareStatement(requestStr); 368 } catch (XMLDBCNotSupportedException xe) { 369 stat = null; 370 } 371 preparedStatemements.put(opSource, stat); 372 } 373 return stat; 374 } 375 } 376 | Popular Tags |