Skip to content

Instantly share code, notes, and snippets.

@Pyppe
Created May 6, 2013 17:31
Show Gist options
  • Save Pyppe/5526629 to your computer and use it in GitHub Desktop.
Save Pyppe/5526629 to your computer and use it in GitHub Desktop.
package foobar
import scala.slick.lifted.{BaseTypeMapper, TypeMapperDelegate}
import scala.slick.driver.BasicProfile
import scala.slick.session.{Session, PositionedParameters, PositionedResult}
import language.implicitConversions
abstract class ArrayTypeMapper[T: scala.reflect.ClassTag] extends BaseTypeMapper[Array[T]] with TypeMapperDelegate[Array[T]] {
def apply(p: BasicProfile) = this
override def sqlType = java.sql.Types.ARRAY
implicit def stringArrayToSqlArray(in: Array[String]): java.sql.Array =
Schema.withSession { implicit session =>
session.conn.createArrayOf("varchar", in.map(s => s.asInstanceOf[Object]))
}
}
object DbArrays {
implicit object StringArrayTypeMapper extends ArrayTypeMapper[String] {
override def zero = Array[String]()
override def sqlTypeName = "text ARRAY"
override def setValue(value: Array[String], p: PositionedParameters) {
val npos = p.pos + 1
p.ps.setArray(npos, value)
p.pos = npos
}
override def setOption(value: Option[Array[String]], p: PositionedParameters) {
val npos = p.pos + 1
if (value eq None) p.ps.setNull(npos, sqlType)
else p.ps.setArray(npos, value.get)
p.pos = npos
}
override def nextValue(r: PositionedResult) = {
val npos = r.currentPos + 1
val a = r.rs.getArray(npos)
r.skip
a.getArray.asInstanceOf[Array[String]]
}
override final def updateValue(value: Array[String], r: PositionedResult) {
val npos = r.currentPos + 1
r.rs.updateArray (npos, value)
r.skip
}
override def valueToSQLLiteral(v: Array[String]) = "'" + (v: java.sql.Array).toString + "'"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment