Search in sources :

Example 1 with JdbcValue

use of org.springframework.data.jdbc.core.mapping.JdbcValue in project spring-data-jdbc by spring-projects.

the class DefaultDataAccessStrategy method addConvertedValue.

private void addConvertedValue(SqlIdentifierParameterSource parameterSource, @Nullable Object value, SqlIdentifier paramName, Class<?> javaType, SQLType sqlType) {
    JdbcValue jdbcValue = // 
    converter.writeJdbcValue(// 
    value, // 
    javaType, // 
    sqlType);
    // 
    parameterSource.addValue(// 
    paramName, // 
    jdbcValue.getValue(), jdbcValue.getJdbcType().getVendorTypeNumber());
}
Also used : JdbcValue(org.springframework.data.jdbc.core.mapping.JdbcValue)

Example 2 with JdbcValue

use of org.springframework.data.jdbc.core.mapping.JdbcValue in project spring-data-jdbc by spring-projects.

the class BasicJdbcConverterUnitTests method conversionOfPrimitiveArrays.

// GH-945
@Test
void conversionOfPrimitiveArrays() {
    int[] ints = { 1, 2, 3, 4, 5 };
    JdbcValue converted = converter.writeJdbcValue(ints, ints.getClass(), JdbcUtil.targetSqlTypeFor(ints.getClass()));
    assertThat(converted.getValue()).isInstanceOf(Array.class);
    assertThat(typeFactory.arraySource).containsExactly(1, 2, 3, 4, 5);
}
Also used : JdbcValue(org.springframework.data.jdbc.core.mapping.JdbcValue) Test(org.junit.jupiter.api.Test)

Example 3 with JdbcValue

use of org.springframework.data.jdbc.core.mapping.JdbcValue in project spring-data-jdbc by spring-projects.

the class BasicJdbcConverter method writeJdbcValue.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.jdbc.core.convert.JdbcConverter#writeValue(java.lang.Object, java.lang.Class, int)
	 */
@Override
public JdbcValue writeJdbcValue(@Nullable Object value, Class<?> columnType, SQLType sqlType) {
    JdbcValue jdbcValue = tryToConvertToJdbcValue(value);
    if (jdbcValue != null) {
        return jdbcValue;
    }
    Object convertedValue = writeValue(value, ClassTypeInformation.from(columnType));
    if (convertedValue == null || !convertedValue.getClass().isArray()) {
        return JdbcValue.of(convertedValue, sqlType);
    }
    Class<?> componentType = convertedValue.getClass().getComponentType();
    if (componentType != byte.class && componentType != Byte.class) {
        Object[] objectArray = requireObjectArray(convertedValue);
        return JdbcValue.of(typeFactory.createArray(objectArray), JDBCType.ARRAY);
    }
    if (componentType == Byte.class) {
        convertedValue = ArrayUtils.toPrimitive((Byte[]) convertedValue);
    }
    return JdbcValue.of(convertedValue, JDBCType.BINARY);
}
Also used : JdbcValue(org.springframework.data.jdbc.core.mapping.JdbcValue)

Example 4 with JdbcValue

use of org.springframework.data.jdbc.core.mapping.JdbcValue in project spring-data-jdbc by spring-projects.

the class QueryMapper method mapCondition.

private Condition mapCondition(CriteriaDefinition criteria, MapSqlParameterSource parameterSource, Table table, @Nullable RelationalPersistentEntity<?> entity) {
    Field propertyField = createPropertyField(entity, criteria.getColumn(), this.mappingContext);
    // Single embedded entity
    if (propertyField.isEmbedded()) {
        return mapEmbeddedObjectCondition(criteria, parameterSource, table, ((MetadataBackedField) propertyField).getPath().getLeafProperty());
    }
    TypeInformation<?> actualType = propertyField.getTypeHint().getRequiredActualType();
    Column column = table.column(propertyField.getMappedColumnName());
    Object mappedValue;
    SQLType sqlType;
    if (criteria.getValue() instanceof JdbcValue) {
        JdbcValue settableValue = (JdbcValue) criteria.getValue();
        mappedValue = convertValue(settableValue.getValue(), propertyField.getTypeHint());
        sqlType = getTypeHint(mappedValue, actualType.getType(), settableValue);
    } else if (criteria.getValue() instanceof ValueFunction) {
        ValueFunction<Object> valueFunction = (ValueFunction<Object>) criteria.getValue();
        Object value = valueFunction.apply(getEscaper(criteria.getComparator()));
        mappedValue = convertValue(value, propertyField.getTypeHint());
        sqlType = propertyField.getSqlType();
    } else if (// 
    propertyField instanceof MetadataBackedField && // 
    ((MetadataBackedField) propertyField).property != null && (criteria.getValue() == null || !criteria.getValue().getClass().isArray())) {
        RelationalPersistentProperty property = ((MetadataBackedField) propertyField).property;
        JdbcValue jdbcValue = convertToJdbcValue(property, criteria.getValue());
        mappedValue = jdbcValue.getValue();
        sqlType = jdbcValue.getJdbcType() != null ? jdbcValue.getJdbcType() : propertyField.getSqlType();
    } else {
        mappedValue = convertValue(criteria.getValue(), propertyField.getTypeHint());
        sqlType = propertyField.getSqlType();
    }
    return createCondition(column, mappedValue, sqlType, parameterSource, criteria.getComparator(), criteria.isIgnoreCase());
}
Also used : ValueFunction(org.springframework.data.relational.core.query.ValueFunction) RelationalPersistentProperty(org.springframework.data.relational.core.mapping.RelationalPersistentProperty) SQLType(java.sql.SQLType) JdbcValue(org.springframework.data.jdbc.core.mapping.JdbcValue)

Example 5 with JdbcValue

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

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