-
Notifications
You must be signed in to change notification settings - Fork 2
/
mysql.sc
214 lines (196 loc) · 7.99 KB
/
mysql.sc
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
(library (mysql mysql)
(export
connect
query
err
)
(import
(scheme)
(mysql const)
(mysql ftype)
(mysql ffi)
(only (core alist) ref)
)
(define-syntax make-ftype-null
(syntax-rules ()
((_ type)
(make-ftype-pointer type #x00000000))))
(define connect
(lambda (lst)
(define host (or (ref lst 'hostname) '()))
(define port (or (ref lst 'port) 3306))
(define user (or (ref lst 'user) '()))
(define password (or (ref lst 'password) '()))
(define database (or (ref lst 'database) '()))
(define unix_socket (or (ref lst 'socket) #f))
(define clientflag (or (ref lst 'clientflag) 0))
(let* ((conn (mysql-init (make-ftype-null mysql)))
(rst (mysql-real-connect conn host user password database port unix_socket clientflag)))
(if (ftype-pointer-null? rst)
(err conn "connect")
conn))))
(define query
(lambda (conn query)
(if (zero? (mysql-query conn query))
(let ((result (mysql-store-result conn)))
(if (not (ftype-pointer-null? result))
(let* ((num-fields (mysql-num-fields result))
(fields (parser-fields result num-fields))
(rows (parser-rows result fields '())))
(mysql-free-result result)
(mysql-list->json rows fields))))
(err conn "query")
)))
(define parser-fields
(lambda (result num-fields)
(mysql-field-seek result 0)
(let ((fields (make-vector num-fields)))
(let loop ((idx 0))
(let ((field (mysql-fetch-field result)))
(if (ftype-pointer-null? field)
fields
(begin
(vector-set! fields idx (cons (field-name field)(field-type field)))
(loop (+ idx 1))
)))))))
(define parser-rows
(lambda (result fields rows)
(let ((row (mysql-fetch-row result)))
(if (ftype-pointer-null? row)
(reverse rows)
(parser-rows result fields (cons (parser-columns row fields) rows))
))))
(define parser-columns
(lambda (row fields)
(let* ((len (vector-length fields))
(vec (make-vector len)))
(let loop ((idx 0))
(if (= idx len)
vec
(let ((type (cdr (vector-ref fields idx))))
(vector-set! vec idx (parser-cell row idx type))
(loop (+ idx 1))
))))))
(define parser-cell
(lambda (row column-index type)
(if (null-cell? row column-index)
'null
(type-convert (cell-string row column-index) type)
)))
(define null-cell?
(lambda (row column-index)
(eq? 0 (foreign-ref 'unsigned-8
(ftype-pointer-address
(ftype-&ref mysql-row () row column-index))
0))))
(define cell-string
(lambda (row column-index)
(char*->string
(ftype-pointer-address
(ftype-&ref mysql-row (*) row column-index)))))
(define field-name
(lambda (field)
(char*->string
(ftype-pointer-address
(ftype-&ref mysql-field (name 0) field 0)))))
(define field-type-map
(let ((table (make-eq-hashtable)))
(map
(lambda (pair)
(hashtable-set! table (car pair) (cdr pair)))
'((0 . decimal)
(1 . tiny)
(2 . short)
(3 . long)
(4 . float)
(5 . double)
(6 . null)
(7 . timestamp)
(8 . longlong)
(9 . int24)
(10 . date)
(11 . time)
(12 . datetime)
(13 . year)
(14 . newdate)
(15 . varchar)
(16 . bit)
(17 . timestamp2)
(18 . datetime2)
(19 . time2)
(245 . json)
(246 . newdecimal)
(247 . enum)
(248 . set)
(249 . tiny-blob)
(250 . medium-blob)
(251 . long-blob)
(252 . blob)
(253 . var-string)
(254 . string)
(255 . geometry)))
table))
(define field-type-symbol
(lambda (field-type)
(hashtable-ref field-type-map field-type '())))
(define field-type
(lambda (field)
(field-type-symbol
(ftype-ref mysql-field (type) field 0))))
(define char*->string
(lambda (address)
(list->string
(reverse
(let loop ((pointer address) (chars '()))
(let ((char (foreign-ref 'char pointer 0)))
(if (char=? char #\nul)
chars
(loop (+ 1 pointer) (cons char chars)))))))))
(define type-convert
(lambda (value type)
(case type
((decimal tiny short long float double longlong int24 newdecimal)
(string->number value))
((datetime timestamp)
(date-string->date value))
(else value))))
(define date-string->date
(lambda (date-string)
(let ((part (lambda (start end)
(string->number
(substring date-string start end)))))
(let ((year (part 0 4))
(month (part 5 7))
(day (part 8 10))
(hour (part 11 13))
(minute (part 14 16))
(second (part 17 19)))
(make-date 0 second minute hour day month year 0)))))
(define mysql-list->json
(lambda (rows fields)
(let* ((len (length rows))
(vec (make-vector len))
(field-len (vector-length fields)))
(let loop1 ((idx1 0))
(if (< idx1 len)
(let loop2 ((idx2 0)
(lst '()))
(if (< idx2 field-len)
(loop2 (+ 1 idx2)
(cons
(cons
(car (vector-ref fields idx2))
(vector-ref (list-ref rows idx1) idx2))
lst))
(begin
(vector-set! vec idx1 (reverse lst))
(loop1 (+ 1 idx1)))))
vec
)))))
(define err
(lambda (conn name)
(let ((msg (format "errno: ~a, error: ~a" (mysql-errno conn) (mysql-error conn)))
(act (string-append "" name)))
(mysql-close conn)
(error act msg))))
)