Question

I've read a blog post called Blogging with Noir, and I was honestly surprised that the author uses java.jdbc instead of libraries like Korma which I found surprising. What are the advantages of writing SQL queries in your code instead of letting tools do it for you?

Was it helpful?

Solution

I guess it is for the usual reasons that you might choose to use an API directly in Clojure rather than use a wrapper:

  • Existing knowledge: you already know the JDBC well and know that it will get the job done, why spend time learning a new abstraction unless there is a clear advantage?
  • Uncertainty - does the library have all the features you need? Will it continue to be maintained and implement new features in the future?
  • Stability - the wrapper may not yet be mature, so you run the risk of your code having to change if breaking changes occur / bugs are discovered.
  • Completeness - the wrapper may not (yet) encapsulate all of the functionality of the original API that you need
  • Overhead - sometimes extra layers of abstraction add a performance overhead that you don't need/want
  • Extra dependency - adds complexity to your build, and conceptual overhead in terms of the number of abstractions you need to keep in your head.

Ultimately it's a trade-off - the above are reasons that you might want to use the underlying API, but there are equally good reasons that you may choose to use the wrapper:

  • More idiomatic - a wrapper library is likely to give you much cleaner, more elegant code than a Java-based API (particularly if the Java API is imperative/stateful). You have to admit that Korma is pretty elegant!
  • More composable - Clojure wrappers tend to adopt a functional style, which leads to easy composability with other clojure code / libraries.
  • New features - often Clojure wrappers add extra functionality that the original API does not posess (for example, look at the data binding functionality added on top of Swing by Seesaw)

OTHER TIPS

Korma IMO isn't nearly ready to be used as a full replacement for SQL. It's definitely handy, but right now a lot of my queries have (raw "...") snippets in them, and for more complicated stuff all the main querying is done inside SQL views which are then selected on via korma.

The main alternative, ClojureQL, doesn't even work with Clojure 1.3+

In short, it's hard to abstract SQL, and Korma - even though it tries to be minimal, meaning you still have to understand SQL pretty well to use it - isn't finished.

I can think about two reasons:

  1. Almost everybody knows SQL, almost nobody knows Korma
  2. This is a guess, because I do not know Korma myself, but raw SQL is sometimes suitable or even necessary if you want to do something specific like features that are only present in a particular database
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top