} else { Transaction tx = getState().getTransaction(); Connection cx = null; cx = getDataStore().getConnection(tx); SimpleFeatureType featureType = getSchema(); try { getDataStore() .ensureAuthorization(featureType, preFilter, getTransaction(), cx); } catch (SQLException e) { throw (IOException) new IOException().initCause(e); ContentState state = getEntry().getState(transaction); ReferencedEnvelope bounds = ReferencedEnvelope.create(getSchema().getCoordinateReferenceSystem()); if (state.hasListener()) { getBounds(new Query(getSchema().getTypeName(), preFilter)); if (before != null && !before.isEmpty()) { bounds = before; getDataStore().delete(featureType, preFilter, cx); if (state.hasListener()) { getDataStore().closeSafe(cx);
public void testUnlockFeaturesInvalidFilter() throws Exception { FeatureLock lock = FeatureLockFactory.generate(tname("ft1"), 60 * 60 * 1000); try (Transaction tx = new DefaultTransaction()) { store.setTransaction(tx); store.setFeatureLock(lock); tx.addAuthorization(lock.getAuthorization()); store.lockFeatures(); // uhnlock features FilterFactory ff = dataStore.getFilterFactory(); PropertyIsEqualTo f = ff.equals(ff.property(aname("invalidProperty")), ff.literal(1)); try { store.unLockFeatures(new Query(store.getSchema().getTypeName(), f)); fail("Should have failed with an exception, the filter is not valid"); } catch (Exception e) { // fine } store.unLockFeatures(); } }
/** * Creates a new instance of {@link JDBCFeatureStore}. * * @see ContentDataStore#createFeatureSource(ContentEntry) */ protected ContentFeatureSource createFeatureSource(ContentEntry entry) throws IOException { // grab the schema, it carries a flag telling us if the feature type is read only SimpleFeatureType schema = entry.getState(Transaction.AUTO_COMMIT).getFeatureType(); if (schema == null) { // if the schema still haven't been computed, force its computation so // that we can decide if the feature type is read only schema = new JDBCFeatureSource(entry, null).buildFeatureType(); entry.getState(Transaction.AUTO_COMMIT).setFeatureType(schema); } Object readOnlyMarker = schema.getUserData().get(JDBC_READ_ONLY); if (Boolean.TRUE.equals(readOnlyMarker)) { return new JDBCFeatureSource(entry, null); } return new JDBCFeatureStore(entry, null); }
/** * Check null encoding is working properly * * @throws IOException */ public void testAddNullAttributes() throws IOException { SimpleFeatureBuilder b = new SimpleFeatureBuilder(featureStore.getSchema()); SimpleFeature nullFeature = b.buildFeature("testId"); featureStore.addFeatures(Arrays.asList(nullFeature)); }
/** * Check null encoding is working properly * * @throws IOException */ public void testModifyNullAttributes() throws IOException { String[] attributeNames = new String[featureStore.getSchema().getAttributeCount()]; for (int i = 0; i < attributeNames.length; i++) { attributeNames[i] = featureStore.getSchema().getDescriptor(i).getLocalName(); } Object[] nulls = new Object[attributeNames.length]; featureStore.modifyFeatures(attributeNames, nulls, Filter.INCLUDE); }
Connection cx = getDataStore().getConnection(getState()); Query queryNone = new Query(query); queryNone.setFilter(Filter.EXCLUDE); if (getDataStore().getSQLDialect() instanceof PreparedStatementSQLDialect) { PreparedStatement ps = getDataStore().selectSQLPS(getSchema(), queryNone, cx); return new JDBCInsertFeatureWriter(ps, cx, delegate, query); } else { String sql = getDataStore().selectSQL(getSchema(), queryNone); getDataStore().getLogger().fine(sql); if (getDataStore().getSQLDialect() instanceof PreparedStatementSQLDialect) { PreparedStatement ps = getDataStore().selectSQLPS(getSchema(), preQuery, cx); if ((flags | WRITER_UPDATE) == WRITER_UPDATE) { writer = new JDBCUpdateFeatureWriter(ps, cx, delegate, query); String sql = getDataStore().selectSQL(getSchema(), preQuery); getDataStore().getLogger().fine(sql); getDataStore().closeSafe(cx);
@Override public void removeFeatures(Filter filter) throws IOException { Filter[] splitted = delegate.splitFilter(filter); Filter preFilter = splitted[0]; Filter postFilter = splitted[1]; if(postFilter != null && !Filter.INCLUDE.equals(postFilter)) { // we don't have a fast way to perform this delete, let's do it the // feature by feature way then super.removeFeatures(filter); } else { // let's grab the connection Connection cx = getDataStore().getConnection(getState()); // we want to support a "batch" delete, but we need to be weary of locks SimpleFeatureType featureType = getSchema(); try { getDataStore().ensureAuthorization(featureType, preFilter, getTransaction(), cx); } catch (SQLException e) { throw (IOException) new IOException().initCause( e ); } getDataStore().delete(featureType, preFilter, cx); } } }
public void testUniqueIndex() throws Exception { JDBCFeatureStore fs = (JDBCFeatureStore) dataStore.getFeatureSource(tname("uniq")); assertEquals(1, fs.getPrimaryKey().getColumns().size()); assertTrue( fs.getPrimaryKey().getColumns().get(0) instanceof NonIncrementingPrimaryKeyColumn); assertNull(fs.getSchema().getDescriptor(fs.getPrimaryKey().getColumns().get(0).getName())); FeatureCollection features = fs.getFeatures(); assertPrimaryKeyValues(features, 3); addFeature(fs.getSchema(), fs); assertPrimaryKeyValues(features, 4); }
public void testAddInTransaction() throws IOException { SimpleFeatureBuilder b = new SimpleFeatureBuilder(featureStore.getSchema()); DefaultFeatureCollection collection = new DefaultFeatureCollection(null, featureStore.getSchema()); b.set(aname("intProperty"), Integer.valueOf(3)); b.set(aname("geometry"), new GeometryFactory().createPoint(new Coordinate(3, 3))); collection.add(b.buildFeature(null)); FeatureEventWatcher watcher = new FeatureEventWatcher(); try (Transaction t = new DefaultTransaction()) { featureStore.setTransaction(t); featureStore.addFeatureListener(watcher); JDBCFeatureStore featureStore2 = (JDBCFeatureStore) dataStore.getFeatureSource(featureStore.getName().getLocalPart()); List<FeatureId> fids = featureStore.addFeatures((SimpleFeatureCollection) collection); assertEquals(1, fids.size()); // check the store with the transaction sees the new features, but the other store does // not assertEquals(4, featureStore.getFeatures().size()); assertEquals(3, featureStore2.getFeatures().size()); // check that after the commit everybody sees 4 t.commit(); assertEquals(4, featureStore.getFeatures().size()); assertEquals(4, featureStore2.getFeatures().size()); } }
public void testAcceptsVisitor() throws Exception { class TotalVisitor implements FeatureVisitor { int total = 0; public void visit(Feature feature) { total++; } } TotalVisitor visitor = new TotalVisitor(); // initial test on Transaction.AUTO_COMMIT int count = featureSource.getCount(Query.ALL); featureSource.accepts(Query.ALL, visitor, null); assertEquals(count, visitor.total); visitor.total = 0; // reset // test on a transaction JDBCFeatureStore ft1 = (JDBCFeatureStore) dataStore.getFeatureSource(tname("ft1")); try (Transaction transaction = new DefaultTransaction()) { ft1.setTransaction(transaction); Connection connection = ft1.getDataStore().getConnection(ft1.getState()); assertFalse("connection established", connection.isClosed()); ft1.accepts(Query.ALL, visitor, null); assertFalse("connection maintained", connection.isClosed()); } } }
assertEquals(0, store.getCount(new Query(tname("ft1"), f0))); store.setTransaction(tx); store.setFeatureLock(lock); tx.addAuthorization(lock.getAuthorization()); store.lockFeatures(f1); store.setTransaction(tx1); ad = store.getSchema().getDescriptor(aname("intProperty")); v = Integer.valueOf(1000); store.modifyFeatures(ad, v, f1); fail("Locked feature should not be modified."); } catch (FeatureLockException e) { store.setTransaction(tx); store.modifyFeatures(ad, v, f1); tx.commit(); assertEquals(1, store.getCount(new Query(tname("ft1"), f0)));
public void testModifyFeaturesSingleAttribute() throws IOException { SimpleFeatureType t = featureStore.getSchema(); featureStore.modifyFeatures( t.getDescriptor(aname("stringProperty")), "foo", Filter.INCLUDE); SimpleFeatureCollection features = featureStore.getFeatures(); try (SimpleFeatureIterator i = features.features()) { assertTrue(i.hasNext()); while (i.hasNext()) { SimpleFeature feature = (SimpleFeature) i.next(); assertEquals("foo", feature.getAttribute(aname("stringProperty"))); } } }
try (SimpleFeatureIterator fi = featureStore.getFeatures().features()) { while (fi.hasNext()) { fi.next(); SimpleFeatureBuilder b = new SimpleFeatureBuilder(featureStore.getSchema()); DefaultFeatureCollection collection = new DefaultFeatureCollection(null, featureStore.getSchema()); featureStore.setTransaction(t); for (int i = 3; i < 6; i++) { b.set(aname("intProperty"), Integer.valueOf(i)); collection.add(b.buildFeature(null)); featureStore.addFeatures((SimpleFeatureCollection) collection); t.commit(); assertTrue(mockListener.onBorrowCalled);
public void testDeleteLockedFeatures() throws Exception { FeatureLock lock = FeatureLockFactory.generate(tname("ft1"), 60 * 60 * 1000); try (Transaction tx = new DefaultTransaction()) { store.setTransaction(tx); store.setFeatureLock(lock); tx.addAuthorization(lock.getAuthorization()); FilterFactory ff = dataStore.getFilterFactory(); Filter f1 = ff.id(Collections.singleton(ff.featureId(tname("ft1") + ".1"))); assertEquals(1, store.lockFeatures(f1)); try (Transaction tx1 = new DefaultTransaction()) { store.setTransaction(tx1); try { store.removeFeatures(f1); fail("Locked feature should not be deleted."); } catch (FeatureLockException e) { } } store.setTransaction(tx); store.removeFeatures(f1); tx.commit(); } }
public void testLockFeaturesWithInvalidFilter() throws Exception { FeatureLock lock = FeatureLockFactory.generate(tname("ft1"), 60 * 60 * 1000); try (Transaction tx = new DefaultTransaction()) { store.setTransaction(tx); store.setFeatureLock(lock); // lock features FilterFactory ff = dataStore.getFilterFactory(); PropertyIsEqualTo f = ff.equals(ff.property(aname("invalidProperty")), ff.literal(1)); try { store.lockFeatures(f); fail("Should have failed with an exception, the filter is not valid"); } catch (Exception e) { // fine } } }
/** * Creates the new feature store. * * @param entry The datastore entry. * @param query The defining query. */ @SuppressWarnings("unchecked") public JDBCFeatureStore(ContentEntry entry, Query query) throws IOException { super(entry, query); delegate = new JDBCFeatureSource(entry, query) { @Override public void setTransaction(Transaction transaction) { super.setTransaction(transaction); // keep this feature store in sync JDBCFeatureStore.this.setTransaction(transaction); } }; Set<Hints.Key> jdbcHints = new HashSet<Hints.Key>(); jdbcHints.addAll((Set<Hints.Key>) (Set<?>) delegate.getSupportedHints()); getDataStore().getSQLDialect().addSupportedHints(jdbcHints); hints = Collections.unmodifiableSet(jdbcHints); }
public void testUpdateWithExposePrimaryKeyColumns() throws Exception { JDBCFeatureStore fs = (JDBCFeatureStore) dataStore.getFeatureSource(tname("nonfirst")); fs.setExposePrimaryKeyColumns(true); for (AttributeDescriptor ad : fs.getSchema().getAttributeDescriptors()) { if (Number.class.isAssignableFrom(ad.getType().getBinding())) { key = ad.getLocalName(); try (FeatureReader r = fs.getReader()) { assertTrue(r.hasNext()); Filter filter = ff.equal(ff.property(key), ff.literal(keyValue), false); assertEquals(1, fs.getCount(new Query(tname("nonfirst"), filter))); fs.modifyFeatures(key, 10, filter); fail("expected exception"); } catch (IllegalArgumentException e) { fs.modifyFeatures( new String[] {aname("name"), key, aname("geom")}, new Object[] {"foo", 10, null}, fs.getReader(ff.equal(ff.property(key), ff.literal(keyValue), true))) { assertTrue(r.hasNext());
public void testModifyFeatures() throws IOException { FeatureEventWatcher watcher = new FeatureEventWatcher(); SimpleFeatureType t = featureStore.getSchema(); featureStore.addFeatureListener(watcher); featureStore.modifyFeatures( new AttributeDescriptor[] {t.getDescriptor(aname("stringProperty"))}, new Object[] {"foo"}, Filter.INCLUDE); assertTrue("check that at least one event was issued", watcher.count > 0); assertEquals("Should be an update event", Type.CHANGED, watcher.type); assertEquals(Filter.INCLUDE, watcher.filter); SimpleFeatureCollection features = featureStore.getFeatures(); try (SimpleFeatureIterator i = features.features()) { assertTrue(i.hasNext()); while (i.hasNext()) { SimpleFeature feature = (SimpleFeature) i.next(); assertEquals("foo", feature.getAttribute(aname("stringProperty"))); } } }
public void testAddFeaturesUseProvidedFid() throws IOException { assertTrue(featureStore.getQueryCapabilities().isUseProvidedFIDSupported()); SimpleFeatureBuilder b = new SimpleFeatureBuilder(featureStore.getSchema()); DefaultFeatureCollection collection = new DefaultFeatureCollection(null, featureStore.getSchema()); collection.add(b.buildFeature(typeName + "." + (i * 10))); List<FeatureId> fids = featureStore.addFeatures((SimpleFeatureCollection) collection); SimpleFeatureBuilder.createDefaultFeatureIdentifier(typeName + ".50"))); SimpleFeatureCollection features = featureStore.getFeatures(); assertEquals(6, features.size()); Id filter = ff.id(Collections.singleton(identifier)); features = featureStore.getFeatures(filter); assertEquals(1, features.size());
public void testAddFeatures() throws IOException { SimpleFeatureBuilder b = new SimpleFeatureBuilder(featureStore.getSchema()); DefaultFeatureCollection collection = new DefaultFeatureCollection(null, featureStore.getSchema()); collection.add(b.buildFeature(null)); featureStore.addFeatureListener(watcher); List<FeatureId> fids = featureStore.addFeatures((SimpleFeatureCollection) collection); assertEquals(watcher.bounds, collection.getBounds()); SimpleFeatureCollection features = featureStore.getFeatures(); assertEquals(6, features.size()); Id filter = ff.id(Collections.singleton(identifier)); features = featureStore.getFeatures(filter); assertEquals(1, features.size());