Hjertnes.blog

Clojure and JDBC

2019-02-24

I love SQL, I know it well, and how to use it to make fast efficient operations that reflect what I want to do. It is not beautiful, but it works really well.

After starting with raw SQL, moving over to ORM’s of various types, mainly the Django one and Entity Framework, and a few abstractions that are kind of in the middle. Like for example Korma SQL, I’m back at regular SQL.

When I work in Clojure I use the JDBC bindings, and I use a very small section of it, because I don’t like most of the abstractions JDBC have added on top of SQL. I use the query and the execute! functions. The way you use JDBC(if you want to use it efficient) is that you use the with-db-connection macro to make sure that you don’t set up a new connection to the database for each operation.

Then you have the two methods, execute! is what you use when your query does not return any data, and query is what you use when it does. There are settings to control how the data are returned, if you want need that, everything from lists of lists to lists of maps, to functions etc (if you want it to be lazy)

JDBC is the option I prefer, it may not be the easiest option, but you don’t need to do all kinds of weird crap that you often need to when your abstraction of choice was not mad with your SQL command of choice in mind.