layerware / hugsql

A Clojure library for embracing SQL
https://hugsql.org
Apache License 2.0
690 stars 54 forks source link

How To: Create multiple like statements based on passed vector #111

Closed madhat2r closed 4 years ago

madhat2r commented 4 years ago

Does anyone know how this can be accomplished?

(get-lists ["free" "food"]) ->

Select name 
From Lists
Where name like '%free%' and name like '%food%'

I have tried:

-- :name get-lists :? :*
Select id, name
from Lists
where
--~ (clojure.string/join "" (interpose " AND " (map #(str "name LIKE '%" % "%'") :sKeyWords)))

But of course that does not work. Can someone point me in the right direction please?

madhat2r commented 4 years ago

Figured out answer. Putting here for anyone else who might need it.

-- :name get-lists :? :*
Select id, name
from Lists
--~ (str "WHERE " 
        (clojure.string/join "" 
        (interpose " AND " (map #(str "name LIKE '%" % "%'") (:key-words params)))))