57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
(#:primary-key-cols (listof stringish?))
. ->* . string?)]
[make-insert/replace-query (stringish? (listof stringish?) . -> . string?)]
[make-insert-rows-query (stringish? (listof stringish?) (listof (listof stringish?)) . -> . string?)]
[make-select-query (stringish? (listof stringish?) #:where stringish? . -> . string?)]
;; Database operations
[init-db! ((pathish?) #:rest (listof string?) . ->* . void?)]
[query! ((string?) #:rest (listof any/c) . ->* . void?)]
[select-rows! (case->
(stringish? (listof stringish?) any/c . -> . (or/c empty? hash?))
(string? (listof stringish?) . -> . (or/c empty? hash?)))]))
;; ~~~ Private use ~~~
(define uninitialized-connection "No DB connection!")
|
|
|
|
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
(#:primary-key-cols (listof stringish?))
. ->* . string?)]
[make-insert/replace-query (stringish? (listof stringish?) . -> . string?)]
[make-insert-rows-query (stringish? (listof stringish?) (listof (listof stringish?)) . -> . string?)]
[make-select-query (stringish? (listof stringish?) #:where stringish? . -> . string?)]
;; Database operations
[init-db! ((pathish?) () #:rest (listof string?) . ->* . void?)]
[query! ((string?) () #:rest (listof any/c) . ->* . void?)]
[select-rows! (case->
(stringish? (listof stringish?) any/c . -> . (or/c empty? hash?))
(string? (listof stringish?) . -> . (or/c empty? hash?)))]))
;; ~~~ Private use ~~~
(define uninitialized-connection "No DB connection!")
|
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
(for ([q (in-list qs)])
(query! q))))
;; Run a query with logging (if enabled) and return the result
(define (query! q . parameters)
(unless (good-connection?) (error "(query!) DB not connected"))
(log-query q)
(apply query-exec (sqltools:dbc) q parameters))
;; Run a SELECT query, return a hash with field names as keys
(define select-rows!
(case-lambda
;; Use arbitrary query
[(query fieldnames)
(unless (good-connection?) (error "(select-rows!) DB not connected"))
|
>
|
|
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
(for ([q (in-list qs)])
(query! q))))
;; Run a query with logging (if enabled) and return the result
(define (query! q . parameters)
(unless (good-connection?) (error "(query!) DB not connected"))
(log-query q)
(cond [(empty? parameters) (query-exec (sqltools:dbc) q)]
[else (apply query-exec (sqltools:dbc) q parameters)]))
;; Run a SELECT query, return a hash with field names as keys
(define select-rows!
(case-lambda
;; Use arbitrary query
[(query fieldnames)
(unless (good-connection?) (error "(select-rows!) DB not connected"))
|
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
;; TESTING: database connection state and queries
(module+ test
(define TESTDB "SQLITE-TOOLS-TEST.sqlite")
;; Check that things start out uninitialized and that queries don’t work
(check-equal? (sqltools:dbc) uninitialized-connection)
(check-false (file-exists? TESTDB))
(check-exn exn:fail? (lambda () (query! "-- nothing")))
(check-exn exn:fail? (lambda () (select-rows! 'posts '(title) 1)))
;; Initialize db connection, create file with no schema
(test-begin
(check-equal? (init-db! TESTDB) (void))
(check-true (file-exists? TESTDB))
(delete-file TESTDB))
|
|
|
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
;; TESTING: database connection state and queries
(module+ test
(define TESTDB "SQLITE-TOOLS-TEST.sqlite")
;; Check that things start out uninitialized and that queries don’t work
(check-equal? (sqltools:dbc) uninitialized-connection)
(check-false (file-exists? TESTDB))
(check-exn exn:fail? (lambda () (query! "SELECT 1")))
(check-exn exn:fail? (lambda () (select-rows! 'posts '(title) 1)))
;; Initialize db connection, create file with no schema
(test-begin
(check-equal? (init-db! TESTDB) (void))
(check-true (file-exists? TESTDB))
(delete-file TESTDB))
|