Take the 2-minute tour ×

I'm storing a field as binary(64) in a database and I'm trying to extract it using the Anorm parser using Scala. How can that be done?

I am mostly interested in using the ParserAPI, as the StreamAPI is documented here: http://www.playframework.org/documentation/2.0.2/ScalaAnorm

share|improve this question
add comment

1 Answer

Sorry I don't have time to test it for you, but here is a possible solution.

First define an implicit transformer which is able to transform the column content to a byte array:

implicit def rowToByteArray: Column[Array[Byte]] = Column.nonNull { (value, meta) =>
  val MetaDataItem(qualified, nullable, clazz) = meta
  value match 
    case data: Array[Byte] => Right(data)
    case _ => Left(TypeDoesNotMatch("Cannot convert " + value + ":" + value.asInstanceOf[AnyRef].getClass + " to Byte Array for column " + qualified))

Then you can define an appropriate row parser factory:

def bytes(columnName: String): RowParser[Array[Byte]] = get[Array[Byte]](columnName)(implicitly[Column[Array[Byte]]])

Finally just use it:

val images:List[String~Array[Byte]] = {
  SQL("select * from Image").as( str("name") ~ bytes("data") * ) 
share|improve this answer
add comment

Your Answer


