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();
}
}
}
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;
}
}
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;
}
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);
}
}
}
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);
}
});
}
Aggregations