All Downloads are FREE. Search and download functionalities are using the official Maven repository.

slick.jdbc.SpecializedJdbcResultConverters.fm Maven / Gradle / Ivy

The newest version!
package slick.jdbc

import scala.language.existentials
import java.sql.{PreparedStatement, ResultSet}
import slick.relational._
import slick.SlickException
import slick.ast.ScalaBaseType
<#assign specTypes = ["Byte", "Short", "Int", "Long", "Char", "Float", "Double", "Boolean"]>

/** Factory methods for JdbcResultConverters which are manually specialized on
  * the underlying JdbcType. A generic implementation of this factory still
  * provides allocation free call paths but performs almost 100% slower in the
  * fast path benchmark. */
object SpecializedJdbcResultConverter {
  /** Create a new type-specialized `BaseResultConverter` for the given type-specialized `JdbcType` */
  def base[T](ti: JdbcType[T], name: String, idx: Int) = (ti.scalaType match {
    <#list specTypes as tpe>
    case ScalaBaseType.${tpe?lower_case}Type => new BaseResultConverter[${tpe}](ti.asInstanceOf[JdbcType[${tpe}]], name, idx)
    
    case _ => new BaseResultConverter[T](ti.asInstanceOf[JdbcType[T]], name, idx) {
      override def read(pr: ResultSet) = {
        val v = ti.getValue(pr, idx)
        if(v.asInstanceOf[AnyRef] eq null) throw new SlickException("Read NULL value ("+v+") for ResultSet column "+name)
        v
      }
    }
  }).asInstanceOf[ResultConverter[JdbcResultConverterDomain, T]]

  /** Create a new type-specialized `OptionResultConverter` for the given type-specialized `JdbcType` */
  def option[T](ti: JdbcType[T], idx: Int) = (ti.scalaType match {
    <#list specTypes as tpe>
    case ScalaBaseType.${tpe?lower_case}Type => new OptionResultConverter[${tpe}](ti.asInstanceOf[JdbcType[${tpe}]], idx)
    
    case _ => new OptionResultConverter[T](ti.asInstanceOf[JdbcType[T]], idx) {
      override def read(pr: ResultSet) = {
        val v = ti.getValue(pr, idx)
        if(v.asInstanceOf[AnyRef] eq null) None else Some(v)
      }
    }
  }).asInstanceOf[ResultConverter[JdbcResultConverterDomain, Option[T]]]
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy