Skip to content

Instantly share code, notes, and snippets.

@sunnyone
Created October 3, 2018 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunnyone/b9e5c9d3b6bc05fe2b09cdfc5c12dc8a to your computer and use it in GitHub Desktop.
Save sunnyone/b9e5c9d3b6bc05fe2b09cdfc5c12dc8a to your computer and use it in GitHub Desktop.
fun convert(value: Any?, sqlType: Int, typeName: String?): Triple<Any?, Int, String?> {
if (value is CodedEnum) {
return Triple(value.code, sqlType, typeName)
}
return Triple(value, sqlType, typeName)
}
open class CustomizedNamedParameterJdbcTemplate(classicJdbcTemplate: JdbcOperations) :
NamedParameterJdbcTemplate(classicJdbcTemplate) {
override fun getPreparedStatementCreator(sql: String,
paramSource: SqlParameterSource,
customizer: Consumer<PreparedStatementCreatorFactory>?)
: PreparedStatementCreator {
val newParamSource = MapSqlParameterSource()
paramSource.parameterNames?.forEach { name ->
val (value, sqlType, typeName) = convert(
paramSource.getValue(name),
paramSource.getSqlType(name),
paramSource.getTypeName(name))
if (typeName != null) {
newParamSource.addValue(name, value, sqlType, typeName)
} else {
newParamSource.addValue(name, value, sqlType)
}
}
return super.getPreparedStatementCreator(sql, newParamSource, customizer)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment