Search in sources :

Example 1 with CompiledXQuery

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

the class XQueryStartupTrigger method executeQuery.

/**
 * Execute xquery on path
 *
 * @param broker eXist database broker
 * @param path path to query, formatted as xmldb:exist:///db/...
 */
private void executeQuery(DBBroker broker, String path) {
    XQueryContext context = null;
    try {
        // Get path to xquery
        Source source = SourceFactory.getSource(broker, null, path, false);
        if (source == null) {
            LOG.info("No XQuery found at '{}'", path);
        } else {
            // Setup xquery service
            XQuery service = broker.getBrokerPool().getXQueryService();
            context = new XQueryContext(broker.getBrokerPool());
            // Allow use of modules with relative paths
            String moduleLoadPath = StringUtils.substringBeforeLast(path, "/");
            context.setModuleLoadPath(moduleLoadPath);
            // Compile query
            CompiledXQuery compiledQuery = service.compile(context, source);
            LOG.info("Starting XQuery at '{}'", path);
            // Finish preparation
            context.prepareForExecution();
            // Execute
            Sequence result = service.execute(broker, compiledQuery, null);
            // Log results
            LOG.info("Result XQuery: '{}'", result.getStringValue());
        }
    } catch (Throwable t) {
        // Dirty, catch it all
        LOG.error("An error occurred during preparation/execution of the XQuery script {}: {}", path, t.getMessage(), t);
    } finally {
        if (context != null) {
            context.runCleanupTasks();
        }
    }
}
Also used : CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) Source(org.exist.source.Source)

Example 2 with CompiledXQuery

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

the class AbstractFieldConfig method compile.

protected CompiledXQuery compile(DBBroker broker, String code) {
    final XQuery xquery = broker.getBrokerPool().getXQueryService();
    final XQueryContext context = new XQueryContext(broker.getBrokerPool());
    try {
        return xquery.compile(context, code);
    } catch (XPathException | PermissionDeniedException e) {
        LOG.error("Failed to compile expression: {}: {}", code, e.getMessage(), e);
        isValid = false;
        return null;
    }
}
Also used : XPathException(org.exist.xquery.XPathException) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) XQueryContext(org.exist.xquery.XQueryContext) PermissionDeniedException(org.exist.security.PermissionDeniedException)

Example 3 with CompiledXQuery

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

the class Util method compileQuery.

static CompiledXQuery compileQuery(final DBBroker broker, final XQuery xqueryService, final XQueryPool xqueryPool, final Source query) throws PermissionDeniedException, XPathException, IOException {
    CompiledXQuery compiled = xqueryPool.borrowCompiledXQuery(broker, query);
    XQueryContext context;
    if (compiled == null) {
        context = new XQueryContext(broker.getBrokerPool());
    } else {
        context = compiled.getContext();
        context.prepareForReuse();
    }
    if (compiled == null) {
        compiled = xqueryService.compile(context, query);
    } else {
        compiled.getContext().updateContext(context);
        context.getWatchDog().reset();
    }
    return compiled;
}
Also used : CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext)

Example 4 with CompiledXQuery

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

the class Util method withCompiledQuery.

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 5 with CompiledXQuery

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

the class LocalXPathQueryService method execute.

private ResourceSet execute(final LocalXmldbFunction<Source> sourceOp) throws XMLDBException {
    return withDb((broker, transaction) -> {
        final long start = System.currentTimeMillis();
        final Source source = sourceOp.apply(broker, transaction);
        final XmldbURI[] docs = new XmldbURI[] { XmldbURI.create(collection.getName(broker, transaction)) };
        final XQuery xquery = brokerPool.getXQueryService();
        final XQueryPool pool = brokerPool.getXQueryPool();
        XQueryContext context;
        CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);
        if (compiled == null) {
            context = new XQueryContext(broker.getBrokerPool());
        } else {
            context = compiled.getContext();
            context.prepareForReuse();
        }
        context.setStaticallyKnownDocuments(docs);
        if (variableDecls.containsKey(Debuggee.PREFIX + ":session")) {
            context.declareVariable(Debuggee.SESSION, variableDecls.get(Debuggee.PREFIX + ":session"));
            variableDecls.remove(Debuggee.PREFIX + ":session");
        }
        setupContext(source, context);
        if (compiled == null) {
            compiled = xquery.compile(context, source);
        }
        try {
            final Sequence result = xquery.execute(broker, compiled, null, properties);
            if (LOG.isDebugEnabled()) {
                LOG.debug("query took {} ms.", System.currentTimeMillis() - start);
            }
            final Properties resourceSetProperties = new Properties(properties);
            resourceSetProperties.setProperty(EXistOutputKeys.XDM_SERIALIZATION, "yes");
            return result != null ? new LocalResourceSet(user, brokerPool, collection, resourceSetProperties, result, null) : null;
        } finally {
            compiled.getContext().runCleanupTasks();
            pool.returnCompiledXQuery(source, compiled);
        }
    });
}
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) Sequence(org.exist.xquery.value.Sequence) Source(org.exist.source.Source) DBSource(org.exist.source.DBSource) FileSource(org.exist.source.FileSource)

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