Skip to content

Instantly share code, notes, and snippets.

@groundwater
Created April 11, 2012 05:34
Show Gist options
  • Save groundwater/2357231 to your computer and use it in GitHub Desktop.
Save groundwater/2357231 to your computer and use it in GitHub Desktop.
Scala Anorm and Google Protocol Buffers
package controllers
import anorm._
import anorm.SqlParser._
import play.api.db.DB
import play.api.Play.current
import play.api._
import play.api.mvc._
import com.googlecode.protobuf.format._
import play.api.templates.Html
object Application extends Controller {
def index = Action {
import models.Model.Person
val bob = Person.newBuilder().setId(1).setName("BOB").build()
val bbob: Array[Byte] = bob.toByteArray()
val p = DB.withConnection { implicit conn ⇒
SQL("""
INSERT INTO Person (buffer) values ({buffer})
""").on(
'buffer -> bbob).executeInsert()
SQL("SELECT * FROM PERSON")() map {
case Row(id: Long, buffer: Array[Byte]) ⇒
Person.parseFrom(buffer)
}
}
val fmt = HtmlFormat.printToString(p.first)
Ok(Html(fmt))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment