The Jodd SQL Generator

On the jOOQ blog, we never grow tired of comparing ourselves with other free or commercial SQL builders. One of the most interesting ones that we’ve seen in the past was the MyBatis SQL Statement Builder. The funny thing about some of these approaches is the fact that “typesafety” is understood in terms of merely offering a fluent API. Jodd is a platform claiming to offer the The Unbearable Lightness of Java. That’s actually a very nice claim that we can only support. So let’s have a look at the unbearable lightness of SQL query building with Jodd’s SQL Generator:

Boy bb = new Boy();
Girl bg = new Girl();

DbSqlBuilder dsb = sql()
     ._("select")      // "select"
     .columnsAll("bb") // "bb.ID, bb.GIRL_ID, bb.NAME"
     .columnsIds("bg") // "bb.NAME, bg.ID"
     ._(" from")       // " from" (hardcoded string)
     .table(bb, "bb")  // "BOY bb"
     .table(bg, "bg")  // ", GIRL bg"
     ._()              // " " (single space)
     .match("bb", bb)  // "(1=1)"  since all bb fields are null
     ._(" and ")       // " and "
     .match("bg", bg); // "(1=1)" since all bg fields are null.

I must say, I’m intrigued! What is your take on Jodd’s SQL building?

5 thoughts on “The Jodd SQL Generator

  1. Much better example would be usage of SQL templates (, not a builder. While builder serves it purpose, it is much, much more developer friendly to use SQL templates. SQL Templates are just convenient string where you can eg refer entities and not real tables etc. It is worth to mention that this is part of DbOom – a mapping tool, so it is not just a simple sql generator. And it happened to be quite enough in real world (when you target one, specific db): you have mapping, and build relations in runtime, and have that all mapped. Similar to iBatis.

    1. Hi there, and thanks for the authoritative feedback! Right, I missed the templating functionality in Jodd. I’ll fix that with a follow-up blog post by the end of the year. Is there anything else you’d like me to mention?

      1. Depends how long you want to write about it :) Maybe its worth it to mention that the idea behind the template sql (TSql) is to ‘extend’ the sql and introduce entities and shorten some common code. So you still write sql for target database, just easier :)

        Thanx for working on this, let me know if you need more :)

        1. Yes, such SQL templating engines are quite popular. I’ve recently blogged about another one called ElSql… I’m really hoping to be able to implement templating in jOOQ, soon, though.

Leave a Reply