1 22 23 package org.xquark.mediator.plan; 24 25 import java.util.*; 26 27 import org.xquark.mediator.DOMUtils.BufferTuple; 28 import org.xquark.mediator.DOMUtils.Tuple; 29 import org.xquark.mediator.runtime.MediatorException; 30 import org.xquark.mediator.runtime.MediatorResultSet; 31 import org.xquark.mediator.runtime.MediatorStatement; 32 import org.xquark.schema.*; 33 import org.xquark.util.NamespaceContextStack; 34 import org.xquark.xml.xdbc.*; 35 import org.xquark.xpath.datamodel.TypedNode; 36 import org.xquark.xpath.datamodel.TypedValue; 37 import org.xquark.xquery.metadata.MetaDataImpl; 38 import org.xquark.xquery.parser.*; 39 import org.xquark.xquery.parser.hinttree.HintTree; 40 import org.xquark.xquery.typing.*; 41 import org.xquark.xquery.xdbc.XDBCResultSetInterface; 42 43 public class ExecutionPlan { 44 private static final String RCSRevision = "$Revision: 1.20 $"; 48 private static final String RCSName = "$Name: $"; 49 50 54 private Operator root = null; 55 private ResultSet resultset = null; 56 private TypeVisitor typevisitor; 57 private String declarations = null; 58 private ArrayList idvars = null; 59 private HashMap aliases = null; 60 private String baseURI = null; 61 private boolean qaOnly = false; 62 private XQueryExpression qmem; 63 private String queryStringImports = null; 65 private String queryString = null; 66 private XQueryModule module = null; 67 private MetaDataImpl metadata = null; 68 69 private final int defaultsubqueryhandling = HintTree.MERGE_TYPE; 70 72 SchemaManager schemamanager = null; 74 Schema schema = null; 75 public ExecutionPlan(MetaDataImpl metadata, TypeVisitor typevisitor, ArrayList idvars, String queryStringImports, String queryString, XQueryModule module, String baseURI, boolean qaOnly) throws MediatorException { 79 if (typevisitor == null) 80 throw new MediatorException("TypeVisitor in plan constructor cannot be null"); 81 this.metadata = metadata; 82 this.typevisitor = typevisitor; 83 this.idvars = idvars; 84 this.queryString = queryString; 86 this.queryStringImports = queryStringImports; 87 this.module = module; 88 this.baseURI = baseURI; 89 this.qaOnly = qaOnly; 90 NamespaceContextStack declarations = module.getDeclarations(); 92 if (declarations == null) 93 this.declarations = null; 94 else { 95 StringBuffer buf = new StringBuffer (); 96 Collection declPrefixes = declarations.getPrefixes(); 97 Iterator itValues = declPrefixes.iterator(); 101 String prefix = null; 102 while (itValues.hasNext()) { 103 prefix = (String ) itValues.next(); 104 buf.append("declare "); 105 if (prefix != null) { 106 buf.append("namespace "); 107 buf.append(prefix); 108 } else 109 buf.append("default element namespace"); 110 buf.append(" = \""); 111 buf.append(declarations.getNamespaceURI(prefix)); 112 buf.append("\";\n"); 113 } 114 this.declarations = buf.toString(); 115 } 116 schemamanager = typevisitor.getTypingSchemaManager(); 118 schema = schemamanager.getSchema("http://www.xquark.org/EXECUTIONPLAN"); 119 } 120 121 public XDBCResultSetInterface getResultSet() throws MediatorException { 125 try { 126 PlanResultSet presultset = new PlanResultSet(resultset); 127 return presultset; 128 } catch (XMLDBCException xmldbce) { 129 throw new MediatorException("ExecutionPlan.getResultSet: " + xmldbce.getMessage(), xmldbce); 130 } 131 } 132 133 public SchemaManager getSchemaManager() { 134 return schemamanager; 135 } 136 137 public String getDeclarations() { 138 return declarations; 139 } 140 public TypeVisitor getTypeVisitor() { 141 return typevisitor; 142 } 143 public Operator getRoot() { 144 return root; 145 } 146 public void setRoot(Operator algebra) throws MediatorException { 147 root = algebra; 148 } 149 public ArrayList getIdVars() { 150 return idvars; 151 } 152 public void setIdVars(ArrayList idvars) { 153 this.idvars = idvars; 154 } 155 156 public String getAlias(String varstr) { 157 if (aliases == null) 158 return null; 159 for (Iterator it = aliases.keySet().iterator(); it.hasNext();) { 160 String stri = (String ) it.next(); 161 if (varstr.startsWith(stri)) 162 return (String ) aliases.get(stri) + varstr.substring(stri.length()); 163 } 164 return null; 165 } 166 167 public byte getDefaultSubHandling() { 168 return defaultsubqueryhandling; 169 } 170 public void makeAlias(Variable var, XQueryExpression path) { 174 if (aliases == null) 175 aliases = new HashMap(); 176 aliases.put(var.getStringValue(), path.getStringValue()); 177 } 178 179 200 201 public String getBaseURI() { 202 return baseURI; 203 } 204 205 public boolean getQAOnly() { 206 return qaOnly; 207 } 208 209 public XQueryModule getModule() { 210 return module; 211 } 212 213 219 public XMLResultSet executeQuery(MediatorStatement statement) throws XQueryException, XMLDBCException { 220 ResultSet resultset = null; 221 DynamicContext context = new DynamicContext(this, statement); 223 224 if (queryStringImports != null) { 225 PreparedXMLStatement preparedStatement = null; 227 Iterator it = module.getSourceNames().iterator(); 228 String sourceName = (String ) it.next(); 229 try { 230 preparedStatement = context.getPreparedXMLStatement(null, sourceName, queryStringImports); 231 } catch (XMLDBCException xe) { 232 throw new MediatorException(xe.getMessage(), xe); 233 } 234 if (preparedStatement != null) { 235 preparedStatement.setBaseURI(baseURI); 236 HashMap varMap = statement.getVariables(); 237 if (varMap != null) { 238 for (Iterator itns = varMap.keySet().iterator(); itns.hasNext();) { 239 String nsStr = (String ) itns.next(); 240 HashMap nsMap = (HashMap) varMap.get(nsStr); 241 for (Iterator itvn = nsMap.keySet().iterator(); itvn.hasNext();) { 242 String vnStr = (String ) itvn.next(); 243 TypedValue typedValue = (TypedValue) nsMap.get(vnStr); 244 Object value = typedValue.getTypedValue(); 245 preparedStatement.setObject(nsStr, vnStr, value); 246 } 247 } 248 } 249 return preparedStatement.executeQuery(); 250 } else { 251 XMLConnection connection = null; 252 connection = context.getConnection().getSubConnectionByName(sourceName); 253 if (connection == null) 254 throw new MediatorException("AlgSource: couldn't established connection to " + sourceName); 255 XMLStatement stat = connection.createStatement(); 256 stat.setBaseURI(baseURI); 257 StringBuffer sb = new StringBuffer (); 259 HashMap varMap = statement.getVariables(); 260 if (varMap != null) { 261 for (Iterator itns = varMap.keySet().iterator(); itns.hasNext();) { 262 String nsStr = (String ) itns.next(); 263 HashMap nsMap = (HashMap) varMap.get(nsStr); 264 HashMap moduleNsMap = (HashMap) module.getVariables().get(nsStr); 265 for (Iterator itvn = nsMap.keySet().iterator(); itvn.hasNext();) { 266 String vnStr = (String ) itvn.next(); 267 TypedValue typedValue = (TypedValue) nsMap.get(vnStr); 268 Object value = typedValue.getTypedValue(); 269 Variable var = (Variable) moduleNsMap.get(vnStr); 270 if (value != null) { 271 sb.append("declare variable $"); 272 if (nsStr != null) { 273 sb.append(" {"); 274 sb.append(nsStr); 275 sb.append("}"); 276 } 277 sb.append(vnStr); 278 if (var != null && var.getTypeDeclaration() != null) { 279 sb.append(" as "); 280 sb.append(var.getTypeDeclaration()); 281 sb.append(""); 282 } 283 sb.append(" {"); 284 if (value instanceof String ) { 285 sb.append("\""); 286 sb.append(value.toString()); 287 sb.append("\""); 288 } else 289 sb.append(value.toString()); 290 sb.append("};\n"); 291 } 292 } 293 } 294 } 295 sb.append(queryString); 296 return stat.executeQuery(sb.toString()); 297 } 298 } else { 299 HashMap varMap = statement.getVariables(); 301 if (varMap != null) { 302 ArrayList paths = new ArrayList(); 303 ArrayList nodes = new ArrayList(); 304 for (Iterator itns = varMap.keySet().iterator(); itns.hasNext();) { 305 String nsStr = (String ) itns.next(); 306 HashMap nsMap = (HashMap) varMap.get(nsStr); 307 HashMap moduleNsMap = (HashMap) module.getVariables().get(nsStr); 308 for (Iterator itvn = nsMap.keySet().iterator(); itvn.hasNext();) { 309 String vnStr = (String ) itvn.next(); 310 TypedNode typedNode = (TypedNode) nsMap.get(vnStr); 311 Variable var = (Variable) moduleNsMap.get(vnStr); 312 if (var != null) { 313 paths.add(var); 314 nodes.add(typedNode); 315 } 316 } 317 } 318 BufferTuple buf = new BufferTuple(paths, 0); 319 Tuple tuple = buf.newTuple(); 320 for (int i = 0; i < nodes.size(); i++) 321 tuple.addNodeAtIndex(i, (TypedNode) nodes.get(i)); 322 buf.add(tuple); 323 context.addCurrentTuple(tuple); 324 } 325 if (root != null) { 326 resultset = root.executeQuery(context); 327 } else 328 resultset = new FakeResultSet(null, context); 329 return new MediatorResultSet(new PlanResultSet(resultset), getQMEM(), getIdVars(), statement); 330 } 331 } 332 338 public void optimize() throws MediatorException { 339 } 341 342 private final String TYPE_NAME = "PlanType"; 344 private int typeIndex = 0; 345 public void setTypeName(Type type) throws SchemaException { 346 type.setName(TYPE_NAME + (++typeIndex)); 347 schema.register(type); 348 this.schema.initialize(); 349 } 350 356 public String toString() { 357 StringBuffer buf = new StringBuffer (); 358 buf.append("<Plan>\n"); 359 if (root != null) 360 buf.append(root.toCompleteString(1)); 361 buf.append("</Plan>\n"); 362 return buf.toString(); 363 } 364 365 public void setQMEM(XQueryExpression expression) { 366 qmem = expression; 367 } 368 public XQueryExpression getQMEM() { 369 return qmem; 370 } 371 372 public void appendExternalVariables(StringBuffer sbvars, ArrayList externalVariables) { 373 int length = sbvars.length(); 374 for (int i = 0; i < externalVariables.size(); i++) { 375 Variable vari = (Variable) externalVariables.get(i); 376 sbvars.append("declare variable "); 377 sbvars.append(vari.getStringValue(module.getDeclarations())); 378 Type typ = vari.getQType().getType(); 379 if (typ == null) { 380 typ = schemamanager.getAnySimpleType(); 381 } 382 if (typ.getValueType() == null) { 383 sbvars.setLength(length); 384 return; 385 } else { 386 sbvars.append(" as xs:"); 387 sbvars.append((typ.getValueType()).getPrimitive().getName()); 388 } 389 sbvars.append(" external;\n"); 390 } 391 } 392 393 396 public MetaDataImpl getMetadata() { 397 return metadata; 398 } 399 400 } 401 | Popular Tags |