[Special Characters][A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [W] [X] [Y] [Z]


(query (:limit (:order-by (:select 'id 'name :from 'countries) (:random))
)) ((54 "Israel") (62 "South Africa") (195 "Aruba") (79 "Costa Rica") (31 "OECD"))

Raw SQL Statements

We've already seen that the postmodern:query function can take a raw sql string and that the postmodern:sql function can output an sql string. You can combine them if you need to go beyond what postmodern can already provide. Consider the following toy example which uses a part of the eventual query in the let and inserts it into the body.

(let ((param-constraints (sql (:= 'name "Austria")))) (query (:select 'id 'name :from 'countries :where (:raw param-constraints))))

Now consider this function where you have a conditional query based on the variables test1 and test2 passed into the function. In this toy example, if test1 is true then look for countries whose name is a fuzzy match for param1, if test2 is true, look for countries whose name is a fuzzy match for param2. If both are true, then the names need to be fuzzy matches for both param1 and param2. If neither test1 or test2 are true, then return all rows in the countries table.

(defun test3 (test1 test2 param1 param2) (query (:select '* :from 'countries :where (:and (:raw (if test1 (sql (:like 'name (concatenate 'string "%" param1 "%"))) "'t'")) (:raw (if test2 (sql (:like 'name (concatenate 'string "%" param2 "%"))) "'t'")))))) > (test3 nil t "New" "gary") ((10 "Hungary" 11 47 20 "GU" 1 "2005-09-11 00:15:40-07" "Forint" "HUF" 348)) 1