Created
February 13, 2020 11:31
-
-
Save mesut/c583528c822614e9722dddacd8fe08c6 to your computer and use it in GitHub Desktop.
scalikejdbc custom array types
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
implicit def sqlArrayToIntListBinder: Binders[List[Int]] = { | |
def sqlArrayToIntList(a: java.sql.Array): List[Int] = a.getArray.asInstanceOf[Array[AnyRef]].map(_.asInstanceOf[Int]).toList | |
Binders[List[Int]]((r, i) => sqlArrayToIntList(r.getArray(i)))((r, s) => sqlArrayToIntList(r.getArray(s)))(list => (s, i) => { | |
val array = s.getConnection.createArrayOf("integer", list.map(_.asInstanceOf[AnyRef]).toArray) | |
s.setArray(i, array) | |
}) | |
} | |
implicit def sqlArrayToStringListBinder: Binders[List[String]] = { | |
def sqlArrayToStringList(a: java.sql.Array): List[String] = a.getArray.asInstanceOf[Array[AnyRef]].map(_.asInstanceOf[String]).toList | |
Binders[List[String]]((r, i) => sqlArrayToStringList(r.getArray(i)))((r, s) => sqlArrayToStringList(r.getArray(s)))(list => (s, i) => { | |
val array = s.getConnection.createArrayOf("varchar", list.map(_.asInstanceOf[AnyRef]).toArray) | |
s.setArray(i, array) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thx for this little snippet, it helps me to create my own custom converter = )