简体   繁体   中英

How to expand HugSQL parameter into multiple like statements

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?

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 " AND " (map #(str "name LIKE '%" % "%'") 
          (:key-words params))))

To form a query you can also use clojure.pprint/cl-format from clojure's standard lib, which is quite powerful and concise:

user> (require '[clojure.pprint :refer [cl-format]])

user> (cl-format nil "WHERE~{ name LIKE '%~a%' ~^AND~}" 
                 ["me" "you" "somebody"])
;;=> "WHERE name LIKE '%me%' AND name LIKE '%you%' AND name LIKE '%somebody%' "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM