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-2010 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 ;;; Field conversion functions
25 (defun result-field-names (res-ptr)
27 (mysql-field-seek res-ptr 0)
29 (let ((field (mysql-fetch-field res-ptr)))
30 (when (uffi:null-pointer-p field) (return))
31 (push (uffi:convert-from-cstring (clsql-mysql-field-name field)) names)))
34 (defun make-type-list-for-auto (res-ptr)
35 (let ((new-types '()))
36 (mysql-field-seek res-ptr 0)
38 (let ((field (mysql-fetch-field res-ptr)))
39 (when (uffi:null-pointer-p field) (return))
40 (let* ((flags (clsql-mysql-field-flags field))
41 (unsigned (plusp (logand flags 32)))
42 (type (clsql-mysql-field-type field)))
45 ((#.mysql-field-types#tiny
46 #.mysql-field-types#short
47 #.mysql-field-types#int24)
51 (#.mysql-field-types#long
55 (#.mysql-field-types#longlong
59 ((#.mysql-field-types#double
60 #.mysql-field-types#float
61 #.mysql-field-types#decimal)
66 (nreverse new-types)))
68 (defun canonicalize-types (types res-ptr)
70 (let ((auto-list (make-type-list-for-auto res-ptr)))
73 (canonicalize-type-list types auto-list))
79 (defmethod database-initialize-database-type ((database-type (eql :mysql)))
82 ;;(uffi:def-type mysql-mysql-ptr-def (* mysql-mysql))
83 ;;(uffi:def-type mysql-mysql-res-ptr-def (* mysql-mysql-res))
84 (uffi:def-type mysql-mysql-ptr-def mysql-mysql)
85 (uffi:def-type mysql-mysql-res-ptr-def mysql-mysql-res)
86 (uffi:def-type mysql-row-def mysql-row)
88 (defclass mysql-database (database)
89 ((mysql-ptr :accessor database-mysql-ptr :initarg :mysql-ptr
90 :type mysql-mysql-ptr-def)
91 (server-info :accessor database-server-info :initarg :server-info
94 (defmethod database-type ((database mysql-database))
97 (defmethod database-name-from-spec (connection-spec (database-type (eql :mysql)))
98 (check-connection-spec connection-spec database-type
99 (host db user password &optional port))
100 (destructuring-bind (host db user password &optional port) connection-spec
101 (declare (ignore password))
105 (pathname (namestring host))
111 (integer (write-to-string port))
116 (defmethod database-connect (connection-spec (database-type (eql :mysql)))
117 (check-connection-spec connection-spec database-type
118 (host db user password &optional port))
119 (destructuring-bind (host db user password &optional port) connection-spec
120 (let ((mysql-ptr (mysql-init (uffi:make-null-pointer 'mysql-mysql)))
122 (if (uffi:null-pointer-p mysql-ptr)
123 (error 'sql-connection-error
124 :database-type database-type
125 :connection-spec connection-spec
126 :error-id (mysql-errno mysql-ptr)
127 :message (mysql-error-string mysql-ptr))
128 (uffi:with-cstrings ((host-native host)
130 (password-native password)
132 (socket-native socket))
133 (let ((error-occurred nil))
135 (if (uffi:null-pointer-p
137 mysql-ptr host-native user-native password-native
142 (string (parse-integer port)))
145 (setq error-occurred t)
146 (error 'sql-connection-error
147 :database-type database-type
148 :connection-spec connection-spec
149 :error-id (mysql-errno mysql-ptr)
150 :message (mysql-error-string mysql-ptr)))
152 (make-instance 'mysql-database
153 :name (database-name-from-spec connection-spec
155 :database-type :mysql
156 :connection-spec connection-spec
157 :server-info (uffi:convert-from-cstring
158 (mysql:mysql-get-server-info mysql-ptr))
159 :mysql-ptr mysql-ptr))
160 (cmd "SET SESSION sql_mode='ANSI'"))
161 (uffi:with-cstring (cmd-cs cmd)
162 (if (zerop (mysql-real-query mysql-ptr cmd-cs (uffi:foreign-encoded-octet-count cmd)))
165 (warn "Error setting ANSI mode for MySQL.")
167 (when error-occurred (mysql-close mysql-ptr)))))))))
170 (defmethod database-disconnect ((database mysql-database))
171 (mysql-close (database-mysql-ptr database))
172 (setf (database-mysql-ptr database) nil)
175 (defmethod database-execute-command (sql-expression (database mysql-database))
176 (uffi:with-cstring (sql-native sql-expression)
177 (let ((mysql-ptr (database-mysql-ptr database)))
178 (declare (type mysql-mysql-ptr-def mysql-ptr))
179 (if (zerop (mysql-real-query mysql-ptr sql-native
180 (uffi:foreign-encoded-octet-count sql-expression)))
182 (error 'sql-database-data-error
184 :expression sql-expression
185 :error-id (mysql-errno mysql-ptr)
186 :message (mysql-error-string mysql-ptr))))))
189 (defmethod database-query (query-expression (database mysql-database)
190 result-types field-names)
191 (declare (optimize (speed 3) (safety 0) (debug 0) (space 0)))
192 (let ((mysql-ptr (database-mysql-ptr database)))
193 (declare (type mysql-mysql-ptr-def mysql-ptr))
194 (when (database-execute-command query-expression database)
195 (let ((res-ptr (mysql-use-result mysql-ptr)))
196 (declare (type mysql-mysql-res-ptr-def res-ptr))
197 (if (and res-ptr (not (uffi:null-pointer-p res-ptr)))
199 (let ((num-fields (mysql-num-fields res-ptr)))
200 (declare (fixnum num-fields))
201 (setq result-types (canonicalize-types
202 result-types res-ptr))
204 (loop for row = (mysql-fetch-row res-ptr)
205 for lengths = (mysql-fetch-lengths res-ptr)
206 until (uffi:null-pointer-p row)
208 (do* ((rlist (make-list num-fields))
210 (pos rlist (cdr pos)))
211 ((= i num-fields) rlist)
215 (uffi:deref-array row '(:array
219 (uffi:deref-array lengths '(:array :unsigned-long)
222 (result-field-names res-ptr))))
223 (mysql-free-result res-ptr))
224 (unless (zerop (mysql-errno mysql-ptr))
225 ;;from http://dev.mysql.com/doc/refman/5.0/en/mysql-field-count.html
226 ;; if mysql_use_result or mysql_store_result return a null ptr,
227 ;; we use a mysql_errno check to see if it had a problem or just
228 ;; was a query without a result. If no error, just return nil.
229 (error 'sql-database-data-error
231 :expression query-expression
232 :error-id (mysql-errno mysql-ptr)
233 :message (mysql-error-string mysql-ptr))))))))
236 (defstruct mysql-result-set
237 (res-ptr (uffi:make-null-pointer 'mysql-mysql-res) :type mysql-mysql-res-ptr-def)
238 (types nil :type list)
239 (num-fields 0 :type fixnum)
240 (full-set nil :type boolean))
243 (defmethod database-query-result-set ((query-expression string)
244 (database mysql-database)
245 &key full-set result-types)
246 (let ((mysql-ptr (database-mysql-ptr database)))
247 (declare (type mysql-mysql-ptr-def mysql-ptr))
248 (when (database-execute-command query-expression database)
249 (let ((res-ptr (if full-set
250 (mysql-store-result mysql-ptr)
251 (mysql-use-result mysql-ptr))))
252 (declare (type mysql-mysql-res-ptr-def res-ptr))
253 (if (not (uffi:null-pointer-p res-ptr))
254 (let* ((num-fields (mysql-num-fields res-ptr))
255 (result-set (make-mysql-result-set
257 :num-fields num-fields
261 result-types res-ptr))))
265 (mysql-num-rows res-ptr))
268 (error 'sql-database-data-error
270 :expression query-expression
271 :error-id (mysql-errno mysql-ptr)
272 :message (mysql-error-string mysql-ptr))))
275 (defmethod database-dump-result-set (result-set (database mysql-database))
276 (mysql-free-result (mysql-result-set-res-ptr result-set))
280 (defmethod database-store-next-row (result-set (database mysql-database) list)
281 (let* ((res-ptr (mysql-result-set-res-ptr result-set))
282 (row (mysql-fetch-row res-ptr))
283 (lengths (mysql-fetch-lengths res-ptr))
284 (types (mysql-result-set-types result-set)))
285 (declare (type mysql-mysql-res-ptr-def res-ptr)
286 (type mysql-row-def row))
287 (unless (uffi:null-pointer-p row)
288 (loop for i from 0 below (mysql-result-set-num-fields result-set)
293 (uffi:deref-array row '(:array (* :unsigned-char)) i)
296 (uffi:deref-array lengths '(:array :unsigned-long) i))))
300 ;; Table and attribute introspection
302 (defmethod database-list-tables ((database mysql-database) &key (owner nil))
303 (declare (ignore owner))
305 ((eql #\5 (char (database-server-info database) 0))
306 (loop for (name type) in (database-query "SHOW FULL TABLES" database nil nil)
307 when (and (string-equal type "base table")
308 (not (and (>= (length name) 11)
309 (string-equal (subseq name 0 11) "_CLSQL_SEQ_"))))
312 (remove-if #'(lambda (s)
313 (and (>= (length s) 11)
314 (string-equal (subseq s 0 11) "_CLSQL_SEQ_")))
315 (mapcar #'car (database-query "SHOW TABLES" database nil nil))))))
317 (defmethod database-list-views ((database mysql-database)
319 (declare (ignore owner))
321 ((eql #\5 (char (database-server-info database) 0))
322 (loop for (name type) in (database-query "SHOW FULL TABLES" database nil nil)
323 when (string-equal type "view")
328 (defmethod database-list-indexes ((database mysql-database)
331 (dolist (table (database-list-tables database :owner owner) result)
333 (append (database-list-table-indexes table database :owner owner)
336 (defmethod database-list-table-indexes (table (database mysql-database)
338 (declare (ignore owner))
340 (rows (database-query
341 (format nil "SHOW INDEX FROM ~A" table)
344 ((null rows) (nreverse results))
345 (let ((col (nth 2 (car rows))))
346 (unless (find col results :test #'string-equal)
347 (push col results)))))
349 (defmethod database-list-attributes ((table string) (database mysql-database)
351 (declare (ignore owner))
354 (format nil "SHOW COLUMNS FROM ~A" table)
357 (defmethod database-attribute-type (attribute (table string)
358 (database mysql-database)
360 (declare (ignore owner))
361 (let ((row (car (database-query
363 "SHOW COLUMNS FROM ~A LIKE '~A'" table attribute)
365 (let* ((raw-type (second row))
367 (start-length (position #\( raw-type))
368 (type (if start-length
369 (subseq raw-type 0 start-length)
371 (length (when start-length
372 (parse-integer (subseq raw-type (1+ start-length))
375 (values (ensure-keyword type) length nil (if (string-equal null "YES") 1 0))))))
377 ;;; Sequence functions
379 (defun %sequence-name-to-table (sequence-name)
380 (concatenate 'string "_CLSQL_SEQ_" (sql-escape sequence-name)))
382 (defun %table-name-to-sequence-name (table-name)
383 (and (>= (length table-name) 11)
384 (string-equal (subseq table-name 0 11) "_CLSQL_SEQ_")
385 (subseq table-name 11)))
387 (defmethod database-create-sequence (sequence-name
388 (database mysql-database))
389 (let ((table-name (%sequence-name-to-table sequence-name)))
390 (database-execute-command
391 (concatenate 'string "CREATE TABLE " table-name
392 " (id int NOT NULL PRIMARY KEY AUTO_INCREMENT)")
394 (database-execute-command
395 (concatenate 'string "INSERT INTO " table-name
399 (defmethod database-drop-sequence (sequence-name
400 (database mysql-database))
401 (database-execute-command
402 (concatenate 'string "DROP TABLE " (%sequence-name-to-table sequence-name))
405 (defmethod database-list-sequences ((database mysql-database)
407 (declare (ignore owner))
408 (mapcan #'(lambda (s)
409 (let ((sn (%table-name-to-sequence-name (car s))))
411 (database-query "SHOW TABLES" database nil nil)))
413 (defmethod database-set-sequence-position (sequence-name
415 (database mysql-database))
416 (database-execute-command
417 (format nil "UPDATE ~A SET id=~A" (%sequence-name-to-table sequence-name)
420 (mysql:mysql-insert-id (clsql-mysql::database-mysql-ptr database)))
422 (defmethod database-sequence-next (sequence-name (database mysql-database))
424 (database-execute-command
425 (concatenate 'string "UPDATE " (%sequence-name-to-table sequence-name)
426 " SET id=LAST_INSERT_ID(id+1)")
428 (mysql:mysql-insert-id (clsql-mysql::database-mysql-ptr database))))
430 (defmethod database-sequence-last (sequence-name (database mysql-database))
432 (caar (database-query
433 (concatenate 'string "SELECT id from "
434 (%sequence-name-to-table sequence-name))
435 database :auto nil))))
437 (defmethod database-create (connection-spec (type (eql :mysql)))
438 (destructuring-bind (host name user password) connection-spec
439 (let ((database (database-connect (list host "" user password)
441 (setf (slot-value database 'clsql-sys::state) :open)
443 (database-execute-command (format nil "create database ~A" name) database)
444 (database-disconnect database)))))
446 (defmethod database-destroy (connection-spec (type (eql :mysql)))
447 (destructuring-bind (host name user password) connection-spec
448 (let ((database (database-connect (list host "" user password)
450 (setf (slot-value database 'clsql-sys::state) :open)
452 (database-execute-command (format nil "drop database ~A" name) database)
453 (database-disconnect database)))))
455 (defmethod database-probe (connection-spec (type (eql :mysql)))
456 (when (find (second connection-spec) (database-list connection-spec type)
457 :test #'string-equal)
460 (defmethod database-list (connection-spec (type (eql :mysql)))
461 (destructuring-bind (host name user password &optional port) connection-spec
462 (let ((database (database-connect (list host (or name "mysql")
463 user password port) type)))
466 (setf (slot-value database 'clsql-sys::state) :open)
467 (mapcar #'car (database-query "show databases" database :auto nil)))
469 (database-disconnect database)
470 (setf (slot-value database 'clsql-sys::state) :closed))))))
473 ;;; Prepared statements
475 (defclass mysql-stmt ()
476 ((database :initarg :database :reader database)
477 (stmt :initarg :stmt :accessor stmt)
478 (input-bind :initarg :input-bind :reader input-bind)
479 (output-bind :initarg :output-bind :reader output-bind)
480 (types :initarg :types :reader types)
481 (result-set :initarg :result-set :reader result-set)
482 (num-fields :initarg :num-fields :reader num-fields)
483 (field-names :initarg :field-names :accessor stmt-field-names)
484 (length-ptr :initarg :length-ptr :reader length-ptr)
485 (is-null-ptr :initarg :is-null-ptr :reader is-null-ptr)
486 (result-types :initarg :result-types :reader result-types)))
488 (defun clsql-type->mysql-type (type)
490 ((in type :null) mysql-field-types#null)
491 ((in type :int :integer) mysql-field-types#long)
492 ((in type :short) mysql-field-types#short)
493 ((in type :bigint) mysql-field-types#longlong)
494 ((in type :float :double :number) mysql-field-types#double)
495 ((and (consp type) (in (car type) :char :string :varchar)) mysql-field-types#var-string)
496 ((or (eq type :blob) (and (consp type) (in (car type) :blob))) mysql-field-types#var-string)
498 (error 'sql-user-error
500 (format nil "Unknown clsql type ~A." type)))))
503 (defmethod database-prepare (sql-stmt types (database mysql-database) result-types field-names)
504 (let* ((mysql-ptr (database-mysql-ptr database))
505 (stmt (mysql-stmt-init mysql-ptr)))
506 (when (uffi:null-pointer-p stmt)
507 (error 'sql-database-error
508 :error-id (mysql-errno mysql-ptr)
509 :message (mysql-error-string mysql-ptr)))
511 (uffi:with-cstring (native-query sql-stmt)
512 (unless (zerop (mysql-stmt-prepare stmt native-query (uffi:foreign-encoded-octet-count sql-stmt)))
513 (mysql-stmt-close stmt)
514 (error 'sql-database-error
515 :error-id (mysql-errno mysql-ptr)
516 :message (mysql-error-string mysql-ptr))))
518 (unless (= (mysql-stmt-param-count stmt) (length types))
519 (mysql-stmt-close stmt)
520 (error 'sql-database-error
522 (format nil "Mysql param count (~D) does not match number of types (~D)"
523 (mysql-stmt-param-count stmt) (length types))))
525 (let ((rs (mysql-stmt-result-metadata stmt)))
526 (when (uffi:null-pointer-p rs)
527 (warn "mysql_stmt_result_metadata returned NULL")
529 (mysql-stmt-close stmt)
531 (error 'sql-database-error
532 :message "mysql_stmt_result_metadata returned NULL"))
534 (let ((input-bind (uffi:allocate-foreign-object 'mysql-bind (length types)))
535 (mysql-types (mapcar 'clsql-type->mysql-type types))
536 field-vec num-fields is-null-ptr output-bind length-ptr)
539 (dotimes (i (length types))
540 (let* ((binding (uffi:deref-array input-bind '(:array mysql-bind) i)))
541 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-type)
543 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-length) 0)))
546 (unless (uffi:null-pointer-p rs)
547 (setq field-vec (mysql-fetch-fields rs)
548 num-fields (mysql-num-fields rs)
549 is-null-ptr (uffi:allocate-foreign-object :byte num-fields)
550 output-bind (uffi:allocate-foreign-object 'mysql-bind num-fields)
551 length-ptr (uffi:allocate-foreign-object :unsigned-long num-fields))
552 (dotimes (i num-fields)
554 (let* ((field (uffi:deref-array field-vec '(:array mysql-field) i))
555 (type (uffi:get-slot-value field 'mysql-field 'type))
556 (binding (uffi:deref-array output-bind '(:array mysql-bind) i)))
557 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-type) type)
559 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-length) 0)
560 #+need-to-allocate-foreign-object-for-this
561 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::is-null)
562 (+ i (uffi:pointer-address is-null-ptr)))
563 #+need-to-allocate-foreign-object-for-this
564 (setf (uffi:get-slot-value binding 'mysql-bind 'length)
565 (+ (* i 8) (uffi:pointer-address length-ptr)))
568 ((#.mysql-field-types#var-string #.mysql-field-types#string
569 #.mysql-field-types#tiny-blob #.mysql-field-types#blob
570 #.mysql-field-types#medium-blob #.mysql-field-types#long-blob)
571 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-length) 1024)
572 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
573 (uffi:allocate-foreign-object :unsigned-char 1024)))
574 (#.mysql-field-types#tiny
575 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
576 (uffi:allocate-foreign-object :byte)))
577 (#.mysql-field-types#short
578 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
579 (uffi:allocate-foreign-object :short)))
580 (#.mysql-field-types#long
581 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
582 ;; segfaults if supply :int on amd64
583 (uffi:allocate-foreign-object :long)))
585 (#.mysql-field-types#longlong
586 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
587 (uffi:allocate-foreign-object :long)))
588 (#.mysql-field-types#float
589 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
590 (uffi:allocate-foreign-object :float)))
591 (#.mysql-field-types#double
592 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
593 (uffi:allocate-foreign-object :double)))
594 ((#.mysql-field-types#time #.mysql-field-types#date
595 #.mysql-field-types#datetime #.mysql-field-types#timestamp)
596 (uffi:allocate-foreign-object 'mysql-time))
598 (error "mysql type ~D not supported." type)))))
600 (unless (zerop (mysql-stmt-bind-result stmt output-bind))
601 (mysql-stmt-close stmt)
602 (error 'sql-database-error
603 :error-id (mysql-stmt-errno stmt)
604 :message (uffi:convert-from-cstring
605 (mysql-stmt-error stmt)))))
607 (make-instance 'mysql-stmt
610 :num-fields num-fields
611 :input-bind input-bind
612 :output-bind output-bind
614 :result-types result-types
615 :length-ptr length-ptr
616 :is-null-ptr is-null-ptr
618 :field-names field-names)))))
621 (defmethod database-bind-parameter ((stmt mysql-stmt) position value)
622 ;; FIXME: will need to allocate bind structure. This should probably be
623 ;; done in C since the API is not mature and may change
624 (let ((binding (uffi:deref-array (input-bind stmt) '(:array mysql-bind) (1- position)))
625 (type (nth (1- position) (types stmt))))
626 (setf (uffi:get-slot-value binding 'mysql-bind 'length) 0)
629 (when (is-null-ptr stmt)
630 (setf (uffi:deref-array (is-null-ptr stmt) '(:array :byte) (1- position)) 1)))
632 (when (is-null-ptr stmt)
633 (setf (uffi:deref-array (is-null-ptr stmt) '(:array :byte) (1- position)) 0))
635 (#.mysql-field-types#long
636 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer) value))
638 (warn "Unknown input bind type ~D." type))
642 (defmethod database-run-prepared ((stmt mysql-stmt))
644 (when (input-bind stmt)
645 (unless (zerop (mysql-stmt-bind-param (stmt stmt) (input-bind stmt)))
646 (error 'sql-database-error
647 :error-id (mysql-stmt-errno (stmt stmt))
648 :message (uffi:convert-from-cstring
649 (mysql-stmt-error (stmt stmt))))))
651 (unless (zerop (mysql-stmt-execute (stmt 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-store-result (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)))))
662 (database-fetch-prepared-rows stmt))
665 (defun database-fetch-prepared-rows (stmt)
666 (do ((rc (mysql-stmt-fetch (stmt stmt)) (mysql-stmt-fetch (stmt stmt)))
667 (num-fields (num-fields stmt))
669 ((not (zerop rc)) (nreverse rows))
671 (loop for i from 0 below num-fields
674 (not (zerop (uffi:ensure-char-integer
675 (uffi:deref-array (is-null-ptr stmt) '(:array :byte) i))))))
677 (let* ((bind (uffi:deref-array (output-bind stmt) '(:array mysql-bind) i))
678 (type (uffi:get-slot-value bind 'mysql-bind 'mysql::buffer-type))
679 (buffer (uffi:get-slot-value bind 'mysql-bind 'mysql::buffer)))
681 ((#.mysql-field-types#var-string #.mysql-field-types#string
682 #.mysql-field-types#tiny-blob #.mysql-field-types#blob
683 #.mysql-field-types#medium-blob #.mysql-field-types#long-blob)
684 (uffi:convert-from-foreign-string buffer))
685 (#.mysql-field-types#tiny
686 (uffi:ensure-char-integer
687 (uffi:deref-pointer buffer :byte)))
688 (#.mysql-field-types#short
689 (uffi:deref-pointer buffer :short))
690 (#.mysql-field-types#long
691 (uffi:deref-pointer buffer :int))
693 (#.mysql-field-types#longlong
694 (uffi:deref-pointer buffer :long))
695 (#.mysql-field-types#float
696 (uffi:deref-pointer buffer :float))
697 (#.mysql-field-types#double
698 (uffi:deref-pointer buffer :double))
699 ((#.mysql-field-types#time #.mysql-field-types#date
700 #.mysql-field-types#datetime #.mysql-field-types#timestamp)
701 (let ((year (uffi:get-slot-value buffer 'mysql-time 'mysql::year))
702 (month (uffi:get-slot-value buffer 'mysql-time 'mysql::month))
703 (day (uffi:get-slot-value buffer 'mysql-time 'mysql::day))
704 (hour (uffi:get-slot-value buffer 'mysql-time 'mysql::hour))
705 (minute (uffi:get-slot-value buffer 'mysql-time 'mysql::minute))
706 (second (uffi:get-slot-value buffer 'mysql-time 'mysql::second)))
708 (make-time :year year :month month :day day :hour hour
709 :minute minute :second second))))
718 (defmethod database-free-prepared ((stmt mysql-stmt))
719 (with-slots (stmt) stmt
720 (mysql-stmt-close stmt))
724 ;;; Database capabilities
726 (defmethod db-type-use-column-on-drop-index? ((db-type (eql :mysql)))
729 (defmethod db-type-has-views? ((db-type (eql :mysql)))
731 #-mysql-client-v5 nil)
733 (defmethod db-type-has-subqueries? ((db-type (eql :mysql)))
734 #+(or mysql-client-v4.1 mysql-client-v5) t
735 #-(or mysql-client-v4.1 mysql-client-v5) nil)
737 (defmethod db-type-has-boolean-where? ((db-type (eql :mysql)))
738 #+(or mysql-client-v4.1 mysql-client-v5) t
739 #-(or mysql-client-v4.1 mysql-client-v5) nil)
741 (defmethod db-type-has-union? ((db-type (eql :mysql)))
742 (not (eql (schar mysql::*mysql-client-info* 0) #\3)))
744 (defmethod db-type-transaction-capable? ((db-type (eql :mysql)) database)
745 (let ((tuple (car (database-query "SHOW VARIABLES LIKE 'HAVE_INNODB'" database :auto nil))))
746 (and tuple (string-equal "YES" (second tuple)))))
748 (defmethod db-type-has-prepared-stmt? ((db-type (eql :mysql)))
749 #+(or mysql-client-v4.1 mysql-client-v5) t
750 #-(or mysql-client-v4.1 mysql-client-v5) nil)
752 (defmethod db-type-has-auto-increment? ((db-type (eql :mysql)))
755 (when (clsql-sys:database-type-library-loaded :mysql)
756 (clsql-sys:initialize-database-type :database-type :mysql))