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());
}
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);
}
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);
}
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());
}
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);
}
Aggregations