Neaten up the blog site example

And fix some issues.
This commit is contained in:
Christopher Baines 2026-04-14 15:19:11 +03:00
parent dd15e9306a
commit 0d1a31f4c7
4 changed files with 57 additions and 49 deletions

View file

@ -28,6 +28,9 @@ connection to DATABASE-FILE."
(sqlite-busy-timeout db 5000)
(sqlite-exec db "PRAGMA journal_mode=WAL")
(sqlite-exec db "PRAGMA foreign_keys=ON")
(db-init! db)
(list db)))
#:thread-destructor
(lambda (db)
@ -44,18 +47,15 @@ SQLite connection. Returns whatever PROC returns."
;;; Schema
;;;
(define (db-init! pool)
"Create the schema if it doesn't exist."
(call-with-db pool
(lambda (db)
(sqlite-exec db "
CREATE TABLE IF NOT EXISTS posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
body TEXT NOT NULL,
image_url TEXT,
created_at TEXT NOT NULL DEFAULT (datetime('now'))
)"))))
(define (db-init! db)
(sqlite-exec db "
CREATE TABLE IF NOT EXISTS posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
body TEXT NOT NULL,
image_url TEXT,
created_at TEXT NOT NULL DEFAULT (datetime('now'))
)"))
;;;
;;; Row conversions
@ -118,6 +118,14 @@ newest first."
(sqlite-finalize stmt)
(vector-ref row 0)))))))
(define (sqlite-changes db)
"Return the number of rows changed by the most recent INSERT, UPDATE,
or DELETE statement on DB."
(let ((stmt (sqlite-prepare db "SELECT changes()")))
(let ((row (sqlite-step stmt)))
(sqlite-finalize stmt)
(vector-ref row 0))))
(define (db-update-post! pool id title body image-url)
"Update the post with ID. Returns #t if a row was changed, #f otherwise."
(call-with-db pool