Search in sources :

Example 1 with PrimaryKey

use of org.geotoolkit.db.reverse.PrimaryKey in project geotoolkit by Geomatys.

the class JDBCFeatureWriterUpdate method write.

@Override
public void write() throws FeatureStoreRuntimeException {
    if (last == null) {
        throw new FeatureStoreRuntimeException("Cursor is not on a record.");
    }
    try {
        // figure out what the fid is
        final PrimaryKey key = store.getDatabaseModel().getPrimaryKey(type.getName().toString());
        final String fid = key.encodeFID(rs);
        final FilterFactory ff = store.getFilterFactory();
        final ResourceId filter = ff.resourceId(fid);
        // figure out which attributes changed
        final Map<String, Object> changes = new HashMap<>();
        for (final PropertyType att : type.getProperties(true)) {
            if (att instanceof FeatureAssociationRole || att instanceof Operation || AttributeConvention.contains(att.getName())) {
                // not a writable property
                continue;
            }
            changes.put(att.getName().tip().toString(), last.getPropertyValue(att.getName().toString()));
        }
        // do the write
        store.updateSingle(type, changes, filter, st.getConnection());
    } catch (Exception e) {
        throw new FeatureStoreRuntimeException(e);
    }
}
Also used : ResourceId(org.opengis.filter.ResourceId) HashMap(java.util.HashMap) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) PrimaryKey(org.geotoolkit.db.reverse.PrimaryKey) PropertyType(org.opengis.feature.PropertyType) Operation(org.opengis.feature.Operation) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole) FilterFactory(org.opengis.filter.FilterFactory) IOException(java.io.IOException) SQLException(java.sql.SQLException) DataStoreException(org.apache.sis.storage.DataStoreException) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException)

Example 2 with PrimaryKey

use of org.geotoolkit.db.reverse.PrimaryKey in project geotoolkit by Geomatys.

the class SQLQueryBuilder method insertSQL.

public String insertSQL(final FeatureType featureType, final Collection<? extends Feature> features, final Object[] keyValues, final Connection cx) throws DataStoreException {
    final PrimaryKey key = store.getDatabaseModel().getPrimaryKey(featureType.getName().toString());
    final List<ColumnMetaModel> keyColumns = key.getColumns();
    final StringBuilder sqlType = new StringBuilder();
    sqlType.append("INSERT INTO ");
    dialect.encodeSchemaAndTableName(sqlType, databaseSchema, featureType.getName().tip().toString());
    sqlType.append(" ( ");
    // add all fields
    fields: for (PropertyType desc : featureType.getProperties(true)) {
        final String attName = desc.getName().tip().toString();
        // remove the primary key attribut that wil be auto-generated and null
        for (ColumnMetaModel col : keyColumns) {
            if (col.getName().equals(attName)) {
                // only include if its non auto generating and not null
                if (col.getType() == ColumnMetaModel.Type.AUTO) {
                    continue fields;
                }
            }
        }
        // the column
        dialect.encodeColumnName(sqlType, attName);
        sqlType.append(',');
    }
    sqlType.setLength(sqlType.length() - 1);
    sqlType.append(" ) ");
    final StringBuilder sqlValues = new StringBuilder();
    sqlValues.append(" VALUES ");
    // add all fields
    for (Feature feature : features) {
        sqlValues.append(" (");
        fields: for (PropertyType desc : featureType.getProperties(true)) {
            final String attName = desc.getName().tip().toString();
            final Class binding = ((AttributeType) desc).getValueClass();
            final Object value = feature.getProperty(attName).getValue();
            // remove the primary key attribut that wil be auto-generated and null
            for (ColumnMetaModel col : keyColumns) {
                if (col.getName().equals(attName)) {
                    // only include if its non auto generating and not null
                    if (col.getType() == ColumnMetaModel.Type.AUTO) {
                        continue fields;
                    }
                }
            }
            // the value
            if (value == null) {
                // maybe it's an auto generated value from a sequence
                boolean found = false;
                for (int k = 0; k < keyColumns.size(); k++) {
                    if (keyColumns.get(k).getName().equals(attName)) {
                        dialect.encodeValue(sqlValues, keyValues[k], keyColumns.get(k).getJavaType());
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    sqlValues.append("null");
                }
            } else {
                if (Geometry.class.isAssignableFrom(binding)) {
                    final Geometry g = (Geometry) value;
                    final int srid = getGeometrySRID(g, desc);
                    dialect.encodeGeometryValue(sqlValues, g, srid);
                } else {
                    dialect.encodeValue(sqlValues, value, binding);
                }
            }
            sqlValues.append(',');
        }
        sqlValues.setLength(sqlValues.length() - 1);
        sqlValues.append(" ),");
    }
    sqlValues.setLength(sqlValues.length() - 1);
    sqlValues.append(';');
    return sqlType.toString() + sqlValues.toString();
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) PrimaryKey(org.geotoolkit.db.reverse.PrimaryKey) PropertyType(org.opengis.feature.PropertyType) ColumnMetaModel(org.geotoolkit.db.reverse.ColumnMetaModel) Feature(org.opengis.feature.Feature)

Example 3 with PrimaryKey

use of org.geotoolkit.db.reverse.PrimaryKey in project geotoolkit by Geomatys.

the class SQLQueryBuilder method insertSQL.

/**
 * Generates a 'INSERT INFO' sql statement.
 */
public String insertSQL(final FeatureType featureType, final Feature feature, final Object[] keyValues, final Connection cx) throws DataStoreException {
    final PrimaryKey key = store.getDatabaseModel().getPrimaryKey(featureType.getName().toString());
    final List<ColumnMetaModel> keyColumns = key.getColumns();
    final StringBuilder sqlType = new StringBuilder();
    sqlType.append("INSERT INTO ");
    dialect.encodeSchemaAndTableName(sqlType, store.getDatabaseSchema(), featureType.getName().tip().toString());
    sqlType.append(" ( ");
    final StringBuilder sqlValues = new StringBuilder();
    sqlValues.append(" ) VALUES ( ");
    // add all fields
    fields: for (PropertyType desc : featureType.getProperties(true)) {
        if (AttributeConvention.contains(desc.getName())) {
            continue;
        }
        final String attName = desc.getName().tip().toString();
        final Class binding = ((AttributeType) desc).getValueClass();
        final Object value = feature.getProperty(attName).getValue();
        // remove the primary key attribut that wil be auto-generated and null
        for (ColumnMetaModel col : keyColumns) {
            if (col.getName().equals(attName)) {
                // only include if its non auto generating and not null
                if (col.getType() == ColumnMetaModel.Type.AUTO) {
                    if (value == null || (value instanceof Number && ((Number) value).intValue() <= 0) || (value instanceof String && ((String) value).isEmpty())) {
                        continue fields;
                    }
                }
            }
        }
        // the column
        dialect.encodeColumnName(sqlType, attName);
        // the value
        if (value == null) {
            // maybe it's an auto generated value from a sequence
            boolean found = false;
            for (int k = 0; k < keyColumns.size(); k++) {
                if (keyColumns.get(k).getName().equals(attName)) {
                    dialect.encodeValue(sqlValues, keyValues[k], keyColumns.get(k).getJavaType());
                    found = true;
                    break;
                }
            }
            if (!found) {
                sqlValues.append("null");
            }
        } else {
            if (Geometry.class.isAssignableFrom(binding)) {
                final Geometry g = (Geometry) value;
                final int srid = getGeometrySRID(g, desc);
                dialect.encodeGeometryValue(sqlValues, g, srid);
            } else if (GridCoverage.class.isAssignableFrom(binding)) {
                final GridCoverage g = (GridCoverage) value;
                dialect.encodeCoverageValue(sqlValues, g);
            } else {
                dialect.encodeValue(sqlValues, value, binding);
            }
        }
        sqlType.append(',');
        sqlValues.append(',');
    }
    sqlType.setLength(sqlType.length() - 1);
    sqlValues.setLength(sqlValues.length() - 1);
    sqlValues.append(")");
    return sqlType.toString() + sqlValues.toString();
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) PrimaryKey(org.geotoolkit.db.reverse.PrimaryKey) PropertyType(org.opengis.feature.PropertyType) ColumnMetaModel(org.geotoolkit.db.reverse.ColumnMetaModel)

Example 4 with PrimaryKey

use of org.geotoolkit.db.reverse.PrimaryKey in project geotoolkit by Geomatys.

the class PostgresQueryBuilder method selectSQL.

@Override
public String selectSQL(final FeatureType featureType, final Query query) throws SQLException, DataStoreException {
    Date vdate = query.getVersionDate();
    String vlabel = query.getVersionLabel();
    if (vdate == null && vlabel == null) {
        // no versioning, fallback on default method
        return super.selectSQL(featureType, query);
    }
    // search version date
    final PostgresVersionControl versionControl;
    try {
        versionControl = (PostgresVersionControl) store.getVersioning(featureType.getName().toString());
        if (vlabel != null) {
            vdate = versionControl.getVersion(vlabel).getDate();
        }
    } catch (VersioningException ex) {
        throw new DataStoreException(ex.getMessage(), ex);
    }
    final StringBuilder sql = new StringBuilder("SELECT ");
    final PrimaryKey key = store.getDatabaseModel().getPrimaryKey(featureType.getName().toString());
    // column names
    encodeSelectColumnNames(sql, featureType, query.getHints());
    sql.append(" FROM ");
    dialect.encodeSchemaAndTableName(sql, databaseSchema, versionControl.getHSTableName());
    // filtering and version/time filter
    Filter filter = query.getSelection();
    final FilterFactory ff = store.getFilterFactory();
    Filter tempFilter = ff.and(ff.lessOrEqual(ff.property("HS_Begin"), ff.literal(vdate)), ff.or(ff.greater(ff.property("HS_End"), ff.literal(vdate)), ff.isNull(ff.property("HS_End"))));
    filter = ff.and(filter, tempFilter);
    filter = (Filter) SimplifyingFilterVisitor.INSTANCE.visit(filter);
    if (!Filter.include().equals(filter)) {
        // encode filter
        sql.append(" WHERE ");
        sql.append(dialect.encodeFilter(filter, featureType));
    }
    // sorting
    encodeSortBy(featureType, QueryUtilities.getSortProperties(query.getSortBy()), key, sql);
    // finally encode limit/offset, if necessary
    dialect.encodeLimitOffset(sql, !query.getLimit().isPresent() ? null : (int) query.getLimit().getAsLong(), (int) query.getOffset());
    return sql.toString();
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) Filter(org.opengis.filter.Filter) VersioningException(org.geotoolkit.version.VersioningException) PrimaryKey(org.geotoolkit.db.reverse.PrimaryKey) Date(java.util.Date) FilterFactory(org.opengis.filter.FilterFactory)

Example 5 with PrimaryKey

use of org.geotoolkit.db.reverse.PrimaryKey in project geotoolkit by Geomatys.

the class SQLQueryBuilder method selectSQL.

// //////////////////////////////////////////////////////////////////////////
// STATEMENT QURIES ////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////
/**
 * Generates a 'SELECT p1, p2, ... FROM ... WHERE ...' statement.
 *
 * @param featureType
 *            the feature type that the query must return (may contain less
 *            attributes than the native one)
 * @param query
 *            the query to be run. The type name and property will be ignored, as they are
 *            supposed to have been already embedded into the provided feature type
 */
public String selectSQL(final FeatureType featureType, final Query query) throws SQLException, DataStoreException {
    final StringBuilder sql = new StringBuilder("SELECT ");
    final PrimaryKey key = store.getDatabaseModel().getPrimaryKey(featureType.getName().toString());
    // column names
    encodeSelectColumnNames(sql, featureType, query.getHints());
    sql.append(" FROM ");
    dialect.encodeSchemaAndTableName(sql, databaseSchema, featureType.getName().tip().toString());
    // filtering
    final Filter filter = query.getSelection();
    if (!Filter.include().equals(filter)) {
        // encode filter
        sql.append(" WHERE ");
        sql.append(dialect.encodeFilter(filter, featureType));
    }
    // sorting
    encodeSortBy(featureType, QueryUtilities.getSortProperties(query.getSortBy()), key, sql);
    // finally encode limit/offset, if necessary
    dialect.encodeLimitOffset(sql, !query.getLimit().isPresent() ? null : (int) query.getLimit().getAsLong(), (int) query.getOffset());
    return sql.toString();
}
Also used : Filter(org.opengis.filter.Filter) PrimaryKey(org.geotoolkit.db.reverse.PrimaryKey)

Aggregations

PrimaryKey (org.geotoolkit.db.reverse.PrimaryKey)5 PropertyType (org.opengis.feature.PropertyType)3 DataStoreException (org.apache.sis.storage.DataStoreException)2 ColumnMetaModel (org.geotoolkit.db.reverse.ColumnMetaModel)2 Geometry (org.locationtech.jts.geom.Geometry)2 Filter (org.opengis.filter.Filter)2 FilterFactory (org.opengis.filter.FilterFactory)2 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 GridCoverage (org.apache.sis.coverage.grid.GridCoverage)1 FeatureStoreRuntimeException (org.geotoolkit.storage.feature.FeatureStoreRuntimeException)1 VersioningException (org.geotoolkit.version.VersioningException)1 Feature (org.opengis.feature.Feature)1 FeatureAssociationRole (org.opengis.feature.FeatureAssociationRole)1 Operation (org.opengis.feature.Operation)1 ResourceId (org.opengis.filter.ResourceId)1