private int deleteFeatureRow(IdAnalysis analysis) throws FeatureStoreException { int deleted = 0; FeatureTypeMapping ftMapping = schema.getFtMapping(analysis.getFeatureType().getName()); FIDMapping fidMapping = ftMapping.getFidMapping(); PreparedStatement stmt = null; try { StringBuilder sql = new StringBuilder("DELETE FROM " + ftMapping.getFtTable() + " WHERE "); sql.append(fidMapping.getColumns().get(0).first); sql.append("=?"); for (int i = 1; i < fidMapping.getColumns().size(); i++) { sql.append(" AND "); sql.append(fidMapping.getColumns().get(i)); sql.append("=?"); } stmt = conn.prepareStatement(sql.toString()); int i = 1; for (String fidKernel : analysis.getIdKernels()) { PrimitiveType pt = new PrimitiveType(fidMapping.getColumns().get(i - 1).second); PrimitiveValue value = new PrimitiveValue(fidKernel, pt); Object sqlValue = SQLValueMangler.internalToSQL(value); stmt.setObject(i++, sqlValue); } LOG.debug("Executing: " + stmt); deleted += stmt.executeUpdate(); } catch (Throwable e) { LOG.error(e.getMessage(), e); throw new FeatureStoreException(e.getMessage(), e); } finally { JDBCUtils.close(stmt); } return deleted; }
private void setRelationalUpdateValues( List<ParsedPropertyReplacement> replacementProps, FeatureTypeMapping ftMapping, PreparedStatement stmt, IdFilter filter, FIDMapping fidMapping) throws SQLException { int i = 1; for (ParsedPropertyReplacement replacement : replacementProps) { Property replacementProp = replacement.getNewValue(); QName propName = replacementProp.getType().getName(); Mapping mapping = ftMapping.getMapping(propName); if (mapping != null) { if (mapping.getJoinedTable() != null && !mapping.getJoinedTable().isEmpty()) { continue; } Object value = replacementProp.getValue(); if (value != null) { ParticleConverter<TypedObjectNode> converter = (ParticleConverter<TypedObjectNode>) fs.getConverter(mapping); if (mapping instanceof PrimitiveMapping) { MappingExpression me = ((PrimitiveMapping) mapping).getMapping(); if (!(me instanceof DBField)) { continue; } converter.setParticle(stmt, (PrimitiveValue) value, i++); } else if (mapping instanceof GeometryMapping) { MappingExpression me = ((GeometryMapping) mapping).getMapping(); if (!(me instanceof DBField)) { continue; } converter.setParticle(stmt, (Geometry) value, i++); } } else { stmt.setObject(i++, null); } } } for (String id : filter.getMatchingIds()) { IdAnalysis analysis = schema.analyzeId(id); int j = i; for (String fidKernel : analysis.getIdKernels()) { PrimitiveValue value = new PrimitiveValue(fidKernel, new PrimitiveType(fidMapping.getColumnType())); Object sqlValue = SQLValueMangler.internalToSQL(value); stmt.setObject(j++, sqlValue); } stmt.addBatch(); } }
private String createRelationalUpdateStatement( FeatureTypeMapping ftMapping, FIDMapping fidMapping, List<ParsedPropertyReplacement> replacementProps, List<ResourceId> list) throws FilterEvaluationException, FeatureStoreException, SQLException { StringBuffer sql = new StringBuffer("UPDATE "); sql.append(ftMapping.getFtTable()); sql.append(" SET "); boolean first = true; for (ParsedPropertyReplacement replacement : replacementProps) { Property replacementProp = replacement.getNewValue(); QName propName = replacementProp.getType().getName(); Mapping mapping = ftMapping.getMapping(propName); if (mapping != null) { if (mapping.getJoinedTable() != null && !mapping.getJoinedTable().isEmpty()) { addRelationallyMappedMultiProperty(replacement, mapping, ftMapping, list); continue; } String column = null; ParticleConverter<TypedObjectNode> converter = (ParticleConverter<TypedObjectNode>) fs.getConverter(mapping); if (mapping instanceof PrimitiveMapping) { MappingExpression me = ((PrimitiveMapping) mapping).getMapping(); if (!(me instanceof DBField)) { continue; } column = ((DBField) me).getColumn(); if (!first) { sql.append(","); } else { first = false; } sql.append(column); sql.append("="); // TODO communicate value for non-prepared statement converters sql.append(converter.getSetSnippet(null)); } else if (mapping instanceof GeometryMapping) { MappingExpression me = ((GeometryMapping) mapping).getMapping(); if (!(me instanceof DBField)) { continue; } column = ((DBField) me).getColumn(); if (!first) { sql.append(","); } else { first = false; } sql.append(column); sql.append("="); // TODO communicate value for non-prepared statement converters sql.append(converter.getSetSnippet(null)); } else { LOG.warn( "Updating of " + mapping.getClass() + " is currently not implemented. Omitting."); continue; } } else { LOG.warn("No mapping for update property '" + propName + "'. Omitting."); } } // only property changes in multi properties? if (first) { return null; } sql.append(" WHERE "); sql.append(fidMapping.getColumns().get(0).first); sql.append("=?"); for (int i = 1; i < fidMapping.getColumns().size(); i++) { sql.append(" AND "); sql.append(fidMapping.getColumns().get(i)); sql.append("=?"); } return sql.toString(); }