use of org.exist.source.Source in project exist by eXist-db.
the class ImportModuleTest method variableSameAsImportingModule.
/**
* Checks that XQST0049 is raised if an imported module and the importing module contain a variable of the same name.
*/
@Test
public void variableSameAsImportingModule() throws EXistException, IOException, PermissionDeniedException, LockException, SAXException {
final String module = "xquery version \"1.0\";\n" + "module namespace impl = \"http://example.com/impl\";\n" + "declare variable $impl:f1 := \"impl1\";\n";
final String query = "import module namespace impl = \"http://example.com/impl\"" + " at \"xmldb:exist:///db/impl1.xqm\";\n" + "declare variable $impl:f1 := \"this\";\n" + "<result>\n" + " <impl1>{$impl:f1}</impl1>" + "</result>\n";
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final Source source = new StringSource(query);
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
// store module
storeModules(broker, transaction, "/db", Tuple("impl1.xqm", module));
// execute query
try {
final Tuple2<XQueryContext, Sequence> contextAndResult = withCompiledQuery(broker, source, compiledXQuery -> {
final Sequence result = executeQuery(broker, compiledXQuery);
return Tuple(compiledXQuery.getContext(), result);
});
transaction.commit();
fail("expected XQST0049");
} catch (final XPathException e) {
transaction.commit();
assertEquals(ErrorCodes.XQST0049, e.getErrorCode());
}
}
}
use of org.exist.source.Source in project exist by eXist-db.
the class ImportModuleTest method functionsCompositeFromMultipleLocationHintsWithDifferingPrefixes.
/**
* Imports multiple XQuery Library Modules containing functions into the same target namespace.
*/
@Test
public void functionsCompositeFromMultipleLocationHintsWithDifferingPrefixes() throws EXistException, IOException, PermissionDeniedException, LockException, SAXException, XPathException {
final String module1 = "xquery version \"1.0\";\n" + "module namespace impl1 = \"http://example.com/impl\";\n" + "declare function impl1:f1($a as xs:string) as xs:string {\n" + " $a\n" + "};\n";
final String module2 = "xquery version \"1.0\";\n" + "module namespace impl2 = \"http://example.com/impl\";\n" + "declare function impl2:f1($a as xs:string, $b as xs:string) as xs:string {\n" + " fn:concat($a, ' ', $b)\n" + "};\n";
final String module3 = "xquery version \"1.0\";\n" + "module namespace impl3 = \"http://example.com/impl\";\n" + "declare function impl3:f2($a as xs:string) as xs:string {\n" + " $a\n" + "};\n";
final String query = "import module namespace impl = \"http://example.com/impl\"" + " at \"xmldb:exist:///db/impl1.xqm\", \"xmldb:exist:///db/impl2.xqm\", \"xmldb:exist:///db/impl3.xqm\";\n" + "<result>\n" + " <impl1>{impl:f1(\"to impl1\")}</impl1>" + " <impl2>{impl:f1(\"to\", \"impl2\")}</impl2>" + " <impl3>{impl:f2(\"to impl3\")}</impl3>" + "</result>\n";
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final Source source = new StringSource(query);
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
// store modules
storeModules(broker, transaction, "/db", Tuple("impl1.xqm", module1), Tuple("impl2.xqm", module2), Tuple("impl3.xqm", module3));
// execute query
final Tuple2<XQueryContext, Sequence> contextAndResult = withCompiledQuery(broker, source, compiledXQuery -> {
final Sequence result = executeQuery(broker, compiledXQuery);
return Tuple(compiledXQuery.getContext(), result);
});
// check that the result was correct
assertNotNull(contextAndResult._2);
assertEquals(1, contextAndResult._2.getItemCount());
final Element doc = (Element) contextAndResult._2.itemAt(0);
assertNotNull(doc);
final javax.xml.transform.Source actual = Input.fromDocument(doc.getOwnerDocument()).build();
final javax.xml.transform.Source expected = Input.fromString("<result>" + "<impl1>to impl1</impl1>" + "<impl2>to impl2</impl2>" + "<impl3>to impl3</impl3>" + "</result>").build();
final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
assertFalse(diff.toString(), diff.hasDifferences());
transaction.commit();
}
}
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 InternalModuleTest method moduleVariables.
@Test
public void moduleVariables() throws XMLDBException {
final Source querySource = new StringSource(getModuleVariableQuery("org.exist.xquery.InternalModuleTest$TestModuleWithVariables"));
final EXistXQueryService queryService = (EXistXQueryService) existServer.getRoot().getService("XQueryService", "1.0");
moduleVariablesQuery(queryService, querySource, COUNTER.get());
}
use of org.exist.source.Source in project exist by eXist-db.
the class XQueryTrigger method getScript.
private CompiledXQuery getScript(boolean isBefore, DBBroker broker, Txn transaction, XmldbURI src) throws TriggerException {
// get the query
final Source query = getQuerySource(broker);
if (query == null) {
return null;
}
// avoid infinite recursion by allowing just one trigger per thread
if (isBefore && !TriggerStatePerThread.verifyUniqueTriggerPerThreadBeforePrepare(this, src)) {
return null;
} else if (!isBefore && !TriggerStatePerThread.verifyUniqueTriggerPerThreadBeforeFinish(this, src)) {
return null;
}
TriggerStatePerThread.setTransaction(transaction);
final XQueryContext context = new XQueryContext(broker.getBrokerPool());
if (query instanceof DBSource) {
context.setModuleLoadPath(XmldbURI.EMBEDDED_SERVER_URI_PREFIX + ((DBSource) query).getDocumentPath().removeLastSegment().toString());
}
CompiledXQuery compiledQuery;
try {
// compile the XQuery
compiledQuery = service.compile(context, query);
// declare user defined parameters as external variables
for (Object o : userDefinedVariables.keySet()) {
final String varName = (String) o;
final String varValue = userDefinedVariables.getProperty(varName);
context.declareVariable(bindingPrefix + varName, new StringValue(varValue));
}
// reset & prepareForExecution for execution
compiledQuery.reset();
context.getWatchDog().reset();
// do any preparation before execution
context.prepareForExecution();
return compiledQuery;
} catch (final XPathException | IOException | PermissionDeniedException e) {
LOG.warn(e.getMessage(), e);
TriggerStatePerThread.setTriggerRunningState(TriggerStatePerThread.NO_TRIGGER_RUNNING, this, null);
TriggerStatePerThread.setTransaction(null);
throw new TriggerException(PREPARE_EXCEPTION_MESSAGE, e);
}
}
Aggregations