Search in sources :

Example 1 with FeatureQuery

use of org.apache.sis.storage.FeatureQuery in project sis by apache.

the class SQLStoreTest method verifySimpleQueryWithLimit.

/**
 * Requests features with a limit on the number of items.
 *
 * @param  dataset  the store on which to query the features.
 * @throws DataStoreException if an error occurred during query execution.
 */
private void verifySimpleQueryWithLimit(final SQLStore dataset) throws DataStoreException {
    final FeatureSet parks = dataset.findResource("Parks");
    final FeatureQuery query = new FeatureQuery();
    query.setLimit(2);
    final FeatureSet subset = parks.subset(query);
    assertEquals(2, subset.features(false).count());
}
Also used : FeatureSet(org.apache.sis.storage.FeatureSet) FeatureQuery(org.apache.sis.storage.FeatureQuery)

Example 2 with FeatureQuery

use of org.apache.sis.storage.FeatureQuery in project sis by apache.

the class SQLStoreTest method verifyNestedSQLQuery.

/**
 * Tests a user-supplied query followed by another query built from filters.
 */
private void verifyNestedSQLQuery(final StorageConnector connector) throws Exception {
    try (SQLStore store = new SQLStore(null, connector, ResourceDefinition.query("MyParks", "SELECT * FROM " + SCHEMA + ".\"Parks\" ORDER BY \"native_name\" DESC"))) {
        final FeatureSet parks = store.findResource("MyParks");
        /*
             * Add a filter for parks in France.
             */
        final FeatureQuery query = new FeatureQuery();
        query.setSelection(FF.equal(FF.property("country"), FF.literal("FRA")));
        query.setProjection(FF.property("native_name"));
        final FeatureSet frenchParks = parks.subset(query);
        /*
             * Verify the feature type.
             */
        final AbstractIdentifiedType property = TestUtilities.getSingleton(frenchParks.getType().getProperties(true));
        assertEquals("native_name", property.getName().toString());
        assertEquals(String.class, ((DefaultAttributeType<?>) property).getValueClass());
        /*
             * Verify the values.
             */
        final Object[] result;
        try (Stream<AbstractFeature> fs = frenchParks.features(false)) {
            result = fs.map(f -> f.getPropertyValue("native_name")).toArray();
        }
        assertArrayEquals(new String[] { "Jardin du Luxembourg", "Jardin des Tuileries" }, result);
    }
}
Also used : AbstractFeature(org.apache.sis.feature.AbstractFeature) FeatureSet(org.apache.sis.storage.FeatureSet) FeatureQuery(org.apache.sis.storage.FeatureQuery) AbstractIdentifiedType(org.apache.sis.feature.AbstractIdentifiedType)

Example 3 with FeatureQuery

use of org.apache.sis.storage.FeatureQuery in project sis by apache.

the class SQLStoreTest method verifySimpleQuerySorting.

/**
 * Requests a new set of features sorted by country code and park names,
 * and verifies that values are sorted as expected.
 *
 * @param  dataset  the store on which to query the features.
 * @throws DataStoreException if an error occurred during query execution.
 */
private void verifySimpleQuerySorting(final SQLStore dataset) throws DataStoreException {
    /*
         * Property that we are going to request and expected result.
         * Note that "english_name" below is a property of the "Park" feature,
         * not to be confused with the property of same name in "City" feature.
         */
    final String desiredProperty = "english_name";
    final String[] expectedValues = { "Shinjuku Gyoen", "Yoyogi-kōen", "Luxembourg Garden", "Tuileries Garden", "Mount Royal" };
    /*
         * Build the query and get a new set of features.
         */
    final FeatureSet parks = dataset.findResource("Parks");
    final FeatureQuery query = new FeatureQuery();
    query.setProjection(FF.property(desiredProperty));
    final FeatureSet subset = parks.subset(query);
    /*
         * Verify that all features have the expected property.
         */
    final Object[] values;
    try (Stream<AbstractFeature> features = subset.features(false)) {
        values = features.map(f -> {
            final AbstractIdentifiedType p = TestUtilities.getSingleton(f.getType().getProperties(true));
            assertEquals("Feature has wrong property.", desiredProperty, p.getName().toString());
            return f.getPropertyValue(desiredProperty);
        }).toArray();
    }
    assertEquals(new HashSet<>(Arrays.asList(expectedValues)), new HashSet<>(Arrays.asList(values)));
}
Also used : AbstractFeature(org.apache.sis.feature.AbstractFeature) FeatureSet(org.apache.sis.storage.FeatureSet) FeatureQuery(org.apache.sis.storage.FeatureQuery) AbstractIdentifiedType(org.apache.sis.feature.AbstractIdentifiedType)

Example 4 with FeatureQuery

use of org.apache.sis.storage.FeatureQuery in project sis by apache.

the class SQLStoreTest method verifySimpleWhere.

/**
 * Requests a new set of features filtered by an arbitrary condition,
 * and verifies that we get only the expected values.
 *
 * @param  dataset  the store on which to query the features.
 * @throws DataStoreException if an error occurred during query execution.
 */
private void verifySimpleWhere(SQLStore dataset) throws Exception {
    /*
         * Property that we are going to request and expected result.
         */
    final String desiredProperty = "native_name";
    final String[] expectedValues = { "Montréal", "Québec" };
    /*
         * Build the query and get a new set of features. The values returned by the database can be in any order,
         * so we use `assertSetEquals(…)` for making the test insensitive to feature order. An alternative would be
         * to add a `query.setSortBy(…)` call, but we avoid that for making this test only about the `WHERE` clause.
         */
    final FeatureSet cities = dataset.findResource("Cities");
    final FeatureQuery query = new FeatureQuery();
    query.setSelection(FF.equal(FF.property("country"), FF.literal("CAN")));
    final Object[] names;
    try (Stream<AbstractFeature> features = cities.subset(query).features(false)) {
        names = features.map(f -> f.getPropertyValue(desiredProperty)).toArray();
    }
    assertSetEquals(Arrays.asList(expectedValues), Arrays.asList(names));
}
Also used : AbstractFeature(org.apache.sis.feature.AbstractFeature) FeatureSet(org.apache.sis.storage.FeatureSet) FeatureQuery(org.apache.sis.storage.FeatureQuery)

Example 5 with FeatureQuery

use of org.apache.sis.storage.FeatureQuery in project sis by apache.

the class SQLStoreTest method verifyWhereOnLink.

/**
 * Requests a new set of features filtered by a condition on the "sis:identifier" property.
 * The optimizer should replace that link by a condition on the actual column.
 *
 * @param  dataset  the store on which to query the features.
 * @throws DataStoreException if an error occurred during query execution.
 */
private void verifyWhereOnLink(SQLStore dataset) throws Exception {
    final String desiredProperty = "native_name";
    final String[] expectedValues = { "Canada" };
    final FeatureSet countries = dataset.findResource("Countries");
    final FeatureQuery query = new FeatureQuery();
    query.setSelection(FF.equal(FF.property("sis:identifier"), FF.literal("CAN")));
    final String executionMode;
    final Object[] names;
    try (Stream<AbstractFeature> features = countries.subset(query).features(false)) {
        executionMode = features.toString();
        names = features.map(f -> f.getPropertyValue(desiredProperty)).toArray();
    }
    assertArrayEquals(expectedValues, names);
    /*
         * Verify that the query is executed with a SQL statement, not with Java code.
         * The use of SQL is made possible by the replacement of "sis:identifier" link
         * by a reference to "code" column. If that replacement is not properly done,
         * then the "predicates" value would be "Java" instead of "SQL".
         */
    assertEquals("FeatureStream[table=“Countries”, predicates=“SQL”]", executionMode);
}
Also used : AbstractFeature(org.apache.sis.feature.AbstractFeature) FeatureSet(org.apache.sis.storage.FeatureSet) FeatureQuery(org.apache.sis.storage.FeatureQuery)

Aggregations

FeatureQuery (org.apache.sis.storage.FeatureQuery)5 FeatureSet (org.apache.sis.storage.FeatureSet)5 AbstractFeature (org.apache.sis.feature.AbstractFeature)4 AbstractIdentifiedType (org.apache.sis.feature.AbstractIdentifiedType)2