Created
November 30, 2017 13:45
-
-
Save bancek/7c869b9c3cff29db011b33c8c4e66cd1 to your computer and use it in GitHub Desktop.
Monkey patch scalikejdbc String binder to support UTF8 for MySQL VARBINARY columns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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