-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.rkt
85 lines (78 loc) · 2.56 KB
/
database.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#lang racket
(require
database-url
db/base
db/postgresql
json
net/base64
net/url
racket/match
racket/set
racket/string
"util.rkt")
(define database-connection
(virtual-connection (connection-pool (database-url-connector #f))))
(define (database-initialize)
(query-exec database-connection
(string-append "create table if not exists srfi ("
" srfi_number integer not null,"
" srfi_suffix text not null,"
" contents text not null,"
" primary key (srfi_number, srfi_suffix)"
")")))
(define (database-get-srfi-table)
(let ((srfi-table (make-hash)))
(for-each
(lambda (row)
(match-let (((vector srfi-number srfi-suffix contents) row))
(hash-set! srfi-table srfi-number
(hash-ref srfi-table srfi-number (make-hash)))
(hash-set! (hash-ref srfi-table srfi-number)
srfi-suffix
(base64-decode (string->bytes/utf-8 contents)))))
(query-rows database-connection
"select srfi_number, srfi_suffix, contents from srfi"))
srfi-table))
(define (database-get-srfi-file srfi-number srfi-suffix)
(let ((contents
(query-maybe-value
database-connection
(string-append "select contents from srfi"
" where srfi_number = $1 and srfi_suffix = $2")
srfi-number
srfi-suffix)))
(if contents
(base64-decode (string->bytes/utf-8 contents))
#f)))
(define (database-set-srfi-files! srfi-files)
(call-with-transaction
database-connection
(lambda ()
(hash-for-each
srfi-files
(lambda (srfi-number srfi-files-1)
(hash-for-each
srfi-files-1
(lambda (srfi-suffix contents)
(query-exec
database-connection
(string-append
"insert"
" into srfi (srfi_number, srfi_suffix, contents)"
" values ($1, $2, '')"
" on conflict (srfi_number, srfi_suffix) do nothing")
srfi-number
srfi-suffix)
(query-exec
database-connection
(string-append
"update srfi set contents = $3"
" where srfi_number = $1 and srfi_suffix = $2")
srfi-number
srfi-suffix
(bytes->string/utf-8 (base64-encode contents ""))))))))))
(provide
database-initialize
database-get-srfi-table
database-get-srfi-file
database-set-srfi-files!)