#lang racket (provide current-conn connect! disconnect! with-db with-tx) (require db "../debug.rkt") (define current-conn (make-parameter #f)) (define (connect! #:path [path 'memory]) (if (connection? (current-conn)) (debug-log (format "Connection already instantiated: ~a" (current-conn))) (begin (current-conn (sqlite3-connect #:database path #:mode 'create)) (debug-log (format "Connection instantiated: ~a" (current-conn)))))) (define (disconnect!) (disconnect (current-conn)) (current-conn #f) (debug-log "Connection disconnected.")) (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)))))