List of usage examples for java.sql SQLIntegrityConstraintViolationException SQLIntegrityConstraintViolationException
public SQLIntegrityConstraintViolationException(Throwable cause)
SQLIntegrityConstraintViolationException
object with a given cause
. From source file:org.apache.nifi.processors.standard.PutDatabaseRecord.java
SqlAndIncludedColumns generateUpdate(final RecordSchema recordSchema, final String tableName, final String updateKeys, final TableSchema tableSchema, final DMLSettings settings) throws IllegalArgumentException, MalformedRecordException, SQLException { final Set<String> updateKeyNames; if (updateKeys == null) { updateKeyNames = tableSchema.getPrimaryKeyColumnNames(); } else {/*from www .j a v a 2 s. c o m*/ updateKeyNames = new HashSet<>(); for (final String updateKey : updateKeys.split(",")) { updateKeyNames.add(updateKey.trim()); } } if (updateKeyNames.isEmpty()) { throw new SQLIntegrityConstraintViolationException( "Table '" + tableName + "' does not have a Primary Key and no Update Keys were specified"); } final StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.append("UPDATE "); if (settings.quoteTableName) { sqlBuilder.append(tableSchema.getQuotedIdentifierString()).append(tableName) .append(tableSchema.getQuotedIdentifierString()); } else { sqlBuilder.append(tableName); } // Create a Set of all normalized Update Key names, and ensure that there is a field in the record // for each of the Update Key fields. final Set<String> normalizedFieldNames = getNormalizedColumnNames(recordSchema, settings.translateFieldNames); final Set<String> normalizedUpdateNames = new HashSet<>(); for (final String uk : updateKeyNames) { final String normalizedUK = normalizeColumnName(uk, settings.translateFieldNames); normalizedUpdateNames.add(normalizedUK); if (!normalizedFieldNames.contains(normalizedUK)) { String missingColMessage = "Record does not have a value for the " + (updateKeys == null ? "Primary" : "Update") + "Key column '" + uk + "'"; if (settings.failUnmappedColumns) { getLogger().error(missingColMessage); throw new MalformedRecordException(missingColMessage); } else if (settings.warningUnmappedColumns) { getLogger().warn(missingColMessage); } } } // iterate over all of the fields in the record, building the SQL statement by adding the column names List<String> fieldNames = recordSchema.getFieldNames(); final List<Integer> includedColumns = new ArrayList<>(); if (fieldNames != null) { sqlBuilder.append(" SET "); int fieldCount = fieldNames.size(); AtomicInteger fieldsFound = new AtomicInteger(0); for (int i = 0; i < fieldCount; i++) { RecordField field = recordSchema.getField(i); String fieldName = field.getFieldName(); final String normalizedColName = normalizeColumnName(fieldName, settings.translateFieldNames); final ColumnDescription desc = tableSchema.getColumns() .get(normalizeColumnName(fieldName, settings.translateFieldNames)); if (desc == null) { if (!settings.ignoreUnmappedFields) { throw new SQLDataException( "Cannot map field '" + fieldName + "' to any column in the database"); } else { continue; } } // Check if this column is an Update Key. If so, skip it for now. We will come // back to it after we finish the SET clause if (!normalizedUpdateNames.contains(normalizedColName)) { if (fieldsFound.getAndIncrement() > 0) { sqlBuilder.append(", "); } if (settings.escapeColumnNames) { sqlBuilder.append(tableSchema.getQuotedIdentifierString()).append(desc.getColumnName()) .append(tableSchema.getQuotedIdentifierString()); } else { sqlBuilder.append(desc.getColumnName()); } sqlBuilder.append(" = ?"); includedColumns.add(i); } } // Set the WHERE clause based on the Update Key values sqlBuilder.append(" WHERE "); AtomicInteger whereFieldCount = new AtomicInteger(0); for (int i = 0; i < fieldCount; i++) { RecordField field = recordSchema.getField(i); String fieldName = field.getFieldName(); final String normalizedColName = normalizeColumnName(fieldName, settings.translateFieldNames); final ColumnDescription desc = tableSchema.getColumns() .get(normalizeColumnName(fieldName, settings.translateFieldNames)); if (desc != null) { // Check if this column is a Update Key. If so, add it to the WHERE clause if (normalizedUpdateNames.contains(normalizedColName)) { if (whereFieldCount.getAndIncrement() > 0) { sqlBuilder.append(" AND "); } if (settings.escapeColumnNames) { sqlBuilder.append(tableSchema.getQuotedIdentifierString()).append(normalizedColName) .append(tableSchema.getQuotedIdentifierString()); } else { sqlBuilder.append(normalizedColName); } sqlBuilder.append(" = ?"); includedColumns.add(i); } } } } return new SqlAndIncludedColumns(sqlBuilder.toString(), includedColumns); }