database: Turn 'sqlite-exec' into a macro.

* src/cuirass/database.scm (%sqlite-exec): New procedure.
(sqlite-exec): New macro.
This commit is contained in:
Ludovic Courtès 2018-01-25 10:38:19 +01:00
parent 49a341866a
commit c4963deca1
1 changed files with 15 additions and 5 deletions

View File

@ -53,11 +53,8 @@
;; Macros.
with-database))
(define (sqlite-exec db msg . args)
"Wrap 'sqlite-prepare', 'sqlite-step', and 'sqlite-finalize'. Send message
MSG to database DB. MSG and ARGS are passed to 'format'."
(let* ((sql (apply format #f msg args))
(stmt (sqlite-prepare db sql))
(define (%sqlite-exec db sql)
(let* ((stmt (sqlite-prepare db sql))
(res (let loop ((res '()))
(let ((row (sqlite-step stmt)))
(if (not row)
@ -66,6 +63,19 @@ MSG to database DB. MSG and ARGS are passed to 'format'."
(sqlite-finalize stmt)
res))
(define-syntax sqlite-exec
;; Note: Making it a macro so -Wformat can do its job.
(lambda (s)
"Wrap 'sqlite-prepare', 'sqlite-step', and 'sqlite-finalize'. Send to given
SQL statement to DB. FMT and ARGS are passed to 'format'."
(syntax-case s ()
((_ db fmt args ...)
#'(%sqlite-exec db (format #f fmt args ...)))
(id
(identifier? #'id)
#'(lambda (db fmt . args)
(%sqlite-exec db (apply format #f fmt args)))))))
(define %package-database
;; Define to the database file name of this package.
(make-parameter (string-append %localstatedir "/run/" %package