log4p

Peter Maas’s Weblog

Archive for the 'dsl' Category

ScotchBuilder with Scala 2.8

While writing my internal SQL DSL example I stumbled upon this blogpost about a Type-safe Builder Pattern in Scala a couple of times. One of the issues I have with that code is the unnecessary use of mutable state in the example. Which are probably there to demonstrate the validation of the result at the end of the article.

The blogpost concludes with some complex implicit solution to have the builder only create valid configurations, but (IMHO) fails to address the fact that it is still possible to create OrderOfScotch variants which are invalid through its' constructor.

With Scala 2.8 it is possible to vastly simplify the example by using the the copy method on the case classes and default arguments:

  1. sealed abstract class Preparation
  2. case object Neat extends Preparation
  3. case object OnTheRocks extends Preparation
  4. case object WithExtraWater extends Preparation
  5.  
  6. sealed abstract class Glass
  7. case object Short extends Glass
  8. case object Tall extends Glass
  9. case object Tulip extends Glass
  10.  
  11. case class OrderOfScotch(val brand:String , val mode:Preparation = Neat, val double:Boolean = false, val glass:Option[Glass] = None) {
  12.   val invalid: Boolean = double && glass.isDefined && glass.get == Short
  13.   if(invalid) {
  14.       throw new IllegalStateException("Illegal combination")
  15.   }
  16.  
  17.   def prepare(p:Preparation) = copy(mode = p)
  18.   def isDouble(d:Boolean) = copy(double = d)
  19.   def inGlass(g:Glass) = copy(glass = Option(g))
  20. }
  21.  
  22. object OrderBuilder {
  23.   def scotch(brand:String) = OrderOfScotch(brand)
  24. }

Using the above you can write something like:

  1. val scotchOrder1 = scotch("Glenfoobar") prepare WithExtraWater inGlass Tulip
  2. val scotchOrder2 = scotch("Talisker") inGlass Tall prepare Neat

When using an incorrect configuration (in the example implementation a double scotch, in a short glass) creating the Order throws an exception. And since we in effect only use the constructor to create the Order this works in any case.

Consider the following tests:

  1. class ScotchBuilderSpec extends Spec with ShouldMatchers {
  2.   import OrderBuilder._
  3.   describe("given two scotches with the same configuration") {
  4.     val constructorResult = OrderOfScotch("Glenfoobar", WithExtraWater, false, Option(Tulip))
  5.     val builderResult = scotch("Glenfoobar") prepare WithExtraWater inGlass Tulip
  6.  
  7.     describe("when created by constructor and by the builder") {
  8.       it("they should return identical results") {
  9.         constructorResult should be (builderResult)
  10.         println("success!")
  11.       }
  12.     }
  13.   }
  14.  
  15.   describe("trying to create an illegal order") {
  16.     describe("like combining a double portion with a short glass") {
  17.       it("should throw an exception, for now") {
  18.         evaluating { OrderOfScotch("Glenfoobar", Neat, true, Option(Short)) } should produce[Throwable]
  19.         evaluating { scotch("Glenfoobar") isDouble(true) inGlass Short } should produce[Throwable]       
  20.       }
  21.     }
  22.   }
  23. }

As you can see the amount of boilerplate code was reduced dramatically, we've got rid of all mutable state and moved validation of the Order to a (IMHO) better location!

One 'drawback' of my solution is of course that it only works on runtime; whereas the solution presented in the catches incorrect configurations created by the builder at compiletime... which was the entire point of that blogpost. I'll try and see if I can factor that into a more compact form as well lator on!

2 comments

external DSLs with scala

Following up on my previous post on writing an internal SQL-like DSL for Scala I decided to bite the bullet and implement a 'real' parser for a subset of the SQL language to create the object tree from a SQL-like string. Since I didn't have any experience in Parser/Combinators this proved to be quite an interesting exercise.

At this point the specs for the following queries succeed and produce valid results when I render SQL from the object graph:

  1. select name from users order by name asc
  2. select name from users where name = "peter"
  3. select age from users where age = 30
  4. select name from users where name = "peter" and age = 30
  5. select name from users where age = 20 or age = 30
  6. select name from users where name = "peter" and age = 20 or age = 30
  7. select name,age from users where name = "peter" and (active = true or age = 30)

The entire parser is just under 54 lines of code!

As you can see in the source code the Parser is basically a collection of small parsers which are combined into a parser for the entire language. Let's have a look at the more basic ones:

The order clause
The order clause is probably the easiest part of the query. The production for the parser looks like this:

  1. def order:Parser[Direction] = {
  2.   "order" ~> "by" ~> ident  ~ ("asc" | "desc") ^^ {
  3.     case f ~ "asc" => Asc(f)
  4.     case f ~ "desc" => Desc(f)
  5.   }
  6. }

It matches a string which starts with order by, then an identifier and ends with asc or desc. The tilde (~) depicts a separater, and the greater then (>) after the tilde is used to drop the fields on the left side of the operator; just to have less repetition in the match algorithm to bind the value into objects which will form the AST.

Quite straightforward and easy to read.

Typed predicates
I had some more troubles implementing the typed predicates and still feel this could be written a bit more concise:

  1. def predicate = (
  2.     ident ~ "=" ~ boolean ^^ { case f ~ "=" ~ b => BooleanEquals(f,b)}
  3.   | ident ~ "=" ~ stringLiteral ^^ { case f ~ "=" ~ v => StringEquals(f,stripQuotes(v))}
  4.   | ident ~ "=" ~ wholeNumber ^^ { case f ~ "=" ~ i => NumberEquals(f,i.toInt)}
  5. )

And/Or en parentheses
Implementing and/or in the where clause proved to be more difficult then I expected; mainly due to the precedence rules when using parentheses. I asked around on stackoverflow and read how this is done in this great article by Jim McBeath.

This resulted in the following solution:

  1. def where:Parser[Where] = "where" ~> rep(clause) ^^ (Where(_:_*))
  2.  
  3. def clause:Parser[Clause] = (predicate|parens) * (
  4.    "and" ^^^ { (a:Clause, b:Clause) => And(a,b) } |
  5.     "or" ^^^ { (a:Clause, b:Clause) => Or(a,b) }
  6. )
  7.  
  8. def parens:Parser[Clause] = "(" ~> clause  <~ ")"

Which effectively is a way of repeating predicates or clauses within parentheses interleaved with and/or. The parens production is just pre- and postfixing the clause with parens.

Optional parts
The select and order clause are optional, this is specified using the opt method:

  1. def query:Parser[Query] = operation ~ from ~ opt(where) ~ opt(order) ^^ {
  2.   case operation ~ from ~ where ~ order => Query(operation, from, where, order)
  3. }

Since the Query object already accepts optional objects as where and order clause binding is straightforward.

Conclusion
Being fairly new to this game I had some trouble finding out how I was supposed to approach some of the problems I faced; but managed to overcome them quickly. There is a lot of documentation on various blogs and books. Writing a parser in Java, Groovy or Ruby would probably have taken me more time and would probably have resulted in far more code.

Now, off to look for some nails to test my new hammer on!

Full sources can be found in the Scala-SQL-DSL github repository.

3 comments

Internal DSLs with Scala

I've been playing around with Scala again lately. Writing a (internal) DSL or a fluent api was still on todo-list.

Instead of writing some arbitrary language for a made-up domain I decided to pick a language and a domain I know: SQL. Or, a rather small subset.

The first step was creating a model of the language (no, I didn't start with this diagram):

sql_dsl

As you can see a query object consists of:

  • Operation (i.e. select, update, delete)
  • From
  • Where, containing various predicates and ways to combine predicates
  • Optional ordering

In Scala my implementation of the model looks like this:

  1. case class Query(val operation:Operation, val from: From, val where: Where, val order: Option[Direction] = None) {
  2.   def order(dir: Direction) = this.copy(order = Option(dir))
  3. }
  4.  
  5. abstract class Operation {
  6.   def from(table: String) = From(this, table)
  7. }
  8. case class Select(val fields:String*) extends Operation
  9.  
  10. case class From(val operation:Operation, val table: String) {
  11.   def where(clauses: Clause*): Query = Query(operation, this, Where(clauses:_*))
  12. }
  13.  
  14. case class Where(val clauses: Clause*)
  15.  
  16. abstract class Clause {
  17.   def and(otherField: Clause): Clause = And(this, otherField)
  18.   def or(otherField: Clause): Clause = Or(this, otherField)
  19. }
  20.  
  21. case class StringEquals(val f: String, val value: String) extends Clause
  22. case class NumberEquals(val f: String, val value: Number) extends Clause
  23. case class BooleanEquals(val f: String, val value: Boolean) extends Clause
  24. case class In(val field: String, val values: String*) extends Clause
  25. case class And(val clauses: Clause*) extends Clause
  26. case class Or(val clauses: Clause*) extends Clause
  27.  
  28. abstract class Direction
  29. case class Asc(field: String) extends Direction
  30. case class Desc(field: String) extends Direction

As you can see the code is a straightforward implementation of the model, only using immutable values. I added some utility methods which will be used to 'chain' objects:

  • Query#order - Clones the query object and overrides the order with the specified order
  • Operation#from - creates a from clause from the operation object
  • Clause#and / Clause#or - combines two clauses

Next to the model I created a QueryBuilder object which contains some implicit conversions and utility methods:

  1. object QueryBuilder {
  2.   implicit def tuple2field(t: (String, String)): StringEquals = StringEquals(t._1, t._2)
  3.   implicit def tuple2field(t: (String, Int)): NumberEquals = NumberEquals(t._1, t._2)
  4.   implicit def tuple2field(t: (String, Boolean)): BooleanEquals = BooleanEquals(t._1, t._2)
  5.  
  6.   /** entrypoint for starting a select query */
  7.   def select(fields:String*) = Select(fields:_*)
  8.   def in(field: String, values: String*) = In(field, values: _*)
  9. }

The implicit conversions allow tuples to be converted to typed case classes. With the above we can write Scala code which resembles SQL. I Wrote some tests (using ScalaTest) which demonstrate how it works. Example inputs for my tests include:

  1. val q = select ("*") from ("user") where (("name","peter") and (("active", true) or ("role", "admin")))
  2. val q = select ("*") from ("user") where (("name","p'eter"))
  3. val q = select ("*") from ("user") where (("id", 100))
  4. val q = select ("*") from ("user") where (in("name","pe'ter","petrus"))
  5. val q = select ("*") from ("user") where (("name","peter")) order Desc("name")

To generate a SQL String from a Query object I wrote a fairly basic generator and an implicit conversion to convert queries to SQL:

  1. case class SQL(val sql:String)
  2.  
  3. object AnsiSqlRenderer {
  4.   implicit def query2sql(q:Query):SQL = SQL(sql(q))
  5.  
  6.   def sql(q: Query): String = {
  7.     List(
  8.       expandOperation(q),
  9.       expandFrom(q),
  10.       expandWhere(q),
  11.       expandOrder(q)
  12.     ).mkString(" ").trim
  13.   }
  14.  
  15.   def expandOperation(q:Query):String = q.operation match {
  16.     case Select(fields) => "select %s".format(fields.mkString(","))
  17.     case _ => throw new IllegalArgumentException("Operation %s not implemented".format(q.operation))
  18.   }
  19.  
  20.   def expandFrom(q: Query) = "from %s".format(q.from.table)
  21.   def expandWhere(q: Query) = "where %s".format(q.where.clauses.map(expandClause(_)).mkString(" "))
  22.  
  23.   def expandClause(clause: Clause): String = clause match {
  24.     case StringEquals(field, value) => "%s = %s".format(field, quote(value))
  25.     case BooleanEquals(field, value) => "%s = %s".format(field, value)
  26.     case NumberEquals(field, value) => "%s = %s".format(field, value)
  27.     case in:In => "%s in (%s)".format(in.field, in.values.map(quote(_)).mkString(","))
  28.     case and:And => and.clauses.map(expandClause(_)).mkString("(", " and ", ")")
  29.     case or:Or => or.clauses.map(expandClause(_)).mkString("(", " or ", ")")
  30.     case _ => throw new IllegalArgumentException("Clause %s not implemented".format(clause))
  31.   }
  32.  
  33.   def expandOrder(q: Query) = q.order match {
  34.     case Some(direction) => direction match {
  35.       case Asc(field) => "order by %s asc".format(field)
  36.       case Desc(field) => "order by %s desc".format(field)
  37.     }
  38.     case None => ""
  39.   }
  40.  
  41.   def quote(value: String) = "'%s'".format(escape(value))
  42.   def escape(value: String) = value.replaceAll("'", "''")
  43. }

Most of the beef is in (recursively) expanding the where clause into a String. When using the implicit conversion you can now do the following:

  1. scala> val q = select ("*") from ("user") where (("name","peter") and (("active", true) or ("role", "admin")))
  2. scala> q.sql
  3. res0: java.lang.String = select * from user where (name = 'peter' and (active = true or role = 'admin'))

Apart from the parentheses (probably I'll figure out how to get rid of some more of them one day) the two look very similar. But the first one creates a typesafe object graph which can be rendered/validated/manipulated in various ways!

The example could be extended to allow 'greater/smaller then' clauses (using operator overloading?) or joins in the from clause; feel free to clone my repository and do so!

Full sources can be found in the Scala-SQL-DSL github repository.

6 comments