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.

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: Mar 11th

By Charlie Afford

Tech round-up: Sep 18th

By Steffan Harries

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