1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
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
13 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
14 ;;;; and Copyright (c) 1999-2001 by Pierre R. Mai
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 ;;;; *************************************************************************
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.
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)
39 (uffi:def-enum mysql-net-type
44 (uffi:def-struct mysql-net
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)
58 (return-errno 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)
70 (uffi:def-struct mysql-used-mem
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))
84 (uffi:def-enum mysql-field-types
110 (uffi:def-struct mysql-field
114 (type mysql-field-types)
115 (length :unsigned-int)
116 (max-length :unsigned-int)
117 (flags :unsigned-int)
118 (decimals :unsigned-int))
120 ;; structure changed in mysql 4 client
121 #+(and mysql-client-v4 (not mysql-client-v4.1))
122 (uffi:def-struct mysql-field
125 (org_table (* :char))
128 (length :unsigned-long)
129 (max-length :unsigned-long)
130 (flags :unsigned-int)
131 (decimals :unsigned-int)
132 (type mysql-field-types))
135 (uffi:def-struct mysql-field
137 (org_table (* :char))
139 (org_table (* :char))
141 (catalog_db (* :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))
159 (uffi:def-array-pointer mysql-row (* :unsigned-char))
161 (uffi:def-array-pointer mysql-field-vector (* mysql-field))
163 (uffi:def-foreign-type mysql-field-offset :unsigned-int)
165 (uffi:def-struct mysql-rows
169 (uffi:def-foreign-type mysql-row-offset (:struct-pointer mysql-rows))
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)))
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)
186 (init-command (* :char))
189 (unix-socket (* :char))
191 (my-cnf-file (* :char))
192 (my-cnf-group (* :char))
193 (charset-dir (* :char))
194 (charset-name (* :char))
199 (ssl-capath (* :char)))
201 (uffi:def-enum mysql-option
207 :read-default-group))
209 (uffi:def-enum mysql-status
214 (uffi:def-struct mysql-mysql
215 (net (:struct mysql-net))
216 (connected-fd (* :char))
220 (unix-socket (* :char))
221 (server-version (* :char))
222 (host-info (* :char))
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))
243 (reconnect mysql-bool)
244 (options (:struct mysql-options))
245 (scramble-buff (:array :char 9))
246 (charset :pointer-void)
247 (server-language :unsigned-int))
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))
261 (current-row mysql-row)
262 (lengths (* :unsigned-long))
263 (handle (:struct-pointer mysql-mysql))
267 (uffi:def-struct mysql-bind
268 (length (* :unsigned-long))
270 (buffer :pointer-void)
272 (buffer-length :unsigned-long)
273 ;; remainder of structure is for internal use
276 ;;;; The Foreign C routines
277 (declaim (inline mysql-init))
278 (uffi:def-function "mysql_init"
279 ((mysql (* mysql-mysql)))
281 :returning (* mysql-mysql))
284 (declaim (inline mysql-connect))
286 (uffi:def-function "mysql_connect"
287 ((mysql (* mysql-mysql))
292 :returning (* mysql-mysql))
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))
304 (unix-socket :cstring)
305 (clientflag :unsigned-long))
307 :returning (* mysql-mysql))
309 (declaim (inline mysql-close))
310 (uffi:def-function "mysql_close"
311 ((sock (* mysql-mysql)))
315 (declaim (inline mysql-select-db))
316 (uffi:def-function "mysql_select_db"
317 ((mysql (* mysql-mysql))
322 (declaim (inline mysql-query))
323 (uffi:def-function "mysql_query"
324 ((mysql (* mysql-mysql))
329 ;;; I doubt that this function is really useful for direct Lisp usage,
330 ;;; but it is here for completeness...
332 (declaim (inline mysql-real-query))
333 (uffi:def-function "mysql_real_query"
334 ((mysql (* mysql-mysql))
336 (length :unsigned-int))
341 (declaim (inline mysql-create-db))
343 (uffi:def-function "mysql_create_db"
344 ((mysql (* mysql-mysql))
350 (declaim (inline mysql-drop-db))
352 (uffi:def-function "mysql_drop_db"
353 ((mysql (* mysql-mysql))
358 (declaim (inline mysql-shutdown))
359 (uffi:def-function "mysql_shutdown"
360 ((mysql (* mysql-mysql)))
364 (declaim (inline mysql-dump-debug-info))
365 (uffi:def-function "mysql_dump_debug_info"
366 ((mysql (* mysql-mysql)))
370 (declaim (inline mysql-refresh))
371 (uffi:def-function "mysql_refresh"
372 ((mysql (* mysql-mysql))
373 (refresh-options :unsigned-int))
377 (declaim (inline mysql-kill))
378 (uffi:def-function "mysql_kill"
379 ((mysql (* mysql-mysql))
380 (pid :unsigned-long))
384 (declaim (inline mysql-ping))
385 (uffi:def-function "mysql_ping"
386 ((mysql (* mysql-mysql)))
390 (declaim (inline mysql-stat))
391 (uffi:def-function "mysql_stat"
392 ((mysql (* mysql-mysql)))
396 (declaim (inline mysql-get-server-info))
397 (uffi:def-function "mysql_get_server_info"
398 ((mysql (* mysql-mysql)))
402 (declaim (inline mysql-get-host-info))
403 (uffi:def-function "mysql_get_host_info"
404 ((mysql (* mysql-mysql)))
408 (declaim (inline mysql-get-proto-info))
409 (uffi:def-function "mysql_get_proto_info"
410 ((mysql (* mysql-mysql)))
412 :returning :unsigned-int)
414 (declaim (inline mysql-list-dbs))
415 (uffi:def-function "mysql_list_dbs"
416 ((mysql (* mysql-mysql))
419 :returning (* mysql-mysql-res))
421 (declaim (inline mysql-list-tables))
422 (uffi:def-function "mysql_list_tables"
423 ((mysql (* mysql-mysql))
426 :returning (* mysql-mysql-res))
428 (declaim (inline mysql-list-fields))
429 (uffi:def-function "mysql_list_fields"
430 ((mysql (* mysql-mysql))
434 :returning (* mysql-mysql-res))
436 (declaim (inline mysql-list-processes))
437 (uffi:def-function "mysql_list_processes"
438 ((mysql (* mysql-mysql)))
440 :returning (* mysql-mysql-res))
442 (declaim (inline mysql-store-result))
443 (uffi:def-function "mysql_store_result"
444 ((mysql (* mysql-mysql)))
446 :returning (* mysql-mysql-res))
448 (declaim (inline mysql-use-result))
449 (uffi:def-function "mysql_use_result"
450 ((mysql (* mysql-mysql)))
452 :returning (* mysql-mysql-res))
454 (declaim (inline mysql-options))
455 (uffi:def-function "mysql_options"
456 ((mysql (* mysql-mysql))
457 (option mysql-option)
462 (declaim (inline mysql-free-result))
463 (uffi:def-function "mysql_free_result"
464 ((res (* mysql-mysql-res)))
468 (declaim (inline mysql-row-seek))
469 (uffi:def-function "mysql_row_seek"
470 ((res (* mysql-mysql-res))
471 (offset mysql-row-offset))
473 :returning mysql-row-offset)
475 (declaim (inline mysql-field-seek))
476 (uffi:def-function "mysql_field_seek"
477 ((res (* mysql-mysql-res))
478 (offset mysql-field-offset))
480 :returning mysql-field-offset)
482 (declaim (inline mysql-fetch-row))
483 (uffi:def-function "mysql_fetch_row"
484 ((res (* mysql-mysql-res)))
486 :returning (* (* :unsigned-char)))
488 (declaim (inline mysql-fetch-lengths))
489 (uffi:def-function "mysql_fetch_lengths"
490 ((res (* mysql-mysql-res)))
492 :returning (* :unsigned-long))
494 (declaim (inline mysql-fetch-field))
495 (uffi:def-function "mysql_fetch_field"
496 ((res (* mysql-mysql-res)))
498 :returning (* mysql-field))
500 (declaim (inline mysql-fetch-fields))
501 (uffi:def-function "mysql_fetch_fields"
502 ((res (* mysql-mysql-res)))
504 :returning (* mysql-field))
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))
511 :returning (* mysql-field))
513 (declaim (inline mysql-escape-string))
514 (uffi:def-function "mysql_escape_string"
517 (length :unsigned-int))
519 :returning :unsigned-int)
521 (declaim (inline mysql-debug))
522 (uffi:def-function "mysql_debug"
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)
535 (uffi:def-foreign-type mysql-stmt-ptr :pointer-void)
538 (uffi:def-function "mysql_stmt_init"
539 ((res (* mysql-mysql-res)))
540 :module "clsql-mysql"
541 :returning mysql-stmt-ptr)
544 (uffi:def-function "mysql_stmt_bind_param"
545 ((stmt mysql-stmt-ptr)
546 (bind (* mysql-bind)))
547 :module "clsql-mysql"
551 (uffi:def-function "mysql_stmt_bind_result"
552 ((stmt mysql-stmt-ptr)
553 (bind (* mysql-bind)))
554 :module "clsql-mysql"
558 (uffi:def-function "mysql_stmt_execute"
559 ((stmt mysql-stmt-ptr))
560 :module "clsql-mysql"
564 (uffi:def-function "mysql_stmt_fetch"
565 ((stmt mysql-stmt-ptr))
566 :module "clsql-mysql"
570 (uffi:def-function "mysql_stmt_free_result"
571 ((stmt mysql-stmt-ptr))
572 :module "clsql-mysql"
575 ;;;; Equivalents of C Macro definitions for accessing various fields
576 ;;;; in the internal MySQL Datastructures
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)))
586 (make-64-bit-integer high32 low32)))))
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")
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)))
600 (make-64-bit-integer high32 low32)))))
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")
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)))
614 (make-64-bit-integer high32 low32)))))
617 (declaim (inline mysql-num-fields))
618 (uffi:def-function "mysql_num_fields"
619 ((res (* mysql-mysql-res)))
620 :returning :unsigned-int
623 (declaim (inline clsql-mysql-eof))
624 (uffi:def-function ("mysql_eof" clsql-mysql-eof)
625 ((res (* mysql-mysql-res)))
629 (declaim (inline mysql-eof))
630 (defun mysql-eof (res)
631 (if (zerop (clsql-mysql-eof res))
635 (declaim (inline mysql-error))
636 (uffi:def-function ("mysql_error" mysql-error)
637 ((mysql (* mysql-mysql)))
641 (declaim (inline mysql-error-string))
642 (defun mysql-error-string (mysql)
643 (uffi:convert-from-cstring (mysql-error mysql)))
645 (declaim (inline mysql-errno))
646 (uffi:def-function "mysql_errno"
647 ((mysql (* mysql-mysql)))
648 :returning :unsigned-int
651 (declaim (inline mysql-info))
652 (uffi:def-function ("mysql_info" mysql-info)
653 ((mysql (* mysql-mysql)))
657 (declaim (inline mysql-info-string))
658 (defun mysql-info-string (mysql)
659 (uffi:convert-from-cstring (mysql-info mysql)))
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"
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)))