Search in sources :

Example 1 with StructConverter

use of org.eclipse.persistence.platform.database.converters.StructConverter in project eclipselink by eclipse-ee4j.

the class EntityManagerSetupImpl method addStructConverters.

/**
 * Add the StructConverters that were specified by annotation on the DatabasePlatform
 * This method must be called after the DatabasePlatform has been detected
 */
public void addStructConverters() {
    if (this.compositeMemberEmSetupImpls == null) {
        for (StructConverter structConverter : structConverters) {
            if (session.getPlatform().getTypeConverters().get(structConverter.getJavaType()) != null) {
                throw ValidationException.twoStructConvertersAddedForSameClass(structConverter.getJavaType().getName());
            }
            session.getPlatform().addStructConverter(structConverter);
        }
    } else {
        // composite
        for (EntityManagerSetupImpl compositeMemberEmSetupImpl : this.compositeMemberEmSetupImpls) {
            if (!compositeMemberEmSetupImpl.structConverters.isEmpty()) {
                String compositeMemberPuName = compositeMemberEmSetupImpl.getPersistenceUnitInfo().getPersistenceUnitName();
                // debug output added to make it easier to navigate the log because the method is called outside of composite member deploy
                compositeMemberEmSetupImpl.session.log(SessionLog.FINEST, SessionLog.PROPERTIES, "composite_member_begin_call", new Object[] { "addStructConverters", compositeMemberPuName, state });
                compositeMemberEmSetupImpl.addStructConverters();
                compositeMemberEmSetupImpl.session.log(SessionLog.FINEST, SessionLog.PROPERTIES, "composite_member_end_call", new Object[] { "addStructConverters", compositeMemberPuName, state });
            }
        }
    }
}
Also used : StructConverter(org.eclipse.persistence.platform.database.converters.StructConverter) EntityManagerFactoryProvider.getConfigPropertyAsString(org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.getConfigPropertyAsString)

Example 2 with StructConverter

use of org.eclipse.persistence.platform.database.converters.StructConverter in project eclipselink by eclipse-ee4j.

the class StructConverterTestSuite method testPlatform.

/**
 * Ensure the DatabasePlatform is setup with the proper StructConverters
 */
public void testPlatform() {
    if (this.supported) {
        EntityManager em = createEntityManager(STRUCT_CONVERTER_PU);
        try {
            // trigger deploy
            em.find(SimpleSpatial.class, Long.valueOf(1));
        } catch (Exception e) {
        }
        ;
        StructConverter converter = getServerSession(STRUCT_CONVERTER_PU).getPlatform().getTypeConverters().get(JGeometry.class);
        assertNotNull("Platform does not have correct JGeometryConverter.", converter);
        assertTrue("JGeometery struct converter is wrong type.", converter.getClass().getName().indexOf("JGeometryConverter") >= 0);
        converter = getServerSession(STRUCT_CONVERTER_PU).getPlatform().getTypeConverters().get(DummyStructConverterType.class);
        assertNotNull("Platform does not have correct DummyStructConverter.", converter);
        assertTrue("JGeometery struct converter is wrong type.", converter.getClass().getName().indexOf("DummyStructConverter") >= 0);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) StructConverter(org.eclipse.persistence.platform.database.converters.StructConverter) DummyStructConverterType(org.eclipse.persistence.testing.models.jpa.structconverter.DummyStructConverterType)

Example 3 with StructConverter

use of org.eclipse.persistence.platform.database.converters.StructConverter in project eclipselink by eclipse-ee4j.

the class StructConverterTestSuite method testXMLPlatform.

/**
 * Ensure the DatabasePlatform is setup with the proper StructConverters
 */
public void testXMLPlatform() {
    if (this.supported && !isOnServer()) {
        EntityManager em = createEntityManager(XML_STRUCT_CONVERTER_PU);
        try {
            // trigger deploy
            em.find(SimpleXMLSpatial.class, Long.valueOf(1));
        } catch (Exception e) {
        }
        ;
        StructConverter converter = getServerSession(XML_STRUCT_CONVERTER_PU).getPlatform().getTypeConverters().get(JGeometry.class);
        assertNotNull("Platform does not have correct JGeometryConverter.", converter);
        assertTrue("JGeometery struct converter is wrong type.", converter.getClass().getName().indexOf("JGeometryConverter") >= 0);
        converter = getServerSession(XML_STRUCT_CONVERTER_PU).getPlatform().getTypeConverters().get(DummyStructConverterType.class);
        assertNotNull("Platform does not have correct DummyStructConverter.", converter);
        assertTrue("JGeometery struct converter is wrong type.", converter.getClass().getName().indexOf("DummyStructConverter") >= 0);
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) StructConverter(org.eclipse.persistence.platform.database.converters.StructConverter) DummyStructConverterType(org.eclipse.persistence.testing.models.jpa.structconverter.DummyStructConverterType)

Example 4 with StructConverter

use of org.eclipse.persistence.platform.database.converters.StructConverter in project eclipselink by eclipse-ee4j.

the class DatabasePlatform method setParameterValueInDatabaseCall.

/**
 * INTERNAL
 * Set the parameter in the JDBC statement with the given name.
 * This support a wide range of different parameter types,
 * and is heavily optimized for common types.
 */
public void setParameterValueInDatabaseCall(Object parameter, CallableStatement statement, String name, AbstractSession session) throws SQLException {
    // Process common types first.
    if (parameter instanceof String) {
        // Check for stream binding of large strings.
        if (usesStringBinding() && (((String) parameter).length() > getStringBindingSize())) {
            CharArrayReader reader = new CharArrayReader(((String) parameter).toCharArray());
            statement.setCharacterStream(name, reader, ((String) parameter).length());
        } else {
            if (shouldUseGetSetNString()) {
                statement.setNString(name, (String) parameter);
            } else {
                statement.setString(name, (String) parameter);
            }
        }
    } else if (parameter instanceof Number) {
        Number number = (Number) parameter;
        if (number instanceof Integer) {
            statement.setInt(name, number.intValue());
        } else if (number instanceof Long) {
            statement.setLong(name, number.longValue());
        } else if (number instanceof BigDecimal) {
            statement.setBigDecimal(name, (BigDecimal) number);
        } else if (number instanceof Double) {
            statement.setDouble(name, number.doubleValue());
        } else if (number instanceof Float) {
            statement.setFloat(name, number.floatValue());
        } else if (number instanceof Short) {
            statement.setShort(name, number.shortValue());
        } else if (number instanceof Byte) {
            statement.setByte(name, number.byteValue());
        } else if (number instanceof BigInteger) {
            // Convert to BigDecimal.
            statement.setBigDecimal(name, new BigDecimal((BigInteger) number));
        } else {
            statement.setObject(name, parameter);
        }
    } else if (parameter instanceof java.sql.Date) {
        statement.setDate(name, (java.sql.Date) parameter);
    } else if (parameter instanceof java.time.LocalDate) {
        statement.setDate(name, java.sql.Date.valueOf((java.time.LocalDate) parameter));
    } else if (parameter instanceof java.sql.Timestamp) {
        statement.setTimestamp(name, (java.sql.Timestamp) parameter);
    } else if (parameter instanceof java.time.LocalDateTime) {
        statement.setTimestamp(name, java.sql.Timestamp.valueOf((java.time.LocalDateTime) parameter));
    } else if (parameter instanceof java.time.OffsetDateTime) {
        statement.setTimestamp(name, java.sql.Timestamp.from(((java.time.OffsetDateTime) parameter).toInstant()));
    } else if (parameter instanceof java.sql.Time) {
        statement.setTime(name, (java.sql.Time) parameter);
    } else if (parameter instanceof java.time.LocalTime) {
        java.time.LocalTime lt = (java.time.LocalTime) parameter;
        java.sql.Timestamp ts = java.sql.Timestamp.valueOf(java.time.LocalDateTime.of(java.time.LocalDate.ofEpochDay(0), lt));
        statement.setTimestamp(name, ts);
    } else if (parameter instanceof java.time.OffsetTime) {
        java.time.OffsetTime ot = (java.time.OffsetTime) parameter;
        java.sql.Timestamp ts = java.sql.Timestamp.valueOf(java.time.LocalDateTime.of(java.time.LocalDate.ofEpochDay(0), ot.toLocalTime()));
        statement.setTimestamp(name, ts);
    } else if (parameter instanceof Boolean) {
        statement.setBoolean(name, (Boolean) parameter);
    } else if (parameter == null) {
        // Normally null is passed as a DatabaseField so the type is included, but in some case may be passed directly.
        statement.setNull(name, getJDBCType((Class<?>) null));
    } else if (parameter instanceof DatabaseField) {
        setNullFromDatabaseField((DatabaseField) parameter, statement, name);
    } else if (parameter instanceof byte[]) {
        if (usesStreamsForBinding()) {
            ByteArrayInputStream inputStream = new ByteArrayInputStream((byte[]) parameter);
            statement.setBinaryStream(name, inputStream, ((byte[]) parameter).length);
        } else {
            statement.setBytes(name, (byte[]) parameter);
        }
    } else // Next process types that need conversion.
    if (parameter instanceof Calendar) {
        statement.setTimestamp(name, Helper.timestampFromDate(((Calendar) parameter).getTime()));
    } else if (parameter.getClass() == ClassConstants.UTILDATE) {
        statement.setTimestamp(name, Helper.timestampFromDate((java.util.Date) parameter));
    } else if (parameter instanceof Character) {
        statement.setString(name, ((Character) parameter).toString());
    } else if (parameter instanceof char[]) {
        statement.setString(name, new String((char[]) parameter));
    } else if (parameter instanceof Character[]) {
        statement.setString(name, convertObject(parameter, ClassConstants.STRING));
    } else if (parameter instanceof Byte[]) {
        statement.setBytes(name, (byte[]) convertObject(parameter, ClassConstants.APBYTE));
    } else if (parameter instanceof SQLXML) {
        statement.setSQLXML(name, (SQLXML) parameter);
    } else if (parameter instanceof BindCallCustomParameter) {
        ((BindCallCustomParameter) (parameter)).set(this, statement, name, session);
    } else if (typeConverters != null && typeConverters.containsKey(parameter.getClass())) {
        StructConverter converter = typeConverters.get(parameter.getClass());
        parameter = converter.convertToStruct(parameter, getConnection(session, statement.getConnection()));
        statement.setObject(name, parameter);
    } else {
        statement.setObject(name, parameter);
    }
}
Also used : StructConverter(org.eclipse.persistence.platform.database.converters.StructConverter) Calendar(java.util.Calendar) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) SQLXML(java.sql.SQLXML) CharArrayReader(java.io.CharArrayReader) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectRelationalDatabaseField(org.eclipse.persistence.mappings.structures.ObjectRelationalDatabaseField) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) BigInteger(java.math.BigInteger)

Example 5 with StructConverter

use of org.eclipse.persistence.platform.database.converters.StructConverter in project eclipselink by eclipse-ee4j.

the class DatabasePlatform method setParameterValueInDatabaseCall.

/**
 * INTERNAL
 * Set the parameter in the JDBC statement at the given index.
 * This support a wide range of different parameter types,
 * and is heavily optimized for common types.
 */
public void setParameterValueInDatabaseCall(Object parameter, PreparedStatement statement, int index, AbstractSession session) throws SQLException {
    // Process common types first.
    if (parameter instanceof String) {
        // Check for stream binding of large strings.
        if (usesStringBinding() && (((String) parameter).length() > getStringBindingSize())) {
            CharArrayReader reader = new CharArrayReader(((String) parameter).toCharArray());
            statement.setCharacterStream(index, reader, ((String) parameter).length());
        } else {
            if (shouldUseGetSetNString()) {
                statement.setNString(index, (String) parameter);
            } else {
                statement.setString(index, (String) parameter);
            }
        }
    } else if (parameter instanceof Number) {
        Number number = (Number) parameter;
        if (number instanceof Integer) {
            statement.setInt(index, number.intValue());
        } else if (number instanceof Long) {
            statement.setLong(index, number.longValue());
        } else if (number instanceof BigDecimal) {
            statement.setBigDecimal(index, (BigDecimal) number);
        } else if (number instanceof Double) {
            statement.setDouble(index, number.doubleValue());
        } else if (number instanceof Float) {
            statement.setFloat(index, number.floatValue());
        } else if (number instanceof Short) {
            statement.setShort(index, number.shortValue());
        } else if (number instanceof Byte) {
            statement.setByte(index, number.byteValue());
        } else if (number instanceof BigInteger) {
            // Convert to BigDecimal.
            statement.setBigDecimal(index, new BigDecimal((BigInteger) number));
        } else {
            statement.setObject(index, parameter);
        }
    } else if (parameter instanceof java.sql.Date) {
        statement.setDate(index, (java.sql.Date) parameter);
    } else if (parameter instanceof java.time.LocalDate) {
        statement.setDate(index, java.sql.Date.valueOf((java.time.LocalDate) parameter));
    } else if (parameter instanceof java.sql.Timestamp) {
        statement.setTimestamp(index, (java.sql.Timestamp) parameter);
    } else if (parameter instanceof java.time.LocalDateTime) {
        statement.setTimestamp(index, java.sql.Timestamp.valueOf((java.time.LocalDateTime) parameter));
    } else if (parameter instanceof java.time.OffsetDateTime) {
        statement.setTimestamp(index, java.sql.Timestamp.from(((java.time.OffsetDateTime) parameter).toInstant()));
    } else if (parameter instanceof java.sql.Time) {
        statement.setTime(index, (java.sql.Time) parameter);
    } else if (parameter instanceof java.time.LocalTime) {
        java.time.LocalTime lt = (java.time.LocalTime) parameter;
        java.sql.Timestamp ts = java.sql.Timestamp.valueOf(java.time.LocalDateTime.of(java.time.LocalDate.ofEpochDay(0), lt));
        statement.setTimestamp(index, ts);
    } else if (parameter instanceof java.time.OffsetTime) {
        java.time.OffsetTime ot = (java.time.OffsetTime) parameter;
        java.sql.Timestamp ts = java.sql.Timestamp.valueOf(java.time.LocalDateTime.of(java.time.LocalDate.ofEpochDay(0), ot.toLocalTime()));
        statement.setTimestamp(index, ts);
    } else if (parameter instanceof Boolean) {
        statement.setBoolean(index, (Boolean) parameter);
    } else if (parameter == null) {
        // Normally null is passed as a DatabaseField so the type is included, but in some case may be passed directly.
        statement.setNull(index, getJDBCType((Class<?>) null));
    } else if (parameter instanceof DatabaseField) {
        setNullFromDatabaseField((DatabaseField) parameter, statement, index);
    } else if (parameter instanceof byte[]) {
        if (usesStreamsForBinding()) {
            ByteArrayInputStream inputStream = new ByteArrayInputStream((byte[]) parameter);
            statement.setBinaryStream(index, inputStream, ((byte[]) parameter).length);
        } else {
            statement.setBytes(index, (byte[]) parameter);
        }
    } else // Next process types that need conversion.
    if (parameter instanceof Calendar) {
        statement.setTimestamp(index, Helper.timestampFromDate(((Calendar) parameter).getTime()));
    } else if (parameter.getClass() == ClassConstants.UTILDATE) {
        statement.setTimestamp(index, Helper.timestampFromDate((java.util.Date) parameter));
    } else if (parameter instanceof Character) {
        statement.setString(index, ((Character) parameter).toString());
    } else if (parameter instanceof char[]) {
        statement.setString(index, new String((char[]) parameter));
    } else if (parameter instanceof Character[]) {
        statement.setString(index, convertObject(parameter, ClassConstants.STRING));
    } else if (parameter instanceof Byte[]) {
        statement.setBytes(index, (byte[]) convertObject(parameter, ClassConstants.APBYTE));
    } else if (parameter instanceof SQLXML) {
        statement.setSQLXML(index, (SQLXML) parameter);
    } else if (parameter instanceof BindCallCustomParameter) {
        ((BindCallCustomParameter) (parameter)).set(this, statement, index, session);
    } else if (typeConverters != null && typeConverters.containsKey(parameter.getClass())) {
        StructConverter converter = typeConverters.get(parameter.getClass());
        parameter = converter.convertToStruct(parameter, getConnection(session, statement.getConnection()));
        statement.setObject(index, parameter);
    } else if (parameter instanceof UUID) {
        statement.setString(index, convertObject(parameter, ClassConstants.STRING));
    } else {
        statement.setObject(index, parameter);
    }
}
Also used : StructConverter(org.eclipse.persistence.platform.database.converters.StructConverter) UUID(java.util.UUID) Calendar(java.util.Calendar) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) SQLXML(java.sql.SQLXML) CharArrayReader(java.io.CharArrayReader) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectRelationalDatabaseField(org.eclipse.persistence.mappings.structures.ObjectRelationalDatabaseField) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) BigInteger(java.math.BigInteger)

Aggregations

StructConverter (org.eclipse.persistence.platform.database.converters.StructConverter)8 ObjectRelationalDatabaseField (org.eclipse.persistence.mappings.structures.ObjectRelationalDatabaseField)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 CharArrayReader (java.io.CharArrayReader)3 BigDecimal (java.math.BigDecimal)3 BigInteger (java.math.BigInteger)3 Calendar (java.util.Calendar)3 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)3 EntityManager (jakarta.persistence.EntityManager)2 PrivilegedActionException (java.security.PrivilegedActionException)2 SQLXML (java.sql.SQLXML)2 DummyStructConverterType (org.eclipse.persistence.testing.models.jpa.structconverter.DummyStructConverterType)2 Method (java.lang.reflect.Method)1 UUID (java.util.UUID)1 SessionLoaderException (org.eclipse.persistence.exceptions.SessionLoaderException)1 ValidationException (org.eclipse.persistence.exceptions.ValidationException)1 BindCallCustomParameter (org.eclipse.persistence.internal.databaseaccess.BindCallCustomParameter)1 DatabasePlatform (org.eclipse.persistence.internal.databaseaccess.DatabasePlatform)1 DatasourcePlatform (org.eclipse.persistence.internal.databaseaccess.DatasourcePlatform)1 Platform (org.eclipse.persistence.internal.databaseaccess.Platform)1