Search in sources :

Example 21 with SerialBlob

use of javax.sql.rowset.serial.SerialBlob 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 22 with SerialBlob

use of javax.sql.rowset.serial.SerialBlob in project nutz by nutzam.

the class UpdateTest method test_update_clob.

@Test
public void test_update_clob() throws SerialException, SQLException {
    dao.create(UpdateClobBlobBean.class, true);
    UpdateClobBlobBean bean = new UpdateClobBlobBean();
    bean.setManytext(new SerialClob(Strings.dup('8', 4097).toCharArray()));
    bean.setManybinary(new SerialBlob(Strings.dup('9', 4097).getBytes()));
    dao.insert(bean);
    bean.setManytext(new SerialClob(Strings.dup('7', 4097).toCharArray()));
    bean.setManybinary(new SerialBlob(Strings.dup('6', 4097).getBytes()));
    dao.update(bean);
}
Also used : UpdateClobBlobBean(org.nutz.dao.test.meta.other.UpdateClobBlobBean) SerialBlob(javax.sql.rowset.serial.SerialBlob) SerialClob(javax.sql.rowset.serial.SerialClob) Test(org.junit.Test)

Example 23 with SerialBlob

use of javax.sql.rowset.serial.SerialBlob in project open-kilda by telstra.

the class SamlConversionUtil method toUpdateSamlConfigEntity.

/**
 * To update saml config entity.
 *
 * @param samlConfigEntity the saml config entity
 * @param roleEntities the role entities
 * @param file the metadata file
 * @param name the provider name
 * @param url the metadata url
 * @param entityId the entityId
 * @param status the provider status
 * @param userCreation the userCreation
 * @param attribute the attribute
 * @return the requireManagerUpdateStatus
 */
public static boolean toUpdateSamlConfigEntity(SamlConfigEntity samlConfigEntity, Set<RoleEntity> roleEntities, MultipartFile file, String name, String url, String entityId, boolean status, boolean userCreation, String attribute) {
    Blob blob = null;
    boolean requireManagerUpdate = false;
    try {
        if (file != null) {
            byte[] bytes = file.getBytes();
            try {
                blob = new SerialBlob(bytes);
            } catch (SerialException e) {
                LOGGER.error("Error occurred while updating saml provider" + e);
            } catch (SQLException e) {
                LOGGER.error("Error occurred while updating saml provider" + e);
            }
            samlConfigEntity.setMetadata(blob);
            samlConfigEntity.setType(IConstants.ProviderType.FILE);
            requireManagerUpdate = true;
            samlConfigEntity.setUrl(null);
        } else if (url != null) {
            samlConfigEntity.setUrl(url);
            samlConfigEntity.setType(IConstants.ProviderType.URL);
            requireManagerUpdate = true;
            samlConfigEntity.setMetadata(null);
        }
        samlConfigEntity.setEntityId(entityId);
        if (userCreation) {
            samlConfigEntity.setUserCreation(true);
            if (!samlConfigEntity.getRoles().isEmpty()) {
                samlConfigEntity.getRoles().clear();
            }
            samlConfigEntity.getRoles().addAll(roleEntities);
        } else {
            samlConfigEntity.setRoles(null);
        }
        samlConfigEntity.setName(name);
        samlConfigEntity.setUserCreation(userCreation);
        samlConfigEntity.setAttribute(attribute);
        samlConfigEntity.setStatus(status);
    } catch (RequestValidationException e) {
        throw new RequestValidationException(e.getMessage());
    } catch (FileNotFoundException e) {
        LOGGER.error("Error occurred while updating saml provider" + e);
    } catch (IOException e) {
        LOGGER.error("Error occurred while updating saml provider" + e);
    }
    return requireManagerUpdate;
}
Also used : SerialBlob(javax.sql.rowset.serial.SerialBlob) Blob(java.sql.Blob) SerialException(javax.sql.rowset.serial.SerialException) SQLException(java.sql.SQLException) FileNotFoundException(java.io.FileNotFoundException) SerialBlob(javax.sql.rowset.serial.SerialBlob) IOException(java.io.IOException) RequestValidationException(org.usermanagement.exception.RequestValidationException)

Example 24 with SerialBlob

use of javax.sql.rowset.serial.SerialBlob in project jdk8u_jdk by JetBrains.

the class SQLOutputImplTests method test05.

/*
     * Validate a Blob can be written and returned
     */
@Test(enabled = true)
public void test05() throws Exception {
    Blob b = new StubBlob();
    outImpl.writeBlob(b);
    SerialBlob sb = (SerialBlob) results.get(0);
    assertTrue(Arrays.equals(b.getBytes(1, (int) b.length()), sb.getBytes(1, (int) sb.length())));
}
Also used : StubBlob(util.StubBlob) SerialBlob(javax.sql.rowset.serial.SerialBlob) Blob(java.sql.Blob) SerialBlob(javax.sql.rowset.serial.SerialBlob) StubBlob(util.StubBlob) Test(org.testng.annotations.Test) BaseTest(util.BaseTest)

Example 25 with SerialBlob

use of javax.sql.rowset.serial.SerialBlob in project jdk8u_jdk by JetBrains.

the class SerialBlobTests method test11.

/*
     * Validate getBinaryStream returns the correct bytes
     */
@Test
public void test11() throws Exception {
    byte[] expected = new byte[] { 1, 2, 3 };
    SerialBlob sb = new SerialBlob(bytes);
    InputStream is = sb.getBinaryStream(1, 3);
    for (byte b : expected) {
        byte val = (byte) is.read();
        assertTrue(b == val, val + " does not match " + b);
    }
}
Also used : InputStream(java.io.InputStream) SerialBlob(javax.sql.rowset.serial.SerialBlob) Test(org.testng.annotations.Test) BaseTest(util.BaseTest)

Aggregations

SerialBlob (javax.sql.rowset.serial.SerialBlob)54 Test (org.testng.annotations.Test)32 BaseTest (util.BaseTest)32 StubBlob (util.StubBlob)16 InputStream (java.io.InputStream)11 SQLException (java.sql.SQLException)8 SerialClob (javax.sql.rowset.serial.SerialClob)8 Test (org.junit.Test)8 IOException (java.io.IOException)7 Blob (java.sql.Blob)7 BlobType (org.teiid.core.types.BlobType)7 Clob (java.sql.Clob)6 BigDecimal (java.math.BigDecimal)3 BlobImpl (org.teiid.core.types.BlobImpl)3 FileNotFoundException (java.io.FileNotFoundException)2 OutputStream (java.io.OutputStream)2 Reader (java.io.Reader)2 SQLXML (java.sql.SQLXML)2 Time (java.sql.Time)2 Timestamp (java.sql.Timestamp)2