Search in sources :

Example 1 with SQLQueryBuilder

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();
}
Also used : FeatureType(org.opengis.feature.FeatureType) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) DataStoreException(org.apache.sis.storage.DataStoreException) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) SQLQueryBuilder(org.geotoolkit.db.dialect.SQLQueryBuilder) AttributeTypeBuilder(org.apache.sis.feature.builder.AttributeTypeBuilder) GenericName(org.opengis.util.GenericName) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole)

Aggregations

Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 AttributeTypeBuilder (org.apache.sis.feature.builder.AttributeTypeBuilder)1 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)1 DataStoreException (org.apache.sis.storage.DataStoreException)1 SQLQueryBuilder (org.geotoolkit.db.dialect.SQLQueryBuilder)1 FeatureAssociationRole (org.opengis.feature.FeatureAssociationRole)1 FeatureType (org.opengis.feature.FeatureType)1 GenericName (org.opengis.util.GenericName)1