[Racket] Ferti hydroponic nutrient solver, redux.
1
#lang racket
2
3
(provide current-conn
4
connect!
5
disconnect!
6
with-db
7
with-tx)
8
9
(require db
10
"../debug.rkt")
11
12
(define current-conn (make-parameter #f))
13
14
(define (connect! #:path [path 'memory])
15
(if (connection? (current-conn))
16
(debug-log (format "Connection already instantiated: ~a" (current-conn)))
17
(begin
18
(current-conn (sqlite3-connect #:database path #:mode 'create))
19
(query-exec (current-conn) "PRAGMA foreign_keys = ON")
20
(debug-log (format "Connection instantiated: ~a" (current-conn))))))
21
22
(define (disconnect!)
23
(disconnect (current-conn))
24
(current-conn #f)
25
(debug-log "Connection disconnected."))
26
27
(define-syntax-rule (with-db body ...)
28
(begin
29
(connect!)
30
body ...))
31
32
(define-syntax-rule (with-tx body ...)
33
(call-with-transaction (current-conn)
34
(λ ()
35
body ...)))
36
37
(module+ test
38
(require rackunit)
39
(check-equal? (current-conn) #f)
40
(connect!)
41
(check-true (connection? (current-conn)))
42
(disconnect!)
43
(check-equal? (current-conn) #f)
44
(with-db (check-true (connection? (current-conn)))))
45