1 12 13 package org.xquark.mediator.runtime; 14 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.math.BigDecimal ; 18 import java.util.*; 19 20 import org.xquark.mediator.plan.ExecutionPlan; 21 import org.xquark.schema.SchemaException; 22 import org.xquark.schema.SimpleType; 23 import org.xquark.schema.Type; 24 import org.xquark.xml.xdbc.PreparedXMLStatement; 25 import org.xquark.xml.xdbc.XMLDBCException; 26 import org.xquark.xml.xdbc.XMLResultSet; 27 import org.xquark.xpath.datamodel.TypedDocumentImpl; 28 import org.xquark.xpath.datamodel.TypedValue; 29 import org.xquark.xpath.datamodel.TypedValueImpl; 30 import org.xquark.xquery.parser.*; 31 32 public class PreparedMediatorStatement extends MediatorStatement implements PreparedXMLStatement { 33 34 ExecutionPlan plan = null; 35 36 String query = null; 37 38 HashMap variables = new HashMap(); 40 41 protected static TypedDocumentImpl docimpl = null; 42 static { 43 docimpl = new TypedDocumentImpl(); 44 } 45 46 50 public PreparedMediatorStatement(_MediatorConnection connection, String baseURI, String query, Map cachedStatements) throws XMLDBCException { 51 super(connection); 52 this.query = query; 53 if (cachedStatements != null) { 54 plan = (ExecutionPlan) cachedStatements.get(query); 55 } 56 if (plan == null) { 57 plan = compileXQuery(query, baseURI); 58 if (cachedStatements != null) { 59 cachedStatements.put(query, plan); 60 } 61 } 62 } 63 64 public HashMap getVariables() { 65 return variables; 66 } 67 68 private Variable getVariable(String ns, String varName) throws XMLDBCException { 69 HashMap varMap = plan.getModule().getVariables(); 70 if (varMap == null) 71 throw new XMLDBCException("Variable $" + ((ns == null) ? "" : "{" + ns + "}") + varName + " is not defined."); 72 varMap = (HashMap) varMap.get(ns); 73 if (varMap == null) 74 throw new XMLDBCException("Variable $" + ((ns == null) ? "" : "{" + ns + "}") + varName + " is not defined."); 75 Variable var = (Variable) varMap.get(varName); 76 if (var == null) 77 throw new XMLDBCException("Variable $" + ((ns == null) ? "" : "{" + ns + "}") + varName + " is not defined."); 78 return var; 79 } 80 81 86 public void setExternalVariable(String ns, String varName, String value) throws XMLDBCException { 87 Variable var = getVariable(ns, varName); 88 if (!var.getQType().isAtom()) 89 throw new XMLDBCException("Variable " + var.getStringValue() + " cannot be set with string value."); 90 SimpleType st = var.getQType().getSimpleType(); 91 Object obj = null; 92 try { 93 obj = st.convert(value); 94 } catch (SchemaException se) { 95 throw new XMLDBCException("Illegal string value " + value + "for variable {" + ns + "}" + varName + " of type " + st.getPrimitive(), se); 96 } 97 HashMap map = (HashMap) variables.get(ns); 98 if (map == null) { 99 map = new HashMap(); 100 variables.put(ns, map); 101 } 102 TypedValue typedValue = new TypedValueImpl(docimpl, value); 103 typedValue.setTypedValue(obj); 104 typedValue.setType(st); 105 map.put(varName, typedValue); 106 } 107 108 113 public void setExternalVariable(String varName, String value) throws XMLDBCException { 114 setExternalVariable(null, varName, value); 115 } 116 117 122 public void setObject(String ns, String varName, Object value) throws XMLDBCException { 123 Variable var = getVariable(ns, varName); 124 if (!var.getQType().isAtom()) 125 throw new XMLDBCException("Variable " + var.getStringValue() + " complex type not supported yet."); 126 SimpleType st = var.getQType().getSimpleType(); 127 if (value instanceof ArrayList) { 129 if (true) 130 throw new XMLDBCException("Variable " + var.getStringValue() + " sequence not supported yet."); 131 ArrayList list = (ArrayList) value; 132 if (list.size() > 1 && !var.getQType().isMultiple()) 133 throw new XMLDBCException("Illegal value " + value + "for variable {" + ns + "}" + varName + " of type " + st.getPrimitive()); 134 ArrayList typeValues = new ArrayList(list.size()); 135 for (int i = 0; i < list.size(); i++) { 136 Object valuei = list.get(i); 137 Object obj = null; 138 try { 139 obj = st.convertObject(valuei); 140 } catch (SchemaException se) { 141 throw new XMLDBCException("Illegal value " + valuei + "for variable {" + ns + "}" + varName + " of type " + st.getPrimitive(), se); 142 } 143 TypedValue typedValue = new TypedValueImpl(docimpl, valuei.toString()); 144 typedValue.setTypedValue(obj); 145 typedValue.setType(st); 146 typeValues.add(typedValue); 147 } 148 HashMap map = (HashMap) variables.get(ns); 149 if (map == null) { 150 map = new HashMap(); 151 variables.put(ns, map); 152 } 153 map.put(varName, typeValues); 154 } else { 155 Object obj = null; 156 try { 157 obj = st.convertObject(value); 158 } catch (SchemaException se) { 159 throw new XMLDBCException("Illegal value " + value + "for variable {" + ns + "}" + varName + " of type " + st.getPrimitive(), se); 160 } 161 HashMap map = (HashMap) variables.get(ns); 162 if (map == null) { 163 map = new HashMap(); 164 variables.put(ns, map); 165 } 166 TypedValue typedValue = new TypedValueImpl(docimpl, value.toString()); 167 typedValue.setTypedValue(obj); 168 typedValue.setType(st); 169 map.put(varName, typedValue); 170 } 171 } 172 173 178 public void setObject(String varName, Object value) throws XMLDBCException { 179 setObject(null, varName, value); 180 } 181 182 187 public void setBigDecimal(String ns, String varName, BigDecimal value) throws XMLDBCException { 188 setObject(ns, varName, value); 189 } 190 191 196 public void setBigDecimal(String varName, BigDecimal value) throws XMLDBCException { 197 setBigDecimal(null, varName, value); 198 } 199 200 205 public void setInputStream(String ns, String varName, InputStream value, int length) throws XMLDBCException { 206 byte[] bytes = new byte[length]; 207 try { 208 int res = value.read(bytes, 0, length); 209 setBytes(ns, varName, bytes); 210 } catch (IOException ioe) { 211 throw new XMLDBCException(ioe.getMessage(), ioe); 212 } 213 } 214 215 220 public void setInputStream(String varName, InputStream value, int length) throws XMLDBCException { 221 setInputStream(null, varName, value, length); 222 } 223 224 229 public void setBytes(String ns, String varName, byte[] value) throws XMLDBCException { 230 setObject(ns, varName, value); 231 } 232 233 238 public void setBytes(String varName, byte[] value) throws XMLDBCException { 239 setBytes(null, varName, value); 240 } 241 242 247 public void setBoolean(String ns, String varName, boolean value) throws XMLDBCException { 248 setObject(ns, varName, new Boolean (value)); 249 } 250 251 256 public void setBoolean(String varName, boolean value) throws XMLDBCException { 257 setBoolean(null, varName, value); 258 } 259 260 265 public void setDate(String ns, String varName, Date value) throws XMLDBCException { 266 setObject(ns, varName, value); 267 } 268 269 274 public void setDate(String varName, Date value) throws XMLDBCException { 275 setDate(null, varName, value); 276 } 277 278 283 public void setDouble(String ns, String varName, double value) throws XMLDBCException { 284 setObject(ns, varName, new Double (value)); 285 } 286 287 292 public void setDouble(String varName, double value) throws XMLDBCException { 293 setDouble(null, varName, value); 294 } 295 296 301 public void setFloat(String ns, String varName, float value) throws XMLDBCException { 302 setObject(ns, varName, new Float (value)); 303 } 304 305 310 public void setFloat(String varName, float value) throws XMLDBCException { 311 setFloat(null, varName, value); 312 } 313 314 319 public void setLong(String ns, String varName, long value) throws XMLDBCException { 320 setObject(ns, varName, new Long (value)); 321 } 322 323 328 public void setLong(String varName, long value) throws XMLDBCException { 329 setLong(null, varName, value); 330 } 331 332 337 public void setString(String ns, String varName, String value) throws XMLDBCException { 338 setObject(ns, varName, value); 339 } 340 341 346 public void setString(String varName, String value) throws XMLDBCException { 347 setString(null, varName, value); 348 } 349 350 355 public void setSequence(String ns, String varName, List value) throws XMLDBCException { 356 setObject(ns, varName, value); 357 } 358 359 364 public void setSequence(String varName, List value) throws XMLDBCException { 365 setSequence(null, varName, value); 366 } 367 368 373 public void clearVariables() throws XMLDBCException { 374 variables.clear(); 375 } 376 377 382 public boolean execute() throws XMLDBCException { 383 throw new XMLDBCException("Not supported yet"); 385 } 387 388 393 public XMLResultSet executeQuery() throws XMLDBCException { 394 if (currentResultSet != null) { 395 currentResultSet.close(); 396 } 397 if (plan == null) { 398 currentResultSet = null; 399 return null; 400 } 401 try { 402 currentResultSet = plan.executeQuery(this); 403 return currentResultSet; 404 } catch (XQueryException xe) { 405 currentResultSet = null; 406 throw new XMLDBCException(xe.getMessage(), xe); 407 } 408 } 409 410 public void close() throws XMLDBCException { 411 super.close(); 412 super.closePreparedStatements(); 413 } 414 415 public void closePreparedStatements() throws XMLDBCException { 416 } 418 419 public Collection getParameters() { 420 return variables.values(); 421 } 422 423 public javax.xml.namespace.QName [] getParameterNames() { 424 ArrayList names = new ArrayList(); 425 XQueryModule module = plan.getModule(); 426 if (module.getDeclList() != null) { 427 for (int i = 0; i < module.getDeclList().size(); i++) { 428 QName name = (QName) module.getDeclList().get(i); 429 HashMap map = null; 430 if (module.getVariables() != null) { 431 map = (HashMap) module.getVariables().get(name.getNameSpace()); 432 if (map != null) { 433 Variable vari = (Variable) map.get(name.getLocalName()); 434 if (vari != null && vari.getExpression() == null) { 435 names.add(new javax.xml.namespace.QName (vari.getVarName().getNameSpace(), vari.getVarName().getLocalName())); 436 } 437 } 438 } 439 } 440 } 441 javax.xml.namespace.QName qnames[] = new javax.xml.namespace.QName [names.size()]; 442 return (javax.xml.namespace.QName []) names.toArray(qnames); 443 } 444 445 public javax.xml.namespace.QName getParameterType(javax.xml.namespace.QName varName) { 446 XQueryModule module = plan.getModule(); 447 org.xquark.xquery.parser.ExternalVariable extVar = (org.xquark.xquery.parser.ExternalVariable) module.getExternalVariables().get(varName); 448 Type type = extVar.getQType().getSimpleType().getBaseType(); 449 return new javax.xml.namespace.QName (type.getNamespace(), type.getName()); 450 } 451 452 } | Popular Tags |