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
109 #-(or mysql-client-v3 mysql-client-v4)
110 (eval-when (:compile-toplevel :load-toplevel :execute)
111 (pushnew :mysql-client-v3 cl:*features*))
114 (uffi:def-struct mysql-field
118 (type mysql-field-types)
119 (length :unsigned-int)
120 (max-length :unsigned-int)
121 (flags :unsigned-int)
122 (decimals :unsigned-int))
124 ;; structure changed in mysql 4 client
126 (uffi:def-struct mysql-field
129 (org_table (* :char))
132 (length :unsigned-int)
133 (max-length :unsigned-int)
134 (flags :unsigned-int)
135 (decimals :unsigned-int)
136 (type mysql-field-types))
140 (uffi:def-array-pointer mysql-row (* :unsigned-char))
142 (uffi:def-array-pointer mysql-field-vector (* mysql-field))
144 (uffi:def-foreign-type mysql-field-offset :unsigned-int)
146 (uffi:def-struct mysql-rows
150 (uffi:def-foreign-type mysql-row-offset (:struct-pointer mysql-rows))
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)))
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)
167 (init-command (* :char))
170 (unix-socket (* :char))
172 (my-cnf-file (* :char))
173 (my-cnf-group (* :char))
174 (charset-dir (* :char))
175 (charset-name (* :char))
180 (ssl-capath (* :char)))
182 (uffi:def-enum mysql-option
188 :read-default-group))
190 (uffi:def-enum mysql-status
195 (uffi:def-struct mysql-mysql
196 (net (:struct mysql-net))
197 (connected-fd (* :char))
201 (unix-socket (* :char))
202 (server-version (* :char))
203 (host-info (* :char))
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))
224 (reconnect mysql-bool)
225 (options (:struct mysql-options))
226 (scramble-buff (:array :char 9))
227 (charset :pointer-void)
228 (server-language :unsigned-int))
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))
242 (current-row mysql-row)
243 (lengths (* :unsigned-long))
244 (handle (:struct-pointer mysql-mysql))
247 ;;;; The Foreign C routines
248 (declaim (inline mysql-init))
249 (uffi:def-function "mysql_init"
250 ((mysql (* mysql-mysql)))
252 :returning (* mysql-mysql))
254 (declaim (inline mysql-connect))
255 (uffi:def-function "mysql_connect"
256 ((mysql (* mysql-mysql))
261 :returning (* mysql-mysql))
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))
273 (unix-socket :cstring)
274 (clientflag :unsigned-int))
276 :returning (* mysql-mysql))
278 (declaim (inline mysql-close))
279 (uffi:def-function "mysql_close"
280 ((sock (* mysql-mysql)))
284 (declaim (inline mysql-select-db))
285 (uffi:def-function "mysql_select_db"
286 ((mysql (* mysql-mysql))
291 (declaim (inline mysql-query))
292 (uffi:def-function "mysql_query"
293 ((mysql (* mysql-mysql))
298 ;;; I doubt that this function is really useful for direct Lisp usage,
299 ;;; but it is here for completeness...
301 (declaim (inline mysql-real-query))
302 (uffi:def-function "mysql_real_query"
303 ((mysql (* mysql-mysql))
305 (length :unsigned-int))
309 (declaim (inline mysql-create-db))
310 (uffi:def-function "mysql_create_db"
311 ((mysql (* mysql-mysql))
316 (declaim (inline mysql-drop-db))
317 (uffi:def-function "mysql_drop_db"
318 ((mysql (* mysql-mysql))
323 (declaim (inline mysql-shutdown))
324 (uffi:def-function "mysql_shutdown"
325 ((mysql (* mysql-mysql)))
329 (declaim (inline mysql-dump-debug-info))
330 (uffi:def-function "mysql_dump_debug_info"
331 ((mysql (* mysql-mysql)))
335 (declaim (inline mysql-refresh))
336 (uffi:def-function "mysql_refresh"
337 ((mysql (* mysql-mysql))
338 (refresh-options :unsigned-int))
342 (declaim (inline mysql-kill))
343 (uffi:def-function "mysql_kill"
344 ((mysql (* mysql-mysql))
345 (pid :unsigned-long))
349 (declaim (inline mysql-ping))
350 (uffi:def-function "mysql_ping"
351 ((mysql (* mysql-mysql)))
355 (declaim (inline mysql-stat))
356 (uffi:def-function "mysql_stat"
357 ((mysql (* mysql-mysql)))
361 (declaim (inline mysql-get-server-info))
362 (uffi:def-function "mysql_get_server_info"
363 ((mysql (* mysql-mysql)))
367 (declaim (inline mysql-get-client-info))
368 (uffi:def-function "mysql_get_client_info"
373 (declaim (inline mysql-get-host-info))
374 (uffi:def-function "mysql_get_host_info"
375 ((mysql (* mysql-mysql)))
379 (declaim (inline mysql-get-proto-info))
380 (uffi:def-function "mysql_get_proto_info"
381 ((mysql (* mysql-mysql)))
383 :returning :unsigned-int)
385 (declaim (inline mysql-list-dbs))
386 (uffi:def-function "mysql_list_dbs"
387 ((mysql (* mysql-mysql))
390 :returning (* mysql-mysql-res))
392 (declaim (inline mysql-list-tables))
393 (uffi:def-function "mysql_list_tables"
394 ((mysql (* mysql-mysql))
397 :returning (* mysql-mysql-res))
399 (declaim (inline mysql-list-fields))
400 (uffi:def-function "mysql_list_fields"
401 ((mysql (* mysql-mysql))
405 :returning (* mysql-mysql-res))
407 (declaim (inline mysql-list-processes))
408 (uffi:def-function "mysql_list_processes"
409 ((mysql (* mysql-mysql)))
411 :returning (* mysql-mysql-res))
413 (declaim (inline mysql-store-result))
414 (uffi:def-function "mysql_store_result"
415 ((mysql (* mysql-mysql)))
417 :returning (* mysql-mysql-res))
419 (declaim (inline mysql-use-result))
420 (uffi:def-function "mysql_use_result"
421 ((mysql (* mysql-mysql)))
423 :returning (* mysql-mysql-res))
425 (declaim (inline mysql-options))
426 (uffi:def-function "mysql_options"
427 ((mysql (* mysql-mysql))
428 (option mysql-option)
433 (declaim (inline mysql-free-result))
434 (uffi:def-function "mysql_free_result"
435 ((res (* mysql-mysql-res)))
439 (declaim (inline mysql-row-seek))
440 (uffi:def-function "mysql_row_seek"
441 ((res (* mysql-mysql-res))
442 (offset mysql-row-offset))
444 :returning mysql-row-offset)
446 (declaim (inline mysql-field-seek))
447 (uffi:def-function "mysql_field_seek"
448 ((res (* mysql-mysql-res))
449 (offset mysql-field-offset))
451 :returning mysql-field-offset)
453 (declaim (inline mysql-fetch-row))
454 (uffi:def-function "mysql_fetch_row"
455 ((res (* mysql-mysql-res)))
457 :returning (* (* :unsigned-char)))
459 (declaim (inline mysql-fetch-lengths))
460 (uffi:def-function "mysql_fetch_lengths"
461 ((res (* mysql-mysql-res)))
463 :returning (* :unsigned-long))
465 (declaim (inline mysql-fetch-field))
466 (uffi:def-function "mysql_fetch_field"
467 ((res (* mysql-mysql-res)))
469 :returning (* mysql-field))
471 (declaim (inline mysql-fetch-fields))
472 (uffi:def-function "mysql_fetch_fields"
473 ((res (* mysql-mysql-res)))
475 :returning (* mysql-field))
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))
482 :returning (* mysql-field))
484 (declaim (inline mysql-escape-string))
485 (uffi:def-function "mysql_escape_string"
488 (length :unsigned-int))
490 :returning :unsigned-int)
492 (declaim (inline mysql-debug))
493 (uffi:def-function "mysql_debug"
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)
506 ;;;; Equivalents of C Macro definitions for accessing various fields
507 ;;;; in the internal MySQL Datastructures
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)))
517 (make-64-bit-integer high32 low32)))))
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")
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)))
531 (make-64-bit-integer high32 low32)))))
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")
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)))
545 (make-64-bit-integer high32 low32)))))
548 (declaim (inline mysql-num-fields))
549 (uffi:def-function "mysql_num_fields"
550 ((res (* mysql-mysql-res)))
551 :returning :unsigned-int
554 (declaim (inline clsql-mysql-eof))
555 (uffi:def-function ("mysql_eof" clsql-mysql-eof)
556 ((res (* mysql-mysql-res)))
560 (declaim (inline mysql-eof))
561 (defun mysql-eof (res)
562 (if (zerop (clsql-mysql-eof res))
566 (declaim (inline mysql-error))
567 (uffi:def-function ("mysql_error" mysql-error)
568 ((mysql (* mysql-mysql)))
572 (declaim (inline mysql-error-string))
573 (defun mysql-error-string (mysql)
574 (uffi:convert-from-cstring (mysql-error mysql)))
576 (declaim (inline mysql-errno))
577 (uffi:def-function "mysql_errno"
578 ((mysql (* mysql-mysql)))
579 :returning :unsigned-int
582 (declaim (inline mysql-info))
583 (uffi:def-function ("mysql_info" mysql-info)
584 ((mysql (* mysql-mysql)))
588 (declaim (inline mysql-info-string))
589 (defun mysql-info-string (mysql)
590 (uffi:convert-from-cstring (mysql-info mysql)))
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"
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)))