use of org.exist.xquery.CompiledXQuery in project exist by eXist-db.
the class DatabaseResources method executeQuery.
public Sequence executeQuery(String queryPath, Map<String, String> params, Subject user) {
final String namespace = params.get(TARGETNAMESPACE);
final String publicId = params.get(PUBLICID);
final String catalogPath = params.get(CATALOG);
final String collection = params.get(COLLECTION);
if (logger.isDebugEnabled()) {
logger.debug("collection={} namespace={} publicId={} catalogPath={}", collection, namespace, publicId, catalogPath);
}
Sequence result = null;
try (final DBBroker broker = brokerPool.get(Optional.ofNullable(user))) {
final XQuery xquery = brokerPool.getXQueryService();
final XQueryContext context = new XQueryContext(brokerPool);
if (collection != null) {
context.declareVariable(COLLECTION, collection);
}
if (namespace != null) {
context.declareVariable(TARGETNAMESPACE, namespace);
}
if (publicId != null) {
context.declareVariable(PUBLICID, publicId);
}
if (catalogPath != null) {
context.declareVariable(CATALOG, catalogPath);
}
CompiledXQuery compiled = xquery.compile(context, new ClassLoaderSource(queryPath));
result = xquery.execute(broker, compiled, null);
} catch (final EXistException | XPathException | IOException | PermissionDeniedException ex) {
logger.error("Problem executing xquery", ex);
result = null;
}
return result;
}
use of org.exist.xquery.CompiledXQuery in project exist by eXist-db.
the class TestDataGenerator method generate.
public Path[] generate(final DBBroker broker, final Collection collection, final String xqueryContent) throws SAXException {
try {
final DocumentSet docs = collection.allDocs(broker, new DefaultDocumentSet(), true);
final XQuery service = broker.getBrokerPool().getXQueryService();
final XQueryContext context = new XQueryContext(broker.getBrokerPool());
context.declareVariable("filename", "");
context.declareVariable("count", "0");
context.setStaticallyKnownDocuments(docs);
final String query = IMPORT + xqueryContent;
final CompiledXQuery compiled = service.compile(context, query);
for (int i = 0; i < count; i++) {
generatedFiles[i] = Files.createTempFile(prefix, ".xml");
context.declareVariable("filename", generatedFiles[i].getFileName().toString());
context.declareVariable("count", new Integer(i));
final Sequence results = service.execute(broker, compiled, Sequence.EMPTY_SEQUENCE);
final Serializer serializer = broker.borrowSerializer();
try (final Writer out = Files.newBufferedWriter(generatedFiles[i], StandardCharsets.UTF_8)) {
final SAXSerializer sax = new SAXSerializer(out, outputProps);
serializer.setSAXHandlers(sax, sax);
for (final SequenceIterator iter = results.iterate(); iter.hasNext(); ) {
final Item item = iter.nextItem();
if (!Type.subTypeOf(item.getType(), Type.NODE)) {
continue;
}
serializer.toSAX((NodeValue) item);
}
} finally {
broker.returnSerializer(serializer);
}
}
} catch (final XPathException | PermissionDeniedException | LockException | IOException e) {
LOG.error(e.getMessage(), e);
throw new SAXException(e.getMessage(), e);
}
return generatedFiles;
}
use of org.exist.xquery.CompiledXQuery 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.xquery.CompiledXQuery in project exist by eXist-db.
the class Util method compileQuery.
public 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 LuceneIndexTest method queryTranslation.
@Test
public void queryTranslation() throws EXistException, CollectionConfigurationException, PermissionDeniedException, SAXException, TriggerException, LockException, IOException, XPathException {
configureAndStore(COLLECTION_CONFIG1, XML7, "test.xml");
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
final XQuery xquery = pool.getXQueryService();
assertNotNull(xquery);
final XQueryContext context = new XQueryContext(broker.getBrokerPool());
final CompiledXQuery compiled = xquery.compile(context, "declare variable $q external; " + "ft:query(//p, parse-xml($q)/query)");
context.declareVariable("q", "<query><term>heiterkeit</term></query>");
Sequence seq = xquery.execute(broker, compiled, null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
context.declareVariable("q", "<query>" + " <bool>" + " <term>heiterkeit</term><term>blablabla</term>" + " </bool>" + "</query>");
seq = xquery.execute(broker, compiled, null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
context.declareVariable("q", "<query>" + " <bool>" + " <term occur='should'>heiterkeit</term><term occur='should'>blablabla</term>" + " </bool>" + "</query>");
seq = xquery.execute(broker, compiled, null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
context.declareVariable("q", "<query>" + " <bool>" + " <term occur='must'>heiterkeit</term><term occur='must'>blablabla</term>" + " </bool>" + "</query>");
seq = xquery.execute(broker, compiled, null);
assertNotNull(seq);
assertEquals(0, seq.getItemCount());
context.declareVariable("q", "<query>" + " <bool>" + " <term occur='must'>heiterkeit</term><term occur='not'>herzen</term>" + " </bool>" + "</query>");
seq = xquery.execute(broker, compiled, null);
assertNotNull(seq);
assertEquals(0, seq.getItemCount());
context.declareVariable("q", "<query>" + " <bool>" + " <phrase occur='must'>wunderbare heiterkeit</phrase><term occur='must'>herzen</term>" + " </bool>" + "</query>");
seq = xquery.execute(broker, compiled, null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
context.declareVariable("q", "<query>" + " <phrase slop='5'>heiterkeit seele eingenommen</phrase>" + "</query>");
seq = xquery.execute(broker, compiled, null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
// phrase with wildcards
context.declareVariable("q", "<query>" + " <phrase slop='5'><term>heiter*</term><term>se?nnnle*</term></phrase>" + "</query>");
seq = xquery.execute(broker, compiled, null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
context.declareVariable("q", "<query>" + " <wildcard>?eiter*</wildcard>" + "</query>");
seq = xquery.execute(broker, compiled, null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
context.declareVariable("q", "<query>" + " <fuzzy max-edits='2'>selee</fuzzy>" + "</query>");
seq = xquery.execute(broker, compiled, null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
context.declareVariable("q", "<query>" + " <bool>" + " <fuzzy occur='must' max-edits='2'>selee</fuzzy>" + " <wildcard occur='should'>bla*</wildcard>" + " </bool>" + "</query>");
seq = xquery.execute(broker, compiled, null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
context.declareVariable("q", "<query>" + " <regex>heit.*keit</regex>" + "</query>");
seq = xquery.execute(broker, compiled, null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
context.declareVariable("q", "<query>" + " <phrase><term>wunderbare</term><regex>heit.*keit</regex></phrase>" + "</query>");
seq = xquery.execute(broker, compiled, null);
assertNotNull(seq);
assertEquals(1, seq.getItemCount());
}
}
Aggregations