Search in sources :

Example 6 with SQLType

use of java.sql.SQLType in project spring-data-jdbc by spring-projects.

the class QueryMapper method convertToJdbcValue.

/**
 * Converts values while taking specific value types like arrays, {@link Iterable}, or {@link Pair}.
 *
 * @param property the property to which the value relates. It determines the type to convert to. Must not be
 *          {@literal null}.
 * @param value the value to be converted.
 * @return a non null {@link JdbcValue} holding the converted value and the appropriate JDBC type information.
 */
private JdbcValue convertToJdbcValue(RelationalPersistentProperty property, @Nullable Object value) {
    if (value == null) {
        return JdbcValue.of(null, JDBCType.NULL);
    }
    if (value instanceof Pair) {
        JdbcValue first = getWriteValue(property, ((Pair<?, ?>) value).getFirst());
        JdbcValue second = getWriteValue(property, ((Pair<?, ?>) value).getSecond());
        return JdbcValue.of(Pair.of(first.getValue(), second.getValue()), first.getJdbcType());
    }
    if (value instanceof Iterable) {
        List<Object> mapped = new ArrayList<>();
        SQLType jdbcType = null;
        for (Object o : (Iterable<?>) value) {
            JdbcValue jdbcValue = getWriteValue(property, o);
            if (jdbcType == null) {
                jdbcType = jdbcValue.getJdbcType();
            }
            mapped.add(jdbcValue.getValue());
        }
        return JdbcValue.of(mapped, jdbcType);
    }
    if (value.getClass().isArray()) {
        Object[] valueAsArray = (Object[]) value;
        Object[] mappedValueArray = new Object[valueAsArray.length];
        SQLType jdbcType = null;
        for (int i = 0; i < valueAsArray.length; i++) {
            JdbcValue jdbcValue = getWriteValue(property, valueAsArray[i]);
            if (jdbcType == null) {
                jdbcType = jdbcValue.getJdbcType();
            }
            mappedValueArray[i] = jdbcValue.getValue();
        }
        return JdbcValue.of(mappedValueArray, jdbcType);
    }
    return getWriteValue(property, value);
}
Also used : ArrayList(java.util.ArrayList) SQLType(java.sql.SQLType) Pair(org.springframework.data.util.Pair) JdbcValue(org.springframework.data.jdbc.core.mapping.JdbcValue)

Example 7 with SQLType

use of java.sql.SQLType in project spring-data-jdbc by spring-projects.

the class DefaultDataAccessStrategy method addConvertedPropertyValuesAsList.

private void addConvertedPropertyValuesAsList(SqlIdentifierParameterSource parameterSource, RelationalPersistentProperty property, Iterable<?> values, SqlIdentifier paramName) {
    List<Object> convertedIds = new ArrayList<>();
    JdbcValue jdbcValue = null;
    for (Object id : values) {
        Class<?> columnType = converter.getColumnType(property);
        SQLType sqlType = converter.getTargetSqlType(property);
        jdbcValue = converter.writeJdbcValue(id, columnType, sqlType);
        convertedIds.add(jdbcValue.getValue());
    }
    Assert.state(jdbcValue != null, "JdbcValue must be not null at this point. Please report this as a bug.");
    SQLType jdbcType = jdbcValue.getJdbcType();
    int typeNumber = jdbcType == null ? JdbcUtils.TYPE_UNKNOWN : jdbcType.getVendorTypeNumber();
    parameterSource.addValue(paramName, convertedIds, typeNumber);
}
Also used : ArrayList(java.util.ArrayList) SQLType(java.sql.SQLType) JdbcValue(org.springframework.data.jdbc.core.mapping.JdbcValue)

Example 8 with SQLType

use of java.sql.SQLType in project spring-data-jdbc by spring-projects.

the class DefaultJdbcTypeFactory method createArray.

@Override
public Array createArray(Object[] value) {
    Assert.notNull(value, "Value must not be null.");
    Class<?> componentType = arrayColumns.getArrayType(value.getClass());
    SQLType jdbcType = JdbcUtil.targetSqlTypeFor(componentType);
    Assert.notNull(jdbcType, () -> String.format("Couldn't determine JDBCType for %s", componentType));
    String typeName = arrayColumns.getArrayTypeName(jdbcType);
    return operations.execute((ConnectionCallback<Array>) c -> c.createArrayOf(typeName, value));
}
Also used : Array(java.sql.Array) JdbcOperations(org.springframework.jdbc.core.JdbcOperations) JdbcArrayColumns(org.springframework.data.jdbc.core.dialect.JdbcArrayColumns) SQLType(java.sql.SQLType) JdbcUtil(org.springframework.data.jdbc.support.JdbcUtil) ConnectionCallback(org.springframework.jdbc.core.ConnectionCallback) Array(java.sql.Array) Assert(org.springframework.util.Assert) SQLType(java.sql.SQLType)

Example 9 with SQLType

use of java.sql.SQLType in project spring-data-jdbc by spring-projects.

the class StringBasedJdbcQuery method convertAndAddParameter.

private void convertAndAddParameter(MapSqlParameterSource parameters, Parameter p, Object value) {
    String parameterName = p.getName().orElseThrow(() -> new IllegalStateException(PARAMETER_NEEDS_TO_BE_NAMED));
    Class<?> parameterType = queryMethod.getParameters().getParameter(p.getIndex()).getType();
    Class<?> conversionTargetType = JdbcColumnTypes.INSTANCE.resolvePrimitiveType(parameterType);
    JdbcValue jdbcValue = converter.writeJdbcValue(value, conversionTargetType, JdbcUtil.targetSqlTypeFor(conversionTargetType));
    SQLType jdbcType = jdbcValue.getJdbcType();
    if (jdbcType == null) {
        parameters.addValue(parameterName, jdbcValue.getValue());
    } else {
        parameters.addValue(parameterName, jdbcValue.getValue(), jdbcType.getVendorTypeNumber());
    }
}
Also used : SQLType(java.sql.SQLType) JdbcValue(org.springframework.data.jdbc.core.mapping.JdbcValue)

Example 10 with SQLType

use of java.sql.SQLType in project oracle-db-examples by oracle.

the class DateTimeStampSample method updateEmployee.

/**
 * Updates the employee record for given employee id.
 *
 * @param id
 *          Employee id.
 * @param conn
 *          Connection to be used to update employee data.
 * @throws SQLException
 */
private void updateEmployee(int id, Connection conn) throws SQLException {
    final String updateQuery = "UPDATE EMP_DATE_JDBC_SAMPLE SET DATE_OF_JOINING=? WHERE EMP_ID =?";
    try (PreparedStatement pstmt = conn.prepareStatement(updateQuery)) {
        SQLType dataType = OracleType.TIMESTAMP_WITH_LOCAL_TIME_ZONE;
        pstmt.setObject(1, ZonedDateTime.parse("2015-12-09T22:22:22-08:00[PST8PDT]"), dataType);
        pstmt.setInt(2, id);
        int updateCount = pstmt.executeUpdate();
        show("Successfully updated employee details.");
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) SQLType(java.sql.SQLType)

Aggregations

SQLType (java.sql.SQLType)10 JdbcValue (org.springframework.data.jdbc.core.mapping.JdbcValue)4 PreparedStatement (java.sql.PreparedStatement)2 ArrayList (java.util.ArrayList)2 RelationalPersistentProperty (org.springframework.data.relational.core.mapping.RelationalPersistentProperty)2 Array (java.sql.Array)1 JDBCType (java.sql.JDBCType)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 Date (java.util.Date)1 Test (org.junit.jupiter.api.Test)1 JdbcArrayColumns (org.springframework.data.jdbc.core.dialect.JdbcArrayColumns)1 JdbcUtil (org.springframework.data.jdbc.support.JdbcUtil)1 ValueFunction (org.springframework.data.relational.core.query.ValueFunction)1 Pair (org.springframework.data.util.Pair)1 ConnectionCallback (org.springframework.jdbc.core.ConnectionCallback)1 JdbcOperations (org.springframework.jdbc.core.JdbcOperations)1 Assert (org.springframework.util.Assert)1 JavaSqlType (schemacrawler.schema.JavaSqlType)1