Skip to content

Instantly share code, notes, and snippets.

@bancek
Created November 30, 2017 13:45
Show Gist options
  • Save bancek/7c869b9c3cff29db011b33c8c4e66cd1 to your computer and use it in GitHub Desktop.
Save bancek/7c869b9c3cff29db011b33c8c4e66cd1 to your computer and use it in GitHub Desktop.
Monkey patch scalikejdbc String binder to support UTF8 for MySQL VARBINARY columns
import scalikejdbc._
package object models {
val stringUTF8: Binders[String] = Binders((rs, index) => new String(rs.getBytes(index), "UTF-8"))((rs, label) => new String(rs.getBytes(label), "UTF-8"))(v => (ps, idx) => ps.setBytes(idx, v.getBytes("UTF-8")))
{ // patch TypeBinder.string
val field = TypeBinder.getClass.getDeclaredField("string")
field.setAccessible(true)
field.set(TypeBinder, stringUTF8)
}
{ // patch ParameterBinderFactory.stringParameterBinderFactory
val field = ParameterBinderFactory.getClass.getDeclaredField("stringParameterBinderFactory")
field.setAccessible(true)
field.set(ParameterBinderFactory, stringUTF8)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment