Search in sources :

Example 26 with SQLDataException

use of java.sql.SQLDataException in project derby by apache.

the class SQLExceptionFactory method getSQLException.

/**
 * <p>
 * method to construct SQLException
 * version specific drivers can overload this method to create
 * version specific exceptions
 * </p>
 *
 * <p>
 * This implementation creates JDBC 4 exceptions.
 * </p>
 *
 * <pre>
 * SQLSTATE CLASS (prefix)     Exception
 * 0A                          java.sql.SQLFeatureNotSupportedException
 * 08                          java.sql.SQLNonTransientConnectionException
 * 22                          java.sql.SQLDataException
 * 28                          java.sql.SQLInvalidAuthorizationSpecException
 * 40                          java.sql.SQLTransactionRollbackException
 * 42                          java.sql.SQLSyntaxErrorException
 * </pre>
 */
@Override
public SQLException getSQLException(String message, String messageId, SQLException next, int severity, Throwable t, Object... args) {
    String sqlState = StandardException.getSQLStateFromIdentifier(messageId);
    // 
    // Create dummy exception which ferries arguments needed to serialize
    // SQLExceptions across the DRDA network layer.
    // 
    StandardException ferry = wrapArgsForTransportAcrossDRDA(messageId, t, args);
    final SQLException ex;
    if (sqlState.startsWith(SQLState.CONNECTIVITY_PREFIX)) {
        // no derby sqlstate belongs to
        // TransientConnectionException DERBY-3074
        ex = new SQLNonTransientConnectionException(message, sqlState, severity, ferry);
    } else if (sqlState.startsWith(SQLState.SQL_DATA_PREFIX)) {
        ex = new SQLDataException(message, sqlState, severity, ferry);
    } else if (sqlState.startsWith(SQLState.INTEGRITY_VIOLATION_PREFIX)) {
        if (sqlState.equals(SQLState.LANG_NULL_INTO_NON_NULL))
            ex = new SQLIntegrityConstraintViolationException(message, sqlState, severity, ferry);
        else if (sqlState.equals(SQLState.LANG_CHECK_CONSTRAINT_VIOLATED))
            ex = new DerbySQLIntegrityConstraintViolationException(message, sqlState, severity, ferry, args[1], args[0]);
        else
            ex = new DerbySQLIntegrityConstraintViolationException(message, sqlState, severity, ferry, args[0], args[1]);
    } else if (sqlState.startsWith(SQLState.AUTHORIZATION_SPEC_PREFIX)) {
        ex = new SQLInvalidAuthorizationSpecException(message, sqlState, severity, ferry);
    } else if (sqlState.startsWith(SQLState.TRANSACTION_PREFIX)) {
        ex = new SQLTransactionRollbackException(message, sqlState, severity, ferry);
    } else if (sqlState.startsWith(SQLState.LSE_COMPILATION_PREFIX)) {
        ex = new SQLSyntaxErrorException(message, sqlState, severity, ferry);
    } else if (sqlState.startsWith(SQLState.UNSUPPORTED_PREFIX)) {
        ex = new SQLFeatureNotSupportedException(message, sqlState, severity, ferry);
    } else if (sqlState.equals(SQLState.LANG_STATEMENT_CANCELLED_OR_TIMED_OUT.substring(0, 5)) || sqlState.equals(SQLState.LOGIN_TIMEOUT.substring(0, 5))) {
        ex = new SQLTimeoutException(message, sqlState, severity, ferry);
    } else {
        ex = new SQLException(message, sqlState, severity, ferry);
    }
    // If the argument ferry has recorded any extra next exceptions,
    // graft them into the parent exception.
    SQLException ferriedExceptions = ferry.getNextException();
    if (ferriedExceptions != null) {
        ex.setNextException(ferriedExceptions);
    }
    if (next != null) {
        ex.setNextException(next);
    }
    return ex;
}
Also used : SQLNonTransientConnectionException(java.sql.SQLNonTransientConnectionException) StandardException(org.apache.derby.shared.common.error.StandardException) SQLTransactionRollbackException(java.sql.SQLTransactionRollbackException) SQLDataException(java.sql.SQLDataException) SQLException(java.sql.SQLException) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) DerbySQLIntegrityConstraintViolationException(org.apache.derby.shared.common.error.DerbySQLIntegrityConstraintViolationException) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) SQLSyntaxErrorException(java.sql.SQLSyntaxErrorException) SQLTimeoutException(java.sql.SQLTimeoutException) SQLInvalidAuthorizationSpecException(java.sql.SQLInvalidAuthorizationSpecException) DerbySQLIntegrityConstraintViolationException(org.apache.derby.shared.common.error.DerbySQLIntegrityConstraintViolationException)

Example 27 with SQLDataException

use of java.sql.SQLDataException in project metacat by Netflix.

the class JdbcConnectorTableService method buildSourceType.

/**
 * Rebuild a source type definition.
 *
 * @param type      The base type e.g. VARCHAR
 * @param size      The size if applicable to the {@code type}
 * @param precision The precision if applicable to the {@code type} e.g. DECIMAL's
 * @return The representation of source type e.g. INTEGER, VARCHAR(50) or DECIMAL(20, 10)
 * @throws SQLDataException When size or precision can't be parsed to integers if non null
 */
protected String buildSourceType(@Nonnull @NonNull final String type, @Nullable final String size, @Nullable final String precision) throws SQLDataException {
    if (size != null) {
        final int sizeInt;
        try {
            sizeInt = Integer.parseInt(size);
        } catch (final NumberFormatException nfe) {
            throw new SQLDataException("Size field could not be converted to integer", nfe);
        }
        // Make sure if the type is unsigned it's created correctly
        final String baseType;
        final String afterMagnitude;
        final int unsignedIndex = StringUtils.indexOfIgnoreCase(type, UNSIGNED);
        if (unsignedIndex != -1) {
            baseType = StringUtils.trim(type.substring(0, unsignedIndex));
            afterMagnitude = type.substring(unsignedIndex);
        } else {
            baseType = type;
            afterMagnitude = null;
        }
        if (precision != null) {
            final int precisionInt;
            try {
                precisionInt = Integer.parseInt(precision);
            } catch (final NumberFormatException nfe) {
                throw new SQLDataException("Precision field could not be converted to integer", nfe);
            }
            return baseType + LEFT_PAREN + sizeInt + COMMA_SPACE + precisionInt + RIGHT_PAREN + (afterMagnitude != null ? SPACE + afterMagnitude : EMPTY);
        } else {
            return baseType + LEFT_PAREN + sizeInt + RIGHT_PAREN + (afterMagnitude != null ? SPACE + afterMagnitude : EMPTY);
        }
    } else {
        return type;
    }
}
Also used : SQLDataException(java.sql.SQLDataException)

Example 28 with SQLDataException

use of java.sql.SQLDataException in project providence by morimekta.

the class MessageRowMapper method map.

@Override
public M map(int idx, ResultSet rs, StatementContext ctx) throws SQLException {
    PMessageBuilder<M, F> builder = descriptor.builder();
    for (int i = 1; i <= rs.getMetaData().getColumnCount(); ++i) {
        if (!tableName.isEmpty() && !tableName.equalsIgnoreCase(rs.getMetaData().getTableName(i))) {
            continue;
        }
        String name = rs.getMetaData().getColumnLabel(i).toUpperCase(Locale.US);
        F field = fieldNameMapping.get(name);
        if (field != null) {
            int columnType = rs.getMetaData().getColumnType(i);
            switch(field.getType()) {
                case BOOL:
                    {
                        if (columnType == Types.BOOLEAN || columnType == Types.BIT) {
                            boolean b = rs.getBoolean(i);
                            if (!rs.wasNull()) {
                                builder.set(field, b);
                            }
                        } else {
                            int b = rs.getInt(i);
                            if (!rs.wasNull()) {
                                builder.set(field, b != 0);
                            }
                        }
                        break;
                    }
                case BYTE:
                    {
                        byte b = rs.getByte(i);
                        if (!rs.wasNull()) {
                            builder.set(field, b);
                        }
                        break;
                    }
                case I16:
                    {
                        short b = rs.getShort(i);
                        if (!rs.wasNull()) {
                            builder.set(field, b);
                        }
                        break;
                    }
                case I32:
                    {
                        if (columnType == Types.TIMESTAMP) {
                            Timestamp ts = rs.getTimestamp(i);
                            if (ts != null) {
                                builder.set(field, (int) (ts.getTime() / 1000L));
                            }
                        } else {
                            int b = rs.getInt(i);
                            if (!rs.wasNull()) {
                                builder.set(field, b);
                            }
                        }
                        break;
                    }
                case I64:
                    {
                        if (columnType == Types.TIMESTAMP) {
                            Timestamp ts = rs.getTimestamp(i);
                            if (ts != null) {
                                builder.set(field, ts.getTime());
                            }
                        } else {
                            long b = rs.getLong(i);
                            if (!rs.wasNull()) {
                                builder.set(field, b);
                            }
                        }
                        break;
                    }
                case DOUBLE:
                    {
                        double b = rs.getDouble(i);
                        if (!rs.wasNull()) {
                            builder.set(field, b);
                        }
                        break;
                    }
                case STRING:
                    {
                        builder.set(field, rs.getString(i));
                        break;
                    }
                case BINARY:
                    {
                        switch(columnType) {
                            case Types.BINARY:
                            case Types.VARBINARY:
                                byte[] ts = rs.getBytes(i);
                                if (ts != null) {
                                    builder.set(field, Binary.copy(ts));
                                }
                                break;
                            case Types.BLOB:
                                Blob blob = rs.getBlob(i);
                                if (blob != null) {
                                    try {
                                        builder.set(field, Binary.read(blob.getBinaryStream(), (int) blob.length()));
                                    } catch (IOException e) {
                                        throw new UncheckedIOException(e.getMessage(), e);
                                    }
                                }
                                break;
                            case Types.CHAR:
                            case Types.VARCHAR:
                            case Types.NCHAR:
                            case Types.NVARCHAR:
                                {
                                    String tmp = rs.getString(i);
                                    if (tmp != null) {
                                        builder.set(field, Binary.fromBase64(tmp));
                                    }
                                    break;
                                }
                            case Types.NULL:
                                break;
                            default:
                                throw new SQLDataException("Unknown column type " + rs.getMetaData().getColumnTypeName(i) + " for " + descriptor.getType().toString() + " field " + name + " in " + descriptor.getQualifiedName());
                        }
                        break;
                    }
                case ENUM:
                    {
                        int val = rs.getInt(i);
                        if (!rs.wasNull()) {
                            PEnumDescriptor ed = (PEnumDescriptor) field.getDescriptor();
                            builder.set(field, ed.findById(val));
                        }
                        break;
                    }
                case MESSAGE:
                    {
                        try {
                            PMessageDescriptor<?, ?> md = (PMessageDescriptor) field.getDescriptor();
                            switch(columnType) {
                                case Types.BINARY:
                                case Types.VARBINARY:
                                    byte[] data = rs.getBytes(i);
                                    if (data != null) {
                                        ByteArrayInputStream in = new ByteArrayInputStream(data);
                                        builder.set(field, BINARY.deserialize(in, md));
                                    }
                                    break;
                                case Types.BLOB:
                                    {
                                        Blob blob = rs.getBlob(i);
                                        if (blob != null) {
                                            builder.set(field, BINARY.deserialize(blob.getBinaryStream(), md));
                                        }
                                        break;
                                    }
                                case Types.CHAR:
                                case Types.VARCHAR:
                                case Types.NCHAR:
                                case Types.NVARCHAR:
                                    {
                                        String tmp = rs.getString(i);
                                        if (tmp != null) {
                                            StringReader reader = new StringReader(tmp);
                                            builder.set(field, JSON.deserialize(reader, md));
                                        }
                                        break;
                                    }
                                case Types.CLOB:
                                    {
                                        Clob clob = rs.getClob(i);
                                        if (clob != null) {
                                            builder.set(field, JSON.deserialize(clob.getCharacterStream(), md));
                                        }
                                        break;
                                    }
                                case Types.NULL:
                                    break;
                                default:
                                    throw new SQLDataException("Unknown column type " + rs.getMetaData().getColumnTypeName(i) + " for " + descriptor.getType().toString() + " field " + name + " in " + descriptor.getQualifiedName());
                            }
                        } catch (IOException e) {
                            throw new UncheckedIOException(e.getMessage(), e);
                        }
                        break;
                    }
                case LIST:
                case SET:
                case MAP:
                    {
                    // ... woot?
                    }
                case VOID:
                default:
                    {
                        throw new SQLDataException("Unhandled column of type " + rs.getMetaData().getColumnTypeName(i) + " for " + descriptor.getType().toString() + " field " + name + " in " + descriptor.getQualifiedName());
                    }
            }
        }
    }
    return builder.build();
}
Also used : Blob(java.sql.Blob) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) PEnumDescriptor(net.morimekta.providence.descriptor.PEnumDescriptor) Timestamp(java.sql.Timestamp) SQLDataException(java.sql.SQLDataException) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) PMessageDescriptor(net.morimekta.providence.descriptor.PMessageDescriptor) Clob(java.sql.Clob)

Aggregations

SQLDataException (java.sql.SQLDataException)28 Test (org.testng.annotations.Test)14 BaseTest (util.BaseTest)14 SQLException (java.sql.SQLException)7 Connection (java.sql.Connection)3 ResultSet (java.sql.ResultSet)3 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 RecordField (org.apache.nifi.serialization.record.RecordField)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 SQLIntegrityConstraintViolationException (java.sql.SQLIntegrityConstraintViolationException)2 Statement (java.sql.Statement)2 Timestamp (java.sql.Timestamp)2 Matcher (java.util.regex.Matcher)2 MalformedRecordException (org.apache.nifi.serialization.MalformedRecordException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1