Search in sources :

Example 26 with CompiledXQuery

use of org.exist.xquery.CompiledXQuery in project exist by eXist-db.

the class CollectionTest method doc_dynamicallyAvailableCollection_relativeUri.

@Test
public void doc_dynamicallyAvailableCollection_relativeUri() throws XPathException, EXistException, PermissionDeniedException, URISyntaxException {
    final BrokerPool pool = BrokerPool.getInstance();
    final String doc = "<timestamp>" + System.currentTimeMillis() + "</timestamp>";
    final String baseUri = "http://from-dynamic-context/";
    final String collectionRelativeUri = "collection1";
    final String query = "fn:collection('" + collectionRelativeUri + "')";
    try (final DBBroker broker = pool.getBroker()) {
        final XQueryContext context = new XQueryContext(pool);
        context.setBaseURI(new AnyURIValue(new URI(baseUri)));
        context.addDynamicallyAvailableCollection(baseUri + collectionRelativeUri, (broker2, transaction, uri) -> asInMemoryDocument(doc));
        final XQuery xqueryService = pool.getXQueryService();
        final CompiledXQuery compiled = xqueryService.compile(context, query);
        final Sequence result = xqueryService.execute(broker, compiled, null);
        assertFalse(result.isEmpty());
        assertEquals(1, result.getItemCount());
        assertTrue(result.itemAt(0) instanceof Node);
        final Source expectedSource = Input.fromString(doc).build();
        final Source actualSource = Input.fromNode((Node) result.itemAt(0)).build();
        final Diff diff = DiffBuilder.compare(expectedSource).withTest(actualSource).checkForIdentical().checkForSimilar().build();
        assertFalse(diff.toString(), diff.hasDifferences());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) Diff(org.xmlunit.diff.Diff) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) AnyURIValue(org.exist.xquery.value.AnyURIValue) Node(org.w3c.dom.Node) XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) URI(java.net.URI) BrokerPool(org.exist.storage.BrokerPool) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) Test(org.junit.Test)

Example 27 with CompiledXQuery

use of org.exist.xquery.CompiledXQuery in project exist by eXist-db.

the class Modification method select.

/**
 * Evaluate the select expression.
 *
 * @param docs the documents to evaludate the expression over
 *
 * @return The selected nodes.
 *
 * @throws PermissionDeniedException if the caller has insufficient priviledges
 * @throws EXistException if the database raises an error
 * @throws XPathException if the XPath raises an error
 */
protected NodeList select(DocumentSet docs) throws PermissionDeniedException, EXistException, XPathException {
    final XQuery xquery = broker.getBrokerPool().getXQueryService();
    final XQueryPool pool = broker.getBrokerPool().getXQueryPool();
    final Source source = new StringSource(selectStmt);
    CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);
    XQueryContext context;
    if (compiled == null) {
        context = new XQueryContext(broker.getBrokerPool());
    } else {
        context = compiled.getContext();
        context.prepareForReuse();
    }
    context.setStaticallyKnownDocuments(docs);
    declareNamespaces(context);
    declareVariables(context);
    if (compiled == null)
        try {
            compiled = xquery.compile(context, source);
        } catch (final IOException e) {
            throw new EXistException("An exception occurred while compiling the query: " + e.getMessage());
        }
    Sequence resultSeq = null;
    try {
        resultSeq = xquery.execute(broker, compiled, null);
    } finally {
        context.runCleanupTasks();
        pool.returnCompiledXQuery(source, compiled);
    }
    if (!(resultSeq.isEmpty() || Type.subTypeOf(resultSeq.getItemType(), Type.NODE))) {
        throw new EXistException("select expression should evaluate to a node-set; got " + Type.getTypeName(resultSeq.getItemType()));
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("found {} for select: {}", resultSeq.getItemCount(), selectStmt);
    }
    return resultSeq.toNodeSet();
}
Also used : XQueryPool(org.exist.storage.XQueryPool) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) StringSource(org.exist.source.StringSource) IOException(java.io.IOException) EXistException(org.exist.EXistException) Sequence(org.exist.xquery.value.Sequence) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source)

Example 28 with CompiledXQuery

use of org.exist.xquery.CompiledXQuery in project exist by eXist-db.

the class Util method withCompiledQuery.

public static <T> T withCompiledQuery(final DBBroker broker, final Source source, final Function2E<CompiledXQuery, T, XPathException, PermissionDeniedException> op) throws XPathException, PermissionDeniedException, IOException {
    final BrokerPool pool = broker.getBrokerPool();
    final XQuery xqueryService = pool.getXQueryService();
    final XQueryPool xqueryPool = pool.getXQueryPool();
    final CompiledXQuery compiledQuery = compileQuery(broker, xqueryService, xqueryPool, source);
    try {
        return op.apply(compiledQuery);
    } finally {
        if (compiledQuery != null) {
            xqueryPool.returnCompiledXQuery(source, compiledQuery);
        }
    }
}
Also used : XQueryPool(org.exist.storage.XQueryPool) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) BrokerPool(org.exist.storage.BrokerPool)

Example 29 with CompiledXQuery

use of org.exist.xquery.CompiledXQuery in project exist by eXist-db.

the class Util method executeQuery.

public static Sequence executeQuery(final DBBroker broker, final CompiledXQuery compiledXQuery) throws PermissionDeniedException, XPathException {
    final BrokerPool pool = broker.getBrokerPool();
    final XQuery xqueryService = pool.getXQueryService();
    return xqueryService.execute(broker, compiledXQuery, null, new Properties());
}
Also used : CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) Properties(java.util.Properties) BrokerPool(org.exist.storage.BrokerPool)

Example 30 with CompiledXQuery

use of org.exist.xquery.CompiledXQuery in project exist by eXist-db.

the class Util method executeQuery.

static Sequence executeQuery(final DBBroker broker, final CompiledXQuery compiledXQuery) throws PermissionDeniedException, XPathException {
    final BrokerPool pool = broker.getBrokerPool();
    final XQuery xqueryService = pool.getXQueryService();
    return xqueryService.execute(broker, compiledXQuery, null, new Properties());
}
Also used : CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) Properties(java.util.Properties) BrokerPool(org.exist.storage.BrokerPool)

Aggregations

CompiledXQuery (org.exist.xquery.CompiledXQuery)48 XQuery (org.exist.xquery.XQuery)39 XQueryContext (org.exist.xquery.XQueryContext)35 BrokerPool (org.exist.storage.BrokerPool)23 DBBroker (org.exist.storage.DBBroker)23 Sequence (org.exist.xquery.value.Sequence)21 XQueryPool (org.exist.storage.XQueryPool)14 XPathException (org.exist.xquery.XPathException)9 IOException (java.io.IOException)8 Source (org.exist.source.Source)8 URI (java.net.URI)7 EXistException (org.exist.EXistException)7 AnyURIValue (org.exist.xquery.value.AnyURIValue)7 Test (org.junit.Test)7 Properties (java.util.Properties)6 InputStreamReader (java.io.InputStreamReader)5 PermissionDeniedException (org.exist.security.PermissionDeniedException)5 InputSource (org.xml.sax.InputSource)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Source (javax.xml.transform.Source)4