Search in sources :

Example 66 with KettleDatabaseException

use of org.pentaho.di.core.exception.KettleDatabaseException in project pentaho-kettle by pentaho.

the class ValueMetaBase method setPreparedStatementValue.

@Override
public void setPreparedStatementValue(DatabaseMeta databaseMeta, PreparedStatement preparedStatement, int index, Object data) throws KettleDatabaseException {
    try {
        switch(getType()) {
            case ValueMetaInterface.TYPE_NUMBER:
                if (!isNull(data)) {
                    double num = getNumber(data).doubleValue();
                    if (databaseMeta.supportsFloatRoundingOnUpdate() && getPrecision() >= 0) {
                        num = Const.round(num, getPrecision());
                    }
                    preparedStatement.setDouble(index, num);
                } else {
                    preparedStatement.setNull(index, java.sql.Types.DOUBLE);
                }
                break;
            case ValueMetaInterface.TYPE_INTEGER:
                if (!isNull(data)) {
                    if (databaseMeta.supportsSetLong()) {
                        preparedStatement.setLong(index, getInteger(data).longValue());
                    } else {
                        double d = getNumber(data).doubleValue();
                        if (databaseMeta.supportsFloatRoundingOnUpdate() && getPrecision() >= 0) {
                            preparedStatement.setDouble(index, d);
                        } else {
                            preparedStatement.setDouble(index, Const.round(d, getPrecision()));
                        }
                    }
                } else {
                    preparedStatement.setNull(index, java.sql.Types.INTEGER);
                }
                break;
            case ValueMetaInterface.TYPE_STRING:
                if (getLength() < databaseMeta.getMaxTextFieldLength()) {
                    if (!isNull(data)) {
                        preparedStatement.setString(index, getString(data));
                    } else {
                        preparedStatement.setNull(index, java.sql.Types.VARCHAR);
                    }
                } else {
                    if (!isNull(data)) {
                        String string = getString(data);
                        int maxlen = databaseMeta.getMaxTextFieldLength();
                        int len = string.length();
                        // Take the last maxlen characters of the string...
                        int begin = Math.max(len - maxlen, 0);
                        if (begin > 0) {
                            // Truncate if logging result if it exceeds database maximum string field length
                            log.logMinimal(String.format("Truncating %d symbols of original message in '%s' field", begin, getName()));
                            string = string.substring(begin);
                        }
                        if (databaseMeta.supportsSetCharacterStream()) {
                            StringReader sr = new StringReader(string);
                            preparedStatement.setCharacterStream(index, sr, string.length());
                        } else {
                            preparedStatement.setString(index, string);
                        }
                    } else {
                        preparedStatement.setNull(index, java.sql.Types.VARCHAR);
                    }
                }
                break;
            case ValueMetaInterface.TYPE_DATE:
                if (!isNull(data)) {
                    // into the local java timezone
                    if (getPrecision() == 1 || !databaseMeta.supportsTimeStampToDateConversion()) {
                        // Convert to DATE!
                        // converts using Date.getTime()
                        long dat = getInteger(data).longValue();
                        java.sql.Date ddate = new java.sql.Date(dat);
                        if (ignoreTimezone || this.getDateFormatTimeZone() == null) {
                            preparedStatement.setDate(index, ddate);
                        } else {
                            preparedStatement.setDate(index, ddate, Calendar.getInstance(this.getDateFormatTimeZone()));
                        }
                    } else {
                        if (data instanceof java.sql.Timestamp) {
                            // 
                            if (ignoreTimezone || this.getDateFormatTimeZone() == null) {
                                preparedStatement.setTimestamp(index, (java.sql.Timestamp) data);
                            } else {
                                preparedStatement.setTimestamp(index, (java.sql.Timestamp) data, Calendar.getInstance(this.getDateFormatTimeZone()));
                            }
                        } else {
                            // converts using Date.getTime()
                            long dat = getInteger(data).longValue();
                            java.sql.Timestamp sdate = new java.sql.Timestamp(dat);
                            if (ignoreTimezone || this.getDateFormatTimeZone() == null) {
                                preparedStatement.setTimestamp(index, sdate);
                            } else {
                                preparedStatement.setTimestamp(index, sdate, Calendar.getInstance(this.getDateFormatTimeZone()));
                            }
                        }
                    }
                } else {
                    if (getPrecision() == 1 || !databaseMeta.supportsTimeStampToDateConversion()) {
                        preparedStatement.setNull(index, java.sql.Types.DATE);
                    } else {
                        preparedStatement.setNull(index, java.sql.Types.TIMESTAMP);
                    }
                }
                break;
            case ValueMetaInterface.TYPE_BOOLEAN:
                if (databaseMeta.supportsBooleanDataType()) {
                    if (!isNull(data)) {
                        preparedStatement.setBoolean(index, getBoolean(data).booleanValue());
                    } else {
                        preparedStatement.setNull(index, java.sql.Types.BOOLEAN);
                    }
                } else {
                    if (!isNull(data)) {
                        preparedStatement.setString(index, getBoolean(data).booleanValue() ? "Y" : "N");
                    } else {
                        preparedStatement.setNull(index, java.sql.Types.CHAR);
                    }
                }
                break;
            case ValueMetaInterface.TYPE_BIGNUMBER:
                if (!isNull(data)) {
                    preparedStatement.setBigDecimal(index, getBigNumber(data));
                } else {
                    preparedStatement.setNull(index, java.sql.Types.DECIMAL);
                }
                break;
            case ValueMetaInterface.TYPE_BINARY:
                if (!isNull(data)) {
                    preparedStatement.setBytes(index, getBinary(data));
                } else {
                    preparedStatement.setNull(index, java.sql.Types.BINARY);
                }
                break;
            default:
                // placeholder
                preparedStatement.setNull(index, java.sql.Types.VARCHAR);
                break;
        }
    } catch (Exception e) {
        throw new KettleDatabaseException("Error setting value #" + index + " [" + toStringMeta() + "] on prepared statement", e);
    }
}
Also used : KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) Timestamp(java.sql.Timestamp) Timestamp(java.sql.Timestamp) Date(java.util.Date) KettleFileException(org.pentaho.di.core.exception.KettleFileException) ParseException(java.text.ParseException) KettleEOFException(org.pentaho.di.core.exception.KettleEOFException) EOFException(java.io.EOFException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) SQLException(java.sql.SQLException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) StringReader(java.io.StringReader)

Example 67 with KettleDatabaseException

use of org.pentaho.di.core.exception.KettleDatabaseException in project pentaho-kettle by pentaho.

the class ValueMetaBase method getValueFromSQLType.

@SuppressWarnings("fallthrough")
@Override
public ValueMetaInterface getValueFromSQLType(DatabaseMeta databaseMeta, String name, java.sql.ResultSetMetaData rm, int index, boolean ignoreLength, boolean lazyConversion) throws KettleDatabaseException {
    try {
        int length = -1;
        int precision = -1;
        int valtype = ValueMetaInterface.TYPE_NONE;
        boolean isClob = false;
        int type = rm.getColumnType(index);
        boolean signed = false;
        try {
            signed = rm.isSigned(index);
        } catch (Exception ignored) {
        // This JDBC Driver doesn't support the isSigned method
        // nothing more we can do here by catch the exception.
        }
        switch(type) {
            case java.sql.Types.CHAR:
            case java.sql.Types.VARCHAR:
            case java.sql.Types.NVARCHAR:
            case // Character Large Object
            java.sql.Types.LONGVARCHAR:
                valtype = ValueMetaInterface.TYPE_STRING;
                if (!ignoreLength) {
                    length = rm.getColumnDisplaySize(index);
                }
                break;
            case java.sql.Types.CLOB:
            case java.sql.Types.NCLOB:
                valtype = ValueMetaInterface.TYPE_STRING;
                length = DatabaseMeta.CLOB_LENGTH;
                isClob = true;
                break;
            case java.sql.Types.BIGINT:
                // 
                if (signed) {
                    valtype = ValueMetaInterface.TYPE_INTEGER;
                    // Max 9.223.372.036.854.775.807
                    precision = 0;
                    length = 15;
                } else {
                    valtype = ValueMetaInterface.TYPE_BIGNUMBER;
                    // Max 18.446.744.073.709.551.615
                    precision = 0;
                    length = 16;
                }
                break;
            case java.sql.Types.INTEGER:
                valtype = ValueMetaInterface.TYPE_INTEGER;
                // Max 2.147.483.647
                precision = 0;
                length = 9;
                break;
            case java.sql.Types.SMALLINT:
                valtype = ValueMetaInterface.TYPE_INTEGER;
                // Max 32.767
                precision = 0;
                length = 4;
                break;
            case java.sql.Types.TINYINT:
                valtype = ValueMetaInterface.TYPE_INTEGER;
                // Max 127
                precision = 0;
                length = 2;
                break;
            case java.sql.Types.DECIMAL:
            case java.sql.Types.DOUBLE:
            case java.sql.Types.FLOAT:
            case java.sql.Types.REAL:
            case java.sql.Types.NUMERIC:
                valtype = ValueMetaInterface.TYPE_NUMBER;
                length = rm.getPrecision(index);
                precision = rm.getScale(index);
                if (length >= 126) {
                    length = -1;
                }
                if (precision >= 126) {
                    precision = -1;
                }
                if (type == java.sql.Types.DOUBLE || type == java.sql.Types.FLOAT || type == java.sql.Types.REAL) {
                    if (precision == 0) {
                        // precision is obviously incorrect if the type if
                        precision = -1;
                    // Double/Float/Real
                    }
                    // If we're dealing with PostgreSQL and double precision types
                    if (databaseMeta.getDatabaseInterface() instanceof PostgreSQLDatabaseMeta && type == java.sql.Types.DOUBLE && precision >= 16 && length >= 16) {
                        precision = -1;
                        length = -1;
                    }
                    // The (12,31) that is given back is not correct
                    if (databaseMeta.getDatabaseInterface().isMySQLVariant()) {
                        if (precision >= length) {
                            precision = -1;
                            length = -1;
                        }
                    }
                    // if the length or precision needs a BIGNUMBER
                    if (length > 15 || precision > 15) {
                        valtype = ValueMetaInterface.TYPE_BIGNUMBER;
                    }
                } else {
                    if (precision == 0) {
                        if (length <= 18 && length > 0) {
                            // Among others Oracle is affected
                            // here.
                            // Long can hold up to 18
                            valtype = ValueMetaInterface.TYPE_INTEGER;
                        // significant digits
                        } else if (length > 18) {
                            valtype = ValueMetaInterface.TYPE_BIGNUMBER;
                        }
                    } else {
                        // we have a precision: keep NUMBER or change to BIGNUMBER?
                        if (length > 15 || precision > 15) {
                            valtype = ValueMetaInterface.TYPE_BIGNUMBER;
                        }
                    }
                }
                if (databaseMeta.getDatabaseInterface() instanceof PostgreSQLDatabaseMeta || databaseMeta.getDatabaseInterface() instanceof GreenplumDatabaseMeta) {
                    // undefined size => arbitrary precision
                    if (type == java.sql.Types.NUMERIC && length == 0 && precision == 0) {
                        valtype = ValueMetaInterface.TYPE_BIGNUMBER;
                        length = -1;
                        precision = -1;
                    }
                }
                if (databaseMeta.getDatabaseInterface() instanceof OracleDatabaseMeta) {
                    if (precision == 0 && length == 38) {
                        valtype = ((OracleDatabaseMeta) databaseMeta.getDatabaseInterface()).strictBigNumberInterpretation() ? TYPE_BIGNUMBER : TYPE_INTEGER;
                    }
                    if (precision <= 0 && length <= 0) {
                        // undefined size: BIGNUMBER,
                        // precision on Oracle can be 38, too
                        // big for a Number type
                        valtype = ValueMetaInterface.TYPE_BIGNUMBER;
                        length = -1;
                        precision = -1;
                    }
                }
                break;
            case java.sql.Types.TIMESTAMP:
                if (databaseMeta.supportsTimestampDataType()) {
                    valtype = ValueMetaInterface.TYPE_TIMESTAMP;
                    length = rm.getScale(index);
                }
                break;
            case java.sql.Types.DATE:
                if (databaseMeta.getDatabaseInterface() instanceof TeradataDatabaseMeta) {
                    precision = 1;
                }
            case java.sql.Types.TIME:
                valtype = ValueMetaInterface.TYPE_DATE;
                // 
                if (databaseMeta.getDatabaseInterface().isMySQLVariant()) {
                    String property = databaseMeta.getConnectionProperties().getProperty("yearIsDateType");
                    if (property != null && property.equalsIgnoreCase("false") && rm.getColumnTypeName(index).equalsIgnoreCase("YEAR")) {
                        valtype = ValueMetaInterface.TYPE_INTEGER;
                        precision = 0;
                        length = 4;
                        break;
                    }
                }
                break;
            case java.sql.Types.BOOLEAN:
            case java.sql.Types.BIT:
                valtype = ValueMetaInterface.TYPE_BOOLEAN;
                break;
            case java.sql.Types.BINARY:
            case java.sql.Types.BLOB:
            case java.sql.Types.VARBINARY:
            case java.sql.Types.LONGVARBINARY:
                valtype = ValueMetaInterface.TYPE_BINARY;
                if (databaseMeta.isDisplaySizeTwiceThePrecision() && (2 * rm.getPrecision(index)) == rm.getColumnDisplaySize(index)) {
                    // set the length for "CHAR(X) FOR BIT DATA"
                    length = rm.getPrecision(index);
                } else if ((databaseMeta.getDatabaseInterface() instanceof OracleDatabaseMeta) && (type == java.sql.Types.VARBINARY || type == java.sql.Types.LONGVARBINARY)) {
                    // set the length for Oracle "RAW" or "LONGRAW" data types
                    valtype = ValueMetaInterface.TYPE_STRING;
                    length = rm.getColumnDisplaySize(index);
                } else if (databaseMeta.isMySQLVariant() && (type == java.sql.Types.VARBINARY || type == java.sql.Types.LONGVARBINARY)) {
                    // PDI-6677 - don't call 'length = rm.getColumnDisplaySize(index);'
                    // keep the length to -1, e.g. for string functions (e.g.
                    length = -1;
                // CONCAT see PDI-4812)
                } else if (databaseMeta.getDatabaseInterface() instanceof SQLiteDatabaseMeta) {
                    valtype = ValueMetaInterface.TYPE_STRING;
                } else {
                    length = -1;
                }
                precision = -1;
                break;
            default:
                valtype = ValueMetaInterface.TYPE_STRING;
                precision = rm.getScale(index);
                break;
        }
        ValueMetaInterface v = ValueMetaFactory.createValueMeta(name, valtype);
        v.setLength(length);
        v.setPrecision(precision);
        v.setLargeTextField(isClob);
        getOriginalColumnMetadata(v, rm, index, ignoreLength);
        // 
        if (lazyConversion && valtype == ValueMetaInterface.TYPE_STRING) {
            v.setStorageType(ValueMetaInterface.STORAGE_TYPE_BINARY_STRING);
            // 
            try {
                ValueMetaInterface storageMetaData = ValueMetaFactory.cloneValueMeta(v, ValueMetaInterface.TYPE_STRING);
                storageMetaData.setStorageType(ValueMetaInterface.STORAGE_TYPE_NORMAL);
                v.setStorageMetadata(storageMetaData);
            } catch (Exception e) {
                throw new SQLException(e);
            }
        }
        ValueMetaInterface newV = null;
        try {
            newV = databaseMeta.getDatabaseInterface().customizeValueFromSQLType(v, rm, index);
        } catch (SQLException e) {
            throw new SQLException(e);
        }
        return newV == null ? v : newV;
    } catch (Exception e) {
        throw new KettleDatabaseException("Error determining value metadata from SQL resultset metadata", e);
    }
}
Also used : SQLException(java.sql.SQLException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) OracleDatabaseMeta(org.pentaho.di.core.database.OracleDatabaseMeta) SQLiteDatabaseMeta(org.pentaho.di.core.database.SQLiteDatabaseMeta) TeradataDatabaseMeta(org.pentaho.di.core.database.TeradataDatabaseMeta) KettleFileException(org.pentaho.di.core.exception.KettleFileException) ParseException(java.text.ParseException) KettleEOFException(org.pentaho.di.core.exception.KettleEOFException) EOFException(java.io.EOFException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) SQLException(java.sql.SQLException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) PostgreSQLDatabaseMeta(org.pentaho.di.core.database.PostgreSQLDatabaseMeta) GreenplumDatabaseMeta(org.pentaho.di.core.database.GreenplumDatabaseMeta) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Example 68 with KettleDatabaseException

use of org.pentaho.di.core.exception.KettleDatabaseException in project pentaho-kettle by pentaho.

the class ValueMetaTimestamp method getValueFromSQLType.

@Override
public ValueMetaInterface getValueFromSQLType(DatabaseMeta databaseMeta, String name, ResultSetMetaData rm, int index, boolean ignoreLength, boolean lazyConversion) throws KettleDatabaseException {
    try {
        int type = rm.getColumnType(index);
        if (type == java.sql.Types.TIMESTAMP) {
            int length = rm.getScale(index);
            ValueMetaInterface valueMeta;
            if (databaseMeta.supportsTimestampDataType()) {
                valueMeta = new ValueMetaTimestamp(name);
            } else {
                valueMeta = new ValueMetaDate(name);
            }
            valueMeta.setLength(length);
            // Also get original column details, comment, etc.
            // 
            getOriginalColumnMetadata(valueMeta, rm, index, ignoreLength);
            return valueMeta;
        }
        return null;
    } catch (Exception e) {
        throw new KettleDatabaseException("Error evaluating timestamp value metadata", e);
    }
}
Also used : KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleEOFException(org.pentaho.di.core.exception.KettleEOFException) IOException(java.io.IOException) EOFException(java.io.EOFException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) SocketTimeoutException(java.net.SocketTimeoutException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) ParseException(java.text.ParseException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Example 69 with KettleDatabaseException

use of org.pentaho.di.core.exception.KettleDatabaseException in project pentaho-kettle by pentaho.

the class NeoviewDatabaseMetaTest method testGetValueFromResultSet.

@Test
public void testGetValueFromResultSet() throws Exception {
    Object rtn = null;
    ResultSet resultSet = Mockito.mock(ResultSet.class);
    ResultSetMetaData metaData = Mockito.mock(ResultSetMetaData.class);
    Mockito.when(resultSet.getMetaData()).thenReturn(metaData);
    Mockito.when(resultSet.getTimestamp(1)).thenReturn(new java.sql.Timestamp(65535));
    Mockito.when(resultSet.getTime(2)).thenReturn(new java.sql.Time(1000));
    // ValueMetaDate -> Timestamp
    Mockito.when(resultSet.getTimestamp(3)).thenReturn(new java.sql.Timestamp(65535));
    ValueMetaTimestamp ts = new ValueMetaTimestamp("FOO");
    ts.setOriginalColumnType(java.sql.Types.TIMESTAMP);
    ValueMetaDate tm = new ValueMetaDate("BAR");
    tm.setOriginalColumnType(java.sql.Types.TIME);
    ValueMetaDate dt = new ValueMetaDate("WIBBLE");
    dt.setOriginalColumnType(java.sql.Types.DATE);
    rtn = nativeMeta.getValueFromResultSet(resultSet, ts, 0);
    assertNotNull(rtn);
    assertEquals("java.sql.Timestamp", rtn.getClass().getName());
    rtn = nativeMeta.getValueFromResultSet(resultSet, tm, 1);
    assertNotNull(rtn);
    assertEquals("java.sql.Time", rtn.getClass().getName());
    rtn = nativeMeta.getValueFromResultSet(resultSet, dt, 2);
    assertNotNull(rtn);
    assertEquals("java.sql.Timestamp", rtn.getClass().getName());
    Mockito.when(resultSet.wasNull()).thenReturn(true);
    rtn = nativeMeta.getValueFromResultSet(resultSet, new ValueMetaString("WOBBLE"), 3);
    assertNull(rtn);
    // Verify that getDate is not called, getTime is called once, and getTimestamp was called 2 times (once for TimeStamp, once for Date)
    Mockito.verify(resultSet, Mockito.times(0)).getDate(Mockito.anyInt());
    Mockito.verify(resultSet, Mockito.times(1)).getTime(Mockito.anyInt());
    Mockito.verify(resultSet, Mockito.times(2)).getTimestamp(Mockito.anyInt());
    // Now that the date stuff is done, validate the behaviors of other aspects of getValueFromResultSet
    Mockito.when(resultSet.wasNull()).thenReturn(false);
    Mockito.when(resultSet.getBoolean(1)).thenReturn(new Boolean(true));
    Mockito.when(resultSet.getDouble(1)).thenReturn(new Double(15));
    Mockito.when(resultSet.getBigDecimal(1)).thenReturn(new BigDecimal("15"));
    Mockito.when(resultSet.getLong(1)).thenReturn(new Long("15"));
    Mockito.when(resultSet.getString(1)).thenReturn("ASTRING");
    Mockito.when(resultSet.getBytes(1)).thenReturn("ASTRING".getBytes());
    Blob mockBlob = Mockito.mock(Blob.class);
    byte[] bytes = "FOO".getBytes();
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    Mockito.when(mockBlob.getBinaryStream()).thenReturn(bais);
    Mockito.when(mockBlob.length()).thenReturn(new Long(bytes.length));
    Mockito.when(mockBlob.getBytes(Mockito.anyLong(), Mockito.anyInt())).thenReturn(bytes);
    Mockito.when(resultSet.getBlob(1)).thenReturn(mockBlob);
    rtn = nativeMeta.getValueFromResultSet(resultSet, new ValueMetaBoolean("FOO"), 0);
    assertNotNull(rtn);
    assertTrue(rtn instanceof Boolean);
    rtn = nativeMeta.getValueFromResultSet(resultSet, new ValueMetaNumber("FOO", 15, 5), 0);
    assertNotNull(rtn);
    assertTrue(rtn instanceof Double);
    rtn = nativeMeta.getValueFromResultSet(resultSet, new ValueMetaBigNumber("FOO", 15, 5), 0);
    assertNotNull(rtn);
    assertTrue(rtn instanceof BigDecimal);
    rtn = nativeMeta.getValueFromResultSet(resultSet, new ValueMetaInteger("FOO", 5, 0), 0);
    assertNotNull(rtn);
    assertTrue(rtn instanceof Long);
    rtn = nativeMeta.getValueFromResultSet(resultSet, new ValueMetaString("FOO", 25, 0), 0);
    assertNotNull(rtn);
    assertTrue(rtn instanceof String);
    ValueMetaString binStr = new ValueMetaString("FOO");
    binStr.setStorageType(ValueMetaString.STORAGE_TYPE_BINARY_STRING);
    rtn = nativeMeta.getValueFromResultSet(resultSet, binStr, 0);
    assertNotNull(rtn);
    assertTrue(rtn instanceof byte[]);
    rtn = nativeMeta.getValueFromResultSet(resultSet, new ValueMetaBinary("FOO", 150, 0), 0);
    assertNotNull(rtn);
    assertTrue(rtn instanceof byte[]);
    try {
        Mockito.when(resultSet.getBoolean(15)).thenThrow(new SQLException("Expected Exception Here"));
        rtn = nativeMeta.getValueFromResultSet(resultSet, new ValueMetaBoolean("FOO"), 14);
        fail("Should not get here");
    } catch (Exception someException) {
        assertTrue(someException instanceof KettleDatabaseException);
    }
}
Also used : SQLException(java.sql.SQLException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) ValueMetaBoolean(org.pentaho.di.core.row.value.ValueMetaBoolean) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) ResultSetMetaData(java.sql.ResultSetMetaData) ValueMetaNumber(org.pentaho.di.core.row.value.ValueMetaNumber) ValueMetaBigNumber(org.pentaho.di.core.row.value.ValueMetaBigNumber) ResultSet(java.sql.ResultSet) ValueMetaInteger(org.pentaho.di.core.row.value.ValueMetaInteger) ValueMetaBoolean(org.pentaho.di.core.row.value.ValueMetaBoolean) ValueMetaDate(org.pentaho.di.core.row.value.ValueMetaDate) ValueMetaTimestamp(org.pentaho.di.core.row.value.ValueMetaTimestamp) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Blob(java.sql.Blob) BigDecimal(java.math.BigDecimal) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) SQLException(java.sql.SQLException) ValueMetaBinary(org.pentaho.di.core.row.value.ValueMetaBinary) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 70 with KettleDatabaseException

use of org.pentaho.di.core.exception.KettleDatabaseException in project pentaho-kettle by pentaho.

the class OracleDatabaseMetaTest method testOverriddenSettings.

@Test
public void testOverriddenSettings() throws Exception {
    // Tests the settings of the Oracle Database Meta
    // according to the features of the DB as we know them
    assertEquals(1521, nativeMeta.getDefaultDatabasePort());
    assertEquals(-1, odbcMeta.getDefaultDatabasePort());
    assertFalse(nativeMeta.supportsAutoInc());
    assertFalse(nativeMeta.needsToLockAllTables());
    assertEquals("oracle.jdbc.driver.OracleDriver", nativeMeta.getDriverClass());
    assertEquals("sun.jdbc.odbc.JdbcOdbcDriver", odbcMeta.getDriverClass());
    assertEquals("jdbc:odbc:FOO", odbcMeta.getURL(null, null, "FOO"));
    assertEquals("jdbc:oracle:thin:@FOO:1024:BAR", nativeMeta.getURL("FOO", "1024", "BAR"));
    assertEquals("jdbc:oracle:thin:@FOO:11:BAR", nativeMeta.getURL("FOO", "11", ":BAR"));
    assertEquals("jdbc:oracle:thin:@BAR:65534/FOO", nativeMeta.getURL("BAR", "65534", "/FOO"));
    assertEquals("jdbc:oracle:thin:@FOO", nativeMeta.getURL("", "", "FOO"));
    assertEquals("jdbc:oracle:thin:@FOO", nativeMeta.getURL(null, "-1", "FOO"));
    assertEquals("jdbc:oracle:thin:@FOO", nativeMeta.getURL(null, null, "FOO"));
    assertEquals("jdbc:oracle:thin:@FOO:1234:BAR", nativeMeta.getURL("FOO", "1234", "BAR"));
    // Pretty sure this is a bug...
    assertEquals("jdbc:oracle:thin:@", nativeMeta.getURL("", "", ""));
    assertEquals("jdbc:oracle:oci:@BAR", ociMeta.getURL(null, null, "BAR"));
    assertEquals("jdbc:oracle:oci:@(description=(address=(host=FOO)(protocol=tcp)(port=9876))(connect_data=(sid=BAR)))", ociMeta.getURL("FOO", "9876", "BAR"));
    try {
        ociMeta.getURL(null, null, null);
        fail("Expected KettleDatabaseException here");
    } catch (KettleDatabaseException expected) {
    // Keep going ...
    }
    try {
        ociMeta.getURL("", "", "");
        fail("Expected KettleDatabaseException here");
    } catch (KettleDatabaseException expected) {
    // Keep going ...
    }
    assertFalse(nativeMeta.supportsOptionsInURL());
    assertTrue(nativeMeta.supportsSequences());
    assertTrue(nativeMeta.supportsSequenceNoMaxValueOption());
    assertTrue(nativeMeta.useSchemaNameForTableList());
    assertTrue(nativeMeta.supportsSynonyms());
    String[] reservedWords = new String[] { "ACCESS", "ADD", "ALL", "ALTER", "AND", "ANY", "ARRAYLEN", "AS", "ASC", "AUDIT", "BETWEEN", "BY", "CHAR", "CHECK", "CLUSTER", "COLUMN", "COMMENT", "COMPRESS", "CONNECT", "CREATE", "CURRENT", "DATE", "DECIMAL", "DEFAULT", "DELETE", "DESC", "DISTINCT", "DROP", "ELSE", "EXCLUSIVE", "EXISTS", "FILE", "FLOAT", "FOR", "FROM", "GRANT", "GROUP", "HAVING", "IDENTIFIED", "IMMEDIATE", "IN", "INCREMENT", "INDEX", "INITIAL", "INSERT", "INTEGER", "INTERSECT", "INTO", "IS", "LEVEL", "LIKE", "LOCK", "LONG", "MAXEXTENTS", "MINUS", "MODE", "MODIFY", "NOAUDIT", "NOCOMPRESS", "NOT", "NOTFOUND", "NOWAIT", "NULL", "NUMBER", "OF", "OFFLINE", "ON", "ONLINE", "OPTION", "OR", "ORDER", "PCTFREE", "PRIOR", "PRIVILEGES", "PUBLIC", "RAW", "RENAME", "RESOURCE", "REVOKE", "ROW", "ROWID", "ROWLABEL", "ROWNUM", "ROWS", "SELECT", "SESSION", "SET", "SHARE", "SIZE", "SMALLINT", "SQLBUF", "START", "SUCCESSFUL", "SYNONYM", "SYSDATE", "TABLE", "THEN", "TO", "TRIGGER", "UID", "UNION", "UNIQUE", "UPDATE", "USER", "VALIDATE", "VALUES", "VARCHAR", "VARCHAR2", "VIEW", "WHENEVER", "WHERE", "WITH" };
    assertArrayEquals(reservedWords, nativeMeta.getReservedWords());
    assertEquals("http://download.oracle.com/docs/cd/B19306_01/java.102/b14355/urls.htm#i1006362", nativeMeta.getExtraOptionsHelpText());
    assertArrayEquals(new String[] { "ojdbc14.jar", "orai18n.jar" }, nativeMeta.getUsedLibraries());
    assertTrue(nativeMeta.requiresCreateTablePrimaryKeyAppend());
    assertFalse(nativeMeta.supportsPreparedStatementMetadataRetrieval());
    String quoteTest1 = "FOO 'BAR' \r TEST \n";
    String quoteTest2 = "FOO 'BAR' \\r TEST \\n";
    assertEquals("'FOO ''BAR'' '||chr(10)||' TEST '||chr(13)||''", nativeMeta.quoteSQLString(quoteTest1));
    assertEquals("'FOO ''BAR'' \\r TEST \\n'", nativeMeta.quoteSQLString(quoteTest2));
    assertFalse(nativeMeta.releaseSavepoint());
    Variables v = new Variables();
    v.setVariable("FOOVARIABLE", "FOOVALUE");
    DatabaseMeta dm = new DatabaseMeta();
    dm.setDatabaseInterface(nativeMeta);
    assertEquals("TABLESPACE FOOVALUE", nativeMeta.getTablespaceDDL(v, dm, "${FOOVARIABLE}"));
    assertEquals("", nativeMeta.getTablespaceDDL(v, dm, ""));
    assertFalse(nativeMeta.supportsErrorHandlingOnBatchUpdates());
    assertTrue(nativeMeta.supportsRepository());
    assertEquals(2000, nativeMeta.getMaxVARCHARLength());
    assertFalse(nativeMeta.supportsTimestampDataType());
    assertEquals(32, nativeMeta.getMaxColumnsInIndex());
}
Also used : Variables(org.pentaho.di.core.variables.Variables) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Test(org.junit.Test)

Aggregations

KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)279 KettleException (org.pentaho.di.core.exception.KettleException)176 SQLException (java.sql.SQLException)69 Database (org.pentaho.di.core.database.Database)46 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)41 KettleValueException (org.pentaho.di.core.exception.KettleValueException)39 DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)37 KettleDatabaseBatchException (org.pentaho.di.core.exception.KettleDatabaseBatchException)33 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)31 BatchUpdateException (java.sql.BatchUpdateException)27 ResultSet (java.sql.ResultSet)27 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)26 KettleExtensionPoint (org.pentaho.di.core.extension.KettleExtensionPoint)25 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)24 RowMeta (org.pentaho.di.core.row.RowMeta)22 FileObject (org.apache.commons.vfs2.FileObject)18 LongObjectId (org.pentaho.di.repository.LongObjectId)17 Savepoint (java.sql.Savepoint)16 ArrayList (java.util.ArrayList)16 Test (org.junit.Test)14