From 3008eb25f79ef1ed54fcc2b3f5b6635b34394680 Mon Sep 17 00:00:00 2001 From: Marius Peter Date: Sun, 19 Oct 2025 21:15:18 +0200 Subject: Absorb existing domain data. --- db/conn.rkt | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 db/conn.rkt (limited to 'db/conn.rkt') diff --git a/db/conn.rkt b/db/conn.rkt new file mode 100644 index 0000000..e083d94 --- /dev/null +++ b/db/conn.rkt @@ -0,0 +1,41 @@ +#lang racket + +(require db) + +(provide current-conn + connect! + disconnect! + with-db + with-tx) + +(define current-conn (make-parameter #f)) + +(define (connect! #:path [path 'memory]) + (cond + [(connection? (current-conn)) + (printf "Database connection already exists: ~e\n" (current-conn))] + [else + (current-conn (sqlite3-connect #:database path + #:mode 'create)) + (printf "Created database connection at path: ~a\n" path)])) + +(define (disconnect!) + (disconnect (current-conn)) + (printf "Closing database connection: ~e\n" (current-conn)) + (current-conn #f)) + +(define-syntax-rule (with-db body ...) + (begin (connect!) body ...)) + +(define-syntax-rule (with-tx body ...) + (call-with-transaction (current-conn) (λ () body ...))) + +(module+ test + (require rackunit) + (check-equal? (current-conn) #f) + (connect!) + (check-true (connection? (current-conn))) + (disconnect!) + (check-equal? (current-conn) #f) + (with-db + (check-true (connection? (current-conn))))) -- cgit v1.2.3