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