Example usage for java.sql SQLDataException SQLDataException

List of usage examples for java.sql SQLDataException SQLDataException

Introduction

In this page you can find the example usage for java.sql SQLDataException SQLDataException.

Prototype

public SQLDataException(Throwable cause) 

Source Link

Document

Constructs a SQLDataException object with a given cause.

Usage

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 w w  w .j a v a2s.  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);
}

From source file:org.apache.nifi.processors.standard.PutDatabaseRecord.java

SqlAndIncludedColumns generateDelete(final RecordSchema recordSchema, final String tableName,
        final TableSchema tableSchema, final DMLSettings settings)
        throws IllegalArgumentException, MalformedRecordException, SQLDataException {

    final Set<String> normalizedFieldNames = getNormalizedColumnNames(recordSchema,
            settings.translateFieldNames);
    for (final String requiredColName : tableSchema.getRequiredColumnNames()) {
        final String normalizedColName = normalizeColumnName(requiredColName, settings.translateFieldNames);
        if (!normalizedFieldNames.contains(normalizedColName)) {
            String missingColMessage = "Record does not have a value for the Required column '"
                    + requiredColName + "'";
            if (settings.failUnmappedColumns) {
                getLogger().error(missingColMessage);
                throw new MalformedRecordException(missingColMessage);
            } else if (settings.warningUnmappedColumns) {
                getLogger().warn(missingColMessage);
            }/*ww w . j  a v a2s . c  o  m*/
        }
    }

    final StringBuilder sqlBuilder = new StringBuilder();
    sqlBuilder.append("DELETE FROM ");
    if (settings.quoteTableName) {
        sqlBuilder.append(tableSchema.getQuotedIdentifierString()).append(tableName)
                .append(tableSchema.getQuotedIdentifierString());
    } else {
        sqlBuilder.append(tableName);
    }

    // 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(" WHERE ");
        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 ColumnDescription desc = tableSchema.getColumns()
                    .get(normalizeColumnName(fieldName, settings.translateFieldNames));
            if (desc == null && !settings.ignoreUnmappedFields) {
                throw new SQLDataException(
                        "Cannot map field '" + fieldName + "' to any column in the database");
            }

            if (desc != null) {
                if (fieldsFound.getAndIncrement() > 0) {
                    sqlBuilder.append(" AND ");
                }

                String columnName;
                if (settings.escapeColumnNames) {
                    columnName = tableSchema.getQuotedIdentifierString() + desc.getColumnName()
                            + tableSchema.getQuotedIdentifierString();
                } else {
                    columnName = desc.getColumnName();
                }
                // Need to build a null-safe construct for the WHERE clause, since we are using PreparedStatement and won't know if the values are null. If they are null,
                // then the filter should be "column IS null" vs "column = null". Since we don't know whether the value is null, we can use the following construct (from NIFI-3742):
                //   (column = ? OR (column is null AND ? is null))
                sqlBuilder.append("(");
                sqlBuilder.append(columnName);
                sqlBuilder.append(" = ? OR (");
                sqlBuilder.append(columnName);
                sqlBuilder.append(" is null AND ? is null))");
                includedColumns.add(i);

            }
        }

        if (fieldsFound.get() == 0) {
            throw new SQLDataException("None of the fields in the record map to the columns defined by the "
                    + tableName + " table");
        }
    }

    return new SqlAndIncludedColumns(sqlBuilder.toString(), includedColumns);
}

From source file:org.xenei.jdbc4sparql.iface.TypeConverter.java

/**
 * Get the java type from the sql type./*from   www.jav  a2  s.  com*/
 *
 * @param sqlType
 *            The sql type to lookup.
 * @return The java class for the type.
 * @throws SQLDataException
 */
public static Class<?> getJavaType(final int sqlType) throws SQLDataException {
    for (final SQLToJava map : TypeConverter.SQL_TO_JAVA) {
        if (map.sqlType == sqlType) {
            return map.javaType;
        }
    }
    throw new SQLDataException(String.format("SQL Type %s", sqlType));
}

From source file:org.xenei.jdbc4sparql.iface.TypeConverter.java

public static Class<?> getJavaType(final RDFDatatype dataType) throws SQLDataException {
    if (dataType == null) {
        return String.class;
    }// ww  w  .  j a v  a2 s  .  c  om
    for (final SPARQLToJava map : TypeConverter.SPARQL_TO_JAVA) {
        if (map.sparqlType.equals(dataType.getURI())) {
            return map.javaType;
        }
    }
    throw new SQLDataException(String.format("SPARQL Type %s", dataType.getURI()));
}