-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.lsp
97 lines (78 loc) · 2.7 KB
/
db.lsp
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
86
87
88
89
90
91
92
93
94
95
96
97
(module "mysql.lsp")
;;; defs
;; attempt to load a db configuration file
(define (maybe-load-settings filename)
(if (file? filename)
(eval-string (read-file filename))))
;; quick error and exit
(define (die error-message)
(println error-message)
(exit 1))
;; returns true if the string is not empty
(define (non-empty? s)
(if (empty? s) nil true))
;; returns true and print the error if a MySQL error is detected, else return nil
(define (maybe-print-mysql-error)
(if (non-empty? (MySQL:error))
(print "MySQL Error: " (MySQL:error))
nil))
;; predicate returns true if connected to the database
(define (connected? db-config)
(apply MySQL:connect db-config))
;; print a string followed by a variable number of spaces for alignement
(define (printsp s)
(let (spacing 16)
(if (nil? s)
(print "\"\" " (dup " " (- spacing 2)))
(print s (dup " " (- spacing (length s)))))))
;; utility function that prints SQL query results as table
(define (display-sql query)
(MySQL:query query)
(dotimes (x (MySQL:num-rows))
(map printsp (MySQL:fetch-row))
(println)))
; unit test
;(display-sql "select * from mw_lans")
;; builds a 2-dimentional list of results
(define (query-record query)
(MySQL:query query)
(unless (maybe-print-mysql-error)
(MySQL:fetch-all)))
;; returns a value from a single query
;; i.e. "SELECT count(*) FROM product WHERE status = 1"
(define (query-value query)
(MySQL:query query)
(unless (maybe-print-mysql-error)
(first (MySQL:fetch-row))))
; unit test
;(print (query-value "SELECT count(*) FROM mw_lans WHERE id_lan = 1 AND id_lanp = 1"))
;; returns a value from a single query
;; i.e. "SELECT name FROM product WHERE status = 1"
(define (query-string query)
(MySQL:query query)
(unless (maybe-print-mysql-error)
(first (MySQL:fetch-row))))
; unit test
;(print (query-string "SELECT Nom FROM mw_lans WHERE id_lan = 1 AND id_lanp = 1"))
;; prints the description of a table
(define (describe table)
(println "\nTable " table " description:")
(display-sql (append "DESCRIBE " table)))
; unit test
;(describe "mw_lans")
;; shortcut to close db
(define (close-db)
;(println "\nConnection to database closed.")
(MySQL:close-db))
;;; main
(maybe-load-settings "db.conf")
(unless SETTINGS:db-config
(die "Could not load database configuration file."))
; initialize database client
(MySQL:init)
; connect to database and display an error if not successful
(unless (connected? SETTINGS:db-config)
(die "Could not connect to database."))
(close-db)
(exit)
; EOF