Search in sources :

Example 1 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 2 with SQLType

use of java.sql.SQLType 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 3 with SQLType

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

the class QueryMapper method mapEmbeddedObjectCondition.

private Condition mapEmbeddedObjectCondition(CriteriaDefinition criteria, MapSqlParameterSource parameterSource, Table table, RelationalPersistentProperty embeddedProperty) {
    RelationalPersistentEntity<?> persistentEntity = this.mappingContext.getRequiredPersistentEntity(embeddedProperty);
    Assert.isInstanceOf(persistentEntity.getType(), criteria.getValue(), () -> "Value must be of type " + persistentEntity.getType().getName() + " for embedded entity matching");
    PersistentPropertyAccessor<Object> embeddedAccessor = persistentEntity.getPropertyAccessor(criteria.getValue());
    String prefix = embeddedProperty.getEmbeddedPrefix();
    Condition condition = null;
    for (RelationalPersistentProperty nestedProperty : persistentEntity) {
        SqlIdentifier sqlIdentifier = nestedProperty.getColumnName().transform(prefix::concat);
        Object mappedNestedValue = convertValue(embeddedAccessor.getProperty(nestedProperty), nestedProperty.getTypeInformation());
        SQLType sqlType = converter.getTargetSqlType(nestedProperty);
        Condition mappedCondition = createCondition(table.column(sqlIdentifier), mappedNestedValue, sqlType, parameterSource, criteria.getComparator(), criteria.isIgnoreCase());
        if (condition != null) {
            condition = condition.and(mappedCondition);
        } else {
            condition = mappedCondition;
        }
    }
    return Conditions.nest(condition);
}
Also used : RelationalPersistentProperty(org.springframework.data.relational.core.mapping.RelationalPersistentProperty) SQLType(java.sql.SQLType)

Example 4 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 5 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)

Aggregations

SQLType (java.sql.SQLType)6 JdbcValue (org.springframework.data.jdbc.core.mapping.JdbcValue)4 ArrayList (java.util.ArrayList)2 RelationalPersistentProperty (org.springframework.data.relational.core.mapping.RelationalPersistentProperty)2 Array (java.sql.Array)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