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
163 cmd :encoding (encoding db))))
166 (warn "Error setting ANSI mode for MySQL.")
168 (when error-occurred (mysql-close mysql-ptr)))))))))
171 (defmethod database-disconnect ((database mysql-database))
172 (mysql-close (database-mysql-ptr database))
173 (setf (database-mysql-ptr database) nil)
176 (defmethod database-execute-command (sql-expression (database mysql-database))
177 (uffi:with-cstring (sql-native sql-expression)
178 (let ((mysql-ptr (database-mysql-ptr database)))
179 (declare (type mysql-mysql-ptr-def mysql-ptr))
180 (if (zerop (mysql-real-query mysql-ptr sql-native
181 (uffi:foreign-encoded-octet-count
182 sql-expression :encoding (encoding database))))
184 (error 'sql-database-data-error
186 :expression sql-expression
187 :error-id (mysql-errno mysql-ptr)
188 :message (mysql-error-string mysql-ptr))))))
191 (defmethod database-query (query-expression (database mysql-database)
192 result-types field-names)
193 (declare (optimize (speed 3) (safety 0) (debug 0) (space 0)))
194 (let ((mysql-ptr (database-mysql-ptr database)))
195 (declare (type mysql-mysql-ptr-def mysql-ptr))
196 (when (database-execute-command query-expression database)
197 (let ((res-ptr (mysql-use-result mysql-ptr)))
198 (declare (type mysql-mysql-res-ptr-def res-ptr))
199 (if (and res-ptr (not (uffi:null-pointer-p res-ptr)))
201 (let ((num-fields (mysql-num-fields res-ptr)))
202 (declare (fixnum num-fields))
203 (setq result-types (canonicalize-types
204 result-types res-ptr))
206 (loop for row = (mysql-fetch-row res-ptr)
207 for lengths = (mysql-fetch-lengths res-ptr)
208 until (uffi:null-pointer-p row)
210 (do* ((rlist (make-list num-fields))
212 (pos rlist (cdr pos)))
213 ((= i num-fields) rlist)
217 (uffi:deref-array row '(:array
222 (uffi:deref-array lengths '(:array :unsigned-long) i)
223 :encoding (encoding database)))))
225 (result-field-names res-ptr))))
226 (mysql-free-result res-ptr))
227 (unless (zerop (mysql-errno mysql-ptr))
228 ;;from http://dev.mysql.com/doc/refman/5.0/en/mysql-field-count.html
229 ;; if mysql_use_result or mysql_store_result return a null ptr,
230 ;; we use a mysql_errno check to see if it had a problem or just
231 ;; was a query without a result. If no error, just return nil.
232 (error 'sql-database-data-error
234 :expression query-expression
235 :error-id (mysql-errno mysql-ptr)
236 :message (mysql-error-string mysql-ptr))))))))
239 (defstruct mysql-result-set
240 (res-ptr (uffi:make-null-pointer 'mysql-mysql-res) :type mysql-mysql-res-ptr-def)
241 (types nil :type list)
242 (num-fields 0 :type fixnum)
243 (full-set nil :type boolean))
246 (defmethod database-query-result-set ((query-expression string)
247 (database mysql-database)
248 &key full-set result-types)
249 (let ((mysql-ptr (database-mysql-ptr database)))
250 (declare (type mysql-mysql-ptr-def mysql-ptr))
251 (when (database-execute-command query-expression database)
252 (let ((res-ptr (if full-set
253 (mysql-store-result mysql-ptr)
254 (mysql-use-result mysql-ptr))))
255 (declare (type mysql-mysql-res-ptr-def res-ptr))
256 (if (not (uffi:null-pointer-p res-ptr))
257 (let* ((num-fields (mysql-num-fields res-ptr))
258 (result-set (make-mysql-result-set
260 :num-fields num-fields
264 result-types res-ptr))))
268 (mysql-num-rows res-ptr))
271 (error 'sql-database-data-error
273 :expression query-expression
274 :error-id (mysql-errno mysql-ptr)
275 :message (mysql-error-string mysql-ptr))))
278 (defmethod database-dump-result-set (result-set (database mysql-database))
279 (mysql-free-result (mysql-result-set-res-ptr result-set))
283 (defmethod database-store-next-row (result-set (database mysql-database) list)
284 (let* ((res-ptr (mysql-result-set-res-ptr result-set))
285 (row (mysql-fetch-row res-ptr))
286 (lengths (mysql-fetch-lengths res-ptr))
287 (types (mysql-result-set-types result-set)))
288 (declare (type mysql-mysql-res-ptr-def res-ptr)
289 (type mysql-row-def row))
290 (unless (uffi:null-pointer-p row)
291 (loop for i from 0 below (mysql-result-set-num-fields result-set)
296 (uffi:deref-array row '(:array (* :unsigned-char)) i)
299 (uffi:deref-array lengths '(:array :unsigned-long) i)
300 :encoding (encoding database))))
304 ;; Table and attribute introspection
306 (defmethod database-list-tables ((database mysql-database) &key (owner nil))
307 (declare (ignore owner))
309 ((eql #\5 (char (database-server-info database) 0))
310 (loop for (name type) in (database-query "SHOW FULL TABLES" database nil nil)
311 when (and (string-equal type "base table")
312 (not (and (>= (length name) 11)
313 (string-equal (subseq name 0 11) "_CLSQL_SEQ_"))))
316 (remove-if #'(lambda (s)
317 (and (>= (length s) 11)
318 (string-equal (subseq s 0 11) "_CLSQL_SEQ_")))
319 (mapcar #'car (database-query "SHOW TABLES" database nil nil))))))
321 (defmethod database-list-views ((database mysql-database)
323 (declare (ignore owner))
325 ((eql #\5 (char (database-server-info database) 0))
326 (loop for (name type) in (database-query "SHOW FULL TABLES" database nil nil)
327 when (string-equal type "view")
332 (defmethod database-list-indexes ((database mysql-database)
335 (dolist (table (database-list-tables database :owner owner) result)
337 (append (database-list-table-indexes table database :owner owner)
340 (defmethod database-list-table-indexes (table (database mysql-database)
342 (declare (ignore owner))
344 (rows (database-query
345 (format nil "SHOW INDEX FROM ~A" table)
348 ((null rows) (nreverse results))
349 (let ((col (nth 2 (car rows))))
350 (unless (find col results :test #'string-equal)
351 (push col results)))))
353 (defmethod database-list-attributes ((table string) (database mysql-database)
355 (declare (ignore owner))
358 (format nil "SHOW COLUMNS FROM ~A" table)
361 (defmethod database-attribute-type (attribute (table string)
362 (database mysql-database)
364 (declare (ignore owner))
365 (let ((row (car (database-query
367 "SHOW COLUMNS FROM ~A LIKE '~A'" table attribute)
369 (let* ((raw-type (second row))
371 (start-length (position #\( raw-type))
372 (type (if start-length
373 (subseq raw-type 0 start-length)
375 (length (when start-length
376 (parse-integer (subseq raw-type (1+ start-length))
379 (values (ensure-keyword type) length nil (if (string-equal null "YES") 1 0))))))
381 ;;; Sequence functions
383 (defun %sequence-name-to-table (sequence-name)
384 (concatenate 'string "_CLSQL_SEQ_" (sql-escape sequence-name)))
386 (defun %table-name-to-sequence-name (table-name)
387 (and (>= (length table-name) 11)
388 (string-equal (subseq table-name 0 11) "_CLSQL_SEQ_")
389 (subseq table-name 11)))
391 (defmethod database-create-sequence (sequence-name
392 (database mysql-database))
393 (let ((table-name (%sequence-name-to-table sequence-name)))
394 (database-execute-command
395 (concatenate 'string "CREATE TABLE " table-name
396 " (id int NOT NULL PRIMARY KEY AUTO_INCREMENT)")
398 (database-execute-command
399 (concatenate 'string "INSERT INTO " table-name
403 (defmethod database-drop-sequence (sequence-name
404 (database mysql-database))
405 (database-execute-command
406 (concatenate 'string "DROP TABLE " (%sequence-name-to-table sequence-name))
409 (defmethod database-list-sequences ((database mysql-database)
411 (declare (ignore owner))
412 (mapcan #'(lambda (s)
413 (let ((sn (%table-name-to-sequence-name (car s))))
415 (database-query "SHOW TABLES" database nil nil)))
417 (defmethod database-set-sequence-position (sequence-name
419 (database mysql-database))
420 (database-execute-command
421 (format nil "UPDATE ~A SET id=~A" (%sequence-name-to-table sequence-name)
424 (mysql:mysql-insert-id (clsql-mysql::database-mysql-ptr database)))
426 (defmethod database-sequence-next (sequence-name (database mysql-database))
428 (database-execute-command
429 (concatenate 'string "UPDATE " (%sequence-name-to-table sequence-name)
430 " SET id=LAST_INSERT_ID(id+1)")
432 (mysql:mysql-insert-id (clsql-mysql::database-mysql-ptr database))))
434 (defmethod database-sequence-last (sequence-name (database mysql-database))
436 (caar (database-query
437 (concatenate 'string "SELECT id from "
438 (%sequence-name-to-table sequence-name))
439 database :auto nil))))
441 (defmethod database-create (connection-spec (type (eql :mysql)))
442 (destructuring-bind (host name user password) connection-spec
443 (let ((database (database-connect (list host "" user password)
445 (setf (slot-value database 'clsql-sys::state) :open)
447 (database-execute-command (format nil "create database ~A" name) database)
448 (database-disconnect database)))))
450 (defmethod database-destroy (connection-spec (type (eql :mysql)))
451 (destructuring-bind (host name user password) connection-spec
452 (let ((database (database-connect (list host "" user password)
454 (setf (slot-value database 'clsql-sys::state) :open)
456 (database-execute-command (format nil "drop database ~A" name) database)
457 (database-disconnect database)))))
459 (defmethod database-probe (connection-spec (type (eql :mysql)))
460 (when (find (second connection-spec) (database-list connection-spec type)
461 :test #'string-equal)
464 (defmethod database-list (connection-spec (type (eql :mysql)))
465 (destructuring-bind (host name user password &optional port) connection-spec
466 (let ((database (database-connect (list host (or name "mysql")
467 user password port) type)))
470 (setf (slot-value database 'clsql-sys::state) :open)
471 (mapcar #'car (database-query "show databases" database :auto nil)))
473 (database-disconnect database)
474 (setf (slot-value database 'clsql-sys::state) :closed))))))
477 ;;; Prepared statements
479 (defclass mysql-stmt ()
480 ((database :initarg :database :reader database)
481 (stmt :initarg :stmt :accessor stmt)
482 (input-bind :initarg :input-bind :reader input-bind)
483 (output-bind :initarg :output-bind :reader output-bind)
484 (types :initarg :types :reader types)
485 (result-set :initarg :result-set :reader result-set)
486 (num-fields :initarg :num-fields :reader num-fields)
487 (field-names :initarg :field-names :accessor stmt-field-names)
488 (length-ptr :initarg :length-ptr :reader length-ptr)
489 (is-null-ptr :initarg :is-null-ptr :reader is-null-ptr)
490 (result-types :initarg :result-types :reader result-types)))
492 (defun clsql-type->mysql-type (type)
494 ((in type :null) mysql-field-types#null)
495 ((in type :int :integer) mysql-field-types#long)
496 ((in type :short) mysql-field-types#short)
497 ((in type :bigint) mysql-field-types#longlong)
498 ((in type :float :double :number) mysql-field-types#double)
499 ((and (consp type) (in (car type) :char :string :varchar)) mysql-field-types#var-string)
500 ((or (eq type :blob) (and (consp type) (in (car type) :blob))) mysql-field-types#var-string)
502 (error 'sql-user-error
504 (format nil "Unknown clsql type ~A." type)))))
507 (defmethod database-prepare (sql-stmt types (database mysql-database) result-types field-names)
508 (let* ((mysql-ptr (database-mysql-ptr database))
509 (stmt (mysql-stmt-init mysql-ptr)))
510 (when (uffi:null-pointer-p stmt)
511 (error 'sql-database-error
512 :error-id (mysql-errno mysql-ptr)
513 :message (mysql-error-string mysql-ptr)))
515 (uffi:with-cstring (native-query sql-stmt)
516 (unless (zerop (mysql-stmt-prepare stmt native-query (uffi:foreign-encoded-octet-count
517 sql-stmt :encoding (encoding database))))
518 (mysql-stmt-close stmt)
519 (error 'sql-database-error
520 :error-id (mysql-errno mysql-ptr)
521 :message (mysql-error-string mysql-ptr))))
523 (unless (= (mysql-stmt-param-count stmt) (length types))
524 (mysql-stmt-close stmt)
525 (error 'sql-database-error
527 (format nil "Mysql param count (~D) does not match number of types (~D)"
528 (mysql-stmt-param-count stmt) (length types))))
530 (let ((rs (mysql-stmt-result-metadata stmt)))
531 (when (uffi:null-pointer-p rs)
532 (warn "mysql_stmt_result_metadata returned NULL")
534 (mysql-stmt-close stmt)
536 (error 'sql-database-error
537 :message "mysql_stmt_result_metadata returned NULL"))
539 (let ((input-bind (uffi:allocate-foreign-object 'mysql-bind (length types)))
540 (mysql-types (mapcar 'clsql-type->mysql-type types))
541 field-vec num-fields is-null-ptr output-bind length-ptr)
544 (dotimes (i (length types))
545 (let* ((binding (uffi:deref-array input-bind '(:array mysql-bind) i)))
546 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-type)
548 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-length) 0)))
551 (unless (uffi:null-pointer-p rs)
552 (setq field-vec (mysql-fetch-fields rs)
553 num-fields (mysql-num-fields rs)
554 is-null-ptr (uffi:allocate-foreign-object :byte num-fields)
555 output-bind (uffi:allocate-foreign-object 'mysql-bind num-fields)
556 length-ptr (uffi:allocate-foreign-object :unsigned-long num-fields))
557 (dotimes (i num-fields)
559 (let* ((field (uffi:deref-array field-vec '(:array mysql-field) i))
560 (type (uffi:get-slot-value field 'mysql-field 'type))
561 (binding (uffi:deref-array output-bind '(:array mysql-bind) i)))
562 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-type) type)
564 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-length) 0)
565 #+need-to-allocate-foreign-object-for-this
566 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::is-null)
567 (+ i (uffi:pointer-address is-null-ptr)))
568 #+need-to-allocate-foreign-object-for-this
569 (setf (uffi:get-slot-value binding 'mysql-bind 'length)
570 (+ (* i 8) (uffi:pointer-address length-ptr)))
573 ((#.mysql-field-types#var-string #.mysql-field-types#string
574 #.mysql-field-types#tiny-blob #.mysql-field-types#blob
575 #.mysql-field-types#medium-blob #.mysql-field-types#long-blob)
576 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-length) 1024)
577 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
578 (uffi:allocate-foreign-object :unsigned-char 1024)))
579 (#.mysql-field-types#tiny
580 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
581 (uffi:allocate-foreign-object :byte)))
582 (#.mysql-field-types#short
583 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
584 (uffi:allocate-foreign-object :short)))
585 (#.mysql-field-types#long
586 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
587 ;; segfaults if supply :int on amd64
588 (uffi:allocate-foreign-object :long)))
590 (#.mysql-field-types#longlong
591 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
592 (uffi:allocate-foreign-object :long)))
593 (#.mysql-field-types#float
594 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
595 (uffi:allocate-foreign-object :float)))
596 (#.mysql-field-types#double
597 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
598 (uffi:allocate-foreign-object :double)))
599 ((#.mysql-field-types#time #.mysql-field-types#date
600 #.mysql-field-types#datetime #.mysql-field-types#timestamp)
601 (uffi:allocate-foreign-object 'mysql-time))
603 (error "mysql type ~D not supported." type)))))
605 (unless (zerop (mysql-stmt-bind-result stmt output-bind))
606 (mysql-stmt-close stmt)
607 (error 'sql-database-error
608 :error-id (mysql-stmt-errno stmt)
609 :message (uffi:convert-from-cstring
610 (mysql-stmt-error stmt)))))
612 (make-instance 'mysql-stmt
615 :num-fields num-fields
616 :input-bind input-bind
617 :output-bind output-bind
619 :result-types result-types
620 :length-ptr length-ptr
621 :is-null-ptr is-null-ptr
623 :field-names field-names)))))
626 (defmethod database-bind-parameter ((stmt mysql-stmt) position value)
627 ;; FIXME: will need to allocate bind structure. This should probably be
628 ;; done in C since the API is not mature and may change
629 (let ((binding (uffi:deref-array (input-bind stmt) '(:array mysql-bind) (1- position)))
630 (type (nth (1- position) (types stmt))))
631 (setf (uffi:get-slot-value binding 'mysql-bind 'length) 0)
634 (when (is-null-ptr stmt)
635 (setf (uffi:deref-array (is-null-ptr stmt) '(:array :byte) (1- position)) 1)))
637 (when (is-null-ptr stmt)
638 (setf (uffi:deref-array (is-null-ptr stmt) '(:array :byte) (1- position)) 0))
640 (#.mysql-field-types#long
641 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer) value))
643 (warn "Unknown input bind type ~D." type))
647 (defmethod database-run-prepared ((stmt mysql-stmt))
649 (when (input-bind stmt)
650 (unless (zerop (mysql-stmt-bind-param (stmt stmt) (input-bind stmt)))
651 (error 'sql-database-error
652 :error-id (mysql-stmt-errno (stmt stmt))
653 :message (uffi:convert-from-cstring
654 (mysql-stmt-error (stmt stmt))))))
656 (unless (zerop (mysql-stmt-execute (stmt stmt)))
657 (error 'sql-database-error
658 :error-id (mysql-stmt-errno (stmt stmt))
659 :message (uffi:convert-from-cstring
660 (mysql-stmt-error (stmt stmt)))))
662 (unless (zerop (mysql-stmt-store-result (stmt stmt)))
663 (error 'sql-database-error
664 :error-id (mysql-stmt-errno (stmt stmt))
665 :message (uffi:convert-from-cstring
666 (mysql-stmt-error (stmt stmt)))))
667 (database-fetch-prepared-rows stmt))
670 (defun database-fetch-prepared-rows (stmt)
671 (do ((rc (mysql-stmt-fetch (stmt stmt)) (mysql-stmt-fetch (stmt stmt)))
672 (num-fields (num-fields stmt))
674 ((not (zerop rc)) (nreverse rows))
676 (loop for i from 0 below num-fields
679 (not (zerop (uffi:ensure-char-integer
680 (uffi:deref-array (is-null-ptr stmt) '(:array :byte) i))))))
682 (let* ((bind (uffi:deref-array (output-bind stmt) '(:array mysql-bind) i))
683 (type (uffi:get-slot-value bind 'mysql-bind 'mysql::buffer-type))
684 (buffer (uffi:get-slot-value bind 'mysql-bind 'mysql::buffer)))
686 ((#.mysql-field-types#var-string #.mysql-field-types#string
687 #.mysql-field-types#tiny-blob #.mysql-field-types#blob
688 #.mysql-field-types#medium-blob #.mysql-field-types#long-blob)
689 (uffi:convert-from-foreign-string buffer :encoding (encoding (database stmt))))
690 (#.mysql-field-types#tiny
691 (uffi:ensure-char-integer
692 (uffi:deref-pointer buffer :byte)))
693 (#.mysql-field-types#short
694 (uffi:deref-pointer buffer :short))
695 (#.mysql-field-types#long
696 (uffi:deref-pointer buffer :int))
698 (#.mysql-field-types#longlong
699 (uffi:deref-pointer buffer :long))
700 (#.mysql-field-types#float
701 (uffi:deref-pointer buffer :float))
702 (#.mysql-field-types#double
703 (uffi:deref-pointer buffer :double))
704 ((#.mysql-field-types#time #.mysql-field-types#date
705 #.mysql-field-types#datetime #.mysql-field-types#timestamp)
706 (let ((year (uffi:get-slot-value buffer 'mysql-time 'mysql::year))
707 (month (uffi:get-slot-value buffer 'mysql-time 'mysql::month))
708 (day (uffi:get-slot-value buffer 'mysql-time 'mysql::day))
709 (hour (uffi:get-slot-value buffer 'mysql-time 'mysql::hour))
710 (minute (uffi:get-slot-value buffer 'mysql-time 'mysql::minute))
711 (second (uffi:get-slot-value buffer 'mysql-time 'mysql::second)))
713 (make-time :year year :month month :day day :hour hour
714 :minute minute :second second))))
723 (defmethod database-free-prepared ((stmt mysql-stmt))
724 (with-slots (stmt) stmt
725 (mysql-stmt-close stmt))
729 ;;; Database capabilities
731 (defmethod db-type-use-column-on-drop-index? ((db-type (eql :mysql)))
734 (defmethod db-type-has-views? ((db-type (eql :mysql)))
736 #-mysql-client-v5 nil)
738 (defmethod db-type-has-subqueries? ((db-type (eql :mysql)))
739 #+(or mysql-client-v4.1 mysql-client-v5) t
740 #-(or mysql-client-v4.1 mysql-client-v5) nil)
742 (defmethod db-type-has-boolean-where? ((db-type (eql :mysql)))
743 #+(or mysql-client-v4.1 mysql-client-v5) t
744 #-(or mysql-client-v4.1 mysql-client-v5) nil)
746 (defmethod db-type-has-union? ((db-type (eql :mysql)))
747 (not (eql (schar mysql::*mysql-client-info* 0) #\3)))
749 (defmethod db-type-transaction-capable? ((db-type (eql :mysql)) database)
750 (let ((tuple (car (database-query "SHOW VARIABLES LIKE 'HAVE_INNODB'" database :auto nil))))
751 (and tuple (string-equal "YES" (second tuple)))))
753 (defmethod db-type-has-prepared-stmt? ((db-type (eql :mysql)))
754 #+(or mysql-client-v4.1 mysql-client-v5) t
755 #-(or mysql-client-v4.1 mysql-client-v5) nil)
757 (defmethod db-type-has-auto-increment? ((db-type (eql :mysql)))
760 (when (clsql-sys:database-type-library-loaded :mysql)
761 (clsql-sys:initialize-database-type :database-type :mysql))