Search in sources :

Example 76 with Blob

use of java.sql.Blob in project incubator-skywalking by apache.

the class SWCallableStatementTest method testSetParam.

@Test
public void testSetParam() throws SQLException, MalformedURLException {
    CallableStatement callableStatement = multiHostConnection.prepareCall("SELECT * FROM test WHERE a = ? OR b = ? OR c=? OR d = ? OR e = ?" + " OR e = ? OR f = ? OR g = ? OR h = ? OR i = ? OR j = ? OR k = ? OR l = ? OR m = ?  OR n = ? OR o = ? OR p = ? " + " OR r = ?  OR s = ? OR t = ?  OR u = ?  OR v = ?  OR w = ?  OR x = ?  OR y = ? OR z = ? OR a1 = ? OR a2 = ? OR a3 = ?" + " OR a4 = ? OR a5 = ? OR a6 = ?  OR a7 = ?  OR a8 = ?  OR a9 = ? OR b1 = ? OR b2 = ? OR b3 = ? OR b4 = ? OR b5 = ?" + " OR b6 = ? OR b7 = ? OR b8  = ? OR b9 = ? OR c1 = ?  OR c2 = ? OR c3 = ?");
    callableStatement.clearParameters();
    callableStatement.setAsciiStream(1, inputStream);
    callableStatement.setAsciiStream(2, inputStream, 10);
    callableStatement.setAsciiStream(3, inputStream, 1000000L);
    callableStatement.setCharacterStream(4, reader);
    callableStatement.setCharacterStream(4, reader, 10);
    callableStatement.setCharacterStream(5, reader, 10L);
    callableStatement.setShort(6, (short) 12);
    callableStatement.setInt(7, 1);
    callableStatement.setString(8, "test");
    callableStatement.setBoolean(9, true);
    callableStatement.setLong(10, 100L);
    callableStatement.setDouble(11, 12.0);
    callableStatement.setFloat(12, 12.0f);
    callableStatement.setByte(13, (byte) 1);
    callableStatement.setBytes(14, bytesParam);
    callableStatement.setDate(15, new Date(System.currentTimeMillis()));
    callableStatement.setNull(16, 1);
    callableStatement.setNull(17, 1, "test");
    callableStatement.setBigDecimal(18, new BigDecimal(10000));
    callableStatement.setBlob(19, inputStream);
    callableStatement.setBlob(20, inputStream, 1000000L);
    callableStatement.setClob(21, clob);
    callableStatement.setClob(22, reader);
    callableStatement.setClob(23, reader, 100L);
    callableStatement.setNString(24, "test");
    callableStatement.setNCharacterStream(25, reader);
    callableStatement.setNCharacterStream(26, reader, 1);
    callableStatement.setNClob(27, nClob);
    callableStatement.setNClob(28, reader, 1);
    callableStatement.setObject(29, new Object());
    callableStatement.setObject(30, new Object(), 1);
    callableStatement.setObject(31, new Object(), 1, 1);
    callableStatement.setRef(32, ref);
    callableStatement.setRowId(33, rowId);
    callableStatement.setSQLXML(34, sqlxml);
    callableStatement.setTime(35, new Time(System.currentTimeMillis()));
    callableStatement.setTimestamp(36, new Timestamp(System.currentTimeMillis()));
    callableStatement.setTimestamp(37, new Timestamp(System.currentTimeMillis()), Calendar.getInstance());
    callableStatement.setURL(38, new URL("http", "127.0.0.1", "test"));
    callableStatement.setBinaryStream(39, inputStream);
    callableStatement.setBinaryStream(40, inputStream, 1);
    callableStatement.setBinaryStream(41, inputStream, 1L);
    callableStatement.setNClob(42, reader);
    callableStatement.setTime(43, new Time(System.currentTimeMillis()), Calendar.getInstance());
    callableStatement.setArray(45, array);
    callableStatement.setBlob(46, blob);
    callableStatement.setDate(47, new Date(System.currentTimeMillis()), Calendar.getInstance());
    callableStatement.getCharacterStream(4);
    callableStatement.getCharacterStream("d");
    callableStatement.getShort(6);
    callableStatement.getShort("g");
    callableStatement.getInt(7);
    callableStatement.getInt("h");
    callableStatement.getString(8);
    callableStatement.getString("i");
    callableStatement.getBoolean(9);
    callableStatement.getBoolean("j");
    callableStatement.getLong(10);
    callableStatement.getLong("k");
    callableStatement.getDouble(11);
    callableStatement.getDouble("l");
    callableStatement.getFloat(12);
    callableStatement.getFloat("m");
    callableStatement.getByte(13);
    callableStatement.getByte("n");
    callableStatement.getBytes(14);
    callableStatement.getBytes("o");
    callableStatement.getDate(15);
    callableStatement.getDate("p");
    callableStatement.getBigDecimal(18);
    callableStatement.getBigDecimal("s");
    callableStatement.getBlob(19);
    callableStatement.getBlob("t");
    callableStatement.getClob(21);
    callableStatement.getClob(21);
    callableStatement.getClob("u");
    callableStatement.getNString(24);
    callableStatement.getNString("y");
    callableStatement.getNCharacterStream(25);
    callableStatement.getNCharacterStream("z");
    callableStatement.getNClob(27);
    callableStatement.getNClob("a1");
    callableStatement.getRef(32);
    callableStatement.getRef("a2");
    callableStatement.getRowId(33);
    callableStatement.getRowId("a7");
    callableStatement.getSQLXML(34);
    callableStatement.getSQLXML("a8");
    callableStatement.getTime(35);
    callableStatement.getTime("a9");
    callableStatement.getTimestamp(36);
    callableStatement.getTimestamp("b1");
    callableStatement.getURL(38);
    callableStatement.getURL("b3");
    callableStatement.getArray(45);
    callableStatement.getArray("c4");
    callableStatement.getDate(15);
    callableStatement.getDate("p");
    callableStatement.getDate(15, Calendar.getInstance());
    callableStatement.getDate("p", Calendar.getInstance());
    callableStatement.getTime("a9");
    callableStatement.getTime("a9", Calendar.getInstance());
    callableStatement.getTime(43);
    callableStatement.getTime(43, Calendar.getInstance());
    callableStatement.getTimestamp("p", Calendar.getInstance());
    callableStatement.getTimestamp(36, Calendar.getInstance());
    callableStatement.getObject(29);
    callableStatement.getObject(29, new HashMap<String, Class<?>>());
    callableStatement.getObject("a4");
    callableStatement.getObject("a4", new HashMap<String, Class<?>>());
    callableStatement.getBigDecimal(18, 1);
    callableStatement.wasNull();
    callableStatement.setAsciiStream("a", inputStream);
    callableStatement.setAsciiStream("b", inputStream, 10);
    callableStatement.setAsciiStream("c", inputStream, 1000000L);
    callableStatement.setCharacterStream("d", reader);
    callableStatement.setCharacterStream("e", reader, 10);
    callableStatement.setCharacterStream("f", reader, 10L);
    callableStatement.setShort("g", (short) 12);
    callableStatement.setInt("h", 1);
    callableStatement.setString("i", "test");
    callableStatement.setBoolean("j", true);
    callableStatement.setLong("k", 100L);
    callableStatement.setDouble("l", 12.0);
    callableStatement.setFloat("m", 12.0f);
    callableStatement.setByte("n", (byte) 1);
    callableStatement.setBytes("o", bytesParam);
    callableStatement.setDate("p", new Date(System.currentTimeMillis()));
    callableStatement.setNull("q", 1);
    callableStatement.setNull("r", 1, "test");
    callableStatement.setBigDecimal("s", new BigDecimal(10000));
    callableStatement.setBlob("t", inputStream);
    callableStatement.setBlob("u", inputStream, 1000000L);
    callableStatement.setClob("v", clob);
    callableStatement.setClob("w", reader);
    callableStatement.setClob("x", reader, 100L);
    callableStatement.setNString("y", "test");
    callableStatement.setNCharacterStream("z", reader);
    callableStatement.setNCharacterStream("a1", reader, 1);
    callableStatement.setNClob("a2", nClob);
    callableStatement.setNClob("a3", reader, 1);
    callableStatement.setObject("a4", new Object());
    callableStatement.setObject("a5", new Object(), 1);
    callableStatement.setObject("a6", new Object(), 1, 1);
    callableStatement.setRowId("a7", rowId);
    callableStatement.setSQLXML("a8", sqlxml);
    callableStatement.setTime("a9", new Time(System.currentTimeMillis()));
    callableStatement.setTimestamp("b1", new Timestamp(System.currentTimeMillis()));
    callableStatement.setTimestamp("b2", new Timestamp(System.currentTimeMillis()), Calendar.getInstance());
    callableStatement.setURL("b3", new URL("http", "127.0.0.1", "test"));
    callableStatement.setBinaryStream("b4", inputStream);
    callableStatement.setBinaryStream("b5", inputStream, 1);
    callableStatement.setBinaryStream("b6", inputStream, 1L);
    callableStatement.setNClob("b7", reader);
    callableStatement.setTime("b8", new Time(System.currentTimeMillis()), Calendar.getInstance());
    callableStatement.setBlob("c1", blob);
    callableStatement.setDate("c2", new Date(System.currentTimeMillis()), Calendar.getInstance());
    callableStatement.registerOutParameter("c4", 1);
    callableStatement.registerOutParameter("c5", 1, 1);
    callableStatement.registerOutParameter("c6", 1, "test");
    callableStatement.registerOutParameter(48, 1);
    callableStatement.registerOutParameter(49, 1, 1);
    callableStatement.registerOutParameter(50, 1, "test");
    ResultSet resultSet = callableStatement.executeQuery();
    callableStatement.close();
    verify(mysqlCallableStatement, times(1)).clearParameters();
    verify(mysqlCallableStatement, times(1)).executeQuery();
    verify(mysqlCallableStatement, times(1)).close();
    verify(mysqlCallableStatement, times(1)).setAsciiStream(anyInt(), any(InputStream.class));
    verify(mysqlCallableStatement, times(1)).setAsciiStream(anyInt(), any(InputStream.class), anyInt());
    verify(mysqlCallableStatement, times(1)).setAsciiStream(anyInt(), any(InputStream.class), anyLong());
    verify(mysqlCallableStatement, times(1)).setCharacterStream(anyInt(), any(Reader.class));
    verify(mysqlCallableStatement, times(1)).setCharacterStream(anyInt(), any(Reader.class), anyInt());
    verify(mysqlCallableStatement, times(1)).setCharacterStream(anyInt(), any(Reader.class), anyLong());
    verify(mysqlCallableStatement, times(1)).setShort(anyInt(), anyShort());
    verify(mysqlCallableStatement, times(1)).setInt(anyInt(), anyInt());
    verify(mysqlCallableStatement, times(1)).setString(anyInt(), anyString());
    verify(mysqlCallableStatement, times(1)).setBoolean(anyInt(), anyBoolean());
    verify(mysqlCallableStatement, times(1)).setLong(anyInt(), anyLong());
    verify(mysqlCallableStatement, times(1)).setDouble(anyInt(), anyDouble());
    verify(mysqlCallableStatement, times(1)).setFloat(anyInt(), anyFloat());
    verify(mysqlCallableStatement, times(1)).setByte(anyInt(), anyByte());
    verify(mysqlCallableStatement, times(1)).setBytes(14, bytesParam);
    verify(mysqlCallableStatement, times(1)).setDate(anyInt(), any(Date.class));
    verify(mysqlCallableStatement, times(1)).setNull(anyInt(), anyInt());
    verify(mysqlCallableStatement, times(1)).setNull(anyInt(), anyInt(), anyString());
    verify(mysqlCallableStatement, times(1)).setBigDecimal(anyInt(), any(BigDecimal.class));
    verify(mysqlCallableStatement, times(1)).setBlob(anyInt(), any(InputStream.class));
    verify(mysqlCallableStatement, times(1)).setBlob(anyInt(), any(InputStream.class), anyLong());
    verify(mysqlCallableStatement, times(1)).setClob(anyInt(), any(Clob.class));
    verify(mysqlCallableStatement, times(1)).setClob(anyInt(), any(Reader.class));
    verify(mysqlCallableStatement, times(1)).setClob(anyInt(), any(Reader.class), anyInt());
    verify(mysqlCallableStatement, times(1)).setNString(anyInt(), anyString());
    verify(mysqlCallableStatement, times(1)).setNCharacterStream(anyInt(), any(Reader.class));
    verify(mysqlCallableStatement, times(1)).setNCharacterStream(anyInt(), any(Reader.class), anyInt());
    verify(mysqlCallableStatement, times(1)).setNClob(27, nClob);
    verify(mysqlCallableStatement, times(1)).setNClob(28, reader, 1);
    verify(mysqlCallableStatement, times(1)).setObject(anyInt(), Matchers.anyObject());
    verify(mysqlCallableStatement, times(1)).setObject(anyInt(), Matchers.anyObject(), anyInt());
    verify(mysqlCallableStatement, times(1)).setObject(anyInt(), Matchers.anyObject(), anyInt(), anyInt());
    verify(mysqlCallableStatement, times(1)).setRef(anyInt(), any(Ref.class));
    verify(mysqlCallableStatement, times(1)).setRowId(anyInt(), any(RowId.class));
    verify(mysqlCallableStatement, times(1)).setSQLXML(anyInt(), any(SQLXML.class));
    verify(mysqlCallableStatement, times(1)).setTime(anyInt(), any(Time.class));
    verify(mysqlCallableStatement, times(1)).setTimestamp(anyInt(), any(Timestamp.class));
    verify(mysqlCallableStatement, times(1)).setTimestamp(anyInt(), any(Timestamp.class), any(Calendar.class));
    verify(mysqlCallableStatement, times(1)).setURL(anyInt(), any(URL.class));
    verify(mysqlCallableStatement, times(1)).setBinaryStream(anyInt(), any(InputStream.class));
    verify(mysqlCallableStatement, times(1)).setBinaryStream(anyInt(), any(InputStream.class), anyInt());
    verify(mysqlCallableStatement, times(1)).setBinaryStream(anyInt(), any(InputStream.class), anyLong());
    verify(mysqlCallableStatement, times(1)).setNClob(42, reader);
    verify(mysqlCallableStatement, times(1)).setTime(anyInt(), any(Time.class), any(Calendar.class));
    verify(mysqlCallableStatement, times(1)).setTimestamp(anyInt(), any(Timestamp.class), any(Calendar.class));
    verify(mysqlCallableStatement, times(1)).setArray(anyInt(), any(Array.class));
    verify(mysqlCallableStatement, times(1)).setBlob(anyInt(), any(Blob.class));
    verify(mysqlCallableStatement, times(1)).setDate(anyInt(), any(Date.class), any(Calendar.class));
    verify(mysqlCallableStatement, times(1)).clearParameters();
    verify(mysqlCallableStatement, times(1)).executeQuery();
    verify(mysqlCallableStatement, times(1)).close();
    verify(mysqlCallableStatement, times(1)).setAsciiStream(anyString(), any(InputStream.class));
    verify(mysqlCallableStatement, times(1)).setAsciiStream(anyString(), any(InputStream.class), anyInt());
    verify(mysqlCallableStatement, times(1)).setAsciiStream(anyString(), any(InputStream.class), anyLong());
    verify(mysqlCallableStatement, times(1)).setCharacterStream(anyString(), any(Reader.class));
    verify(mysqlCallableStatement, times(1)).setCharacterStream(anyString(), any(Reader.class), anyInt());
    verify(mysqlCallableStatement, times(1)).setCharacterStream(anyString(), any(Reader.class), anyLong());
    verify(mysqlCallableStatement, times(1)).setShort(anyString(), anyShort());
    verify(mysqlCallableStatement, times(1)).setInt(anyString(), anyInt());
    verify(mysqlCallableStatement, times(1)).setString(anyString(), anyString());
    verify(mysqlCallableStatement, times(1)).setBoolean(anyString(), anyBoolean());
    verify(mysqlCallableStatement, times(1)).setLong(anyString(), anyLong());
    verify(mysqlCallableStatement, times(1)).setDouble(anyString(), anyDouble());
    verify(mysqlCallableStatement, times(1)).setFloat(anyString(), anyFloat());
    verify(mysqlCallableStatement, times(1)).setByte(anyString(), anyByte());
    verify(mysqlCallableStatement, times(1)).setBytes(14, bytesParam);
    verify(mysqlCallableStatement, times(1)).setDate(anyString(), any(Date.class));
    verify(mysqlCallableStatement, times(1)).setNull(anyString(), anyInt());
    verify(mysqlCallableStatement, times(1)).setNull(anyString(), anyInt(), anyString());
    verify(mysqlCallableStatement, times(1)).setBigDecimal(anyString(), any(BigDecimal.class));
    verify(mysqlCallableStatement, times(1)).setBlob(anyString(), any(InputStream.class));
    verify(mysqlCallableStatement, times(1)).setBlob(anyString(), any(InputStream.class), anyLong());
    verify(mysqlCallableStatement, times(1)).setClob(anyString(), any(Clob.class));
    verify(mysqlCallableStatement, times(1)).setClob(anyString(), any(Reader.class));
    verify(mysqlCallableStatement, times(1)).setClob(anyString(), any(Reader.class), anyInt());
    verify(mysqlCallableStatement, times(1)).setNString(anyString(), anyString());
    verify(mysqlCallableStatement, times(1)).setNCharacterStream(anyString(), any(Reader.class));
    verify(mysqlCallableStatement, times(1)).setNCharacterStream(anyString(), any(Reader.class), anyInt());
    verify(mysqlCallableStatement, times(1)).setNClob(27, nClob);
    verify(mysqlCallableStatement, times(1)).setNClob(28, reader, 1);
    verify(mysqlCallableStatement, times(1)).setObject(anyString(), Matchers.anyObject());
    verify(mysqlCallableStatement, times(1)).setObject(anyString(), Matchers.anyObject(), anyInt());
    verify(mysqlCallableStatement, times(1)).setObject(anyString(), Matchers.anyObject(), anyInt(), anyInt());
    verify(mysqlCallableStatement, times(1)).setRowId(anyString(), any(RowId.class));
    verify(mysqlCallableStatement, times(1)).setSQLXML(anyString(), any(SQLXML.class));
    verify(mysqlCallableStatement, times(1)).setTime(anyString(), any(Time.class));
    verify(mysqlCallableStatement, times(1)).setTimestamp(anyString(), any(Timestamp.class));
    verify(mysqlCallableStatement, times(1)).setTimestamp(anyString(), any(Timestamp.class), any(Calendar.class));
    verify(mysqlCallableStatement, times(1)).setURL(anyString(), any(URL.class));
    verify(mysqlCallableStatement, times(1)).setBinaryStream(anyString(), any(InputStream.class));
    verify(mysqlCallableStatement, times(1)).setBinaryStream(anyString(), any(InputStream.class), anyInt());
    verify(mysqlCallableStatement, times(1)).setBinaryStream(anyString(), any(InputStream.class), anyLong());
    verify(mysqlCallableStatement, times(1)).setNClob(42, reader);
    verify(mysqlCallableStatement, times(1)).setTime(anyString(), any(Time.class), any(Calendar.class));
    verify(mysqlCallableStatement, times(1)).setTimestamp(anyString(), any(Timestamp.class), any(Calendar.class));
    verify(mysqlCallableStatement, times(1)).setBlob(anyString(), any(Blob.class));
    verify(mysqlCallableStatement, times(1)).setDate(anyString(), any(Date.class), any(Calendar.class));
}
Also used : RowId(java.sql.RowId) Blob(java.sql.Blob) InputStream(java.io.InputStream) Calendar(java.util.Calendar) Reader(java.io.Reader) Time(java.sql.Time) Matchers.anyString(org.mockito.Matchers.anyString) Timestamp(java.sql.Timestamp) Date(java.sql.Date) BigDecimal(java.math.BigDecimal) URL(java.net.URL) Array(java.sql.Array) Ref(java.sql.Ref) SQLXML(java.sql.SQLXML) CallableStatement(java.sql.CallableStatement) ResultSet(java.sql.ResultSet) NClob(java.sql.NClob) Clob(java.sql.Clob) Test(org.junit.Test)

Example 77 with Blob

use of java.sql.Blob in project ofbiz-framework by apache.

the class SqlJdbcUtil method getValue.

public static void getValue(ResultSet rs, int ind, ModelField curField, GenericEntity entity, ModelFieldTypeReader modelFieldTypeReader) throws GenericEntityException {
    ModelFieldType mft = modelFieldTypeReader.getModelFieldType(curField.getType());
    if (mft == null) {
        throw new GenericModelException("definition fieldType " + curField.getType() + " not found, cannot getValue for field " + entity.getEntityName() + "." + curField.getName() + ".");
    }
    ModelEntity model = entity.getModelEntity();
    String encryptionKeyName = entity.getEntityName();
    if (curField.getEncryptMethod().isEncrypted() && model instanceof ModelViewEntity) {
        ModelViewEntity modelView = (ModelViewEntity) model;
        encryptionKeyName = modelView.getAliasedEntity(modelView.getAlias(curField.getName()).getEntityAlias(), entity.getDelegator().getModelReader()).getEntityName();
    }
    // ----- Try out the new handler code -----
    JdbcValueHandler<?> handler = mft.getJdbcValueHandler();
    if (handler != null) {
        try {
            Object jdbcValue = handler.getValue(rs, ind);
            if (jdbcValue instanceof String && curField.getEncryptMethod().isEncrypted()) {
                jdbcValue = entity.getDelegator().decryptFieldValue(encryptionKeyName, curField.getEncryptMethod(), (String) jdbcValue);
            }
            entity.dangerousSetNoCheckButFast(curField, jdbcValue);
            return;
        } catch (Exception e) {
            Debug.logError(e, module);
        }
    } else {
        Debug.logWarning("JdbcValueHandler not found for java-type " + mft.getJavaType() + ", falling back on switch statement. Entity = " + curField.getModelEntity().getEntityName() + ", field = " + curField.getName() + ".", module);
    }
    // ------------------------------------------
    String fieldType = mft.getJavaType();
    try {
        // checking to see if the object is null is really only necessary for the numbers
        int typeValue = getType(fieldType);
        ResultSetMetaData rsmd = rs.getMetaData();
        int colType = rsmd.getColumnType(ind);
        if (typeValue <= 4 || typeValue >= 11) {
            switch(typeValue) {
                case 1:
                    if (java.sql.Types.CLOB == colType) {
                        // Debug.logInfo("For field " + curField.getName() + " of entity " + entity.getEntityName() + " getString is a CLOB, trying getCharacterStream", module);
                        // if the String is empty, try to get a text input stream, this is required for some databases for larger fields, like CLOBs
                        Clob valueClob = rs.getClob(ind);
                        Reader valueReader = null;
                        if (valueClob != null) {
                            valueReader = valueClob.getCharacterStream();
                        }
                        // Reader valueReader = rs.getCharacterStream(ind);
                        if (valueReader != null) {
                            char[] inCharBuffer = new char[CHAR_BUFFER_SIZE];
                            StringBuilder strBuf = new StringBuilder();
                            int charsRead = 0;
                            try {
                                while ((charsRead = valueReader.read(inCharBuffer, 0, CHAR_BUFFER_SIZE)) > 0) {
                                    strBuf.append(inCharBuffer, 0, charsRead);
                                }
                                valueReader.close();
                            } catch (IOException e) {
                                throw new GenericEntityException("Error reading long character stream for field " + curField.getName() + " of entity " + entity.getEntityName(), e);
                            }
                            entity.dangerousSetNoCheckButFast(curField, strBuf.toString());
                        } else {
                            entity.dangerousSetNoCheckButFast(curField, null);
                        }
                    } else {
                        String value = rs.getString(ind);
                        if (curField.getEncryptMethod().isEncrypted()) {
                            value = (String) entity.getDelegator().decryptFieldValue(encryptionKeyName, curField.getEncryptMethod(), value);
                        }
                        entity.dangerousSetNoCheckButFast(curField, value);
                    }
                    break;
                case 2:
                    entity.dangerousSetNoCheckButFast(curField, rs.getTimestamp(ind));
                    break;
                case 3:
                    entity.dangerousSetNoCheckButFast(curField, rs.getTime(ind));
                    break;
                case 4:
                    entity.dangerousSetNoCheckButFast(curField, rs.getDate(ind));
                    break;
                case 11:
                    Object obj = null;
                    byte[] originalBytes = rs.getBytes(ind);
                    obj = deserializeField(originalBytes, ind, curField);
                    if (obj != null) {
                        entity.dangerousSetNoCheckButFast(curField, obj);
                    } else {
                        entity.dangerousSetNoCheckButFast(curField, originalBytes);
                    }
                    break;
                case 12:
                    Object originalObject;
                    byte[] fieldBytes;
                    try {
                        Blob theBlob = rs.getBlob(ind);
                        fieldBytes = theBlob != null ? theBlob.getBytes(1, (int) theBlob.length()) : null;
                        originalObject = theBlob;
                    } catch (SQLException e) {
                        // for backward compatibility if getBlob didn't work try getBytes
                        fieldBytes = rs.getBytes(ind);
                        originalObject = fieldBytes;
                    }
                    if (originalObject != null) {
                        // for backward compatibility, check to see if there is a serialized object and if so return that
                        Object blobObject = deserializeField(fieldBytes, ind, curField);
                        if (blobObject != null) {
                            entity.dangerousSetNoCheckButFast(curField, blobObject);
                        } else {
                            if (originalObject instanceof Blob) {
                                // NOTE using SerialBlob here instead of the Blob from the database to make sure we can pass it around, serialize it, etc
                                entity.dangerousSetNoCheckButFast(curField, new SerialBlob((Blob) originalObject));
                            } else {
                                entity.dangerousSetNoCheckButFast(curField, originalObject);
                            }
                        }
                    }
                    break;
                case 13:
                    entity.dangerousSetNoCheckButFast(curField, new SerialClob(rs.getClob(ind)));
                    break;
                case 14:
                case 15:
                    entity.dangerousSetNoCheckButFast(curField, rs.getObject(ind));
                    break;
            }
        } else {
            switch(typeValue) {
                case 5:
                    int intValue = rs.getInt(ind);
                    if (rs.wasNull()) {
                        entity.dangerousSetNoCheckButFast(curField, null);
                    } else {
                        entity.dangerousSetNoCheckButFast(curField, Integer.valueOf(intValue));
                    }
                    break;
                case 6:
                    long longValue = rs.getLong(ind);
                    if (rs.wasNull()) {
                        entity.dangerousSetNoCheckButFast(curField, null);
                    } else {
                        entity.dangerousSetNoCheckButFast(curField, Long.valueOf(longValue));
                    }
                    break;
                case 7:
                    float floatValue = rs.getFloat(ind);
                    if (rs.wasNull()) {
                        entity.dangerousSetNoCheckButFast(curField, null);
                    } else {
                        entity.dangerousSetNoCheckButFast(curField, Float.valueOf(floatValue));
                    }
                    break;
                case 8:
                    double doubleValue = rs.getDouble(ind);
                    if (rs.wasNull()) {
                        entity.dangerousSetNoCheckButFast(curField, null);
                    } else {
                        entity.dangerousSetNoCheckButFast(curField, Double.valueOf(doubleValue));
                    }
                    break;
                case 9:
                    BigDecimal bigDecimalValue = rs.getBigDecimal(ind);
                    if (rs.wasNull()) {
                        entity.dangerousSetNoCheckButFast(curField, null);
                    } else {
                        entity.dangerousSetNoCheckButFast(curField, bigDecimalValue);
                    }
                    break;
                case 10:
                    boolean booleanValue = rs.getBoolean(ind);
                    if (rs.wasNull()) {
                        entity.dangerousSetNoCheckButFast(curField, null);
                    } else {
                        entity.dangerousSetNoCheckButFast(curField, Boolean.valueOf(booleanValue));
                    }
                    break;
            }
        }
    } catch (SQLException sqle) {
        throw new GenericDataSourceException("SQL Exception while getting value : " + curField.getName() + " [" + curField.getColName() + "] (" + ind + ")", sqle);
    }
}
Also used : GenericModelException(org.apache.ofbiz.entity.GenericModelException) SQLException(java.sql.SQLException) ModelFieldTypeReader(org.apache.ofbiz.entity.model.ModelFieldTypeReader) Reader(java.io.Reader) SerialBlob(javax.sql.rowset.serial.SerialBlob) ResultSetMetaData(java.sql.ResultSetMetaData) ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity) GenericDataSourceException(org.apache.ofbiz.entity.GenericDataSourceException) SerialBlob(javax.sql.rowset.serial.SerialBlob) Blob(java.sql.Blob) IOException(java.io.IOException) SerialClob(javax.sql.rowset.serial.SerialClob) SQLException(java.sql.SQLException) GenericDataSourceException(org.apache.ofbiz.entity.GenericDataSourceException) GenericNotImplementedException(org.apache.ofbiz.entity.GenericNotImplementedException) GenericModelException(org.apache.ofbiz.entity.GenericModelException) IOException(java.io.IOException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) BigDecimal(java.math.BigDecimal) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ModelFieldType(org.apache.ofbiz.entity.model.ModelFieldType) Clob(java.sql.Clob) SerialClob(javax.sql.rowset.serial.SerialClob)

Example 78 with Blob

use of java.sql.Blob in project whole by wholeplatform.

the class SQLPrettyPrinterVisitor method visit.

public void visit(BlobValue entity) {
    // TODO Oracle "'"
    out.printRaw("X'");
    Blob blob = entity.getValue();
    try {
        out.printRaw(SQLDataTypeParser.instance().unparseObject(SQLEntityDescriptorEnum.BinaryValue, // TODO Oracle .toUpperCase());
        blob.getBytes(1l, (int) blob.length())));
    } catch (SQLException e) {
    }
    out.print('\'');
}
Also used : Blob(java.sql.Blob) SQLException(java.sql.SQLException)

Example 79 with Blob

use of java.sql.Blob in project syndesis by syndesisio.

the class SqlFileStore method doReadDerby.

/**
 * Derby does not allow to read from the blob after the connection has been closed.
 * It also requires an outcome of commit/rollback.
 */
@SuppressWarnings("PMD.EmptyCatchBlock")
private InputStream doReadDerby(String path) {
    Handle h = dbi.open();
    try {
        h.getConnection().setAutoCommit(false);
        List<Map<String, Object>> res = h.select("SELECT data FROM filestore WHERE path=?", path);
        Optional<Blob> blob = res.stream().map(row -> row.get("data")).map(Blob.class::cast).findFirst();
        if (blob.isPresent()) {
            return new HandleCloserInputStream(h, blob.get().getBinaryStream());
        } else {
            h.commit();
            h.close();
            return null;
        }
    } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") Exception e) {
        // Do cleanup
        try {
            h.rollback();
        } catch (@SuppressWarnings("PMD.AvoidCatchingGenericException") Exception ex) {
        // ignore
        }
        IOUtils.closeQuietly(h);
        throw DaoException.launderThrowable(e);
    }
}
Also used : Blob(java.sql.Blob) Map(java.util.Map) SQLException(java.sql.SQLException) IOException(java.io.IOException) CallbackFailedException(org.skife.jdbi.v2.exceptions.CallbackFailedException) DaoException(io.syndesis.server.dao.DaoException) Handle(org.skife.jdbi.v2.Handle)

Example 80 with Blob

use of java.sql.Blob in project BroadleafCommerce by BroadleafCommerce.

the class StaticAssetStorageServiceImpl method createBlob.

@Override
public Blob createBlob(final MultipartFile uploadedFile) throws IOException {
    final Blob[] blob = new Blob[1];
    transUtil.runTransactionalOperation(new StreamCapableTransactionalOperationAdapter() {

        @Override
        public void execute() {
            try {
                blob[0] = staticAssetStorageDao.createBlob(uploadedFile);
            } catch (IOException e) {
                LOG.error("Unable to create blob from MultipartFile.", e);
            }
        }
    }, RuntimeException.class);
    return blob[0];
}
Also used : Blob(java.sql.Blob) StreamCapableTransactionalOperationAdapter(org.broadleafcommerce.common.util.StreamCapableTransactionalOperationAdapter) IOException(java.io.IOException)

Aggregations

Blob (java.sql.Blob)328 ResultSet (java.sql.ResultSet)130 PreparedStatement (java.sql.PreparedStatement)106 SQLException (java.sql.SQLException)104 InputStream (java.io.InputStream)94 Clob (java.sql.Clob)80 ByteArrayInputStream (java.io.ByteArrayInputStream)57 IOException (java.io.IOException)52 Statement (java.sql.Statement)52 Connection (java.sql.Connection)36 Test (org.junit.Test)31 BigDecimal (java.math.BigDecimal)25 Timestamp (java.sql.Timestamp)25 SQLXML (java.sql.SQLXML)22 OutputStream (java.io.OutputStream)20 NClob (java.sql.NClob)20 Reader (java.io.Reader)18 Date (java.sql.Date)18 ArrayList (java.util.ArrayList)17 List (java.util.List)17