r9527: add function declarations for prepared statements
[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$
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 #+mysql-client-v3
110 (uffi:def-struct mysql-field
111     (name (* :char))
112   (table (* :char))
113   (def (* :char))
114   (type mysql-field-types)
115   (length :unsigned-int)
116   (max-length :unsigned-int)
117   (flags :unsigned-int)
118   (decimals :unsigned-int))
119
120 ;; structure changed in mysql 4 client
121 #+(and mysql-client-v4 (not mysql-client-v4.1))
122 (uffi:def-struct mysql-field
123     (name (* :char))
124   (table (* :char))
125   (org_table (* :char))
126   (db (* :char))
127   (def (* :char))
128   (length :unsigned-long)
129   (max-length :unsigned-long)
130   (flags :unsigned-int)
131   (decimals :unsigned-int)
132   (type mysql-field-types))
133
134 #+mysql-client-v4.1
135 (uffi:def-struct mysql-field
136     (name (* :char))
137   (org_table (* :char))
138   (table (* :char))
139   (org_table (* :char))
140   (db (* :char))
141   (catalog_db (* :char))
142   (def (* :char))
143   (length :unsigned-long)
144   (max-length :unsigned-long)
145   (name-length :unsigned-int)
146   (org-name-length :unsigned-int)
147   (table-length :unsigned-int)
148   (org-table-length :unsigned-int)
149   (db-length :unsigned-int)
150   (catalog-length :unsigned-int)
151   (def-length :unsigned-int)
152   (flags :unsigned-int)
153   (decimals :unsigned-int)
154   (charsetnr :unsigned-int)
155   (type mysql-field-types))
156
157 ;;; MYSQL-ROWS
158
159 (uffi:def-array-pointer mysql-row (* :unsigned-char))
160
161 (uffi:def-array-pointer mysql-field-vector (* mysql-field))
162
163 (uffi:def-foreign-type mysql-field-offset :unsigned-int)
164
165 (uffi:def-struct mysql-rows
166     (next :pointer-self)
167   (data mysql-row))
168
169 (uffi:def-foreign-type mysql-row-offset (:struct-pointer mysql-rows))
170
171 (uffi:def-struct mysql-data
172     (rows-high32 :unsigned-long)
173   (rows-low32 :unsigned-long)
174   (fields :unsigned-int)
175   (data (:struct-pointer mysql-rows))
176   (alloc (:struct mysql-mem-root)))
177
178 ;;; MYSQL
179 (uffi:def-struct mysql-options
180     (connect-timeout :unsigned-int)
181   (client-flag :unsigned-int)
182   (compress mysql-bool)
183   (named-pipe mysql-bool)
184   (port :unsigned-int)
185   (host (* :char))
186   (init-command (* :char))
187   (user (* :char))
188   (password (* :char))
189   (unix-socket (* :char))
190   (db (* :char))
191   (my-cnf-file (* :char))
192   (my-cnf-group (* :char))
193   (charset-dir (* :char))
194   (charset-name (* :char))
195   (use-ssl mysql-bool)
196   (ssl-key (* :char))
197   (ssl-cert (* :char))
198   (ssl-ca (* :char))
199   (ssl-capath (* :char)))
200
201 (uffi:def-enum mysql-option
202     (:connect-timeout
203      :compress
204      :named-pipe
205      :init-command
206      :read-default-file
207      :read-default-group))
208
209 (uffi:def-enum mysql-status
210     (:ready 
211      :get-result
212      :use-result))
213
214 (uffi:def-struct mysql-mysql
215     (net (:struct mysql-net))
216   (connected-fd (* :char))
217   (host (* :char))
218   (user (* :char))
219   (passwd (* :char))
220   (unix-socket (* :char))
221   (server-version (* :char))
222   (host-info (* :char))
223   (info (* :char))
224   (db (* :char))
225   (port :unsigned-int)
226   (client-flag :unsigned-int)
227   (server-capabilities :unsigned-int)
228   (protocol-version :unsigned-int)
229   (field-count :unsigned-int)
230   (server-status :unsigned-int)
231   (thread-id :unsigned-long)
232   (affected-rows-high32 :unsigned-long)
233   (affected-rows-low32 :unsigned-long)
234   (insert-id-high32 :unsigned-long)
235   (insert-id-low32 :unsigned-long)
236   (extra-info-high32 :unsigned-long)
237   (extra-info-low32 :unsigned-long)
238   (packet-length :unsigned-long)
239   (status mysql-status)
240   (fields (:struct-pointer mysql-field))
241   (field-alloc (:struct mysql-mem-root))
242   (free-me mysql-bool)
243   (reconnect mysql-bool)
244   (options (:struct mysql-options))
245   (scramble-buff (:array :char 9))
246   (charset :pointer-void)
247   (server-language :unsigned-int))
248
249
250 ;;; MYSQL-RES
251 (uffi:def-struct mysql-mysql-res
252     (row-count-high32 :unsigned-long)
253   (row-count-low32 :unsigned-long)
254   (field-count :unsigned-int)
255   (current-field :unsigned-int)
256   (fields (:struct-pointer mysql-field))
257   (data (:struct-pointer mysql-data))
258   (data-cursor (:struct-pointer mysql-rows))
259   (field-alloc (:struct mysql-mem-root))
260   (row mysql-row)
261   (current-row mysql-row)
262   (lengths (* :unsigned-long))
263   (handle (:struct-pointer mysql-mysql))
264   (eof mysql-bool))
265
266 #+mysql-client-v4.1
267 (uffi:def-struct mysql-bind
268     (length (* :unsigned-long))
269   (is-null (* :short))
270   (buffer :pointer-void)
271   (buffer-type :int)
272   (buffer-length :unsigned-long)
273   ;; remainder of structure is for internal use
274   )
275
276 ;;;; The Foreign C routines
277 (declaim (inline mysql-init))
278 (uffi:def-function "mysql_init"
279   ((mysql (* mysql-mysql)))
280   :module "mysql" 
281   :returning (* mysql-mysql))
282
283 #-mysql-client-v4
284 (declaim (inline mysql-connect))
285 #-mysql-client-v4
286 (uffi:def-function "mysql_connect"
287     ((mysql (* mysql-mysql))
288      (host :cstring)
289      (user :cstring)
290      (passwd :cstring))
291   :module "mysql"
292   :returning (* mysql-mysql))
293
294 ;; Need to comment this out for LW 4.2.6
295 ;; ? bug in LW version
296 #-lispworks (declaim (inline mysql-real-connect))
297 (uffi:def-function "mysql_real_connect"
298     ((mysql (* mysql-mysql))
299      (host :cstring)
300      (user :cstring)
301      (passwd :cstring)
302      (db :cstring)
303      (port :unsigned-int)
304      (unix-socket :cstring)
305      (clientflag :unsigned-long))
306   :module "mysql"
307   :returning (* mysql-mysql))
308
309 (declaim (inline mysql-close))
310 (uffi:def-function "mysql_close"
311     ((sock (* mysql-mysql)))
312   :module "mysql"
313   :returning :void)
314
315 (declaim (inline mysql-select-db))
316 (uffi:def-function "mysql_select_db"
317   ((mysql (* mysql-mysql))
318    (db :cstring))
319   :module "mysql"
320   :returning :int)
321
322 (declaim (inline mysql-query))
323 (uffi:def-function "mysql_query"
324     ((mysql (* mysql-mysql))
325      (query :cstring))
326   :module "mysql"
327   :returning :int)
328
329  ;;; I doubt that this function is really useful for direct Lisp usage,
330 ;;; but it is here for completeness...
331
332 (declaim (inline mysql-real-query))
333 (uffi:def-function "mysql_real_query"
334     ((mysql (* mysql-mysql))
335      (query :cstring)
336      (length :unsigned-int))
337   :module "mysql"
338   :returning :int)
339
340 #-mysql-client-v4
341 (declaim (inline mysql-create-db))
342 #-mysql-client-v4
343 (uffi:def-function "mysql_create_db"
344   ((mysql (* mysql-mysql))
345    (db :cstring))
346   :module "mysql"
347   :returning :int)
348
349 #-mysql-client-v4
350 (declaim (inline mysql-drop-db))
351 #-mysql-client-v4
352 (uffi:def-function "mysql_drop_db"
353     ((mysql (* mysql-mysql))
354      (db :cstring))
355   :module "mysql"
356   :returning :int)
357
358 (declaim (inline mysql-shutdown))
359 (uffi:def-function "mysql_shutdown"
360   ((mysql (* mysql-mysql)))
361   :module "mysql"
362   :returning :int)
363
364 (declaim (inline mysql-dump-debug-info))
365 (uffi:def-function "mysql_dump_debug_info"
366   ((mysql (* mysql-mysql)))
367   :module "mysql"
368   :returning :int)
369
370 (declaim (inline mysql-refresh))
371 (uffi:def-function "mysql_refresh"
372   ((mysql (* mysql-mysql))
373    (refresh-options :unsigned-int))
374   :module "mysql"
375   :returning :int)
376
377 (declaim (inline mysql-kill))
378 (uffi:def-function "mysql_kill"
379     ((mysql (* mysql-mysql))
380      (pid :unsigned-long))
381   :module "mysql"
382   :returning :int)
383
384 (declaim (inline mysql-ping))
385 (uffi:def-function "mysql_ping"
386     ((mysql (* mysql-mysql)))
387   :module "mysql"
388   :returning :int)
389
390 (declaim (inline mysql-stat))
391 (uffi:def-function "mysql_stat"
392   ((mysql (* mysql-mysql)))
393   :module "mysql"
394   :returning :cstring)
395
396 (declaim (inline mysql-get-server-info))
397 (uffi:def-function "mysql_get_server_info"
398     ((mysql (* mysql-mysql)))
399   :module "mysql"
400   :returning :cstring)
401
402 (declaim (inline mysql-get-host-info))
403 (uffi:def-function "mysql_get_host_info"
404     ((mysql (* mysql-mysql)))
405   :module "mysql"
406   :returning :cstring)
407
408 (declaim (inline mysql-get-proto-info))
409 (uffi:def-function "mysql_get_proto_info"
410   ((mysql (* mysql-mysql)))
411   :module "mysql"
412   :returning :unsigned-int)
413
414 (declaim (inline mysql-list-dbs))
415 (uffi:def-function "mysql_list_dbs"
416   ((mysql (* mysql-mysql))
417    (wild :cstring))
418   :module "mysql"
419   :returning (* mysql-mysql-res))
420
421 (declaim (inline mysql-list-tables))
422 (uffi:def-function "mysql_list_tables"
423   ((mysql (* mysql-mysql))
424    (wild :cstring))
425   :module "mysql"
426   :returning (* mysql-mysql-res))
427
428 (declaim (inline mysql-list-fields))
429 (uffi:def-function "mysql_list_fields"
430   ((mysql (* mysql-mysql))
431    (table :cstring)
432    (wild :cstring))
433   :module "mysql"
434   :returning (* mysql-mysql-res))
435
436 (declaim (inline mysql-list-processes))
437 (uffi:def-function "mysql_list_processes"
438   ((mysql (* mysql-mysql)))
439   :module "mysql"
440   :returning (* mysql-mysql-res))
441
442 (declaim (inline mysql-store-result))
443 (uffi:def-function "mysql_store_result"
444   ((mysql (* mysql-mysql)))
445   :module "mysql"
446   :returning (* mysql-mysql-res))
447
448 (declaim (inline mysql-use-result))
449 (uffi:def-function "mysql_use_result"
450   ((mysql (* mysql-mysql)))
451   :module "mysql"
452   :returning (* mysql-mysql-res))
453
454 (declaim (inline mysql-options))
455 (uffi:def-function "mysql_options"
456   ((mysql (* mysql-mysql))
457    (option mysql-option)
458    (arg :cstring))
459   :module "mysql"
460   :returning :int)
461
462 (declaim (inline mysql-free-result))
463 (uffi:def-function "mysql_free_result"
464     ((res (* mysql-mysql-res)))
465   :module "mysql"
466   :returning :void)
467
468 (declaim (inline mysql-row-seek))
469 (uffi:def-function "mysql_row_seek"
470   ((res (* mysql-mysql-res))
471    (offset mysql-row-offset))
472   :module "mysql"
473   :returning mysql-row-offset)
474
475 (declaim (inline mysql-field-seek))
476 (uffi:def-function "mysql_field_seek"
477   ((res (* mysql-mysql-res))
478   (offset mysql-field-offset))
479   :module "mysql"
480   :returning mysql-field-offset)
481
482 (declaim (inline mysql-fetch-row))
483 (uffi:def-function "mysql_fetch_row"
484     ((res (* mysql-mysql-res)))
485   :module "mysql"
486   :returning (* (* :unsigned-char)))
487
488 (declaim (inline mysql-fetch-lengths))
489 (uffi:def-function "mysql_fetch_lengths"
490   ((res (* mysql-mysql-res)))
491   :module "mysql"
492   :returning (* :unsigned-long))
493
494 (declaim (inline mysql-fetch-field))
495 (uffi:def-function "mysql_fetch_field"
496   ((res (* mysql-mysql-res)))
497   :module "mysql"
498   :returning (* mysql-field))
499
500 (declaim (inline mysql-fetch-fields))
501 (uffi:def-function "mysql_fetch_fields"
502   ((res (* mysql-mysql-res)))
503   :module "mysql"
504   :returning (* mysql-field))
505
506 (declaim (inline mysql-fetch-field-direct))
507 (uffi:def-function "mysql_fetch_field_direct"
508   ((res (* mysql-mysql-res))
509    (field-num :unsigned-int))
510   :module "mysql"
511   :returning (* mysql-field))
512
513 (declaim (inline mysql-escape-string))
514 (uffi:def-function "mysql_escape_string"
515     ((to :cstring)
516      (from :cstring)
517      (length :unsigned-int))
518   :module "mysql"
519   :returning :unsigned-int)
520
521 (declaim (inline mysql-debug))
522 (uffi:def-function "mysql_debug"
523     ((debug :cstring))
524   :module "mysql"
525   :returning :void)
526
527 (declaim (inline clsql-mysql-num-rows))
528 (uffi:def-function "clsql_mysql_num_rows"
529     ((res (* mysql-mysql-res))
530      (p-high32 (* :unsigned-int)))
531   :module "clsql-mysql"
532   :returning :unsigned-int)
533
534 #+mysql-client-v4.1
535 (uffi:def-foreign-type mysql-stmt-ptr :pointer-void)
536
537 #+mysql-client-v4.1
538 (uffi:def-function "mysql_stmt_init"
539     ((res (* mysql-mysql-res)))
540   :module "clsql-mysql"
541   :returning mysql-stmt-ptr)
542
543 #+mysql-client-v4.1
544 (uffi:def-function "mysql_stmt_bind_param"
545     ((stmt mysql-stmt-ptr)
546      (bind (* mysql-bind)))
547   :module "clsql-mysql"
548   :returning :short)
549
550 #+mysql-client-v4.1
551 (uffi:def-function "mysql_stmt_bind_result"
552     ((stmt mysql-stmt-ptr)
553      (bind (* mysql-bind)))
554   :module "clsql-mysql"
555   :returning :short)
556
557 #+mysql-client-v4.1
558 (uffi:def-function "mysql_stmt_execute"
559     ((stmt mysql-stmt-ptr))
560   :module "clsql-mysql"
561   :returning :int)
562
563 #+mysql-client-v4.1
564 (uffi:def-function "mysql_stmt_fetch"
565     ((stmt mysql-stmt-ptr))
566   :module "clsql-mysql"
567   :returning :int)
568
569 #+mysql-client-v4.1
570 (uffi:def-function "mysql_stmt_free_result"
571     ((stmt mysql-stmt-ptr))
572   :module "clsql-mysql"
573   :returning :short)
574
575 ;;;; Equivalents of C Macro definitions for accessing various fields
576 ;;;; in the internal MySQL Datastructures
577
578
579 (declaim (inline mysql-num-rows))
580 (defun mysql-num-rows (res)
581   (uffi:with-foreign-object (p-high32 :unsigned-int)
582     (let ((low32 (clsql-mysql-num-rows res p-high32))
583           (high32 (uffi:deref-pointer p-high32 :unsigned-int)))
584       (if (zerop high32)
585           low32
586         (make-64-bit-integer high32 low32)))))
587
588 (uffi:def-function "clsql_mysql_affected_rows"
589     ((mysql (* mysql-mysql))
590      (p-high32 (* :unsigned-int)))
591   :returning :unsigned-int
592   :module "clsql-mysql")
593
594 (defun mysql-affected-rows (mysql)
595   (uffi:with-foreign-object (p-high32 :unsigned-int)
596     (let ((low32 (clsql-mysql-affected-rows mysql p-high32))
597           (high32 (uffi:deref-pointer p-high32 :unsigned-int)))
598       (if (zerop high32)
599           low32
600         (make-64-bit-integer high32 low32)))))
601
602 (uffi:def-function "clsql_mysql_insert_id"
603     ((res (* mysql-mysql))
604      (p-high32 (* :unsigned-int)))
605   :returning :unsigned-int
606   :module "clsql-mysql")
607
608 (defun mysql-insert-id (mysql)
609   (uffi:with-foreign-object (p-high32 :unsigned-int)
610   (let ((low32 (clsql-mysql-insert-id mysql p-high32))
611         (high32 (uffi:deref-pointer p-high32 :unsigned-int)))
612     (if (zerop high32)
613         low32
614       (make-64-bit-integer high32 low32)))))
615
616
617 (declaim (inline mysql-num-fields))
618 (uffi:def-function "mysql_num_fields" 
619   ((res (* mysql-mysql-res)))
620   :returning :unsigned-int
621   :module "mysql")
622                  
623 (declaim (inline clsql-mysql-eof))
624 (uffi:def-function ("mysql_eof" clsql-mysql-eof)
625   ((res (* mysql-mysql-res)))
626   :returning :char
627   :module "mysql")
628
629 (declaim (inline mysql-eof))
630 (defun mysql-eof (res)
631   (if (zerop (clsql-mysql-eof res))
632       nil
633     t))
634
635 (declaim (inline mysql-error))
636 (uffi:def-function ("mysql_error" mysql-error)
637   ((mysql (* mysql-mysql)))
638   :returning :cstring
639   :module "mysql")
640
641 (declaim (inline mysql-error-string))
642 (defun mysql-error-string (mysql)
643   (uffi:convert-from-cstring (mysql-error mysql)))
644
645 (declaim (inline mysql-errno))
646 (uffi:def-function "mysql_errno"
647   ((mysql (* mysql-mysql)))
648   :returning :unsigned-int
649   :module "mysql")
650
651 (declaim (inline mysql-info))
652 (uffi:def-function ("mysql_info" mysql-info)
653   ((mysql (* mysql-mysql)))
654   :returning :cstring
655   :module "mysql")
656
657 (declaim (inline mysql-info-string))
658 (defun mysql-info-string (mysql)
659   (uffi:convert-from-cstring (mysql-info mysql)))
660
661 (declaim (inline clsql-mysql-data-seek))
662 (uffi:def-function "clsql_mysql_data_seek"
663   ((res (* mysql-mysql-res))
664    (offset-high32 :unsigned-int)
665    (offset-low32 :unsigned-int))
666   :module "clsql-mysql"
667   :returning :void)
668
669 (defun mysql-data-seek (res offset)
670   (multiple-value-bind (high32 low32) (split-64-bit-integer offset)
671     (clsql-mysql-data-seek res high32 low32)))