#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)))))