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
221 (uffi:deref-array lengths '(:array :unsigned-long)
224 (result-field-names res-ptr))))
225 (mysql-free-result res-ptr))
226 (unless (zerop (mysql-errno mysql-ptr))
227 ;;from http://dev.mysql.com/doc/refman/5.0/en/mysql-field-count.html
228 ;; if mysql_use_result or mysql_store_result return a null ptr,
229 ;; we use a mysql_errno check to see if it had a problem or just
230 ;; was a query without a result. If no error, just return nil.
231 (error 'sql-database-data-error
233 :expression query-expression
234 :error-id (mysql-errno mysql-ptr)
235 :message (mysql-error-string mysql-ptr))))))))
238 (defstruct mysql-result-set
239 (res-ptr (uffi:make-null-pointer 'mysql-mysql-res) :type mysql-mysql-res-ptr-def)
240 (types nil :type list)
241 (num-fields 0 :type fixnum)
242 (full-set nil :type boolean))
245 (defmethod database-query-result-set ((query-expression string)
246 (database mysql-database)
247 &key full-set result-types)
248 (let ((mysql-ptr (database-mysql-ptr database)))
249 (declare (type mysql-mysql-ptr-def mysql-ptr))
250 (when (database-execute-command query-expression database)
251 (let ((res-ptr (if full-set
252 (mysql-store-result mysql-ptr)
253 (mysql-use-result mysql-ptr))))
254 (declare (type mysql-mysql-res-ptr-def res-ptr))
255 (if (not (uffi:null-pointer-p res-ptr))
256 (let* ((num-fields (mysql-num-fields res-ptr))
257 (result-set (make-mysql-result-set
259 :num-fields num-fields
263 result-types res-ptr))))
267 (mysql-num-rows res-ptr))
270 (error 'sql-database-data-error
272 :expression query-expression
273 :error-id (mysql-errno mysql-ptr)
274 :message (mysql-error-string mysql-ptr))))
277 (defmethod database-dump-result-set (result-set (database mysql-database))
278 (mysql-free-result (mysql-result-set-res-ptr result-set))
282 (defmethod database-store-next-row (result-set (database mysql-database) list)
283 (let* ((res-ptr (mysql-result-set-res-ptr result-set))
284 (row (mysql-fetch-row res-ptr))
285 (lengths (mysql-fetch-lengths res-ptr))
286 (types (mysql-result-set-types result-set)))
287 (declare (type mysql-mysql-res-ptr-def res-ptr)
288 (type mysql-row-def row))
289 (unless (uffi:null-pointer-p row)
290 (loop for i from 0 below (mysql-result-set-num-fields result-set)
295 (uffi:deref-array row '(:array (* :unsigned-char)) i)
298 (uffi:deref-array lengths '(:array :unsigned-long) i))))
302 ;; Table and attribute introspection
304 (defmethod database-list-tables ((database mysql-database) &key (owner nil))
305 (declare (ignore owner))
307 ((eql #\5 (char (database-server-info database) 0))
308 (loop for (name type) in (database-query "SHOW FULL TABLES" database nil nil)
309 when (and (string-equal type "base table")
310 (not (and (>= (length name) 11)
311 (string-equal (subseq name 0 11) "_CLSQL_SEQ_"))))
314 (remove-if #'(lambda (s)
315 (and (>= (length s) 11)
316 (string-equal (subseq s 0 11) "_CLSQL_SEQ_")))
317 (mapcar #'car (database-query "SHOW TABLES" database nil nil))))))
319 (defmethod database-list-views ((database mysql-database)
321 (declare (ignore owner))
323 ((eql #\5 (char (database-server-info database) 0))
324 (loop for (name type) in (database-query "SHOW FULL TABLES" database nil nil)
325 when (string-equal type "view")
330 (defmethod database-list-indexes ((database mysql-database)
333 (dolist (table (database-list-tables database :owner owner) result)
335 (append (database-list-table-indexes table database :owner owner)
338 (defmethod database-list-table-indexes (table (database mysql-database)
340 (declare (ignore owner))
342 (rows (database-query
343 (format nil "SHOW INDEX FROM ~A" table)
346 ((null rows) (nreverse results))
347 (let ((col (nth 2 (car rows))))
348 (unless (find col results :test #'string-equal)
349 (push col results)))))
351 (defmethod database-list-attributes ((table string) (database mysql-database)
353 (declare (ignore owner))
356 (format nil "SHOW COLUMNS FROM ~A" table)
359 (defmethod database-attribute-type (attribute (table string)
360 (database mysql-database)
362 (declare (ignore owner))
363 (let ((row (car (database-query
365 "SHOW COLUMNS FROM ~A LIKE '~A'" table attribute)
367 (let* ((raw-type (second row))
369 (start-length (position #\( raw-type))
370 (type (if start-length
371 (subseq raw-type 0 start-length)
373 (length (when start-length
374 (parse-integer (subseq raw-type (1+ start-length))
377 (values (ensure-keyword type) length nil (if (string-equal null "YES") 1 0))))))
379 ;;; Sequence functions
381 (defun %sequence-name-to-table (sequence-name)
382 (concatenate 'string "_CLSQL_SEQ_" (sql-escape sequence-name)))
384 (defun %table-name-to-sequence-name (table-name)
385 (and (>= (length table-name) 11)
386 (string-equal (subseq table-name 0 11) "_CLSQL_SEQ_")
387 (subseq table-name 11)))
389 (defmethod database-create-sequence (sequence-name
390 (database mysql-database))
391 (let ((table-name (%sequence-name-to-table sequence-name)))
392 (database-execute-command
393 (concatenate 'string "CREATE TABLE " table-name
394 " (id int NOT NULL PRIMARY KEY AUTO_INCREMENT)")
396 (database-execute-command
397 (concatenate 'string "INSERT INTO " table-name
401 (defmethod database-drop-sequence (sequence-name
402 (database mysql-database))
403 (database-execute-command
404 (concatenate 'string "DROP TABLE " (%sequence-name-to-table sequence-name))
407 (defmethod database-list-sequences ((database mysql-database)
409 (declare (ignore owner))
410 (mapcan #'(lambda (s)
411 (let ((sn (%table-name-to-sequence-name (car s))))
413 (database-query "SHOW TABLES" database nil nil)))
415 (defmethod database-set-sequence-position (sequence-name
417 (database mysql-database))
418 (database-execute-command
419 (format nil "UPDATE ~A SET id=~A" (%sequence-name-to-table sequence-name)
422 (mysql:mysql-insert-id (clsql-mysql::database-mysql-ptr database)))
424 (defmethod database-sequence-next (sequence-name (database mysql-database))
426 (database-execute-command
427 (concatenate 'string "UPDATE " (%sequence-name-to-table sequence-name)
428 " SET id=LAST_INSERT_ID(id+1)")
430 (mysql:mysql-insert-id (clsql-mysql::database-mysql-ptr database))))
432 (defmethod database-sequence-last (sequence-name (database mysql-database))
434 (caar (database-query
435 (concatenate 'string "SELECT id from "
436 (%sequence-name-to-table sequence-name))
437 database :auto nil))))
439 (defmethod database-create (connection-spec (type (eql :mysql)))
440 (destructuring-bind (host name user password) connection-spec
441 (let ((database (database-connect (list host "" user password)
443 (setf (slot-value database 'clsql-sys::state) :open)
445 (database-execute-command (format nil "create database ~A" name) database)
446 (database-disconnect database)))))
448 (defmethod database-destroy (connection-spec (type (eql :mysql)))
449 (destructuring-bind (host name user password) connection-spec
450 (let ((database (database-connect (list host "" user password)
452 (setf (slot-value database 'clsql-sys::state) :open)
454 (database-execute-command (format nil "drop database ~A" name) database)
455 (database-disconnect database)))))
457 (defmethod database-probe (connection-spec (type (eql :mysql)))
458 (when (find (second connection-spec) (database-list connection-spec type)
459 :test #'string-equal)
462 (defmethod database-list (connection-spec (type (eql :mysql)))
463 (destructuring-bind (host name user password &optional port) connection-spec
464 (let ((database (database-connect (list host (or name "mysql")
465 user password port) type)))
468 (setf (slot-value database 'clsql-sys::state) :open)
469 (mapcar #'car (database-query "show databases" database :auto nil)))
471 (database-disconnect database)
472 (setf (slot-value database 'clsql-sys::state) :closed))))))
475 ;;; Prepared statements
477 (defclass mysql-stmt ()
478 ((database :initarg :database :reader database)
479 (stmt :initarg :stmt :accessor stmt)
480 (input-bind :initarg :input-bind :reader input-bind)
481 (output-bind :initarg :output-bind :reader output-bind)
482 (types :initarg :types :reader types)
483 (result-set :initarg :result-set :reader result-set)
484 (num-fields :initarg :num-fields :reader num-fields)
485 (field-names :initarg :field-names :accessor stmt-field-names)
486 (length-ptr :initarg :length-ptr :reader length-ptr)
487 (is-null-ptr :initarg :is-null-ptr :reader is-null-ptr)
488 (result-types :initarg :result-types :reader result-types)))
490 (defun clsql-type->mysql-type (type)
492 ((in type :null) mysql-field-types#null)
493 ((in type :int :integer) mysql-field-types#long)
494 ((in type :short) mysql-field-types#short)
495 ((in type :bigint) mysql-field-types#longlong)
496 ((in type :float :double :number) mysql-field-types#double)
497 ((and (consp type) (in (car type) :char :string :varchar)) mysql-field-types#var-string)
498 ((or (eq type :blob) (and (consp type) (in (car type) :blob))) mysql-field-types#var-string)
500 (error 'sql-user-error
502 (format nil "Unknown clsql type ~A." type)))))
505 (defmethod database-prepare (sql-stmt types (database mysql-database) result-types field-names)
506 (let* ((mysql-ptr (database-mysql-ptr database))
507 (stmt (mysql-stmt-init mysql-ptr)))
508 (when (uffi:null-pointer-p stmt)
509 (error 'sql-database-error
510 :error-id (mysql-errno mysql-ptr)
511 :message (mysql-error-string mysql-ptr)))
513 (uffi:with-cstring (native-query sql-stmt)
514 (unless (zerop (mysql-stmt-prepare stmt native-query (uffi:foreign-encoded-octet-count
515 sql-stmt :encoding (encoding database))))
516 (mysql-stmt-close stmt)
517 (error 'sql-database-error
518 :error-id (mysql-errno mysql-ptr)
519 :message (mysql-error-string mysql-ptr))))
521 (unless (= (mysql-stmt-param-count stmt) (length types))
522 (mysql-stmt-close stmt)
523 (error 'sql-database-error
525 (format nil "Mysql param count (~D) does not match number of types (~D)"
526 (mysql-stmt-param-count stmt) (length types))))
528 (let ((rs (mysql-stmt-result-metadata stmt)))
529 (when (uffi:null-pointer-p rs)
530 (warn "mysql_stmt_result_metadata returned NULL")
532 (mysql-stmt-close stmt)
534 (error 'sql-database-error
535 :message "mysql_stmt_result_metadata returned NULL"))
537 (let ((input-bind (uffi:allocate-foreign-object 'mysql-bind (length types)))
538 (mysql-types (mapcar 'clsql-type->mysql-type types))
539 field-vec num-fields is-null-ptr output-bind length-ptr)
542 (dotimes (i (length types))
543 (let* ((binding (uffi:deref-array input-bind '(:array mysql-bind) i)))
544 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-type)
546 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-length) 0)))
549 (unless (uffi:null-pointer-p rs)
550 (setq field-vec (mysql-fetch-fields rs)
551 num-fields (mysql-num-fields rs)
552 is-null-ptr (uffi:allocate-foreign-object :byte num-fields)
553 output-bind (uffi:allocate-foreign-object 'mysql-bind num-fields)
554 length-ptr (uffi:allocate-foreign-object :unsigned-long num-fields))
555 (dotimes (i num-fields)
557 (let* ((field (uffi:deref-array field-vec '(:array mysql-field) i))
558 (type (uffi:get-slot-value field 'mysql-field 'type))
559 (binding (uffi:deref-array output-bind '(:array mysql-bind) i)))
560 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-type) type)
562 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-length) 0)
563 #+need-to-allocate-foreign-object-for-this
564 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::is-null)
565 (+ i (uffi:pointer-address is-null-ptr)))
566 #+need-to-allocate-foreign-object-for-this
567 (setf (uffi:get-slot-value binding 'mysql-bind 'length)
568 (+ (* i 8) (uffi:pointer-address length-ptr)))
571 ((#.mysql-field-types#var-string #.mysql-field-types#string
572 #.mysql-field-types#tiny-blob #.mysql-field-types#blob
573 #.mysql-field-types#medium-blob #.mysql-field-types#long-blob)
574 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-length) 1024)
575 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
576 (uffi:allocate-foreign-object :unsigned-char 1024)))
577 (#.mysql-field-types#tiny
578 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
579 (uffi:allocate-foreign-object :byte)))
580 (#.mysql-field-types#short
581 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
582 (uffi:allocate-foreign-object :short)))
583 (#.mysql-field-types#long
584 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
585 ;; segfaults if supply :int on amd64
586 (uffi:allocate-foreign-object :long)))
588 (#.mysql-field-types#longlong
589 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
590 (uffi:allocate-foreign-object :long)))
591 (#.mysql-field-types#float
592 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
593 (uffi:allocate-foreign-object :float)))
594 (#.mysql-field-types#double
595 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
596 (uffi:allocate-foreign-object :double)))
597 ((#.mysql-field-types#time #.mysql-field-types#date
598 #.mysql-field-types#datetime #.mysql-field-types#timestamp)
599 (uffi:allocate-foreign-object 'mysql-time))
601 (error "mysql type ~D not supported." type)))))
603 (unless (zerop (mysql-stmt-bind-result stmt output-bind))
604 (mysql-stmt-close stmt)
605 (error 'sql-database-error
606 :error-id (mysql-stmt-errno stmt)
607 :message (uffi:convert-from-cstring
608 (mysql-stmt-error stmt)))))
610 (make-instance 'mysql-stmt
613 :num-fields num-fields
614 :input-bind input-bind
615 :output-bind output-bind
617 :result-types result-types
618 :length-ptr length-ptr
619 :is-null-ptr is-null-ptr
621 :field-names field-names)))))
624 (defmethod database-bind-parameter ((stmt mysql-stmt) position value)
625 ;; FIXME: will need to allocate bind structure. This should probably be
626 ;; done in C since the API is not mature and may change
627 (let ((binding (uffi:deref-array (input-bind stmt) '(:array mysql-bind) (1- position)))
628 (type (nth (1- position) (types stmt))))
629 (setf (uffi:get-slot-value binding 'mysql-bind 'length) 0)
632 (when (is-null-ptr stmt)
633 (setf (uffi:deref-array (is-null-ptr stmt) '(:array :byte) (1- position)) 1)))
635 (when (is-null-ptr stmt)
636 (setf (uffi:deref-array (is-null-ptr stmt) '(:array :byte) (1- position)) 0))
638 (#.mysql-field-types#long
639 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer) value))
641 (warn "Unknown input bind type ~D." type))
645 (defmethod database-run-prepared ((stmt mysql-stmt))
647 (when (input-bind stmt)
648 (unless (zerop (mysql-stmt-bind-param (stmt stmt) (input-bind stmt)))
649 (error 'sql-database-error
650 :error-id (mysql-stmt-errno (stmt stmt))
651 :message (uffi:convert-from-cstring
652 (mysql-stmt-error (stmt stmt))))))
654 (unless (zerop (mysql-stmt-execute (stmt stmt)))
655 (error 'sql-database-error
656 :error-id (mysql-stmt-errno (stmt stmt))
657 :message (uffi:convert-from-cstring
658 (mysql-stmt-error (stmt stmt)))))
660 (unless (zerop (mysql-stmt-store-result (stmt stmt)))
661 (error 'sql-database-error
662 :error-id (mysql-stmt-errno (stmt stmt))
663 :message (uffi:convert-from-cstring
664 (mysql-stmt-error (stmt stmt)))))
665 (database-fetch-prepared-rows stmt))
668 (defun database-fetch-prepared-rows (stmt)
669 (do ((rc (mysql-stmt-fetch (stmt stmt)) (mysql-stmt-fetch (stmt stmt)))
670 (num-fields (num-fields stmt))
672 ((not (zerop rc)) (nreverse rows))
674 (loop for i from 0 below num-fields
677 (not (zerop (uffi:ensure-char-integer
678 (uffi:deref-array (is-null-ptr stmt) '(:array :byte) i))))))
680 (let* ((bind (uffi:deref-array (output-bind stmt) '(:array mysql-bind) i))
681 (type (uffi:get-slot-value bind 'mysql-bind 'mysql::buffer-type))
682 (buffer (uffi:get-slot-value bind 'mysql-bind 'mysql::buffer)))
684 ((#.mysql-field-types#var-string #.mysql-field-types#string
685 #.mysql-field-types#tiny-blob #.mysql-field-types#blob
686 #.mysql-field-types#medium-blob #.mysql-field-types#long-blob)
687 (uffi:convert-from-foreign-string buffer))
688 (#.mysql-field-types#tiny
689 (uffi:ensure-char-integer
690 (uffi:deref-pointer buffer :byte)))
691 (#.mysql-field-types#short
692 (uffi:deref-pointer buffer :short))
693 (#.mysql-field-types#long
694 (uffi:deref-pointer buffer :int))
696 (#.mysql-field-types#longlong
697 (uffi:deref-pointer buffer :long))
698 (#.mysql-field-types#float
699 (uffi:deref-pointer buffer :float))
700 (#.mysql-field-types#double
701 (uffi:deref-pointer buffer :double))
702 ((#.mysql-field-types#time #.mysql-field-types#date
703 #.mysql-field-types#datetime #.mysql-field-types#timestamp)
704 (let ((year (uffi:get-slot-value buffer 'mysql-time 'mysql::year))
705 (month (uffi:get-slot-value buffer 'mysql-time 'mysql::month))
706 (day (uffi:get-slot-value buffer 'mysql-time 'mysql::day))
707 (hour (uffi:get-slot-value buffer 'mysql-time 'mysql::hour))
708 (minute (uffi:get-slot-value buffer 'mysql-time 'mysql::minute))
709 (second (uffi:get-slot-value buffer 'mysql-time 'mysql::second)))
711 (make-time :year year :month month :day day :hour hour
712 :minute minute :second second))))
721 (defmethod database-free-prepared ((stmt mysql-stmt))
722 (with-slots (stmt) stmt
723 (mysql-stmt-close stmt))
727 ;;; Database capabilities
729 (defmethod db-type-use-column-on-drop-index? ((db-type (eql :mysql)))
732 (defmethod db-type-has-views? ((db-type (eql :mysql)))
734 #-mysql-client-v5 nil)
736 (defmethod db-type-has-subqueries? ((db-type (eql :mysql)))
737 #+(or mysql-client-v4.1 mysql-client-v5) t
738 #-(or mysql-client-v4.1 mysql-client-v5) nil)
740 (defmethod db-type-has-boolean-where? ((db-type (eql :mysql)))
741 #+(or mysql-client-v4.1 mysql-client-v5) t
742 #-(or mysql-client-v4.1 mysql-client-v5) nil)
744 (defmethod db-type-has-union? ((db-type (eql :mysql)))
745 (not (eql (schar mysql::*mysql-client-info* 0) #\3)))
747 (defmethod db-type-transaction-capable? ((db-type (eql :mysql)) database)
748 (let ((tuple (car (database-query "SHOW VARIABLES LIKE 'HAVE_INNODB'" database :auto nil))))
749 (and tuple (string-equal "YES" (second tuple)))))
751 (defmethod db-type-has-prepared-stmt? ((db-type (eql :mysql)))
752 #+(or mysql-client-v4.1 mysql-client-v5) t
753 #-(or mysql-client-v4.1 mysql-client-v5) nil)
755 (defmethod db-type-has-auto-increment? ((db-type (eql :mysql)))
758 (when (clsql-sys:database-type-library-loaded :mysql)
759 (clsql-sys:initialize-database-type :database-type :mysql))