Korma by Brian Carper, Christophe Grand, Chas Emerick

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

Korma

Korma (http://sqlkorma.com) is an up-and-coming domain-specific language for working with relational databases in Clojure. It aims to provide a “batteries included” and Clojure-native database interaction experience; to that end, it takes care of generating SQL for many different popular databases, and handles administrative tasks like managing connection pooling via c3p0. For those familiar with Ruby’s ActiveRecord or a similar object-relational mapper, Korma should seem familiar, although it is decidedly not an Object-Relational Mapping framework.

To use Korma, we need to first add a dependency for it to our project:

[korma "0.3.0"]

Prelude

Let’s set up some tables and insert some data to work with, using clojure.java.jdbc.

(require '[clojure.java.jdbc :as jdbc]) (def db-spec {:classname "org.sqlite.JDBC" :subprotocol "sqlite" :subname "test.db"}) (defn setup [] (jdbc/with-connection db-spec (jdbc/create-table :country [:id "integer primary key"] [:country "varchar"]) (jdbc/create-table :author [:id "integer primary key"] [:country_id "integer constraint fk_country_id references country (id)"] [:first_name "varchar"] [:last_name "varchar"]) (jdbc/insert-records :country {:id 1 :country "USA"} {:id 2 :country "Canada"} {:id 3 :country "France"}) (jdbc/insert-records :author {:first_name "Chas" :last_name "Emerick" :country_id 1} {:first_name "Christophe" :last_name "Grand" :country_id 3} {:first_name "Brian" :last_name "Carper" :country_id 2} {:first_name "Mark" :last_name "Twain" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required