In my initial approach, inspired by this question on the Play! Framework Google Group, was a bit parentheses-heavy.
// Parser for "foo" table
val foo = get[Long]("foo.id")
// Parser for "bar" table
val bar = get[Long]("bar.id")
// Combined parser
val combo = foo ~ (bar ?)
// Results as List[~[Long, Option[Long]]]
val results = SQL("SELECT * FROM foo LEFT JOIN bar ON foo.id = bar.foo_id").as(combo *)
Since the parentheses bothered me a bit, I enriched the RowParser class to provide a convenience method ~? which combines the two functions.
implicit def enrichRowParser[A](a: RowParser[A]) = new RichRowParser(a)
class RichRowParser[A](val delegate: RowParser[A]) extends AnyVal {
def ~?[B](p: RowParser[B]) = {
delegate ~ (p ?)
}
}
The declaration for combo can then be redefined without the parentheses which seems a bit more readable.
val combo = foo ~? bar
No comments:
Post a Comment