diff options
| author | Marius Peter <dev@marius-peter.com> | 2025-10-19 21:15:18 +0200 |
|---|---|---|
| committer | Marius Peter <dev@marius-peter.com> | 2025-10-19 21:15:18 +0200 |
| commit | 3008eb25f79ef1ed54fcc2b3f5b6635b34394680 (patch) | |
| tree | 2b5d2274eff2302e1acd4600869c09ec615262f2 /db/conn.rkt | |
Absorb existing domain data.
Diffstat (limited to 'db/conn.rkt')
| -rw-r--r-- | db/conn.rkt | 41 |
1 files changed, 41 insertions, 0 deletions
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))))) |