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 :byte)
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
111 (uffi:def-struct mysql-field
115 (type mysql-field-types)
116 (length :unsigned-int)
117 (max-length :unsigned-int)
118 (flags :unsigned-int)
119 (decimals :unsigned-int))
121 ;; structure changed in mysql 4 client
122 #+(and mysql-client-v4 (not mysql-client-v4.1))
123 (uffi:def-struct mysql-field
126 (org_table (* :char))
129 (length :unsigned-long)
130 (max-length :unsigned-long)
131 (flags :unsigned-int)
132 (decimals :unsigned-int)
133 (type mysql-field-types))
136 (uffi:def-struct mysql-field
140 (org_table (* :char))
142 (catalog_db (* :char))
144 (length :unsigned-long)
145 (max-length :unsigned-long)
146 (name-length :unsigned-int)
147 (org-name-length :unsigned-int)
148 (table-length :unsigned-int)
149 (org-table-length :unsigned-int)
150 (db-length :unsigned-int)
151 (catalog-length :unsigned-int)
152 (def-length :unsigned-int)
153 (flags :unsigned-int)
154 (decimals :unsigned-int)
155 (charsetnr :unsigned-int)
156 (type mysql-field-types))
158 (uffi:def-struct mysql-time
160 (month :unsigned-int)
163 (minute :unsigned-int)
164 (second :unsigned-int)
165 (second-part :unsigned-long)
171 (uffi:def-array-pointer mysql-row (* :unsigned-char))
173 (uffi:def-array-pointer mysql-field-vector (* mysql-field))
175 (uffi:def-foreign-type mysql-field-offset :unsigned-int)
177 (uffi:def-struct mysql-rows
181 (uffi:def-foreign-type mysql-row-offset (:struct-pointer mysql-rows))
183 (uffi:def-struct mysql-data
184 (rows-high32 :unsigned-long)
185 (rows-low32 :unsigned-long)
186 (fields :unsigned-int)
187 (data (:struct-pointer mysql-rows))
188 (alloc (:struct mysql-mem-root)))
191 (uffi:def-struct mysql-options
192 (connect-timeout :unsigned-int)
193 (client-flag :unsigned-int)
194 (compress mysql-bool)
195 (named-pipe mysql-bool)
198 (init-command (* :char))
201 (unix-socket (* :char))
203 (my-cnf-file (* :char))
204 (my-cnf-group (* :char))
205 (charset-dir (* :char))
206 (charset-name (* :char))
211 (ssl-capath (* :char)))
213 (uffi:def-enum mysql-option
219 :read-default-group))
221 (uffi:def-enum mysql-status
226 (uffi:def-struct mysql-mysql
227 (net (:struct mysql-net))
228 (connected-fd (* :char))
232 (unix-socket (* :char))
233 (server-version (* :char))
234 (host-info (* :char))
238 (client-flag :unsigned-int)
239 (server-capabilities :unsigned-int)
240 (protocol-version :unsigned-int)
241 (field-count :unsigned-int)
242 (server-status :unsigned-int)
243 (thread-id :unsigned-long)
244 (affected-rows-high32 :unsigned-long)
245 (affected-rows-low32 :unsigned-long)
246 (insert-id-high32 :unsigned-long)
247 (insert-id-low32 :unsigned-long)
248 (extra-info-high32 :unsigned-long)
249 (extra-info-low32 :unsigned-long)
250 (packet-length :unsigned-long)
251 (status mysql-status)
252 (fields (:struct-pointer mysql-field))
253 (field-alloc (:struct mysql-mem-root))
255 (reconnect mysql-bool)
256 (options (:struct mysql-options))
257 (scramble-buff (:array :char 9))
258 (charset :pointer-void)
259 (server-language :unsigned-int))
263 (uffi:def-struct mysql-mysql-res
264 (row-count-high32 :unsigned-long)
265 (row-count-low32 :unsigned-long)
266 (field-count :unsigned-int)
267 (current-field :unsigned-int)
268 (fields (:struct-pointer mysql-field))
269 (data (:struct-pointer mysql-data))
270 (data-cursor (:struct-pointer mysql-rows))
271 (field-alloc (:struct mysql-mem-root))
273 (current-row mysql-row)
274 (lengths (* :unsigned-long))
275 (handle (:struct-pointer mysql-mysql))
279 (uffi:def-enum mysql-field-types
285 (uffi:def-struct mysql-bind
286 (length (* :unsigned-long))
287 (is-null (* mysql-bool))
288 (buffer :pointer-void)
290 (buffer-length :unsigned-long)
292 (inter_buffer (* :unsigned-char))
293 (offset :unsigned-long)
294 (internal-length :unsigned-long)
295 (param-number :unsigned-int)
296 (pack-length :unsigned-int)
297 (is-signed mysql-bool)
298 (long-data-used mysql-bool)
299 (internal-is-null mysql-bool)
300 (store-param-func :pointer-void)
301 (fetch-result :pointer-void)
302 (skip-result :pointer-void))
304 ;;;; The Foreign C routines
305 (declaim (inline mysql-init))
306 (uffi:def-function "mysql_init"
307 ((mysql (* mysql-mysql)))
309 :returning (* mysql-mysql))
312 (declaim (inline mysql-connect))
314 (uffi:def-function "mysql_connect"
315 ((mysql (* mysql-mysql))
320 :returning (* mysql-mysql))
322 ;; Need to comment this out for LW 4.2.6
323 ;; ? bug in LW version
324 #-lispworks (declaim (inline mysql-real-connect))
325 (uffi:def-function "mysql_real_connect"
326 ((mysql (* mysql-mysql))
332 (unix-socket :cstring)
333 (clientflag :unsigned-long))
335 :returning (* mysql-mysql))
337 (declaim (inline mysql-close))
338 (uffi:def-function "mysql_close"
339 ((sock (* mysql-mysql)))
343 (declaim (inline mysql-select-db))
344 (uffi:def-function "mysql_select_db"
345 ((mysql (* mysql-mysql))
350 (declaim (inline mysql-query))
351 (uffi:def-function "mysql_query"
352 ((mysql (* mysql-mysql))
357 ;;; I doubt that this function is really useful for direct Lisp usage,
358 ;;; but it is here for completeness...
360 (declaim (inline mysql-real-query))
361 (uffi:def-function "mysql_real_query"
362 ((mysql (* mysql-mysql))
364 (length :unsigned-int))
369 (declaim (inline mysql-create-db))
371 (uffi:def-function "mysql_create_db"
372 ((mysql (* mysql-mysql))
378 (declaim (inline mysql-drop-db))
380 (uffi:def-function "mysql_drop_db"
381 ((mysql (* mysql-mysql))
386 (declaim (inline mysql-shutdown))
387 (uffi:def-function "mysql_shutdown"
388 ((mysql (* mysql-mysql)))
392 (declaim (inline mysql-dump-debug-info))
393 (uffi:def-function "mysql_dump_debug_info"
394 ((mysql (* mysql-mysql)))
398 (declaim (inline mysql-refresh))
399 (uffi:def-function "mysql_refresh"
400 ((mysql (* mysql-mysql))
401 (refresh-options :unsigned-int))
405 (declaim (inline mysql-kill))
406 (uffi:def-function "mysql_kill"
407 ((mysql (* mysql-mysql))
408 (pid :unsigned-long))
412 (declaim (inline mysql-ping))
413 (uffi:def-function "mysql_ping"
414 ((mysql (* mysql-mysql)))
418 (declaim (inline mysql-stat))
419 (uffi:def-function "mysql_stat"
420 ((mysql (* mysql-mysql)))
424 (declaim (inline mysql-get-server-info))
425 (uffi:def-function "mysql_get_server_info"
426 ((mysql (* mysql-mysql)))
430 (declaim (inline mysql-get-host-info))
431 (uffi:def-function "mysql_get_host_info"
432 ((mysql (* mysql-mysql)))
436 (declaim (inline mysql-get-proto-info))
437 (uffi:def-function "mysql_get_proto_info"
438 ((mysql (* mysql-mysql)))
440 :returning :unsigned-int)
442 (declaim (inline mysql-list-dbs))
443 (uffi:def-function "mysql_list_dbs"
444 ((mysql (* mysql-mysql))
447 :returning (* mysql-mysql-res))
449 (declaim (inline mysql-list-tables))
450 (uffi:def-function "mysql_list_tables"
451 ((mysql (* mysql-mysql))
454 :returning (* mysql-mysql-res))
456 (declaim (inline mysql-list-fields))
457 (uffi:def-function "mysql_list_fields"
458 ((mysql (* mysql-mysql))
462 :returning (* mysql-mysql-res))
464 (declaim (inline mysql-list-processes))
465 (uffi:def-function "mysql_list_processes"
466 ((mysql (* mysql-mysql)))
468 :returning (* mysql-mysql-res))
470 (declaim (inline mysql-store-result))
471 (uffi:def-function "mysql_store_result"
472 ((mysql (* mysql-mysql)))
474 :returning (* mysql-mysql-res))
476 (declaim (inline mysql-use-result))
477 (uffi:def-function "mysql_use_result"
478 ((mysql (* mysql-mysql)))
480 :returning (* mysql-mysql-res))
482 (declaim (inline mysql-options))
483 (uffi:def-function "mysql_options"
484 ((mysql (* mysql-mysql))
485 (option mysql-option)
490 (declaim (inline mysql-free-result))
491 (uffi:def-function "mysql_free_result"
492 ((res (* mysql-mysql-res)))
496 (declaim (inline mysql-row-seek))
497 (uffi:def-function "mysql_row_seek"
498 ((res (* mysql-mysql-res))
499 (offset mysql-row-offset))
501 :returning mysql-row-offset)
503 (declaim (inline mysql-field-seek))
504 (uffi:def-function "mysql_field_seek"
505 ((res (* mysql-mysql-res))
506 (offset mysql-field-offset))
508 :returning mysql-field-offset)
510 (declaim (inline mysql-fetch-row))
511 (uffi:def-function "mysql_fetch_row"
512 ((res (* mysql-mysql-res)))
514 :returning (* (* :unsigned-char)))
516 (declaim (inline mysql-fetch-lengths))
517 (uffi:def-function "mysql_fetch_lengths"
518 ((res (* mysql-mysql-res)))
520 :returning (* :unsigned-long))
522 (declaim (inline mysql-fetch-field))
523 (uffi:def-function "mysql_fetch_field"
524 ((res (* mysql-mysql-res)))
526 :returning (* mysql-field))
528 (declaim (inline mysql-fetch-fields))
529 (uffi:def-function "mysql_fetch_fields"
530 ((res (* mysql-mysql-res)))
532 :returning (* mysql-field))
534 (declaim (inline mysql-fetch-field-direct))
535 (uffi:def-function "mysql_fetch_field_direct"
536 ((res (* mysql-mysql-res))
537 (field-num :unsigned-int))
539 :returning (* mysql-field))
541 (declaim (inline mysql-escape-string))
542 (uffi:def-function "mysql_escape_string"
543 ((to (* :unsigned-char))
544 (from (* :unsigned-char))
545 (length :unsigned-int))
547 :returning :unsigned-int)
549 (declaim (inline mysql-debug))
550 (uffi:def-function "mysql_debug"
555 (declaim (inline clsql-mysql-num-rows))
556 (uffi:def-function "clsql_mysql_num_rows"
557 ((res (* mysql-mysql-res))
558 (p-high32 (* :unsigned-int)))
559 :module "clsql-mysql"
560 :returning :unsigned-int)
563 (uffi:def-foreign-type mysql-stmt-ptr :pointer-void)
566 (uffi:def-function "mysql_stmt_init"
567 ((res (* mysql-mysql-res)))
568 :module "clsql-mysql"
569 :returning mysql-stmt-ptr)
572 (uffi:def-function "mysql_stmt_prepare"
573 ((stmt mysql-stmt-ptr)
575 (length :unsigned-long))
576 :module "clsql-mysql"
580 (uffi:def-function "mysql_stmt_param_count"
581 ((stmt mysql-stmt-ptr))
582 :module "clsql-mysql"
583 :returning :unsigned-int)
586 (uffi:def-function "mysql_stmt_bind_param"
587 ((stmt mysql-stmt-ptr)
588 (bind (* mysql-bind)))
589 :module "clsql-mysql"
593 (uffi:def-function "mysql_stmt_bind_result"
594 ((stmt mysql-stmt-ptr)
595 (bind (* mysql-bind)))
596 :module "clsql-mysql"
600 (uffi:def-function "mysql_stmt_result_metadata"
601 ((stmt mysql-stmt-ptr))
602 :module "clsql-mysql"
603 :returning (* mysql-mysql-res))
607 (uffi:def-function "mysql_stmt_execute"
608 ((stmt mysql-stmt-ptr))
609 :module "clsql-mysql"
613 (uffi:def-function "mysql_stmt_store_result"
614 ((stmt mysql-stmt-ptr))
615 :module "clsql-mysql"
619 (uffi:def-function "mysql_stmt_fetch"
620 ((stmt mysql-stmt-ptr))
621 :module "clsql-mysql"
625 (uffi:def-function "mysql_stmt_free_result"
626 ((stmt mysql-stmt-ptr))
627 :module "clsql-mysql"
631 (uffi:def-function "mysql_stmt_close"
632 ((stmt mysql-stmt-ptr))
633 :module "clsql-mysql"
637 (uffi:def-function "mysql_stmt_errno"
638 ((stmt mysql-stmt-ptr))
639 :module "clsql-mysql"
640 :returning :unsigned-int)
643 (uffi:def-function "mysql_stmt_error"
644 ((stmt mysql-stmt-ptr))
645 :module "clsql-mysql"
649 ;;;; Equivalents of C Macro definitions for accessing various fields
650 ;;;; in the internal MySQL Datastructures
653 (declaim (inline mysql-num-rows))
654 (defun mysql-num-rows (res)
655 (uffi:with-foreign-object (p-high32 :unsigned-int)
656 (let ((low32 (clsql-mysql-num-rows res p-high32))
657 (high32 (uffi:deref-pointer p-high32 :unsigned-int)))
660 (make-64-bit-integer high32 low32)))))
662 (uffi:def-function "clsql_mysql_affected_rows"
663 ((mysql (* mysql-mysql))
664 (p-high32 (* :unsigned-int)))
665 :returning :unsigned-int
666 :module "clsql-mysql")
668 (defun mysql-affected-rows (mysql)
669 (uffi:with-foreign-object (p-high32 :unsigned-int)
670 (let ((low32 (clsql-mysql-affected-rows mysql p-high32))
671 (high32 (uffi:deref-pointer p-high32 :unsigned-int)))
674 (make-64-bit-integer high32 low32)))))
676 (uffi:def-function "clsql_mysql_insert_id"
677 ((res (* mysql-mysql))
678 (p-high32 (* :unsigned-int)))
679 :returning :unsigned-int
680 :module "clsql-mysql")
682 (defun mysql-insert-id (mysql)
683 (uffi:with-foreign-object (p-high32 :unsigned-int)
684 (let ((low32 (clsql-mysql-insert-id mysql p-high32))
685 (high32 (uffi:deref-pointer p-high32 :unsigned-int)))
688 (make-64-bit-integer high32 low32)))))
691 (declaim (inline mysql-num-fields))
692 (uffi:def-function "mysql_num_fields"
693 ((res (* mysql-mysql-res)))
694 :returning :unsigned-int
697 (declaim (inline clsql-mysql-eof))
698 (uffi:def-function ("mysql_eof" clsql-mysql-eof)
699 ((res (* mysql-mysql-res)))
703 (declaim (inline mysql-eof))
704 (defun mysql-eof (res)
705 (if (zerop (clsql-mysql-eof res))
709 (declaim (inline mysql-error))
710 (uffi:def-function ("mysql_error" mysql-error)
711 ((mysql (* mysql-mysql)))
715 (declaim (inline mysql-error-string))
716 (defun mysql-error-string (mysql)
717 (uffi:convert-from-cstring (mysql-error mysql)))
719 (declaim (inline mysql-errno))
720 (uffi:def-function "mysql_errno"
721 ((mysql (* mysql-mysql)))
722 :returning :unsigned-int
725 (declaim (inline mysql-info))
726 (uffi:def-function ("mysql_info" mysql-info)
727 ((mysql (* mysql-mysql)))
731 (declaim (inline mysql-info-string))
732 (defun mysql-info-string (mysql)
733 (uffi:convert-from-cstring (mysql-info mysql)))
735 (declaim (inline clsql-mysql-data-seek))
736 (uffi:def-function "clsql_mysql_data_seek"
737 ((res (* mysql-mysql-res))
738 (offset-high32 :unsigned-int)
739 (offset-low32 :unsigned-int))
740 :module "clsql-mysql"
743 (defun mysql-data-seek (res offset)
744 (multiple-value-bind (high32 low32) (split-64-bit-integer offset)
745 (clsql-mysql-data-seek res high32 low32)))