Close

For the most part Korma provides a nice composable set of functions for building SQL queries. It gets tricky with where clauses, however, as unfortunately it’s implemented as a macro. One way to work around this yourself though is:

(:require [korma.sql.engine :as eng])
 
(defn where-clause [query form]
  (eng/bind-query
    query
    (eng/pred-map
      (eval
        (eng/parse-where form)))))

Then add it in when needed:

(:require [korma.core :refer [where*]])
 
(let [form '(or {:name "foo"} {:age [> 18]})
      clause (where-clause query form)]
  (where* query clause))

If you don’t mind eval you can make it quite a bit shorter:

(defn where [query form]
  (eval `(korma.core/where ~query ~form)))

You need to build the where forms yourself, but you now have all the tools required.

Want to find out more about the tools our dev community have been exploring? Take a look at our regular tech round-up series - and sign up to our mailing list to have them delivered direct to your inbox.

About the author

Box UK

Box UK

Box UK's team of simply brilliant thinkers, consultants and application developers mastermind simply brilliant solutions to the world's toughest, performance-critical web and software assignments.

Related content

Tech round-up: May 20th

By Rob Waring

Tech round-up: Aug 5th

By Steve Anderson

We're hiring. Let's talk. View available roles