SqlValue interface for ArgPreparedStatementSetter [SPR-4558] #9235
Labels
in: data
Issues in data modules (jdbc, orm, oxm, tx)
status: declined
A suggestion or change that we don't feel we should currently apply
type: enhancement
A general enhancement
Stepan Koltsov opened SPR-4558 and commented
Spring should have
interface SqlValue {
Object getSqlValue();
}
and StatementCreatorUtils.setParameterValueInternal should have:
if (value instanceof SqlValue) value = ((SqlValue) value).getSqlValue();
This is needed for implicit conversion of some types to SQL values.
In our projects we have SqlValue interface and we've reimplemented ArgPreparedStatementSetter that to SqlValue. And we've made all our DB-related enums (and some other classes) implement SqlValue. Example:
enum UserState implement SqlValue {
UNKNOWN(0),
WAITING_FOR_MODERATION(1),
AVAILABLE(2),
DELETED(3),
BLOCKED(4);
final int value;
Object getSqlValue() { return value; }
}
void updateUserState(long userId, UserState state) {
getSimpleJdbcTemplate().update("UPDATE users SET state = ? WHERE user_id = ?", state, userId);
// instead of
getSimpleJdbcTemplate().update("UPDATE users SET state = ? WHERE user_id = ?", state.value(), userId);
}
Affects: 2.5.2
The text was updated successfully, but these errors were encountered: