Search in sources :

Example 6 with JdbcValue

use of org.springframework.data.jdbc.core.mapping.JdbcValue 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 7 with JdbcValue

use of org.springframework.data.jdbc.core.mapping.JdbcValue 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)

Aggregations

JdbcValue (org.springframework.data.jdbc.core.mapping.JdbcValue)7 SQLType (java.sql.SQLType)4 ArrayList (java.util.ArrayList)2 Test (org.junit.jupiter.api.Test)1 RelationalPersistentProperty (org.springframework.data.relational.core.mapping.RelationalPersistentProperty)1 ValueFunction (org.springframework.data.relational.core.query.ValueFunction)1 Pair (org.springframework.data.util.Pair)1