Search in sources :

Example 1 with SQLQuery

use of org.geotoolkit.storage.feature.query.SQLQuery in project geotoolkit by Geomatys.

the class PostgresComplexTypeTest method testHandMadeSQLQuery.

/**
 * Test hand made query.
 */
@Test
public void testHandMadeSQLQuery() throws Exception {
    reload(false);
    final GeometryFactory gf = JTS.getFactory();
    store.createFeatureType(FTYPE_COMPLEX);
    final FeatureType resType = store.getFeatureType(store.getNames().iterator().next().toString());
    final Feature voyage = resType.newInstance();
    voyage.setPropertyValue("identifier", 120l);
    final Feature driver = FTYPE_DRIVER.newInstance();
    driver.setPropertyValue("name", "jean-michel");
    driver.setPropertyValue("code", "BHF:123456");
    voyage.setPropertyValue("driver", driver);
    final Feature stop1 = FTYPE_STOP.newInstance();
    stop1.setPropertyValue("location", gf.createPoint(new Coordinate(-10, 60)));
    stop1.setPropertyValue("time", new Date(5000000));
    final Feature stop2 = FTYPE_STOP.newInstance();
    stop2.setPropertyValue("location", gf.createPoint(new Coordinate(30, 15)));
    stop2.setPropertyValue("time", new Date(6000000));
    final Feature stop3 = FTYPE_STOP.newInstance();
    stop3.setPropertyValue("location", gf.createPoint(new Coordinate(40, -70)));
    stop3.setPropertyValue("time", new Date(7000000));
    voyage.setPropertyValue("stops", Arrays.asList(stop1, stop2, stop3));
    List<ResourceId> addedIds = store.addFeatures(resType.getName().toString(), Collections.singleton(voyage));
    assertEquals(1, addedIds.size());
    assertEquals(FF.resourceId("Voyage.1"), addedIds.get(0));
    final org.apache.sis.storage.Query query = new SQLQuery("SELECT * FROM \"Stop\"", "s1");
    final FeatureReader ite = store.getFeatureReader(query);
    final boolean[] found = new boolean[3];
    try {
        while (ite.hasNext()) {
            final Feature feature = ite.next();
            final Timestamp time = (Timestamp) feature.getPropertyValue("time");
            final Point location = (Point) feature.getPropertyValue("location");
            if (time.getTime() == 5000000) {
                assertEquals(stop1.getPropertyValue("location"), location);
                found[0] = true;
            } else if (time.getTime() == 6000000) {
                assertEquals(stop2.getPropertyValue("location"), location);
                found[1] = true;
            } else if (time.getTime() == 7000000) {
                assertEquals(stop3.getPropertyValue("location"), location);
                found[2] = true;
            } else {
                fail("Unexpected property \n" + feature);
            }
            final Optional<Geometry> geom = FeatureExt.getDefaultGeometryValue(feature).filter(Geometry.class::isInstance).map(Geometry.class::cast);
            Assert.assertTrue(geom.isPresent());
            assertNotNull(JTS.findCoordinateReferenceSystem(geom.get()));
        }
    } finally {
        ite.close();
    }
    for (boolean b : found) assertTrue(b);
}
Also used : FeatureType(org.opengis.feature.FeatureType) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Point(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint) SQLQuery(org.geotoolkit.storage.feature.query.SQLQuery) Feature(org.opengis.feature.Feature) Timestamp(java.sql.Timestamp) Date(java.util.Date) Geometry(org.locationtech.jts.geom.Geometry) Coordinate(org.locationtech.jts.geom.Coordinate) ResourceId(org.opengis.filter.ResourceId) FeatureReader(org.geotoolkit.storage.feature.FeatureReader) Test(org.junit.Test)

Example 2 with SQLQuery

use of org.geotoolkit.storage.feature.query.SQLQuery in project geotoolkit by Geomatys.

the class DefaultJDBCFeatureStore method getFeatureType.

@Override
public FeatureType getFeatureType(Query query) throws DataStoreException, MismatchedFeatureException {
    if (query instanceof SQLQuery) {
        final SQLQuery sqlQuery = (SQLQuery) query;
        final String sql = sqlQuery.getStatement();
        Connection cnx = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            cnx = getDataSource().getConnection();
            stmt = cnx.createStatement();
            rs = stmt.executeQuery(sql);
            return getDatabaseModel().analyzeResult(rs, sqlQuery.getName());
        } catch (SQLException ex) {
            throw new DataStoreException(ex);
        } finally {
            JDBCFeatureStoreUtilities.closeSafe(getLogger(), cnx, stmt, rs);
        }
    }
    return super.getFeatureType(query);
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SQLQuery(org.geotoolkit.storage.feature.query.SQLQuery)

Aggregations

SQLQuery (org.geotoolkit.storage.feature.query.SQLQuery)2 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 Timestamp (java.sql.Timestamp)1 Date (java.util.Date)1 DataStoreException (org.apache.sis.storage.DataStoreException)1 FeatureReader (org.geotoolkit.storage.feature.FeatureReader)1 Test (org.junit.Test)1 Coordinate (org.locationtech.jts.geom.Coordinate)1 Geometry (org.locationtech.jts.geom.Geometry)1 GeometryFactory (org.locationtech.jts.geom.GeometryFactory)1 MultiPoint (org.locationtech.jts.geom.MultiPoint)1 Point (org.locationtech.jts.geom.Point)1 Feature (org.opengis.feature.Feature)1 FeatureType (org.opengis.feature.FeatureType)1 ResourceId (org.opengis.filter.ResourceId)1