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);
}
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);
}
Aggregations