Beispiel #1
0
 public static Object getValue(
     Class<?> type, Item colItem, Configuration config, CommandContext context)
     throws XPathException, ValidationException, TransformationException {
   Object value = colItem;
   if (value instanceof AtomicValue) {
     value = getValue((AtomicValue) colItem, context);
   } else if (value instanceof Item) {
     Item i = (Item) value;
     if (XMLSystemFunctions.isNull(i)) {
       return null;
     }
     BuiltInAtomicType bat = typeMapping.get(type);
     if (bat != null) {
       AtomicValue av = new StringValue(i.getStringValueCS());
       ConversionResult cr = Converter.convert(av, bat, config.getConversionRules());
       value = cr.asAtomic();
       value = getValue((AtomicValue) value, context);
       if (value instanceof Item) {
         value = ((Item) value).getStringValue();
       }
     } else {
       value = i.getStringValue();
     }
   }
   return FunctionDescriptor.importValue(value, type);
 }
  private void handleLobResult(String charSet, Object result, ServiceResponse response)
      throws SQLException {
    if (result == null) {
      return; // or should this be an empty result?
    }

    if (result instanceof SQLXML) {
      if (charSet != null) {
        XMLSerialize serialize = new XMLSerialize();
        serialize.setTypeString("blob"); // $NON-NLS-1$
        serialize.setDeclaration(true);
        serialize.setEncoding(charSet);
        serialize.setDocument(true);
        try {
          InputStream content =
              ((BlobType) XMLSystemFunctions.serialize(serialize, new XMLType((SQLXML) result)))
                  .getBinaryStream();
          response.writeContent(content, 200, false);
          response.writeOK(ContentType.APPLICATION_OCTET_STREAM);
        } catch (TransformationException e) {
          throw new SQLException(e);
        }
      } else {
        InputStream content = ((SQLXML) result).getBinaryStream();
        response.writeContent(content, 200, false);
        response.writeOK(ContentType.APPLICATION_XML);
      }
    } else if (result instanceof Blob) {
      InputStream content = ((Blob) result).getBinaryStream();
      response.writeContent(content, 200, false);
      response.writeOK(ContentType.APPLICATION_OCTET_STREAM);
    } else if (result instanceof Clob) {
      InputStream content =
          new ReaderInputStream(
              ((Clob) result).getCharacterStream(),
              charSet == null ? Charset.defaultCharset() : Charset.forName(charSet));
      response.writeContent(content, 200, false);
      response.writeOK(ContentType.TEXT_PLAIN);
    } else {
      InputStream content =
          new ByteArrayInputStream(
              result
                  .toString()
                  .getBytes(charSet == null ? Charset.defaultCharset() : Charset.forName(charSet)));
      response.writeContent(content, 200, false);
      response.writeOK(ContentType.APPLICATION_OCTET_STREAM);
    }
  }
  public static SaxonXQueryExpression.Result evaluateXQuery(
      final SaxonXQueryExpression xquery,
      Object context,
      Map<String, Object> parameterValues,
      final RowProcessor processor,
      CommandContext commandContext)
      throws TeiidProcessingException, TeiidComponentException {
    DynamicQueryContext dynamicContext = new DynamicQueryContext(xquery.config);

    SaxonXQueryExpression.Result result = new SaxonXQueryExpression.Result();
    try {
      try {
        for (Map.Entry<String, Object> entry : parameterValues.entrySet()) {
          Object value = entry.getValue();
          if (value instanceof SQLXML) {
            value = XMLSystemFunctions.convertToSource(value);
            result.sources.add((Source) value);
            value = wrapStax((Source) value, xquery.getConfig());
          } else if (value instanceof java.util.Date) {
            value = XMLSystemFunctions.convertToAtomicValue(value);
          } else if (value instanceof BinaryType) {
            value = new HexBinaryValue(((BinaryType) value).getBytesDirect());
          }
          dynamicContext.setParameter(entry.getKey(), value);
        }
      } catch (TransformerException e) {
        throw new TeiidProcessingException(QueryPlugin.Event.TEIID30148, e);
      }
      if (context != null) {
        Source source = XMLSystemFunctions.convertToSource(context);
        result.sources.add(source);
        source = wrapStax(source, xquery.getConfig());
        if (xquery.contextRoot != null) {
          // create our own filter as this logic is not provided in the free saxon
          ProxyReceiver filter = new PathMapFilter(xquery.contextRoot);
          AugmentedSource sourceInput = AugmentedSource.makeAugmentedSource(source);
          sourceInput.addFilter(filter);
          source = sourceInput;

          // use streamable processing instead
          if (xquery.streamingPath != null && processor != null) {
            if (LogManager.isMessageToBeRecorded(LogConstants.CTX_DQP, MessageLevel.DETAIL)) {
              LogManager.logDetail(
                  LogConstants.CTX_DQP,
                  "Using stream processing for evaluation of",
                  xquery.xQueryString); // $NON-NLS-1$
            }
            // set to non-blocking in case default expression evaluation blocks
            boolean isNonBlocking = commandContext.isNonBlocking();
            commandContext.setNonBlocking(true);

            final StreamingTransform myTransform =
                new StreamingTransform() {
                  public Nodes transform(Element elem) {
                    processor.processRow(XQueryEvaluator.wrap(elem, xquery.config));
                    return NONE;
                  }
                };

            Builder builder =
                new Builder(
                    new SaxonReader(xquery.config, sourceInput),
                    false,
                    new StreamingPathFilter(xquery.streamingPath, xquery.namespaceMap)
                        .createNodeFactory(null, myTransform));
            try {
              // the builder is hard wired to parse the source, but the api will throw an exception
              // if the stream is null
              builder.build(FAKE_IS);
              return result;
            } catch (ParsingException e) {
              if (e.getCause() instanceof TeiidRuntimeException) {
                RelationalNode.unwrapException((TeiidRuntimeException) e.getCause());
              }
              throw new TeiidProcessingException(
                  QueryPlugin.Event.TEIID30151,
                  e,
                  QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30151));
            } catch (IOException e) {
              throw new TeiidProcessingException(
                  QueryPlugin.Event.TEIID30151,
                  e,
                  QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30151));
            } finally {
              if (!isNonBlocking) {
                commandContext.setNonBlocking(false);
              }
            }
          }
        }
        DocumentInfo doc;
        try {
          doc = xquery.config.buildDocument(source);
        } catch (XPathException e) {
          throw new TeiidProcessingException(
              QueryPlugin.Event.TEIID30151, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30151));
        }
        dynamicContext.setContextItem(doc);
      }
      try {
        result.iter = xquery.xQuery.iterator(dynamicContext);
        return result;
      } catch (TransformerException e) {
        throw new TeiidProcessingException(
            QueryPlugin.Event.TEIID30152, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30152));
      }
    } finally {
      if (result.iter == null) {
        result.close();
      }
    }
  }