guile-sql

Status: Experimental. The API is still in flux and may change without notice.

guile-sql builds SQL with S-expressions instead of string concatenation. Queries are plain Scheme alists — no special types, no macros, no DSL to learn beyond the SQL you already know. Values are automatically parameterized, so there is no risk of SQL injection. And because queries are just lists, you can compose, transform, and inspect them with standard Scheme operations like assq, map, filter, and quasiquote.

guile-sql is a pure SQL generator — it produces parameterized SQL strings from Scheme data and does not connect to any database itself. See the manual for integration examples with guile-squee (PostgreSQL) and guile-sqlite3 (SQLite).

Inspired by Clojure's HoneySQL and (to a lesser extent) Common Lisp's S-SQL.

Documentation

Full manual: https://docs.guile-sql.org.

Usage

A basic query:

(sql->string '((#:select id name email)
               (#:from users)
               (#:where (#:= active #t))
               (#:order-by (#:asc name))
               (#:limit 20)))
;; => ("SELECT id, name, email FROM users WHERE active = $1
;;      ORDER BY name ASC LIMIT $2" #t 20)

Layer on filters, pagination, or joins by merging alists:

(define base '((#:select id name) (#:from users)))

(define (active-only q) (sql-merge q '((#:where (#:= active #t)))))
(define (paginate q page size)
  (sql-merge q `((#:limit ,size) (#:offset ,(* page size)))))

(sql->string (paginate (active-only base) 0 20))
;; => ("SELECT id, name FROM users WHERE active = $1
;;      LIMIT $2 OFFSET $3" #t 20 0)

Requirements

Installation

From a release tarball:

./configure && make && make install

From Git:

git clone https://git.sr.ht/~campbellr/guile-sql
cd guile-sql
./bootstrap && ./configure && make && make install

License

LGPL-3.0-or-later

Git Repository

guile-sql is developed using the Git version control system. The official repository is hosted at https://git.sr.ht/~campbellr/guile-sql.

To clone the repository, run:

git clone https://git.sr.ht/~campbellr/guile-sql

For full details, see the README at https://git.sr.ht/~campbellr/guile-sql.

Contributing

Send patches with git-send-email to ~campbellr/guile-sql@lists.sr.ht.

Bug tracker: todo.sr.ht/~campbellr/guile-sql.