How do I apply a PostgreSQL function to a query using Clojure?

87 Views Asked by At
(ns training-clj.querys
  (:require [honey.sql :as honey]
            [next.jdbc.sql :as jdbc]))

(defn get-by-username
  [db username]
  (let [query (honey/format {:select [:*]
                             :from   [:user]
                             :where  [:= :username username]]})]
    (jdbc/query db query)))

I'm doing a query in the db (postgresql) per user and I have a return similar to this: ({:id 2 :username "random" :age 18 :telephone 3120-4587}) and given this code base that I defined, I want to apply the PostgreSQL LOWER() function to the :username key, what are the ways I can do this?

1

There are 1 best solutions below

2
On

You're using HoneySQL, so calling a PostgreSQL function should be done via HoneySQL's DSL. The relevant part is described here.

As for your specific question, it's unclear what the lower function must be applied to since :* returns all columns.

But let's assume that you want to return :first-name, just lower-case. Then it would be something like this:

{:select [[[:lower :first-name]]]
 ...}

or

{:select :%lower.first-name
 ...}