[!NOTE]+
The exact same content as this page is also available in the Playground.
The sample code assumes
(require '[bisql.core :as bisql]).
- Input Form:
(render "demo-variables-bind.sql" {:id 42})- Input SQL:
SELECT * FROM users WHERE id = /*$id*/1- Output SQL and Params:
SELECT * FROM users WHERE id = ?{:params [42]}/*$ */ comments become bind variables. The sample value must be written immediately after the comment, as in /*$id*/1.
- Input Form:
(render "demo-variables-literal.sql" {:type "BOOK"})- Input SQL:
SELECT * FROM users WHERE type = /*^type*/'A'- Output SQL and Params:
SELECT * FROM users WHERE type = 'BOOK'{:params []}^ is rendered as a SQL literal. Strings are quoted with '.
- Input Form:
(render "demo-variables-raw.sql" {:order-by "created_at DESC"})- Input SQL:
SELECT * FROM users ORDER BY /*!order-by*/column_name- Output SQL and Params:
SELECT * FROM users ORDER BY created_at DESC{:params []}! is inserted into SQL as-is. It is useful for cases like ORDER BY, but it must not be fed directly from user input.
- Input Form:
(render "demo-variables-default.sql" {:email "alice@example.com", :status bisql/DEFAULT})- Input SQL:
INSERT INTO users (email, status)
VALUES (/*$email*/'user@example.com', /*$status*/'active')- Output SQL and Params:
INSERT INTO users (email, status)
VALUES (?, DEFAULT){:params ["alice@example.com"]}bisql/DEFAULT is rendered as the SQL keyword DEFAULT instead of a bind parameter. This is useful in VALUES clauses.
- Input Form:
(render "demo-variables-all.sql" {:limit bisql/ALL})- Input SQL:
SELECT *
FROM users
LIMIT /*$limit*/10- Output SQL and Params:
SELECT *
FROM users
LIMIT ALL{:params []}bisql/ALL is rendered as the SQL keyword ALL instead of a bind parameter. This is useful in clauses such as LIMIT ALL.
- Input Form:
(render "demo-variables-like.sql" {:search (bisql/LIKE_CONTAINS "100%")})- Input SQL:
SELECT *
FROM products
WHERE name LIKE /*$search*/'100%' ESCAPE '\'- Output SQL and Params:
SELECT *
FROM products
WHERE name LIKE ? ESCAPE '\'{:params ["%100\\%%"]}bisql/LIKE_CONTAINS adds surrounding % characters and escapes %, _, and \. Write ESCAPE '\' in SQL when you want the behavior to be explicit.
- Input Form:
(render "demo-variables-default-to.sql" {:email "alice@example.com"})- Input SQL:
INSERT INTO users (
id,
email
)
VALUES (
/*$id default-to */DEFAULT,
/*$email*/'user@example.com'
)- Output SQL and Params:
INSERT INTO users (
id,
email
)
VALUES (
DEFAULT,
?
){:params ["alice@example.com"]}default-to lets a missing $ parameter fall back to the sample SQL fragment. Passing nil is still explicit and does not use the fallback.
In this example, the id parameter is not provided, so the sample value DEFAULT is used. Generated crud.sql uses this pattern for insert, bulk insert, and upsert templates.
- Input Form:
(render "demo-if.sql" {:id 42})- Input SQL:
SELECT * FROM users /*%if id */ WHERE id = /*$id*/1 /*%end*/- Output SQL and Params:
SELECT * FROM users WHERE id = ?{:params [42]}- Input Form:
(render "demo-if-where.sql" {:active false})- Input SQL:
SELECT *
FROM users
WHERE
/*%if active */
active = true
/*%end */- Output SQL and Params:
SELECT *
FROM users{:params []}When the condition is false, the preceding WHERE or HAVING keyword is removed automatically.
- Input Form:
(render "demo-if-and.sql" {:active false, :status "active"})- Input SQL:
SELECT *
FROM users
WHERE
/*%if active */
active = true
/*%end */
AND status = /*$status*/'active'- Output SQL and Params:
SELECT *
FROM users
WHERE
status = ?{:params ["active"]}When the condition is false, a following AND is removed automatically. In this case, the preceding WHERE or HAVING remains in place.
- Input Form:
(render "demo-if-elseif-else.sql" {:active false, :pending true})- Input SQL:
SELECT *
FROM users
WHERE
/*%if active */
active = true
/*%elseif pending */
status = 'pending'
/*%else */
status = 'inactive'
/*%end */- Output SQL and Params:
SELECT *
FROM users
WHERE
status = 'pending'{:params []}- Input Form:
(render "demo-if-inline-elseif-else.sql" {:active false, :pending true})- Input SQL:
SELECT *
FROM users
WHERE
/*%if active */
active = true
/*%elseif pending => status = 'pending' */
/*%else => status = 'inactive' */
/*%end */- Output SQL and Params:
SELECT *
FROM users
WHERE
status = 'pending'{:params []}else and elseif bodies can also be written inline inside the directive comment by using =>. This keeps the template closer to executable SQL.
- Input Form:
(render "demo-if-expression.sql" {:active false, :status "active", :expected_status "pending", :pending true})- Input SQL:
SELECT *
FROM users
WHERE
/*%if (active and status = expected_status) or pending */
status = 'pending'
/*%else */
status = 'inactive'
/*%end */- Output SQL and Params:
SELECT *
FROM users
WHERE
status = 'pending'{:params []}if and elseif conditions can use a small expression language with and, or, parentheses, and comparison operators. Operands are parameter references.
- Input Form:
(render "demo-for.sql" {:id 42, :items [{:name "display_name", :value "Alice"} {:name "status", :value "active"}]})- Input SQL:
UPDATE users
SET
/*%for item in items separating , */
/*!item.name*/display_name = /*$item.value*/'sample'
/*%end */
WHERE id = /*$id*/1- Output SQL and Params:
UPDATE users
SET
display_name = ?,
status = ?
WHERE id = ?{:params ["Alice" "active" 42]}for loops can declare a separator with separating. This is useful when joining an arbitrary number of fragments with ,, AND, or OR.
- Input Form:
(render "demo-declarations.sql" {:id 42})- Input SQL:
/*:doc Loads a user by id. */
/*:tags
[:example
:user]
*/
/*:category :lookup */
/* This is a normal comment */
SELECT * FROM users WHERE id = /*$id*/1- Output SQL and Params:
/* This is a normal comment */
SELECT * FROM users WHERE id = ?{:params [42],
:meta
{:doc "Loads a user by id.",
:tags [:example :user],
:category :lookup}}/*: */ defines metadata entries for the generated Clojure function, keyed by keywords. /*:doc */ is a special case: unlike other declarations, it can be written as inline text instead of EDN.
- Input Form:
(render (get (bisql/load-queries "demo-multi-queries.sql") "core.find-user-by-id") {:id 42})- Input SQL:
/*:name find-user-by-id */
SELECT * FROM users WHERE id = /*$id*/1- Output SQL and Params:
SELECT * FROM users WHERE id = ?{:params [42], :meta {:name find-user-by-id}}A single SQL file can contain multiple templates. Each template starts with a /*:name */ directive that defines the generated function name.
- Input Form:
(render "demo-join.sql" {:status "active", :state "pending", :limit 20})- Input SQL:
SELECT u.id,
u.email,
o.id AS order_id,
o.state,
o.created_at
FROM users u
JOIN orders o
ON o.user_id = u.id
WHERE u.status = /*$status*/'active'
AND o.state = /*$state*/'pending'
ORDER BY o.created_at DESC
LIMIT /*$limit*/100- Output SQL and Params:
SELECT u.id,
u.email,
o.id AS order_id,
o.state,
o.created_at
FROM users u
JOIN orders o
ON o.user_id = u.id
WHERE u.status = ?
AND o.state = ?
ORDER BY o.created_at DESC
LIMIT ?{:params ["active" "pending" 20]}Templates can use ordinary JOIN clauses. Bisql only replaces the parameterized parts and leaves the SQL structure intact.
- Input Form:
(render {:sql-template "SELECT * FROM users WHERE id = /*$id*/1"} {:id 42})- Input SQL:
SELECT * FROM users WHERE id = /*$id*/1- Output SQL and Params:
SELECT * FROM users WHERE id = ?{:params [42]}You can also pass a SQL template directly from Clojure code. This is useful for debugging or learning, but application queries should generally live in SQL files.
- Input Form:
(render "demo-variables-bind.sql" {:ic 42})- Input SQL:
SELECT * FROM users WHERE id = /*$id*/1- Output Error:
{:message "Missing query parameter: id",
:data
{:query-name "core.demo-variables-bind",
:base-path "sql",
:resource-path "sql/demo-variables-bind.sql",
:project-relative-path "scripts/sql/demo-variables-bind.sql",
:source-line 1,
:parameter :id,
:sigil "$",
:collection? false}}