Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GEOT-2333: removeSchema #308

Merged
merged 1 commit into from
Nov 11, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -653,6 +653,15 @@ public void updateSchema(Name typeName, FeatureType featureType) throws IOExcept
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }


/**
* Not a supported operation.
*
* @see org.geotools.data.DataAccess#removeSchema(org.opengis.feature.type.Name)
*/
public void removeSchema(Name typeName) throws IOException {
throw new UnsupportedOperationException();
}

/** /**
* Return a feature source that can be used to obtain features of a particular name. This name * Return a feature source that can be used to obtain features of a particular name. This name
* would be the mappingName in the TypeMapping if it exists, otherwise it's the target element * would be the mappingName in the TypeMapping if it exists, otherwise it's the target element
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -129,4 +129,13 @@ public void updateSchema(Name typeName, FeatureType featureType) throws IOExcept
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }



/**
* Unsupported operation.
*
* @see org.geotools.data.DataAccess#removeSchema(org.opengis.feature.type.Name)
*/
public void removeSchema(Name typeName) throws IOException {
throw new UnsupportedOperationException("Schema removal not supported");
}
} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ public void updateSchema(String typeName, SimpleFeatureType featureType) throws
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }


@Override
public void removeSchema(Name typeName) throws IOException {
throw new UnsupportedOperationException();
}

@Override
public void removeSchema(String typeName) throws IOException {
throw new UnsupportedOperationException();
}

@Override @Override
public String[] getTypeNames() throws IOException { public String[] getTypeNames() throws IOException {
return new String[] { source.getSchema().getName().getLocalPart() }; return new String[] { source.getSchema().getName().getLocalPart() };
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -131,7 +131,18 @@ public interface DataAccess<T extends FeatureType, F extends Feature> {
*/ */
void updateSchema(Name typeName, T featureType) void updateSchema(Name typeName, T featureType)
throws IOException; throws IOException;


/**
* Used to permanently remove a schema from the underlying storage
* <p>
* This functionality is similar to an "drop table" statement in SQL. Implementation
* is optional; it may not be supported by all servers or files.
* @param typeName
* @throws IOException if the operation failed
* @throws UnsupportedOperation if functionality is not available
*/
void removeSchema(Name typeName) throws IOException;

/** /**
* Names of the available Resources. * Names of the available Resources.
* <p> * <p>
Expand Down
11 changes: 11 additions & 0 deletions modules/library/api/src/main/java/org/geotools/data/DataStore.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ public interface DataStore extends DataAccess<SimpleFeatureType, SimpleFeature>{
void updateSchema(String typeName, SimpleFeatureType featureType) void updateSchema(String typeName, SimpleFeatureType featureType)
throws IOException; throws IOException;


/**
* Used to permanently remove a schema from the underlying storage
* <p>
* This functionality is similar to an "drop table" statement in SQL. Implementation
* is optional; it may not be supported by all servers or files.
* @param typeName
* @throws IOException if the operation failed
* @throws UnsupportedOperation if functionality is not available
*/
void removeSchema(String typeName) throws IOException;

/** /**
* Gets the names of feature types available in this {@code DataStore}. * Gets the names of feature types available in this {@code DataStore}.
* Please note that this is not guaranteed to return a list of unique * Please note that this is not guaranteed to return a list of unique
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -193,4 +193,15 @@ public DataStore getDataStore(String typeName) throws IOException {
return store; return store;
} }


@Override
public void removeSchema(Name name) throws IOException {
removeSchema(name.getLocalPart());

}

@Override
public void removeSchema(String name) throws IOException {
getDataStore(name).removeSchema(name);
}

} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -598,7 +598,18 @@ final protected ContentEntry ensureEntry(Name name)


return entry; return entry;
} }


/**
* Helper method to remove an entry from the cached entry map.
*
* @param name The name of the entry.
*/
final protected void removeEntry(Name name) {
if (entries.containsKey(name)) {
entries.remove(name);
}
}

/** /**
* Creates a set of qualified names corresponding to the types that the * Creates a set of qualified names corresponding to the types that the
* datastore provides. * datastore provides.
Expand Down Expand Up @@ -688,4 +699,18 @@ public SimpleFeatureType getSchema(Name name) throws IOException {
public void updateSchema(Name typeName, SimpleFeatureType featureType) throws IOException { public void updateSchema(Name typeName, SimpleFeatureType featureType) throws IOException {
updateSchema(typeName.getLocalPart(), featureType); updateSchema(typeName.getLocalPart(), featureType);
} }

/**
* @see DataAccess#removeSchema(Name)
*/
public void removeSchema(Name typeName) throws IOException {
throw new UnsupportedOperationException("Schema removal not supported");
}

/**
* @see DataStore#removeSchema(String)
*/
public void removeSchema(String typeName) throws IOException {
throw new UnsupportedOperationException("Schema removal not supported");
}
} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import org.opengis.feature.simple.SimpleFeatureType; import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeDescriptor; import org.opengis.feature.type.AttributeDescriptor;
import org.opengis.feature.type.GeometryDescriptor; import org.opengis.feature.type.GeometryDescriptor;
import org.opengis.feature.type.Name;
import org.opengis.filter.Filter; import org.opengis.filter.Filter;
import org.opengis.filter.Id; import org.opengis.filter.Id;
import org.opengis.filter.PropertyIsLessThanOrEqualTo; import org.opengis.filter.PropertyIsLessThanOrEqualTo;
Expand Down Expand Up @@ -687,6 +688,53 @@ public void createSchema(final SimpleFeatureType featureType)
} }
} }


public void removeSchema(String typeName) throws IOException {
removeSchema(name(typeName));
}

public void removeSchema(Name typeName) throws IOException {
if (entry(typeName) == null) {
String msg = "Schema '" + typeName + "' does not exist";
throw new IllegalArgumentException(msg);
}

//check for virtual table
if (virtualTables.containsKey(typeName.getLocalPart())) {
removeVirtualTable(typeName.getLocalPart());
return;
}

SimpleFeatureType featureType = getSchema(typeName);

//execute the drop table statement
Connection cx = createConnection();
try {
//give the dialect a chance to cleanup pre
dialect.preDropTable(databaseSchema, featureType, cx);

String sql = dropTableSQL(featureType, cx);
LOGGER.log(Level.FINE, "Drop schema: {0}", sql);

Statement st = cx.createStatement();

try {
st.execute(sql);
} finally {
closeSafe(st);
}

dialect.postDropTable(databaseSchema, featureType, cx);
removeEntry(typeName);
}
catch(Exception e) {
String msg = "Error occurred dropping table";
throw (IOException) new IOException(msg).initCause(e);
}
finally {
closeSafe(cx);
}
}

/** /**
* *
*/ */
Expand Down Expand Up @@ -1980,6 +2028,19 @@ protected String findPrimaryKeyColumnName(SimpleFeatureType featureType) {
return "fid"; return "fid";
} }


/**
* Generates a 'DROP TABLE' sql statement.
*/
protected String dropTableSQL(SimpleFeatureType featureType, Connection cx)
throws Exception {
StringBuffer sql = new StringBuffer();
sql.append("DROP TABLE ");

encodeTableName(featureType.getTypeName(), sql, null);

return sql.toString();
}

/** /**
* Ensures that that the specified transaction has access to features specified by a filter. * Ensures that that the specified transaction has access to features specified by a filter.
* <p> * <p>
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -978,6 +978,31 @@ public void postCreateFeatureType(SimpleFeatureType featureType, DatabaseMetaDat


} }


/**
* Callback which executes before a table is about to be dropped.
* <p>
* This base implementation does nothing, subclasses should override as need be.
* </p>
* @param schemaName The database schema containing the table.
* @param featureType The featureType/table being dropped.
* @param cx The database connection.
*/
public void preDropTable(String schemaName, SimpleFeatureType featureType, Connection cx) throws SQLException {
}

/**
* Callback which executes after a table has been dropped.
*
* <p>
* This base implementation does nothing, subclasses should override as need be.
* </p>
* @param schemaName The database schema containing the table.
* @param featureType The featureType/table being dropped.
* @param cx The database connection.
*/
public void postDropTable(String schemaName, SimpleFeatureType featureType, Connection cx) throws SQLException {
}

/** /**
* Controls whether keys are looked up post or pre insert. * Controls whether keys are looked up post or pre insert.
* <p> * <p>
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -128,24 +128,24 @@ public void testCreateSchemaWithConstraints() throws Exception {
builder.setCRS(CRS.decode("EPSG:4326")); builder.setCRS(CRS.decode("EPSG:4326"));
builder.add(aname("geometry"), Geometry.class); builder.add(aname("geometry"), Geometry.class);
builder.nillable(false).add(aname("intProperty"), Integer.class); builder.nillable(false).add(aname("intProperty"), Integer.class);

builder.length(5).add(aname("stringProperty"), String.class); builder.length(5).add(aname("stringProperty"), String.class);

SimpleFeatureType featureType = builder.buildFeatureType(); SimpleFeatureType featureType = builder.buildFeatureType();
dataStore.createSchema(featureType); dataStore.createSchema(featureType);

SimpleFeatureType ft2 = dataStore.getSchema(tname("ft2")); SimpleFeatureType ft2 = dataStore.getSchema(tname("ft2"));
//assertEquals(ft2, featureType); //assertEquals(ft2, featureType);

//grab a writer //grab a writer
FeatureWriter w = dataStore.getFeatureWriter( tname("ft2"),Transaction.AUTO_COMMIT); FeatureWriter w = dataStore.getFeatureWriter( tname("ft2"),Transaction.AUTO_COMMIT);
w.hasNext(); w.hasNext();

SimpleFeature f = (SimpleFeature) w.next(); SimpleFeature f = (SimpleFeature) w.next();
f.setAttribute( 1, new Integer(0)); f.setAttribute( 1, new Integer(0));
f.setAttribute( 2, "hello"); f.setAttribute( 2, "hello");
w.write(); w.write();

w.hasNext(); w.hasNext();
f = (SimpleFeature) w.next(); f = (SimpleFeature) w.next();
f.setAttribute( 1, null ); f.setAttribute( 1, null );
Expand All @@ -155,7 +155,7 @@ public void testCreateSchemaWithConstraints() throws Exception {
} }
catch( Exception e ) { catch( Exception e ) {
} }

f.setAttribute( 1, new Integer(1) ); f.setAttribute( 1, new Integer(1) );
f.setAttribute( 2, "hello!"); f.setAttribute( 2, "hello!");
try { try {
Expand All @@ -164,10 +164,23 @@ public void testCreateSchemaWithConstraints() throws Exception {
} }
catch( Exception e ) { catch( Exception e ) {
} }

w.close(); w.close();
} }


public void testRemoveSchema() throws Exception {
SimpleFeatureType ft = dataStore.getSchema(tname("ft1"));
assertNotNull(ft);

dataStore.removeSchema(tname("ft1"));
try {
dataStore.getSchema(tname("ft1"));
fail("getSchema() should fail if table was deleted");
}
catch(Exception e) {
}
}

public void testCreateSchemaUTMCRS() throws Exception { public void testCreateSchemaUTMCRS() throws Exception {
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName(tname("ft2")); builder.setName(tname("ft2"));
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -634,4 +634,19 @@ public SimpleFeatureType getSchema(Name name) throws IOException {
public void updateSchema(Name typeName, SimpleFeatureType featureType) throws IOException { public void updateSchema(Name typeName, SimpleFeatureType featureType) throws IOException {
updateSchema(typeName.getLocalPart(), featureType); updateSchema(typeName.getLocalPart(), featureType);
} }

/**
* @see DataAccess#removeSchema(Name)
*/
public void removeSchema(Name typeName) throws IOException {
throw new UnsupportedOperationException("Schema removal not supported");
}

/**
* @see DataStore#removeSchema(String)
*/
public void removeSchema(String typeName) throws IOException {
throw new UnsupportedOperationException("Schema removal not supported");
}

} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -347,6 +347,9 @@ public FeatureType getSchema(Name name) throws IOException {


public void updateSchema(Name typeName, FeatureType featureType) throws IOException { public void updateSchema(Name typeName, FeatureType featureType) throws IOException {
} }

public void removeSchema(Name typeName) throws IOException {
}
}; };


/** /**
Expand Down Expand Up @@ -394,6 +397,9 @@ public String[] getTypeNames() throws IOException {
public void updateSchema(String typeName, SimpleFeatureType featureType) throws IOException { public void updateSchema(String typeName, SimpleFeatureType featureType) throws IOException {
} }


public void removeSchema(String typeName) throws IOException {
}

public void createSchema(SimpleFeatureType featureType) throws IOException { public void createSchema(SimpleFeatureType featureType) throws IOException {
} }


Expand All @@ -419,5 +425,8 @@ public SimpleFeatureType getSchema(Name name) throws IOException {


public void updateSchema(Name typeName, SimpleFeatureType featureType) throws IOException { public void updateSchema(Name typeName, SimpleFeatureType featureType) throws IOException {
} }

public void removeSchema(Name typeName) throws IOException {
}
}; };
} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -798,4 +798,14 @@ private void verifyUnsupportedSqlConstruct(List<Object> errors, Object construct
errors.add(construct); errors.add(construct);
} }
} }

@Override
public void removeSchema(Name typeName) throws IOException {
throw new UnsupportedOperationException();
}

@Override
public void removeSchema(String typeName) throws IOException {
throw new UnsupportedOperationException();
}
} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -206,4 +206,13 @@ public void updateSchema(Name typeName, SimpleFeatureType featureType) throws IO


} }


@Override
public void removeSchema(Name typeName) throws IOException {
throw new UnsupportedOperationException();
}

@Override
public void removeSchema(String typeName) throws IOException {
throw new UnsupportedOperationException();
}
} }
Loading