1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: mysql-sql.lisp
6 ;;;; Purpose: High-level MySQL interface using UFFI
7 ;;;; Date Started: Feb 2002
9 ;;;; This file, part of CLSQL, is Copyright (c) 2002-2009 by Kevin M. Rosenberg
11 ;;;; CLSQL users are granted the rights to distribute and use this software
12 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
13 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
14 ;;;; *************************************************************************
16 (defpackage #:clsql-mysql
17 (:use #:common-lisp #:clsql-sys #:mysql #:clsql-uffi)
18 (:export #:mysql-database)
19 (:documentation "This is the CLSQL interface to MySQL."))
21 (in-package #:clsql-mysql)
23 ;; if we have :sb-unicode, UFFI will treat :cstring as a UTF-8 string
24 (defun expression-length (query-expression)
25 (length #+sb-unicode (sb-ext:string-to-octets query-expression
26 :external-format :utf8)
27 #-sb-unicode query-expression))
29 ;;; Field conversion functions
31 (defun result-field-names (res-ptr)
33 (mysql-field-seek res-ptr 0)
35 (let ((field (mysql-fetch-field res-ptr)))
36 (when (uffi:null-pointer-p field) (return))
37 (push (uffi:convert-from-cstring (clsql-mysql-field-name field)) names)))
40 (defun make-type-list-for-auto (res-ptr)
41 (let ((new-types '()))
42 (mysql-field-seek res-ptr 0)
44 (let ((field (mysql-fetch-field res-ptr)))
45 (when (uffi:null-pointer-p field) (return))
46 (let* ((flags (clsql-mysql-field-flags field))
47 (unsigned (plusp (logand flags 32)))
48 (type (clsql-mysql-field-type field)))
51 ((#.mysql-field-types#tiny
52 #.mysql-field-types#short
53 #.mysql-field-types#int24)
57 (#.mysql-field-types#long
61 (#.mysql-field-types#longlong
65 ((#.mysql-field-types#double
66 #.mysql-field-types#float
67 #.mysql-field-types#decimal)
72 (nreverse new-types)))
74 (defun canonicalize-types (types res-ptr)
76 (let ((auto-list (make-type-list-for-auto res-ptr)))
79 (canonicalize-type-list types auto-list))
85 (defmethod database-initialize-database-type ((database-type (eql :mysql)))
88 ;;(uffi:def-type mysql-mysql-ptr-def (* mysql-mysql))
89 ;;(uffi:def-type mysql-mysql-res-ptr-def (* mysql-mysql-res))
90 (uffi:def-type mysql-mysql-ptr-def mysql-mysql)
91 (uffi:def-type mysql-mysql-res-ptr-def mysql-mysql-res)
92 (uffi:def-type mysql-row-def mysql-row)
94 (defclass mysql-database (database)
95 ((mysql-ptr :accessor database-mysql-ptr :initarg :mysql-ptr
96 :type mysql-mysql-ptr-def)
97 (server-info :accessor database-server-info :initarg :server-info
100 (defmethod database-type ((database mysql-database))
103 (defmethod database-name-from-spec (connection-spec (database-type (eql :mysql)))
104 (check-connection-spec connection-spec database-type
105 (host db user password &optional port))
106 (destructuring-bind (host db user password &optional port) connection-spec
107 (declare (ignore password))
111 (pathname (namestring host))
117 (integer (write-to-string port))
122 (defmethod database-connect (connection-spec (database-type (eql :mysql)))
123 (check-connection-spec connection-spec database-type
124 (host db user password &optional port))
125 (destructuring-bind (host db user password &optional port) connection-spec
126 (let ((mysql-ptr (mysql-init (uffi:make-null-pointer 'mysql-mysql)))
128 (if (uffi:null-pointer-p mysql-ptr)
129 (error 'sql-connection-error
130 :database-type database-type
131 :connection-spec connection-spec
132 :error-id (mysql-errno mysql-ptr)
133 :message (mysql-error-string mysql-ptr))
134 (uffi:with-cstrings ((host-native host)
136 (password-native password)
138 (socket-native socket))
139 (let ((error-occurred nil))
141 (if (uffi:null-pointer-p
143 mysql-ptr host-native user-native password-native
148 (string (parse-integer port)))
151 (setq error-occurred t)
152 (error 'sql-connection-error
153 :database-type database-type
154 :connection-spec connection-spec
155 :error-id (mysql-errno mysql-ptr)
156 :message (mysql-error-string mysql-ptr)))
158 (make-instance 'mysql-database
159 :name (database-name-from-spec connection-spec
161 :database-type :mysql
162 :connection-spec connection-spec
163 :server-info (uffi:convert-from-cstring
164 (mysql:mysql-get-server-info mysql-ptr))
165 :mysql-ptr mysql-ptr))
166 (cmd "SET SESSION sql_mode='ANSI'"))
167 (uffi:with-cstring (cmd-cs cmd)
168 (if (zerop (mysql-real-query mysql-ptr cmd-cs (expression-length cmd)))
171 (warn "Error setting ANSI mode for MySQL.")
173 (when error-occurred (mysql-close mysql-ptr)))))))))
176 (defmethod database-disconnect ((database mysql-database))
177 (mysql-close (database-mysql-ptr database))
178 (setf (database-mysql-ptr database) nil)
181 (defmethod database-execute-command (sql-expression (database mysql-database))
182 (uffi:with-cstring (sql-native sql-expression)
183 (let ((mysql-ptr (database-mysql-ptr database)))
184 (declare (type mysql-mysql-ptr-def mysql-ptr))
185 (if (zerop (mysql-real-query mysql-ptr sql-native
186 (expression-length sql-expression)))
188 (error 'sql-database-data-error
190 :expression sql-expression
191 :error-id (mysql-errno mysql-ptr)
192 :message (mysql-error-string mysql-ptr))))))
195 (defmethod database-query (query-expression (database mysql-database)
196 result-types field-names)
197 (declare (optimize (speed 3) (safety 0) (debug 0) (space 0)))
198 (let ((mysql-ptr (database-mysql-ptr database)))
199 (declare (type mysql-mysql-ptr-def mysql-ptr))
200 (when (database-execute-command query-expression database)
201 (let ((res-ptr (mysql-use-result mysql-ptr)))
202 (declare (type mysql-mysql-res-ptr-def res-ptr))
203 (if (and res-ptr (not (uffi:null-pointer-p res-ptr)))
205 (let ((num-fields (mysql-num-fields res-ptr)))
206 (declare (fixnum num-fields))
207 (setq result-types (canonicalize-types
208 result-types res-ptr))
210 (loop for row = (mysql-fetch-row res-ptr)
211 for lengths = (mysql-fetch-lengths res-ptr)
212 until (uffi:null-pointer-p row)
214 (do* ((rlist (make-list num-fields))
216 (pos rlist (cdr pos)))
217 ((= i num-fields) rlist)
221 (uffi:deref-array row '(:array
225 (uffi:deref-array lengths '(:array :unsigned-long)
228 (result-field-names res-ptr))))
229 (mysql-free-result res-ptr))
230 (unless (zerop (mysql-errno mysql-ptr))
231 ;;from http://dev.mysql.com/doc/refman/5.0/en/mysql-field-count.html
232 ;; if mysql_use_result or mysql_store_result return a null ptr,
233 ;; we use a mysql_errno check to see if it had a problem or just
234 ;; was a query without a result. If no error, just return nil.
235 (error 'sql-database-data-error
237 :expression query-expression
238 :error-id (mysql-errno mysql-ptr)
239 :message (mysql-error-string mysql-ptr))))))))
242 (defstruct mysql-result-set
243 (res-ptr (uffi:make-null-pointer 'mysql-mysql-res) :type mysql-mysql-res-ptr-def)
244 (types nil :type list)
245 (num-fields 0 :type fixnum)
246 (full-set nil :type boolean))
249 (defmethod database-query-result-set ((query-expression string)
250 (database mysql-database)
251 &key full-set result-types)
252 (let ((mysql-ptr (database-mysql-ptr database)))
253 (declare (type mysql-mysql-ptr-def mysql-ptr))
254 (when (database-execute-command query-expression database)
255 (let ((res-ptr (if full-set
256 (mysql-store-result mysql-ptr)
257 (mysql-use-result mysql-ptr))))
258 (declare (type mysql-mysql-res-ptr-def res-ptr))
259 (if (not (uffi:null-pointer-p res-ptr))
260 (let* ((num-fields (mysql-num-fields res-ptr))
261 (result-set (make-mysql-result-set
263 :num-fields num-fields
267 result-types res-ptr))))
271 (mysql-num-rows res-ptr))
274 (error 'sql-database-data-error
276 :expression query-expression
277 :error-id (mysql-errno mysql-ptr)
278 :message (mysql-error-string mysql-ptr))))
281 (defmethod database-dump-result-set (result-set (database mysql-database))
282 (mysql-free-result (mysql-result-set-res-ptr result-set))
286 (defmethod database-store-next-row (result-set (database mysql-database) list)
287 (let* ((res-ptr (mysql-result-set-res-ptr result-set))
288 (row (mysql-fetch-row res-ptr))
289 (lengths (mysql-fetch-lengths res-ptr))
290 (types (mysql-result-set-types result-set)))
291 (declare (type mysql-mysql-res-ptr-def res-ptr)
292 (type mysql-row-def row))
293 (unless (uffi:null-pointer-p row)
294 (loop for i from 0 below (mysql-result-set-num-fields result-set)
299 (uffi:deref-array row '(:array (* :unsigned-char)) i)
302 (uffi:deref-array lengths '(:array :unsigned-long) i))))
306 ;; Table and attribute introspection
308 (defmethod database-list-tables ((database mysql-database) &key (owner nil))
309 (declare (ignore owner))
311 ((eql #\5 (char (database-server-info database) 0))
312 (loop for (name type) in (database-query "SHOW FULL TABLES" database nil nil)
313 when (and (string-equal type "base table")
314 (not (and (>= (length name) 11)
315 (string-equal (subseq name 0 11) "_CLSQL_SEQ_"))))
318 (remove-if #'(lambda (s)
319 (and (>= (length s) 11)
320 (string-equal (subseq s 0 11) "_CLSQL_SEQ_")))
321 (mapcar #'car (database-query "SHOW TABLES" database nil nil))))))
323 (defmethod database-list-views ((database mysql-database)
325 (declare (ignore owner))
327 ((eql #\5 (char (database-server-info database) 0))
328 (loop for (name type) in (database-query "SHOW FULL TABLES" database nil nil)
329 when (string-equal type "view")
334 (defmethod database-list-indexes ((database mysql-database)
337 (dolist (table (database-list-tables database :owner owner) result)
339 (append (database-list-table-indexes table database :owner owner)
342 (defmethod database-list-table-indexes (table (database mysql-database)
344 (declare (ignore owner))
346 (rows (database-query
347 (format nil "SHOW INDEX FROM ~A" table)
350 ((null rows) (nreverse results))
351 (let ((col (nth 2 (car rows))))
352 (unless (find col results :test #'string-equal)
353 (push col results)))))
355 (defmethod database-list-attributes ((table string) (database mysql-database)
357 (declare (ignore owner))
360 (format nil "SHOW COLUMNS FROM ~A" table)
363 (defmethod database-attribute-type (attribute (table string)
364 (database mysql-database)
366 (declare (ignore owner))
367 (let ((row (car (database-query
369 "SHOW COLUMNS FROM ~A LIKE '~A'" table attribute)
371 (let* ((raw-type (second row))
373 (start-length (position #\( raw-type))
374 (type (if start-length
375 (subseq raw-type 0 start-length)
377 (length (when start-length
378 (parse-integer (subseq raw-type (1+ start-length))
381 (values (ensure-keyword type) length nil (if (string-equal null "YES") 1 0))))))
383 ;;; Sequence functions
385 (defun %sequence-name-to-table (sequence-name)
386 (concatenate 'string "_CLSQL_SEQ_" (sql-escape sequence-name)))
388 (defun %table-name-to-sequence-name (table-name)
389 (and (>= (length table-name) 11)
390 (string-equal (subseq table-name 0 11) "_CLSQL_SEQ_")
391 (subseq table-name 11)))
393 (defmethod database-create-sequence (sequence-name
394 (database mysql-database))
395 (let ((table-name (%sequence-name-to-table sequence-name)))
396 (database-execute-command
397 (concatenate 'string "CREATE TABLE " table-name
398 " (id int NOT NULL PRIMARY KEY AUTO_INCREMENT)")
400 (database-execute-command
401 (concatenate 'string "INSERT INTO " table-name
405 (defmethod database-drop-sequence (sequence-name
406 (database mysql-database))
407 (database-execute-command
408 (concatenate 'string "DROP TABLE " (%sequence-name-to-table sequence-name))
411 (defmethod database-list-sequences ((database mysql-database)
413 (declare (ignore owner))
414 (mapcan #'(lambda (s)
415 (let ((sn (%table-name-to-sequence-name (car s))))
417 (database-query "SHOW TABLES" database nil nil)))
419 (defmethod database-set-sequence-position (sequence-name
421 (database mysql-database))
422 (database-execute-command
423 (format nil "UPDATE ~A SET id=~A" (%sequence-name-to-table sequence-name)
426 (mysql:mysql-insert-id (clsql-mysql::database-mysql-ptr database)))
428 (defmethod database-sequence-next (sequence-name (database mysql-database))
430 (database-execute-command
431 (concatenate 'string "UPDATE " (%sequence-name-to-table sequence-name)
432 " SET id=LAST_INSERT_ID(id+1)")
434 (mysql:mysql-insert-id (clsql-mysql::database-mysql-ptr database))))
436 (defmethod database-sequence-last (sequence-name (database mysql-database))
438 (caar (database-query
439 (concatenate 'string "SELECT id from "
440 (%sequence-name-to-table sequence-name))
441 database :auto nil))))
443 (defmethod database-create (connection-spec (type (eql :mysql)))
444 (destructuring-bind (host name user password) connection-spec
445 (let ((database (database-connect (list host "" user password)
447 (setf (slot-value database 'clsql-sys::state) :open)
449 (database-execute-command (format nil "create database ~A" name) database)
450 (database-disconnect database)))))
452 (defmethod database-destroy (connection-spec (type (eql :mysql)))
453 (destructuring-bind (host name user password) connection-spec
454 (let ((database (database-connect (list host "" user password)
456 (setf (slot-value database 'clsql-sys::state) :open)
458 (database-execute-command (format nil "drop database ~A" name) database)
459 (database-disconnect database)))))
461 (defmethod database-probe (connection-spec (type (eql :mysql)))
462 (when (find (second connection-spec) (database-list connection-spec type)
463 :test #'string-equal)
466 (defmethod database-list (connection-spec (type (eql :mysql)))
467 (destructuring-bind (host name user password &optional port) connection-spec
468 (let ((database (database-connect (list host (or name "mysql")
469 user password port) type)))
472 (setf (slot-value database 'clsql-sys::state) :open)
473 (mapcar #'car (database-query "show databases" database :auto nil)))
475 (database-disconnect database)
476 (setf (slot-value database 'clsql-sys::state) :closed))))))
479 ;;; Prepared statements
481 (defclass mysql-stmt ()
482 ((database :initarg :database :reader database)
483 (stmt :initarg :stmt :accessor stmt)
484 (input-bind :initarg :input-bind :reader input-bind)
485 (output-bind :initarg :output-bind :reader output-bind)
486 (types :initarg :types :reader types)
487 (result-set :initarg :result-set :reader result-set)
488 (num-fields :initarg :num-fields :reader num-fields)
489 (field-names :initarg :field-names :accessor stmt-field-names)
490 (length-ptr :initarg :length-ptr :reader length-ptr)
491 (is-null-ptr :initarg :is-null-ptr :reader is-null-ptr)
492 (result-types :initarg :result-types :reader result-types)))
494 (defun clsql-type->mysql-type (type)
496 ((in type :null) mysql-field-types#null)
497 ((in type :int :integer) mysql-field-types#long)
498 ((in type :short) mysql-field-types#short)
499 ((in type :bigint) mysql-field-types#longlong)
500 ((in type :float :double :number) mysql-field-types#double)
501 ((and (consp type) (in (car type) :char :string :varchar)) mysql-field-types#var-string)
502 ((or (eq type :blob) (and (consp type) (in (car type) :blob))) mysql-field-types#var-string)
504 (error 'sql-user-error
506 (format nil "Unknown clsql type ~A." type)))))
509 (defmethod database-prepare (sql-stmt types (database mysql-database) result-types field-names)
510 (let* ((mysql-ptr (database-mysql-ptr database))
511 (stmt (mysql-stmt-init mysql-ptr)))
512 (when (uffi:null-pointer-p stmt)
513 (error 'sql-database-error
514 :error-id (mysql-errno mysql-ptr)
515 :message (mysql-error-string mysql-ptr)))
517 (uffi:with-cstring (native-query sql-stmt)
518 (unless (zerop (mysql-stmt-prepare stmt native-query (expression-length sql-stmt)))
519 (mysql-stmt-close stmt)
520 (error 'sql-database-error
521 :error-id (mysql-errno mysql-ptr)
522 :message (mysql-error-string mysql-ptr))))
524 (unless (= (mysql-stmt-param-count stmt) (length types))
525 (mysql-stmt-close stmt)
526 (error 'sql-database-error
528 (format nil "Mysql param count (~D) does not match number of types (~D)"
529 (mysql-stmt-param-count stmt) (length types))))
531 (let ((rs (mysql-stmt-result-metadata stmt)))
532 (when (uffi:null-pointer-p rs)
533 (warn "mysql_stmt_result_metadata returned NULL")
535 (mysql-stmt-close stmt)
537 (error 'sql-database-error
538 :message "mysql_stmt_result_metadata returned NULL"))
540 (let ((input-bind (uffi:allocate-foreign-object 'mysql-bind (length types)))
541 (mysql-types (mapcar 'clsql-type->mysql-type types))
542 field-vec num-fields is-null-ptr output-bind length-ptr)
545 (dotimes (i (length types))
546 (let* ((binding (uffi:deref-array input-bind '(:array mysql-bind) i)))
547 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-type)
549 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-length) 0)))
552 (unless (uffi:null-pointer-p rs)
553 (setq field-vec (mysql-fetch-fields rs)
554 num-fields (mysql-num-fields rs)
555 is-null-ptr (uffi:allocate-foreign-object :byte num-fields)
556 output-bind (uffi:allocate-foreign-object 'mysql-bind num-fields)
557 length-ptr (uffi:allocate-foreign-object :unsigned-long num-fields))
558 (dotimes (i num-fields)
560 (let* ((field (uffi:deref-array field-vec '(:array mysql-field) i))
561 (type (uffi:get-slot-value field 'mysql-field 'type))
562 (binding (uffi:deref-array output-bind '(:array mysql-bind) i)))
563 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-type) type)
565 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-length) 0)
566 #+need-to-allocate-foreign-object-for-this
567 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::is-null)
568 (+ i (uffi:pointer-address is-null-ptr)))
569 #+need-to-allocate-foreign-object-for-this
570 (setf (uffi:get-slot-value binding 'mysql-bind 'length)
571 (+ (* i 8) (uffi:pointer-address length-ptr)))
574 ((#.mysql-field-types#var-string #.mysql-field-types#string
575 #.mysql-field-types#tiny-blob #.mysql-field-types#blob
576 #.mysql-field-types#medium-blob #.mysql-field-types#long-blob)
577 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-length) 1024)
578 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
579 (uffi:allocate-foreign-object :unsigned-char 1024)))
580 (#.mysql-field-types#tiny
581 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
582 (uffi:allocate-foreign-object :byte)))
583 (#.mysql-field-types#short
584 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
585 (uffi:allocate-foreign-object :short)))
586 (#.mysql-field-types#long
587 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
588 ;; segfaults if supply :int on amd64
589 (uffi:allocate-foreign-object :long)))
591 (#.mysql-field-types#longlong
592 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
593 (uffi:allocate-foreign-object :long)))
594 (#.mysql-field-types#float
595 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
596 (uffi:allocate-foreign-object :float)))
597 (#.mysql-field-types#double
598 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
599 (uffi:allocate-foreign-object :double)))
600 ((#.mysql-field-types#time #.mysql-field-types#date
601 #.mysql-field-types#datetime #.mysql-field-types#timestamp)
602 (uffi:allocate-foreign-object 'mysql-time))
604 (error "mysql type ~D not supported." type)))))
606 (unless (zerop (mysql-stmt-bind-result stmt output-bind))
607 (mysql-stmt-close stmt)
608 (error 'sql-database-error
609 :error-id (mysql-stmt-errno stmt)
610 :message (uffi:convert-from-cstring
611 (mysql-stmt-error stmt)))))
613 (make-instance 'mysql-stmt
616 :num-fields num-fields
617 :input-bind input-bind
618 :output-bind output-bind
620 :result-types result-types
621 :length-ptr length-ptr
622 :is-null-ptr is-null-ptr
624 :field-names field-names)))))
627 (defmethod database-bind-parameter ((stmt mysql-stmt) position value)
628 ;; FIXME: will need to allocate bind structure. This should probably be
629 ;; done in C since the API is not mature and may change
630 (let ((binding (uffi:deref-array (input-bind stmt) '(:array mysql-bind) (1- position)))
631 (type (nth (1- position) (types stmt))))
632 (setf (uffi:get-slot-value binding 'mysql-bind 'length) 0)
635 (when (is-null-ptr stmt)
636 (setf (uffi:deref-array (is-null-ptr stmt) '(:array :byte) (1- position)) 1)))
638 (when (is-null-ptr stmt)
639 (setf (uffi:deref-array (is-null-ptr stmt) '(:array :byte) (1- position)) 0))
641 (#.mysql-field-types#long
642 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer) value))
644 (warn "Unknown input bind type ~D." type))
648 (defmethod database-run-prepared ((stmt mysql-stmt))
650 (when (input-bind stmt)
651 (unless (zerop (mysql-stmt-bind-param (stmt stmt) (input-bind stmt)))
652 (error 'sql-database-error
653 :error-id (mysql-stmt-errno (stmt stmt))
654 :message (uffi:convert-from-cstring
655 (mysql-stmt-error (stmt stmt))))))
657 (unless (zerop (mysql-stmt-execute (stmt stmt)))
658 (error 'sql-database-error
659 :error-id (mysql-stmt-errno (stmt stmt))
660 :message (uffi:convert-from-cstring
661 (mysql-stmt-error (stmt stmt)))))
663 (unless (zerop (mysql-stmt-store-result (stmt stmt)))
664 (error 'sql-database-error
665 :error-id (mysql-stmt-errno (stmt stmt))
666 :message (uffi:convert-from-cstring
667 (mysql-stmt-error (stmt stmt)))))
668 (database-fetch-prepared-rows stmt))
671 (defun database-fetch-prepared-rows (stmt)
672 (do ((rc (mysql-stmt-fetch (stmt stmt)) (mysql-stmt-fetch (stmt stmt)))
673 (num-fields (num-fields stmt))
675 ((not (zerop rc)) (nreverse rows))
677 (loop for i from 0 below num-fields
680 (not (zerop (uffi:ensure-char-integer
681 (uffi:deref-array (is-null-ptr stmt) '(:array :byte) i))))))
683 (let* ((bind (uffi:deref-array (output-bind stmt) '(:array mysql-bind) i))
684 (type (uffi:get-slot-value bind 'mysql-bind 'mysql::buffer-type))
685 (buffer (uffi:get-slot-value bind 'mysql-bind 'mysql::buffer)))
687 ((#.mysql-field-types#var-string #.mysql-field-types#string
688 #.mysql-field-types#tiny-blob #.mysql-field-types#blob
689 #.mysql-field-types#medium-blob #.mysql-field-types#long-blob)
690 (uffi:convert-from-foreign-string buffer))
691 (#.mysql-field-types#tiny
692 (uffi:ensure-char-integer
693 (uffi:deref-pointer buffer :byte)))
694 (#.mysql-field-types#short
695 (uffi:deref-pointer buffer :short))
696 (#.mysql-field-types#long
697 (uffi:deref-pointer buffer :int))
699 (#.mysql-field-types#longlong
700 (uffi:deref-pointer buffer :long))
701 (#.mysql-field-types#float
702 (uffi:deref-pointer buffer :float))
703 (#.mysql-field-types#double
704 (uffi:deref-pointer buffer :double))
705 ((#.mysql-field-types#time #.mysql-field-types#date
706 #.mysql-field-types#datetime #.mysql-field-types#timestamp)
707 (let ((year (uffi:get-slot-value buffer 'mysql-time 'mysql::year))
708 (month (uffi:get-slot-value buffer 'mysql-time 'mysql::month))
709 (day (uffi:get-slot-value buffer 'mysql-time 'mysql::day))
710 (hour (uffi:get-slot-value buffer 'mysql-time 'mysql::hour))
711 (minute (uffi:get-slot-value buffer 'mysql-time 'mysql::minute))
712 (second (uffi:get-slot-value buffer 'mysql-time 'mysql::second)))
714 (make-time :year year :month month :day day :hour hour
715 :minute minute :second second))))
724 (defmethod database-free-prepared ((stmt mysql-stmt))
725 (with-slots (stmt) stmt
726 (mysql-stmt-close stmt))
730 ;;; Database capabilities
732 (defmethod db-type-use-column-on-drop-index? ((db-type (eql :mysql)))
735 (defmethod db-type-has-views? ((db-type (eql :mysql)))
737 #-mysql-client-v5 nil)
739 (defmethod db-type-has-subqueries? ((db-type (eql :mysql)))
740 #+(or mysql-client-v4.1 mysql-client-v5) t
741 #-(or mysql-client-v4.1 mysql-client-v5) nil)
743 (defmethod db-type-has-boolean-where? ((db-type (eql :mysql)))
744 #+(or mysql-client-v4.1 mysql-client-v5) t
745 #-(or mysql-client-v4.1 mysql-client-v5) nil)
747 (defmethod db-type-has-union? ((db-type (eql :mysql)))
748 (not (eql (schar mysql::*mysql-client-info* 0) #\3)))
750 (defmethod db-type-transaction-capable? ((db-type (eql :mysql)) database)
751 (let ((tuple (car (database-query "SHOW VARIABLES LIKE 'HAVE_INNODB'" database :auto nil))))
752 (and tuple (string-equal "YES" (second tuple)))))
754 (defmethod db-type-has-prepared-stmt? ((db-type (eql :mysql)))
755 #+(or mysql-client-v4.1 mysql-client-v5) t
756 #-(or mysql-client-v4.1 mysql-client-v5) nil)
758 (defmethod db-type-has-auto-increment? ((db-type (eql :mysql)))
761 (when (clsql-sys:database-type-library-loaded :mysql)
762 (clsql-sys:initialize-database-type :database-type :mysql))