use of org.exist.source.Source in project exist by eXist-db.
the class RpcConnection method execute.
@Deprecated
@Override
public Map<String, Object> execute(final String pathToQuery, final Map<String, Object> parameters) throws EXistException, PermissionDeniedException {
final long startTime = System.currentTimeMillis();
final Optional<String> sortBy = Optional.ofNullable(parameters.get(RpcAPI.SORT_EXPR)).map(Object::toString);
return this.<Map<String, Object>>readDocument(XmldbURI.createInternal(pathToQuery)).apply((document, broker, transaction) -> {
final BinaryDocument xquery = (BinaryDocument) document;
if (xquery.getResourceType() != DocumentImpl.BINARY_FILE) {
throw new EXistException("Document " + pathToQuery + " is not a binary resource");
}
if (!xquery.getPermissions().validate(user, Permission.READ | Permission.EXECUTE)) {
throw new PermissionDeniedException("Insufficient privileges to access resource");
}
final Source source = new DBSource(broker, xquery, true);
try {
final Map<String, Object> rpcResponse = this.<Map<String, Object>>compileQuery(broker, transaction, source, parameters).apply(compiledQuery -> queryResultToRpcResponse(startTime, doQuery(broker, compiledQuery, null, parameters), sortBy));
return rpcResponse;
} catch (final XPathException e) {
throw new EXistException(e);
}
});
}
use of org.exist.source.Source in project exist by eXist-db.
the class DBUtils method xquery.
public static ResourceSet xquery(final Collection collection, final String xquery) throws XMLDBException {
final EXistXQueryService service = getXQueryService(collection);
final Source source = new StringSource(xquery);
return service.execute(source);
}
use of org.exist.source.Source in project exist by eXist-db.
the class EmbeddedBinariesTest method executeXQuery.
@Override
protected QueryResultAccessor<Sequence, IOException> executeXQuery(final String query) throws Exception {
final Source source = new StringSource(query);
final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
final XQueryPool pool = brokerPool.getXQueryPool();
final XQuery xquery = brokerPool.getXQueryService();
try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()))) {
final CompiledXQuery existingCompiled = pool.borrowCompiledXQuery(broker, source);
final XQueryContext context;
final CompiledXQuery compiled;
if (existingCompiled == null) {
context = new XQueryContext(brokerPool);
compiled = xquery.compile(context, source);
} else {
context = existingCompiled.getContext();
context.prepareForReuse();
compiled = existingCompiled;
}
final Sequence results = xquery.execute(broker, compiled, null);
return consumer2E -> {
try {
// context.runCleanupTasks(); //TODO(AR) shows the ordering issue with binary values (see comment below)
consumer2E.accept(results);
} finally {
// TODO(AR) performing #runCleanupTasks causes the stream to be closed, so if we do so before we are finished with the results, serialization fails.
context.runCleanupTasks();
pool.returnCompiledXQuery(source, compiled);
}
};
}
}
use of org.exist.source.Source in project exist by eXist-db.
the class SMEvents method runScript.
protected void runScript(Subject subject, String scriptURI, String script, QName functionName, List<Expression> args) {
final Database db = getDatabase();
try (final DBBroker broker = db.get(Optional.ofNullable(subject))) {
final Source source = getQuerySource(broker, scriptURI, script);
if (source == null) {
return;
}
final XQuery xquery = broker.getBrokerPool().getXQueryService();
final XQueryContext context = new XQueryContext(broker.getBrokerPool());
final CompiledXQuery compiled = xquery.compile(context, source);
// Sequence result = xquery.execute(compiled, subject.getName());
final ProcessMonitor pm = db.getProcessMonitor();
// execute the XQuery
try {
final UserDefinedFunction function = context.resolveFunction(functionName, 0);
if (function != null) {
context.getProfiler().traceQueryStart();
pm.queryStarted(context.getWatchDog());
final FunctionCall call = new FunctionCall(context, function);
if (args != null) {
call.setArguments(args);
}
final Sequence contextSequence;
final ContextItemDeclaration cid = context.getContextItemDeclartion();
if (cid != null) {
contextSequence = cid.eval(null);
} else {
contextSequence = NodeSet.EMPTY_SET;
}
call.analyze(new AnalyzeContextInfo());
call.eval(contextSequence);
}
} catch (final XPathException e) {
// XXX: log
e.printStackTrace();
} finally {
if (pm != null) {
context.getProfiler().traceQueryEnd(context);
pm.queryCompleted(context.getWatchDog());
}
compiled.reset();
context.reset();
}
} catch (final Exception e) {
// XXX: log
e.printStackTrace();
}
}
use of org.exist.source.Source in project exist by eXist-db.
the class FunctionFactory method getXQueryModuleFunction.
/**
* Gets an XQuery function from an XQuery Module
*
* @param throwOnNotFound true to throw an XPST0017 if the functions is not found, false to just return null
*/
private static FunctionCall getXQueryModuleFunction(final XQueryContext context, final XQueryAST ast, final List<Expression> params, final QName qname, final Module module, final boolean throwOnNotFound) throws XPathException {
final FunctionCall fc;
final UserDefinedFunction func = ((ExternalModule) module).getFunction(qname, params.size(), context);
if (func == null) {
// check if the module has been compiled already
if (module.isReady()) {
final StringBuilder msg = new StringBuilder("Function ").append(qname.getStringValue()).append('#').append(params.size()).append(" is not defined in namespace '").append(qname.getNamespaceURI()).append('\'');
if (module instanceof ExternalModule) {
final Source moduleSource = ((ExternalModule) module).getSource();
msg.append(" for module: ").append(moduleSource.pathOrShortIdentifier());
}
if (throwOnNotFound) {
throw new XPathException(ast.getLine(), ast.getColumn(), ErrorCodes.XPST0017, msg.toString());
} else {
return null;
}
// If not, postpone the function resolution
// Register a forward reference with the root module, so it gets resolved
// when the main query has been compiled.
} else {
fc = new FunctionCall(((ExternalModule) module).getContext(), qname, params);
fc.setLocation(ast.getLine(), ast.getColumn());
if (((ExternalModule) module).getContext() == context) {
context.addForwardReference(fc);
} else {
context.getRootContext().addForwardReference(fc);
}
}
} else {
fc = new FunctionCall(context, func);
fc.setArguments(params);
fc.setLocation(ast.getLine(), ast.getColumn());
}
return fc;
}
Aggregations