cff8da4a98dd9a175d8c6eac7c6ab2689f30b027
[clsql.git] / db-mysql / mysql-api.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          mysql-api.lisp
6 ;;;; Purpose:       Low-level MySQL interface using UFFI
7 ;;;; Programmers:   Kevin M. Rosenberg based on 
8 ;;;;                Original code by Pierre R. Mai 
9 ;;;; Date Started:  Feb 2002
10 ;;;;
11 ;;;; $Id: mysql-api.lisp,v 1.6 2003/07/21 01:45:45 kevin Exp $
12 ;;;;
13 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
14 ;;;; and Copyright (c) 1999-2001 by Pierre R. Mai
15 ;;;;
16 ;;;; CLSQL users are granted the rights to distribute and use this software
17 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
18 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
19 ;;;; *************************************************************************
20
21 (in-package #:mysql)
22
23 ;;;; Modifications from original code
24 ;;;;  - Updated C-structures to conform to structures in MySQL 3.23.46
25 ;;;;  - Changed from CMUCL interface to UFFI
26 ;;;;  - Added and call a C-helper file to support 64-bit integers
27 ;;;;    that are used in a few routines.
28 ;;;;  - Removed all references to interiors of C-structions, this will
29 ;;;;    increase robustness when MySQL's internal structures change.
30  
31 ;;;; Type definitions
32
33 ;;; Basic Types
34
35 (uffi:def-foreign-type mysql-socket :int)
36 (uffi:def-foreign-type mysql-bool :char)
37 (uffi:def-foreign-type mysql-byte :unsigned-char)
38
39 (uffi:def-enum mysql-net-type
40     (:tcp-ip
41      :socket
42      :named-pipe))
43
44 (uffi:def-struct mysql-net
45     (vio :pointer-void)
46   (fd mysql-socket)
47   (fcntl :int)
48   (buff (* :unsigned-char))
49   (buff-end (* :unsigned-char))
50   (write-pos (* :unsigned-char))
51   (read-pos (* :unsigned-char))
52   (last-error (:array :char 200))
53   (last-errno :unsigned-int)
54   (max-packet :unsigned-int)
55   (timeout :unsigned-int)
56   (pkt-nr :unsigned-int)
57   (error mysql-bool)
58   (return-errno mysql-bool)
59   (compress mysql-bool)
60   (no-send-ok mysql-bool)
61   (remain-in-buf :unsigned-long)
62   (length :unsigned-long)
63   (buf-length :unsigned-long)
64   (where-b :unsigned-long)
65   (return-status (* :unsigned-int))
66   (reading-or-writing :unsigned-char)
67   (save-char :char))
68
69 ;;; Mem-Root
70 (uffi:def-struct mysql-used-mem
71     (next :pointer-self)
72   (left :unsigned-int)
73   (size :unsigned-int))
74
75 (uffi:def-struct mysql-mem-root
76     (free (:struct-pointer mysql-used-mem))
77   (used (:struct-pointer mysql-used-mem))
78   (pre-alloc (:struct-pointer mysql-used-mem))
79   (min-alloc :unsigned-int)
80   (block-size :unsigned-int)
81   (error-handler :pointer-void))
82
83 ;;; MYSQL-FIELD
84 (uffi:def-enum mysql-field-types
85     (:decimal
86      :tiny
87      :short
88      :long
89      :float
90      :double
91      :null
92      :timestamp
93      :longlong
94      :int24
95      :date
96      :time
97      :datetime
98      :year
99      :newdate
100      (:enum 247)
101      (:set 248)
102      (:tiny-blob 249)
103      (:medium-blob 250)
104      (:long-blob 251)
105      (:blob 252)
106      (:var-string 253)
107      (:string 254)))
108
109 #-(or mysql-client-v3 mysql-client-v4)
110 (eval-when (:compile-toplevel :load-toplevel :execute)
111   (pushnew :mysql-client-v3 cl:*features*))
112
113 #+mysql-client-v3
114 (uffi:def-struct mysql-field
115     (name (* :char))
116   (table (* :char))
117   (def (* :char))
118   (type mysql-field-types)
119   (length :unsigned-int)
120   (max-length :unsigned-int)
121   (flags :unsigned-int)
122   (decimals :unsigned-int))
123
124 ;; structure changed in mysql 4 client
125 #+mysql-client-v4
126 (uffi:def-struct mysql-field
127     (name (* :char))
128   (table (* :char))
129   (org_table (* :char))
130   (db (* :char))
131   (def (* :char))
132   (length :unsigned-int)
133   (max-length :unsigned-int)
134   (flags :unsigned-int)
135   (decimals :unsigned-int)
136   (type mysql-field-types))
137
138 ;;; MYSQL-ROWS
139
140 (uffi:def-array-pointer mysql-row (* :unsigned-char))
141
142 (uffi:def-array-pointer mysql-field-vector (* mysql-field))
143
144 (uffi:def-foreign-type mysql-field-offset :unsigned-int)
145
146 (uffi:def-struct mysql-rows
147     (next :pointer-self)
148   (data mysql-row))
149
150 (uffi:def-foreign-type mysql-row-offset (:struct-pointer mysql-rows))
151
152 (uffi:def-struct mysql-data
153     (rows-high32 :unsigned-long)
154   (rows-low32 :unsigned-long)
155   (fields :unsigned-int)
156   (data (:struct-pointer mysql-rows))
157   (alloc (:struct mysql-mem-root)))
158
159 ;;; MYSQL
160 (uffi:def-struct mysql-options
161     (connect-timeout :unsigned-int)
162   (client-flag :unsigned-int)
163   (compress mysql-bool)
164   (named-pipe mysql-bool)
165   (port :unsigned-int)
166   (host (* :char))
167   (init-command (* :char))
168   (user (* :char))
169   (password (* :char))
170   (unix-socket (* :char))
171   (db (* :char))
172   (my-cnf-file (* :char))
173   (my-cnf-group (* :char))
174   (charset-dir (* :char))
175   (charset-name (* :char))
176   (use-ssl mysql-bool)
177   (ssl-key (* :char))
178   (ssl-cert (* :char))
179   (ssl-ca (* :char))
180   (ssl-capath (* :char)))
181
182 (uffi:def-enum mysql-option
183     (:connect-timeout
184      :compress
185      :named-pipe
186      :init-command
187      :read-default-file
188      :read-default-group))
189
190 (uffi:def-enum mysql-status
191     (:ready 
192      :get-result
193      :use-result))
194
195 (uffi:def-struct mysql-mysql
196     (net (:struct mysql-net))
197   (connected-fd (* :char))
198   (host (* :char))
199   (user (* :char))
200   (passwd (* :char))
201   (unix-socket (* :char))
202   (server-version (* :char))
203   (host-info (* :char))
204   (info (* :char))
205   (db (* :char))
206   (port :unsigned-int)
207   (client-flag :unsigned-int)
208   (server-capabilities :unsigned-int)
209   (protocol-version :unsigned-int)
210   (field-count :unsigned-int)
211   (server-status :unsigned-int)
212   (thread-id :unsigned-long)
213   (affected-rows-high32 :unsigned-long)
214   (affected-rows-low32 :unsigned-long)
215   (insert-id-high32 :unsigned-long)
216   (insert-id-low32 :unsigned-long)
217   (extra-info-high32 :unsigned-long)
218   (extra-info-low32 :unsigned-long)
219   (packet-length :unsigned-long)
220   (status mysql-status)
221   (fields (:struct-pointer mysql-field))
222   (field-alloc (:struct mysql-mem-root))
223   (free-me mysql-bool)
224   (reconnect mysql-bool)
225   (options (:struct mysql-options))
226   (scramble-buff (:array :char 9))
227   (charset :pointer-void)
228   (server-language :unsigned-int))
229
230
231 ;;; MYSQL-RES
232 (uffi:def-struct mysql-mysql-res
233     (row-count-high32 :unsigned-long)
234   (row-count-low32 :unsigned-long)
235   (field-count :unsigned-int)
236   (current-field :unsigned-int)
237   (fields (:struct-pointer mysql-field))
238   (data (:struct-pointer mysql-data))
239   (data-cursor (:struct-pointer mysql-rows))
240   (field-alloc (:struct mysql-mem-root))
241   (row mysql-row)
242   (current-row mysql-row)
243   (lengths (* :unsigned-long))
244   (handle (:struct-pointer mysql-mysql))
245   (eof mysql-bool))
246
247 ;;;; The Foreign C routines
248 (declaim (inline mysql-init))
249 (uffi:def-function "mysql_init"
250   ((mysql (* mysql-mysql)))
251   :module "mysql" 
252   :returning (* mysql-mysql))
253
254 (declaim (inline mysql-connect))
255 (uffi:def-function "mysql_connect"
256     ((mysql (* mysql-mysql))
257      (host :cstring)
258      (user :cstring)
259      (passwd :cstring))
260   :module "mysql"
261   :returning (* mysql-mysql))
262
263 ;; Need to comment this out for LW 4.2.6
264 ;; ? bug in LW version
265 ;;(declaim (inline mysql-real-connect))
266 (uffi:def-function "mysql_real_connect"
267     ((mysql (* mysql-mysql))
268      (host :cstring)
269      (user :cstring)
270      (passwd :cstring)
271      (db :cstring)
272      (port :unsigned-int)
273      (unix-socket :cstring)
274      (clientflag :unsigned-int))
275   :module "mysql"
276   :returning (* mysql-mysql))
277
278 (declaim (inline mysql-close))
279 (uffi:def-function "mysql_close"
280     ((sock (* mysql-mysql)))
281   :module "mysql"
282   :returning :void)
283
284 (declaim (inline mysql-select-db))
285 (uffi:def-function "mysql_select_db"
286   ((mysql (* mysql-mysql))
287    (db :cstring))
288   :module "mysql"
289   :returning :int)
290
291 (declaim (inline mysql-query))
292 (uffi:def-function "mysql_query"
293     ((mysql (* mysql-mysql))
294      (query :cstring))
295   :module "mysql"
296   :returning :int)
297
298  ;;; I doubt that this function is really useful for direct Lisp usage,
299 ;;; but it is here for completeness...
300
301 (declaim (inline mysql-real-query))
302 (uffi:def-function "mysql_real_query"
303     ((mysql (* mysql-mysql))
304      (query :cstring)
305      (length :unsigned-int))
306   :module "mysql"
307   :returning :int)
308
309 (declaim (inline mysql-create-db))
310 (uffi:def-function "mysql_create_db"
311   ((mysql (* mysql-mysql))
312    (db :cstring))
313   :module "mysql"
314   :returning :int)
315
316 (declaim (inline mysql-drop-db))
317 (uffi:def-function "mysql_drop_db"
318     ((mysql (* mysql-mysql))
319      (db :cstring))
320   :module "mysql"
321   :returning :int)
322
323 (declaim (inline mysql-shutdown))
324 (uffi:def-function "mysql_shutdown"
325   ((mysql (* mysql-mysql)))
326   :module "mysql"
327   :returning :int)
328
329 (declaim (inline mysql-dump-debug-info))
330 (uffi:def-function "mysql_dump_debug_info"
331   ((mysql (* mysql-mysql)))
332   :module "mysql"
333   :returning :int)
334
335 (declaim (inline mysql-refresh))
336 (uffi:def-function "mysql_refresh"
337   ((mysql (* mysql-mysql))
338    (refresh-options :unsigned-int))
339   :module "mysql"
340   :returning :int)
341
342 (declaim (inline mysql-kill))
343 (uffi:def-function "mysql_kill"
344     ((mysql (* mysql-mysql))
345      (pid :unsigned-long))
346   :module "mysql"
347   :returning :int)
348
349 (declaim (inline mysql-ping))
350 (uffi:def-function "mysql_ping"
351     ((mysql (* mysql-mysql)))
352   :module "mysql"
353   :returning :int)
354
355 (declaim (inline mysql-stat))
356 (uffi:def-function "mysql_stat"
357   ((mysql (* mysql-mysql)))
358   :module "mysql"
359   :returning :cstring)
360
361 (declaim (inline mysql-get-server-info))
362 (uffi:def-function "mysql_get_server_info"
363     ((mysql (* mysql-mysql)))
364   :module "mysql"
365   :returning :cstring)
366
367 (declaim (inline mysql-get-client-info))
368 (uffi:def-function "mysql_get_client_info"
369     ()
370   :module "mysql"
371   :returning :cstring)
372
373 (declaim (inline mysql-get-host-info))
374 (uffi:def-function "mysql_get_host_info"
375     ((mysql (* mysql-mysql)))
376   :module "mysql"
377   :returning :cstring)
378
379 (declaim (inline mysql-get-proto-info))
380 (uffi:def-function "mysql_get_proto_info"
381   ((mysql (* mysql-mysql)))
382   :module "mysql"
383   :returning :unsigned-int)
384
385 (declaim (inline mysql-list-dbs))
386 (uffi:def-function "mysql_list_dbs"
387   ((mysql (* mysql-mysql))
388    (wild :cstring))
389   :module "mysql"
390   :returning (* mysql-mysql-res))
391
392 (declaim (inline mysql-list-tables))
393 (uffi:def-function "mysql_list_tables"
394   ((mysql (* mysql-mysql))
395    (wild :cstring))
396   :module "mysql"
397   :returning (* mysql-mysql-res))
398
399 (declaim (inline mysql-list-fields))
400 (uffi:def-function "mysql_list_fields"
401   ((mysql (* mysql-mysql))
402    (table :cstring)
403    (wild :cstring))
404   :module "mysql"
405   :returning (* mysql-mysql-res))
406
407 (declaim (inline mysql-list-processes))
408 (uffi:def-function "mysql_list_processes"
409   ((mysql (* mysql-mysql)))
410   :module "mysql"
411   :returning (* mysql-mysql-res))
412
413 (declaim (inline mysql-store-result))
414 (uffi:def-function "mysql_store_result"
415   ((mysql (* mysql-mysql)))
416   :module "mysql"
417   :returning (* mysql-mysql-res))
418
419 (declaim (inline mysql-use-result))
420 (uffi:def-function "mysql_use_result"
421   ((mysql (* mysql-mysql)))
422   :module "mysql"
423   :returning (* mysql-mysql-res))
424
425 (declaim (inline mysql-options))
426 (uffi:def-function "mysql_options"
427   ((mysql (* mysql-mysql))
428    (option mysql-option)
429    (arg :cstring))
430   :module "mysql"
431   :returning :int)
432
433 (declaim (inline mysql-free-result))
434 (uffi:def-function "mysql_free_result"
435     ((res (* mysql-mysql-res)))
436   :module "mysql"
437   :returning :void)
438
439 (declaim (inline mysql-row-seek))
440 (uffi:def-function "mysql_row_seek"
441   ((res (* mysql-mysql-res))
442    (offset mysql-row-offset))
443   :module "mysql"
444   :returning mysql-row-offset)
445
446 (declaim (inline mysql-field-seek))
447 (uffi:def-function "mysql_field_seek"
448   ((res (* mysql-mysql-res))
449   (offset mysql-field-offset))
450   :module "mysql"
451   :returning mysql-field-offset)
452
453 (declaim (inline mysql-fetch-row))
454 (uffi:def-function "mysql_fetch_row"
455     ((res (* mysql-mysql-res)))
456   :module "mysql"
457   :returning (* (* :unsigned-char)))
458
459 (declaim (inline mysql-fetch-lengths))
460 (uffi:def-function "mysql_fetch_lengths"
461   ((res (* mysql-mysql-res)))
462   :module "mysql"
463   :returning (* :unsigned-long))
464
465 (declaim (inline mysql-fetch-field))
466 (uffi:def-function "mysql_fetch_field"
467   ((res (* mysql-mysql-res)))
468   :module "mysql"
469   :returning (* mysql-field))
470
471 (declaim (inline mysql-fetch-fields))
472 (uffi:def-function "mysql_fetch_fields"
473   ((res (* mysql-mysql-res)))
474   :module "mysql"
475   :returning (* mysql-field))
476
477 (declaim (inline mysql-fetch-field-direct))
478 (uffi:def-function "mysql_fetch_field_direct"
479   ((res (* mysql-mysql-res))
480    (field-num :unsigned-int))
481   :module "mysql"
482   :returning (* mysql-field))
483
484 (declaim (inline mysql-escape-string))
485 (uffi:def-function "mysql_escape_string"
486     ((to :cstring)
487      (from :cstring)
488      (length :unsigned-int))
489   :module "mysql"
490   :returning :unsigned-int)
491
492 (declaim (inline mysql-debug))
493 (uffi:def-function "mysql_debug"
494     ((debug :cstring))
495   :module "mysql"
496   :returning :void)
497
498 (declaim (inline clsql-mysql-num-rows))
499 (uffi:def-function "clsql_mysql_num_rows"
500     ((res (* mysql-mysql-res))
501      (p-high32 (* :unsigned-int)))
502   :module "clsql-mysql"
503   :returning :unsigned-int)
504
505
506 ;;;; Equivalents of C Macro definitions for accessing various fields
507 ;;;; in the internal MySQL Datastructures
508
509
510 (declaim (inline mysql-num-rows))
511 (defun mysql-num-rows (res)
512   (uffi:with-foreign-object (p-high32 :unsigned-int)
513     (let ((low32 (clsql-mysql-num-rows res p-high32))
514           (high32 (uffi:deref-pointer p-high32 :unsigned-int)))
515       (if (zerop high32)
516           low32
517         (make-64-bit-integer high32 low32)))))
518
519 (uffi:def-function "clsql_mysql_affected_rows"
520     ((mysql (* mysql-mysql))
521      (p-high32 (* :unsigned-int)))
522   :returning :unsigned-int
523   :module "clsql-mysql")
524
525 (defun mysql-affected-rows (mysql)
526   (uffi:with-foreign-object (p-high32 :unsigned-int)
527     (let ((low32 (clsql-mysql-affected-rows mysql p-high32))
528           (high32 (uffi:deref-pointer p-high32 :unsigned-int)))
529       (if (zerop high32)
530           low32
531         (make-64-bit-integer high32 low32)))))
532
533 (uffi:def-function "clsql_mysql_insert_id"
534     ((res (* mysql-mysql))
535      (p-high32 (* :unsigned-int)))
536   :returning :unsigned-int
537   :module "clsql-mysql")
538
539 (defun mysql-insert-id (mysql)
540   (uffi:with-foreign-object (p-high32 :unsigned-int)
541   (let ((low32 (clsql-mysql-insert-id mysql p-high32))
542         (high32 (uffi:deref-pointer p-high32 :unsigned-int)))
543     (if (zerop high32)
544         low32
545       (make-64-bit-integer high32 low32)))))
546
547
548 (declaim (inline mysql-num-fields))
549 (uffi:def-function "mysql_num_fields" 
550   ((res (* mysql-mysql-res)))
551   :returning :unsigned-int
552   :module "mysql")
553                  
554 (declaim (inline clsql-mysql-eof))
555 (uffi:def-function ("mysql_eof" clsql-mysql-eof)
556   ((res (* mysql-mysql-res)))
557   :returning :char
558   :module "mysql")
559
560 (declaim (inline mysql-eof))
561 (defun mysql-eof (res)
562   (if (zerop (clsql-mysql-eof res))
563       nil
564     t))
565
566 (declaim (inline mysql-error))
567 (uffi:def-function ("mysql_error" mysql-error)
568   ((mysql (* mysql-mysql)))
569   :returning :cstring
570   :module "mysql")
571
572 (declaim (inline mysql-error-string))
573 (defun mysql-error-string (mysql)
574   (uffi:convert-from-cstring (mysql-error mysql)))
575
576 (declaim (inline mysql-errno))
577 (uffi:def-function "mysql_errno"
578   ((mysql (* mysql-mysql)))
579   :returning :unsigned-int
580   :module "mysql")
581
582 (declaim (inline mysql-info))
583 (uffi:def-function ("mysql_info" mysql-info)
584   ((mysql (* mysql-mysql)))
585   :returning :cstring
586   :module "mysql")
587
588 (declaim (inline mysql-info-string))
589 (defun mysql-info-string (mysql)
590   (uffi:convert-from-cstring (mysql-info mysql)))
591
592 (declaim (inline clsql-mysql-data-seek))
593 (uffi:def-function "clsql_mysql_data_seek"
594   ((res (* mysql-mysql-res))
595    (offset-high32 :unsigned-int)
596    (offset-low32 :unsigned-int))
597   :module "clsql-mysql"
598   :returning :void)
599
600
601 (defun mysql-data-seek (res offset)
602   (multiple-value-bind (high32 low32) (split-64-bit-integer offset)
603     (clsql-mysql-data-seek res high32 low32)))