diff options
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)))))  |