Search in sources :

Example 16 with XQuery

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

the class AbstractFieldConfig method doBuild.

protected void doBuild(DBBroker broker, DocumentImpl document, NodeId nodeId, Document luceneDoc, CharSequence text) throws PermissionDeniedException, XPathException {
    if (!expression.isPresent()) {
        processText(text, luceneDoc);
        return;
    }
    compile(broker);
    if (!isValid) {
        return;
    }
    final XQuery xquery = broker.getBrokerPool().getXQueryService();
    final NodeProxy currentNode = new NodeProxy(document, nodeId);
    try {
        Sequence result = xquery.execute(broker, compiled, currentNode);
        if (!result.isEmpty()) {
            processResult(result, luceneDoc);
        }
    } catch (PermissionDeniedException | XPathException e) {
        isValid = false;
        throw e;
    } finally {
        compiled.reset();
        compiled.getContext().reset();
    }
}
Also used : XPathException(org.exist.xquery.XPathException) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) PermissionDeniedException(org.exist.security.PermissionDeniedException) Sequence(org.exist.xquery.value.Sequence) NodeProxy(org.exist.dom.persistent.NodeProxy)

Example 17 with XQuery

use of org.exist.xquery.XQuery 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 18 with XQuery

use of org.exist.xquery.XQuery 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 19 with XQuery

use of org.exist.xquery.XQuery 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)

Example 20 with XQuery

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

the class LocalXPathQueryService method compileAndCheck.

private Either<XPathException, CompiledExpression> compileAndCheck(final DBBroker broker, final Txn transaction, final String query) throws XMLDBException {
    final long start = System.currentTimeMillis();
    final XQuery xquery = broker.getBrokerPool().getXQueryService();
    final XQueryContext context = new XQueryContext(broker.getBrokerPool());
    try {
        setupContext(null, context);
        final CompiledExpression expr = xquery.compile(context, query);
        if (LOG.isDebugEnabled()) {
            LOG.debug("compilation took {}", System.currentTimeMillis() - start);
        }
        return Either.Right(expr);
    } catch (final PermissionDeniedException e) {
        throw new XMLDBException(ErrorCodes.PERMISSION_DENIED, e.getMessage(), e);
    } catch (final IllegalArgumentException e) {
        throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
    } catch (final XPathException e) {
        return Either.Left(e);
    }
}
Also used : XPathException(org.exist.xquery.XPathException) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) PermissionDeniedException(org.exist.security.PermissionDeniedException)

Aggregations

XQuery (org.exist.xquery.XQuery)135 Sequence (org.exist.xquery.value.Sequence)108 DBBroker (org.exist.storage.DBBroker)107 BrokerPool (org.exist.storage.BrokerPool)105 CompiledXQuery (org.exist.xquery.CompiledXQuery)59 Test (org.junit.Test)36 XQueryContext (org.exist.xquery.XQueryContext)33 Txn (org.exist.storage.txn.Txn)32 XPathException (org.exist.xquery.XPathException)21 TransactionManager (org.exist.storage.txn.TransactionManager)17 Item (org.exist.xquery.value.Item)16 InputSource (org.xml.sax.InputSource)16 XQueryPool (org.exist.storage.XQueryPool)15 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)12 DocumentSet (org.exist.dom.persistent.DocumentSet)12 StringReader (java.io.StringReader)11 Properties (java.util.Properties)11 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)11 Modification (org.exist.xupdate.Modification)11 XUpdateProcessor (org.exist.xupdate.XUpdateProcessor)11