Created
April 11, 2012 05:34
-
-
Save groundwater/2357231 to your computer and use it in GitHub Desktop.
Scala Anorm and Google Protocol Buffers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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