Search in sources :

Example 51 with XQuery

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

the class GMLIndexTest method scanIndex.

@Test
public void scanIndex() throws EXistException, PermissionDeniedException, XPathException {
    final BrokerPool pool = server.getBrokerPool();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        XQuery xquery = pool.getXQueryService();
        Sequence seq = xquery.execute(broker, "declare namespace gml = 'http://www.opengis.net/gml'; " + "declare function local:key-callback($term as xs:string, $data as xs:int+) as element() { " + "   <entry>" + "     <term>{$term}</term>" + "     <frequency>{$data[1]}</frequency>" + "     <documents>{$data[2]}</documents>" + "     <position>{$data[3]}</position>" + "   </entry> " + "}; " + // "util:index-keys(//gml:*, '', local:key-callback#2, 1000, 'spatial-index')[entry/frequency > 1] ",
        "util:index-keys(//gml:*, '', local:key-callback#2, 1000, 'spatial-index')", null);
        assertNotNull(seq);
        assertTrue(seq.getItemCount() > 1);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool)

Example 52 with XQuery

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

the class GMLIndexTest method gmlProducers.

@Test
public void gmlProducers() throws PermissionDeniedException, XPathException, EXistException {
    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:transform((//gml:Polygon)[1], 'EPSG:4326')";
        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:getWKT((//gml:Polygon)[1])";
        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:buffer((//gml:Polygon)[1], 100)";
        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:buffer((//gml:Polygon)[1], 100, 1)";
        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:getBbox((//gml:Polygon)[1])";
        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:convexHull((//gml:Polygon)[1])";
        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:boundary((//gml:Polygon)[1])";
        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:intersection((//gml:Polygon)[1], (//gml:Polygon)[2])";
        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:union((//gml:Polygon)[1], (//gml:Polygon)[2])";
        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:difference((//gml:Polygon)[1], (//gml:Polygon)[2])";
        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:symetricDifference((//gml:Polygon)[1], (//gml:Polygon)[2])";
        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:transform((), 'EPSG:4326')";
        seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        assertEquals(0, seq.getItemCount());
        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:getWKT(())";
        seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        assertEquals(0, seq.getItemCount());
        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:buffer((), 100)";
        seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        assertEquals(0, seq.getItemCount());
        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:buffer((), 100, 1)";
        seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        assertEquals(0, seq.getItemCount());
        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:getBbox(())";
        seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        assertEquals(0, seq.getItemCount());
        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:convexHull(())";
        seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        assertEquals(0, seq.getItemCount());
        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:boundary(())";
        seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        assertEquals(0, seq.getItemCount());
        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:union((), ())";
        seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        assertEquals(0, seq.getItemCount());
        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:union((//gml:Polygon)[1], ())";
        seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        assertTrue(seq.getItemCount() == 1);
        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:union((), (//gml:Polygon)[1])";
        seq = xquery.execute(broker, query, null);
        assertNotNull(seq);
        assertTrue(seq.getItemCount() == 1);
        // In-memory tests
        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:transform(" + IN_MEMORY_GML + ", 'EPSG:4326')";
        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:buffer(" + IN_MEMORY_GML + ", 100)";
        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:buffer(" + IN_MEMORY_GML + ", 100, 1)";
        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:getBbox(" + IN_MEMORY_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:convexHull(" + IN_MEMORY_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:boundary(" + IN_MEMORY_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:intersection(" + IN_MEMORY_GML + ", (//gml:Polygon)[2])";
        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:union(" + IN_MEMORY_GML + ", (//gml:Polygon)[2])";
        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:difference(" + IN_MEMORY_GML + ", (//gml:Polygon)[2])";
        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:symetricDifference(" + IN_MEMORY_GML + ", (//gml:Polygon)[2])";
        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:intersection((//gml:Polygon)[1]," + IN_MEMORY_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:union((//gml:Polygon)[1]," + IN_MEMORY_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:difference((//gml:Polygon)[1]," + IN_MEMORY_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:symetricDifference((//gml:Polygon)[1]," + 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)

Example 53 with XQuery

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

the class ExampleModuleTest method executeQuery.

private Sequence executeQuery(final String xquery) throws EXistException, PermissionDeniedException, XPathException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final XQuery xqueryService = pool.getXQueryService();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        return xqueryService.execute(broker, xquery, null);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XQuery(org.exist.xquery.XQuery) BrokerPool(org.exist.storage.BrokerPool)

Example 54 with XQuery

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

the class PackageTriggerTest method checkTriggerFires.

@Test
public void checkTriggerFires() throws EXistException, PermissionDeniedException, XPathException {
    final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
    // Create collection and store document to fire trigger
    try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()))) {
        final XQuery xquery = brokerPool.getXQueryService();
        final Sequence result = xquery.execute(broker, "xmldb:create-collection('/db','trigger-test')", null);
        Assert.assertEquals(1, result.getItemCount());
    }
    // Create collection and store document to fire trigger
    try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()))) {
        final XQuery xquery = brokerPool.getXQueryService();
        final Sequence result = xquery.execute(broker, "xmldb:store('/db/trigger-test', 'test.xml', <a>b</a>)", null);
        Assert.assertEquals(1, result.getItemCount());
    }
    // Verify two documents are now in collection
    try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()))) {
        final XQuery xquery = brokerPool.getXQueryService();
        final Sequence result = xquery.execute(broker, "xmldb:get-child-resources('/db/trigger-test')", null);
        Assert.assertEquals("After trigger execution two documents should be in the collection.", 2, result.getItemCount());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 55 with XQuery

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

the class UserXQueryJob method executeXQuery.

private void executeXQuery(final BrokerPool pool, final DBBroker broker, final Source source, final Properties params) throws PermissionDeniedException, XPathException, JobExecutionException {
    XQueryPool xqPool = null;
    CompiledXQuery compiled = null;
    XQueryContext context = null;
    try {
        // execute the xquery
        final XQuery xquery = pool.getXQueryService();
        xqPool = pool.getXQueryPool();
        // try and get a pre-compiled query from the pool
        compiled = xqPool.borrowCompiledXQuery(broker, source);
        if (compiled == null) {
            context = new XQueryContext(pool);
        } else {
            context = compiled.getContext();
            context.prepareForReuse();
        }
        if (source instanceof DBSource) {
            final XmldbURI collectionUri = ((DBSource) source).getDocumentPath().removeLastSegment();
            context.setModuleLoadPath(XmldbURI.EMBEDDED_SERVER_URI.append(collectionUri.getCollectionPath()).toString());
            context.setStaticallyKnownDocuments(new XmldbURI[] { collectionUri });
        }
        if (compiled == null) {
            try {
                compiled = xquery.compile(context, source);
            } catch (final IOException e) {
                abort("Failed to read query from " + xqueryResource);
            }
        }
        // declare any parameters as external variables
        if (params != null) {
            String bindingPrefix = params.getProperty("bindingPrefix");
            if (bindingPrefix == null) {
                bindingPrefix = "local";
            }
            for (final Entry param : params.entrySet()) {
                final String key = (String) param.getKey();
                final String value = (String) param.getValue();
                context.declareVariable(bindingPrefix + ":" + key, new StringValue(value));
            }
        }
        xquery.execute(broker, compiled, null);
    } finally {
        if (context != null) {
            context.runCleanupTasks();
        }
        // return the compiled query to the pool
        if (xqPool != null && source != null && compiled != null) {
            xqPool.returnCompiledXQuery(source, compiled);
        }
    }
}
Also used : XQueryPool(org.exist.storage.XQueryPool) Entry(java.util.Map.Entry) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) DBSource(org.exist.source.DBSource) IOException(java.io.IOException) StringValue(org.exist.xquery.value.StringValue) XmldbURI(org.exist.xmldb.XmldbURI)

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