Search in sources :

Example 46 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class LowLevelTextTest method setUp.

@Before
public void setUp() throws DatabaseConfigurationException, EXistException, XPathException, PermissionDeniedException, IOException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
    xqueryPool = pool.getXQueryPool();
    stringSource = new StringSource(TEST_XQUERY_SOURCE);
    final XQuery xquery = pool.getXQueryService();
    final XQueryContext context = new XQueryContext(broker.getBrokerPool());
    preCompiledXQuery = xquery.compile(context, stringSource);
}
Also used : CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) XQueryContext(org.exist.xquery.XQueryContext) StringSource(org.exist.source.StringSource)

Example 47 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class DebuggeeJointImpl method evalution.

@Override
public String evalution(String script) throws Exception {
    Database db = compiledXQuery.getContext().getDatabase();
    // TODO: account required
    try (final DBBroker broker = db.getBroker()) {
        XQueryContext context = compiledXQuery.getContext().copyContext();
        context.setDebuggeeJoint(null);
        context.undeclareGlobalVariable(Debuggee.SESSION);
        context.undeclareGlobalVariable(Debuggee.IDEKEY);
        XQuery service = broker.getBrokerPool().getXQueryService();
        CompiledXQuery compiled = service.compile(broker, context, script);
        Sequence resultSequence = service.execute(broker, compiled, null);
        SAXSerializer sax = null;
        Serializer serializer = broker.getSerializer();
        serializer.reset();
        try {
            sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
            Properties outputProps = new Properties();
            StringWriter writer = new StringWriter();
            sax.setOutput(writer, outputProps);
            serializer.setSAXHandlers(sax, sax);
            for (SequenceIterator i = resultSequence.iterate(); i.hasNext(); ) {
                Item next = i.nextItem();
                if (Type.subTypeOf(next.getType(), Type.NODE))
                    serializer.toSAX((NodeValue) next);
                else
                    writer.write(next.getStringValue());
            }
            return writer.toString();
        } finally {
            if (sax != null) {
                SerializerPool.getInstance().returnObject(sax);
            }
        }
    }
}
Also used : CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) DBBroker(org.exist.storage.DBBroker) StringWriter(java.io.StringWriter) Database(org.exist.Database) SAXSerializer(org.exist.util.serializer.SAXSerializer) SAXSerializer(org.exist.util.serializer.SAXSerializer) Serializer(org.exist.storage.serializers.Serializer)

Example 48 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class ScriptRunner method run.

/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
@Override
public void run() {
    try {
        final Database db = BrokerPool.getInstance();
        db.addStatusObserver(this);
        try (final DBBroker broker = db.getBroker()) {
            XQuery xquery = broker.getBrokerPool().getXQueryService();
            xquery.execute(broker, expression, null);
        // XQueryContext context = expression.getContext();
        // 
        // expression.reset();
        // 
        // context.setBroker(broker);
        // context.getWatchDog().reset();
        // 
        // //do any preparation before execution
        // context.prepare();
        // 
        // context.getProfiler().traceQueryStart();
        // broker.getBrokerPool().getProcessMonitor().queryStarted(context.getWatchDog());
        // try {
        // Sequence result = expression.eval(null);
        // 
        // if(outputProperties != null)
        // context.checkOptions(outputProperties); //must be done before context.reset!
        // 
        // //return result;
        // } finally {
        // context.getProfiler().traceQueryEnd(context);
        // expression.reset();
        // context.reset();
        // broker.getBrokerPool().getProcessMonitor().queryCompleted(context.getWatchDog());
        // }
        }
    } catch (Exception e) {
        e.printStackTrace();
        exception = e;
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) Database(org.exist.Database) EXistException(org.exist.EXistException)

Example 49 with XQuery

use of org.exist.xquery.XQuery 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);
            }
        };
    }
}
Also used : XQueryPool(org.exist.storage.XQueryPool) Txn(org.exist.storage.txn.Txn) BrokerPool(org.exist.storage.BrokerPool) StringSource(org.exist.source.StringSource) ManagedCollectionLock(org.exist.storage.lock.ManagedCollectionLock) StringInputSource(org.exist.util.StringInputSource) org.exist.xquery.value(org.exist.xquery.value) IOException(java.io.IOException) ExistEmbeddedServer(org.exist.test.ExistEmbeddedServer) MimeType(org.exist.util.MimeType) Source(org.exist.source.Source) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) DBBroker(org.exist.storage.DBBroker) Collection(org.exist.collections.Collection) XmldbURI(org.exist.xmldb.XmldbURI) Optional(java.util.Optional) XQueryPool(org.exist.storage.XQueryPool) Lock(org.exist.storage.lock.Lock) ClassRule(org.junit.ClassRule) XQueryContext(org.exist.xquery.XQueryContext) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) DBBroker(org.exist.storage.DBBroker) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) StringSource(org.exist.source.StringSource) StringSource(org.exist.source.StringSource) StringInputSource(org.exist.util.StringInputSource) Source(org.exist.source.Source) BrokerPool(org.exist.storage.BrokerPool)

Example 50 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class GMLIndexTest method highLevelSearch.

@Test
public void highLevelSearch() throws EXistException, PermissionDeniedException, XPathException {
    final BrokerPool pool = server.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        XQuery xquery = pool.getXQueryService();
        assertNotNull(xquery);
        String query = "import module namespace spatial='http://exist-db.org/xquery/spatial' " + "at 'java:org.exist.xquery.modules.spatial.SpatialModule'; " + "declare namespace gml = 'http://www.opengis.net/gml'; " + "spatial:equals(//gml:*, //gml:Point[gml:coordinates[. = '278697.450,187740.900']])";
        Sequence seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        assertTrue(seq.getItemCount() > 0);
        query = "import module namespace spatial='http://exist-db.org/xquery/spatial' " + "at 'java:org.exist.xquery.modules.spatial.SpatialModule'; " + "declare namespace gml = 'http://www.opengis.net/gml'; " + "spatial:disjoint(//gml:*, //gml:Point[gml:coordinates[. = '278697.450,187740.900']])";
        seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        assertTrue(seq.getItemCount() > 0);
        query = "import module namespace spatial='http://exist-db.org/xquery/spatial' " + "at 'java:org.exist.xquery.modules.spatial.SpatialModule'; " + "declare namespace gml = 'http://www.opengis.net/gml'; " + "spatial:intersects(//gml:*, //gml:Point[gml:coordinates[. = '278697.450,187740.900']])";
        seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        assertTrue(seq.getItemCount() > 0);
        query = "import module namespace spatial='http://exist-db.org/xquery/spatial' " + "at 'java:org.exist.xquery.modules.spatial.SpatialModule'; " + "declare namespace gml = 'http://www.opengis.net/gml'; " + "spatial:touches(//gml:*, //gml:Point[gml:coordinates[. = '278697.450,187740.900']])";
        seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        // assertTrue(seq.getItemCount() > 0);
        query = "import module namespace spatial='http://exist-db.org/xquery/spatial' " + "at 'java:org.exist.xquery.modules.spatial.SpatialModule'; " + "declare namespace gml = 'http://www.opengis.net/gml'; " + "spatial:crosses(//gml:*, //gml:Point[gml:coordinates[. = '278697.450,187740.900']])";
        seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        // assertTrue(seq.getItemCount() > 0);
        query = "import module namespace spatial='http://exist-db.org/xquery/spatial' " + "at 'java:org.exist.xquery.modules.spatial.SpatialModule'; " + "declare namespace gml = 'http://www.opengis.net/gml'; " + "spatial:within(//gml:*, //gml:Point[gml:coordinates[. = '278697.450,187740.900']])";
        seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        assertTrue(seq.getItemCount() > 0);
        query = "import module namespace spatial='http://exist-db.org/xquery/spatial' " + "at 'java:org.exist.xquery.modules.spatial.SpatialModule'; " + "declare namespace gml = 'http://www.opengis.net/gml'; " + "spatial:contains(//gml:*, //gml:Point[gml:coordinates[. = '278697.450,187740.900']])";
        seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        assertTrue(seq.getItemCount() > 0);
        query = "import module namespace spatial='http://exist-db.org/xquery/spatial' " + "at 'java:org.exist.xquery.modules.spatial.SpatialModule'; " + "declare namespace gml = 'http://www.opengis.net/gml'; " + "spatial:overlaps(//gml:*, //gml:Point[gml:coordinates[. = '278697.450,187740.900']])";
        seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        // assertTrue(seq.getItemCount() > 0);
        // Tests with empty sequences
        query = "import module namespace spatial='http://exist-db.org/xquery/spatial' " + "at 'java:org.exist.xquery.modules.spatial.SpatialModule'; " + "declare namespace gml = 'http://www.opengis.net/gml'; " + "spatial:equals(//gml:*, ())";
        seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        assertTrue(seq.getItemCount() > 0);
        query = "import module namespace spatial='http://exist-db.org/xquery/spatial' " + "at 'java:org.exist.xquery.modules.spatial.SpatialModule'; " + "declare namespace gml = 'http://www.opengis.net/gml'; " + "spatial:overlaps((), //gml:Point[gml:coordinates[. = '278697.450,187740.900']])";
        seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        assertEquals(0, seq.getItemCount());
        // In-memory test
        query = "import module namespace spatial='http://exist-db.org/xquery/spatial' " + "at 'java:org.exist.xquery.modules.spatial.SpatialModule'; " + "declare namespace gml = 'http://www.opengis.net/gml'; " + "spatial:equals(//gml:*, " + IN_MEMORY_GML + ")";
        seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        assertTrue(seq.getItemCount() > 0);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool)

Aggregations

XQuery (org.exist.xquery.XQuery)135 Sequence (org.exist.xquery.value.Sequence)108 DBBroker (org.exist.storage.DBBroker)107 BrokerPool (org.exist.storage.BrokerPool)105 CompiledXQuery (org.exist.xquery.CompiledXQuery)59 Test (org.junit.Test)36 XQueryContext (org.exist.xquery.XQueryContext)33 Txn (org.exist.storage.txn.Txn)32 XPathException (org.exist.xquery.XPathException)21 TransactionManager (org.exist.storage.txn.TransactionManager)17 Item (org.exist.xquery.value.Item)16 InputSource (org.xml.sax.InputSource)16 XQueryPool (org.exist.storage.XQueryPool)15 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)12 DocumentSet (org.exist.dom.persistent.DocumentSet)12 StringReader (java.io.StringReader)11 Properties (java.util.Properties)11 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)11 Modification (org.exist.xupdate.Modification)11 XUpdateProcessor (org.exist.xupdate.XUpdateProcessor)11