use of org.exist.source.StringSource in project exist by eXist-db.
the class Conditional method process.
@Override
public long process(Txn transaction) throws PermissionDeniedException, LockException, EXistException, XPathException, TriggerException {
LOG.debug("Processing xupdate:if ...");
final XQuery xquery = broker.getBrokerPool().getXQueryService();
final XQueryPool pool = broker.getBrokerPool().getXQueryPool();
final Source source = new StringSource(selectStmt);
CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);
XQueryContext context;
if (compiled == null) {
context = new XQueryContext(broker.getBrokerPool());
} else {
context = compiled.getContext();
context.prepareForReuse();
}
// context.setBackwardsCompatibility(true);
context.setStaticallyKnownDocuments(docs);
declareNamespaces(context);
declareVariables(context);
if (compiled == null)
try {
compiled = xquery.compile(context, source);
} catch (final IOException e) {
throw new EXistException("An exception occurred while compiling the query: " + e.getMessage());
}
Sequence seq = null;
try {
seq = xquery.execute(broker, compiled, null);
} finally {
context.runCleanupTasks();
pool.returnCompiledXQuery(source, compiled);
}
if (seq.effectiveBooleanValue()) {
long mods = 0;
for (final Modification modification : modifications) {
mods += modification.process(transaction);
broker.flush();
}
if (LOG.isDebugEnabled()) {
LOG.debug("{} modifications processed.", mods);
}
return mods;
} else {
return 0;
}
}
use of org.exist.source.StringSource in project exist by eXist-db.
the class InternalModuleTest method reusedModuleVariables.
/**
* Similar to {@link #moduleVariables()} but
* re-executes the query to ensure on subsequent
* invocations, reusing the cached query (and query
* context) do not cause problems.
*/
@Test
public void reusedModuleVariables() 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());
moduleVariablesQuery(queryService, querySource, COUNTER.get());
moduleVariablesQuery(queryService, querySource, COUNTER.get());
}
use of org.exist.source.StringSource in project exist by eXist-db.
the class ImportModuleTest method noSuchModuleWithoutLocationHint.
/**
* Checks that XQST0059 is raised if the module to be imported cannot be found (when there is no location hint).
*/
@Test
public void noSuchModuleWithoutLocationHint() throws EXistException, IOException, PermissionDeniedException {
final String query = "import module namespace impl = \"http://example.com/impl\";\n" + "<result>\n" + " <impl1>{impl:f1(\"to impl1\")}</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()) {
// 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 XQST0059");
} catch (final XPathException e) {
transaction.commit();
assertEquals(ErrorCodes.XQST0059, e.getErrorCode());
}
}
}
use of org.exist.source.StringSource in project exist by eXist-db.
the class ImportModuleTest method emptyNamespace.
/**
* Checks that XQST0088 is raised if the namespace part of an `import module` statement is empty.
*/
@Test
public void emptyNamespace() throws EXistException, IOException, PermissionDeniedException, LockException, SAXException {
final String module = "xquery version \"1.0\";\n" + "module namespace impl = \"http://example.com/impl\";\n" + "declare function impl:f1($a as xs:string) as xs:string {\n" + " $a\n" + "};\n";
final String query = "import module namespace impl = \"\" at \"xmldb:exist:///db/impl1.xqm\";\n" + "<result>\n" + " <impl1>{impl:f1(\"to impl1\")}</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 XQST0088");
} catch (final XPathException e) {
transaction.commit();
assertEquals(ErrorCodes.XQST0088, e.getErrorCode());
}
}
}
use of org.exist.source.StringSource in project exist by eXist-db.
the class ImportModuleTest method prefixSameAsOtherNamespaceDeclaration.
/**
* Checks that XQST0033 is raised if the prefix part of an `import module` statement is the same as the prefix
* of a namespace declaration within the same module.
*/
@Test
public void prefixSameAsOtherNamespaceDeclaration() throws EXistException, IOException, SAXException, PermissionDeniedException, LockException {
final String module = "xquery version \"1.0\";\n" + "module namespace impl = \"http://example.com/impl\";\n" + "declare function impl:f1($a as xs:string) as xs:string {\n" + " $a\n" + "};\n";
final String query = "declare namespace impl = \"http://example.com/impl\";\n" + "import module namespace impl = \"http://example.com/impl\" at \"xmldb:exist:///db/impl1.xqm\";\n" + "<result>\n" + " <impl1>{impl:f1(\"to impl1\")}</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 XQST0033");
} catch (final XPathException e) {
transaction.commit();
assertEquals(ErrorCodes.XQST0033, e.getErrorCode());
}
}
}
Aggregations