use of org.geotoolkit.db.dialect.SQLQueryBuilder in project geotoolkit by Geomatys.
the class DefaultJDBCFeatureStore method createFeatureType.
// //////////////////////////////////////////////////////////////////////////
// Schema manipulation /////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////
/**
* Complexe feature types will be decomposed in flat types then relations
* will be rebuilded.
*/
@Override
public void createFeatureType(final FeatureType featureType) throws DataStoreException {
ensureOpen();
final GenericName typeName = featureType.getName();
if (getNames().contains(typeName)) {
throw new DataStoreException("Type name " + typeName + " already exists.");
}
Connection cnx = null;
Statement stmt = null;
String sql = null;
final List<FeatureType> flatTypes = new ArrayList<>();
final List<TypeRelation> relations = new ArrayList<>();
try {
cnx = getDataSource().getConnection();
cnx.setAutoCommit(false);
stmt = cnx.createStatement();
// we must first decompose the feature type in flat types, then recreate relations
decompose(featureType, flatTypes, relations);
// rebuild flat types
for (FeatureType flatType : flatTypes) {
sql = getQueryBuilder().createTableSQL(flatType, cnx);
stmt.execute(sql);
dialect.postCreateTable(getDatabaseSchema(), flatType, cnx);
}
// rebuild relations
if (!relations.isEmpty()) {
// refresh the model to find primary keys
dbmodel.clearCache();
for (TypeRelation relation : relations) {
final String propertyName = relation.property.getName().tip().toString();
FeatureAssociationRole result = (FeatureAssociationRole) relation.property;
final int minOccurs = result.getMinimumOccurs();
final int maxOccurs = result.getMaximumOccurs();
if (maxOccurs <= 1) {
// place relation on the children with a unique constraint
final AttributeTypeBuilder atb = new FeatureTypeBuilder().addAttribute(Integer.class);
atb.setName(propertyName);
final SQLQueryBuilder b = getQueryBuilder();
final String addColumn = b.alterTableAddColumnSQL(relation.child, atb.build(), cnx);
stmt.execute(addColumn);
final String addForeignKey = b.alterTableAddForeignKey(relation.child, propertyName, relation.parent.getName(), "fid", true);
stmt.execute(addForeignKey);
final String addUnique = b.alterTableAddIndex(relation.child, atb.getName().tip().toString());
stmt.execute(addUnique);
} else {
// place relation on the children
final AttributeTypeBuilder atb = new FeatureTypeBuilder().addAttribute(Integer.class);
atb.setName(propertyName);
final SQLQueryBuilder b = getQueryBuilder();
final String addColumn = b.alterTableAddColumnSQL(relation.child, atb.build(), cnx);
stmt.execute(addColumn);
final String addForeignKey = b.alterTableAddForeignKey(relation.child, propertyName, relation.parent.getName(), "fid", true);
stmt.execute(addForeignKey);
}
}
}
cnx.commit();
} catch (SQLException ex) {
if (cnx != null) {
try {
cnx.rollback();
} catch (SQLException ex1) {
getLogger().log(Level.WARNING, ex1.getMessage(), ex1);
}
}
throw new DataStoreException("Failed to create table " + typeName.tip() + "," + ex.getMessage() + "\n Query: " + sql, ex);
} finally {
JDBCFeatureStoreUtilities.closeSafe(getLogger(), cnx, stmt, null);
}
// reset the type name cache, will be recreated when needed.
dbmodel.clearCache();
}
Aggregations