Created
September 19, 2018 15:38
-
-
Save serafdev/49b126e25f87e3a5ee3d054c1e9a0af9 to your computer and use it in GitHub Desktop.
Binders examples for scalikejdbc
This file contains hidden or 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.Binders | |
trait SQLTypeBinders { | |
implicit val 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("INT", list.map(_.asInstanceOf[AnyRef]).toArray) | |
s.setArray(i, array) | |
}) | |
} | |
implicit val sqlArrayToInt2DListBinder: Binders[List[List[Int]]] = { | |
def sqlArrayToInt2DList(a: java.sql.Array): List[List[Int]] = | |
a.getArray.asInstanceOf[Array[AnyRef]] | |
.map(_.asInstanceOf[Array[AnyRef]] | |
.map(_.asInstanceOf[Int]).toList | |
).toList | |
Binders[List[List[Int]]]((r, i) => | |
sqlArrayToInt2DList(r.getArray(i)) | |
)((r, s) => | |
sqlArrayToInt2DList(r.getArray(s)) | |
)(list2D => (s, i) => { | |
val array = s.getConnection.createArrayOf("INT", | |
list2D.map( | |
list => | |
list.map(_.asInstanceOf[AnyRef]).toArray | |
).toArray) | |
s.setArray(i, array) | |
}) | |
} | |
implicit val sqlArrayToDoubleListBinder: Binders[List[Double]] = { | |
def sqlArrayToDoubleList(a: java.sql.Array): List[Double] = | |
a.getArray.asInstanceOf[Array[AnyRef]].map(x => scala.math.BigDecimal(x.asInstanceOf[java.math.BigDecimal]).toDouble).toList | |
Binders[List[Double]]((r, i) => | |
sqlArrayToDoubleList(r.getArray(i)) | |
)((r, s) => | |
sqlArrayToDoubleList(r.getArray(s)) | |
)(list => (s, i) => { | |
val array = s.getConnection.createArrayOf("numeric", list.map(_.asInstanceOf[AnyRef]).toArray) | |
s.setArray(i, array) | |
}) | |
} | |
implicit val sqlArrayToDouble2DListBinder: Binders[List[List[Double]]] = { | |
def sqlArrayToDouble2DList(a: java.sql.Array): List[List[Double]] = | |
a.getArray.asInstanceOf[Array[AnyRef]] | |
.map(_.asInstanceOf[Array[AnyRef]] | |
.map(x => scala.math.BigDecimal(x.asInstanceOf[java.math.BigDecimal]).toDouble).toList | |
).toList | |
Binders[List[List[Double]]]((r, i) => | |
sqlArrayToDouble2DList(r.getArray(i)) | |
)((r, s) => | |
sqlArrayToDouble2DList(r.getArray(s)) | |
)(list2D => (s, i) => { | |
val array = s.getConnection.createArrayOf("numeric", | |
list2D.map( | |
list => | |
list.map(_.asInstanceOf[AnyRef]).toArray | |
).toArray) | |
s.setArray(i, array) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment