package foo.bar
import org.sql2o.converters.{Convert, Converter, ConverterException}
class OptionConverter[T] extends Converter[Option[T]] {
override def convert(value: Any): Option[T] = {
value match {
case null | None => None
case _ =>
val valueConverter = Convert.getConverterIfExists(value.getClass)
if (valueConverter == null)
throw new ConverterException(s"Cannot find a converter for type ${value.getClass.toString}.")
else
Option(valueConverter.convert(value).asInstanceOf[T])
}
}
override def toDatabaseParam(value: Option[T]): AnyRef =
{
value match {
case None => null
case Some(x) =>
val valueConverter = Convert.getConverterIfExists(x.getClass)
if (valueConverter == null)
throw new ConverterException(s"Cannot find a converter for type ${x.getClass.toString}.")
else
valueConverter.toDatabaseParam(x) // compiler error here, for x
}
}
}
private[this] lazy val customQuirks = new PostgresQuirks() {
{
converters.put(classOf[Option[java.lang.Integer]], new OptionConverter[java.lang.Integer])
converters.put(classOf[Option[scala.Int]], new OptionConverter[scala.Int])
// ...
}
}