15.8 Plain SQL queries

Using GNU Artanis, you can also drop to a lower level of database interaction and write raw SQL queries. This can be useful for complex operations involving multiple tables, JOIN statements, GROUP statements and the likes.

We will assume your route context (in the controller) is named rc, you can open a DB connection with (DB-open rc). If you write single statement queries, you don’t need to terminate them with a semicolon ; but if you write multiple expressions in one go then you will need one.

See some example queries:

;; this will fetch a list of rows (a row is an alist of string column name to value)
(DB-get-all-rows (DB-query (DB-open rc) "SELECT * FROM products ORDER BY created_at DESC LIMIT 42"))

;; this will update some row
(DB-query (DB-open rc) "UPDATE products SET name = 'High-end Lisp Machine', updated_at = 1749805637 WHERE id = '50f11747-8714-4159-92e5-ff876f96a65a'")