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 (:import-from :clsql-sys
20 :escaped :unescaped :combine-database-identifiers
21 :escaped-database-identifier :unescaped-database-identifier :database-identifier
22 :%sequence-name-to-table :%table-name-to-sequence-name)
23 (:documentation "This is the CLSQL interface to MySQL."))
25 (in-package #:clsql-mysql)
27 ;;; Field conversion functions
29 (defun result-field-names (res-ptr)
31 (mysql-field-seek res-ptr 0)
33 (let ((field (mysql-fetch-field res-ptr)))
34 (when (uffi:null-pointer-p field) (return))
35 (push (uffi:convert-from-cstring (clsql-mysql-field-name field)) names)))
38 (defun make-type-list-for-auto (res-ptr)
39 (let ((new-types '()))
40 (mysql-field-seek res-ptr 0)
42 (let ((field (mysql-fetch-field res-ptr)))
43 (when (uffi:null-pointer-p field) (return))
44 (let* ((flags (clsql-mysql-field-flags field))
45 (unsigned (plusp (logand flags 32)))
46 (type (clsql-mysql-field-type field)))
49 ((#.mysql-field-types#tiny
50 #.mysql-field-types#short
51 #.mysql-field-types#int24)
55 (#.mysql-field-types#long
59 (#.mysql-field-types#longlong
63 ((#.mysql-field-types#double
64 #.mysql-field-types#float
65 #.mysql-field-types#decimal)
70 (nreverse new-types)))
72 (defun canonicalize-types (types res-ptr)
74 (let ((auto-list (make-type-list-for-auto res-ptr)))
77 (canonicalize-type-list types auto-list))
83 (defmethod database-initialize-database-type ((database-type (eql :mysql)))
86 ;;(uffi:def-type mysql-mysql-ptr-def (* mysql-mysql))
87 ;;(uffi:def-type mysql-mysql-res-ptr-def (* mysql-mysql-res))
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-row-def mysql-row)
92 (defclass mysql-database (database)
93 ((mysql-ptr :accessor database-mysql-ptr :initarg :mysql-ptr
94 :type mysql-mysql-ptr-def)
95 (server-info :accessor database-server-info :initarg :server-info
98 (defmethod database-type ((database mysql-database))
101 (defmethod database-name-from-spec (connection-spec (database-type (eql :mysql)))
102 (check-connection-spec connection-spec database-type
103 (host db user password &optional port options))
104 (destructuring-bind (host db user password &optional port options) connection-spec
105 (declare (ignore password options))
109 (pathname (namestring host))
115 (integer (write-to-string port))
120 (defun lookup-option-code (option)
121 (if (assoc option +mysql-option-parameter-map+)
122 (symbol-value (intern
123 (concatenate 'string (symbol-name-default-case "mysql-option#")
124 (symbol-name option))
125 (symbol-name '#:mysql)))
127 (warn "Unknown mysql option name ~A - ignoring.~%" option)
130 (defun set-mysql-options (mysql-ptr options)
131 (flet ((lookup-option-type (option)
132 (cdr (assoc option +mysql-option-parameter-map+))))
133 (dolist (option options)
135 (let ((option-code (lookup-option-code option)))
137 (mysql-options mysql-ptr option-code uffi:+null-cstring-pointer+)))
138 (destructuring-bind (name . value) option
139 (let ((option-code (lookup-option-code name)))
141 (case (lookup-option-type name)
143 (mysql-options mysql-ptr option-code uffi:+null-cstring-pointer+))
146 (uffi:with-foreign-string (fs value)
147 (mysql-options mysql-ptr option-code fs))
148 (warn "Expecting string argument for mysql option ~A, got ~A ~
153 (uffi:with-foreign-object (fo :unsigned-int)
154 (setf (uffi:deref-pointer fo :unsigned-int) value)
155 (mysql-options mysql-ptr option-code fo))
156 (warn "Expecting integer argument for mysql option ~A, got ~A ~
160 (uffi:with-foreign-object (fo :byte)
161 (setf (uffi:deref-pointer fo :byte)
165 (mysql-options mysql-ptr option-code fo)))))))))))
167 (defmethod database-connect (connection-spec (database-type (eql :mysql)))
168 (check-connection-spec connection-spec database-type
169 (host db user password &optional port options))
170 (destructuring-bind (host db user password &optional port options) connection-spec
171 (let ((mysql-ptr (mysql-init (uffi:make-null-pointer 'mysql-mysql)))
173 (if (uffi:null-pointer-p mysql-ptr)
174 (error 'sql-connection-error
175 :database-type database-type
176 :connection-spec connection-spec
177 :error-id (mysql-errno mysql-ptr)
178 :message (mysql-error-string mysql-ptr))
179 (uffi:with-cstrings ((host-native host)
181 (password-native password)
183 (socket-native socket))
185 (set-mysql-options mysql-ptr options))
186 (let ((error-occurred nil))
188 (if (uffi:null-pointer-p
190 mysql-ptr host-native user-native password-native
195 (string (parse-integer port)))
198 (setq error-occurred t)
199 (error 'sql-connection-error
200 :database-type database-type
201 :connection-spec connection-spec
202 :error-id (mysql-errno mysql-ptr)
203 :message (mysql-error-string mysql-ptr)))
205 (make-instance 'mysql-database
206 :name (database-name-from-spec connection-spec
208 :database-type :mysql
209 :connection-spec connection-spec
210 :server-info (uffi:convert-from-cstring
211 (mysql:mysql-get-server-info mysql-ptr))
212 :mysql-ptr mysql-ptr))
213 (cmd "SET SESSION sql_mode='ANSI'"))
214 (uffi:with-cstring (cmd-cs cmd)
215 (if (zerop (mysql-real-query mysql-ptr cmd-cs (uffi:foreign-encoded-octet-count
216 cmd :encoding (encoding db))))
219 (warn "Error setting ANSI mode for MySQL.")
221 (when error-occurred (mysql-close mysql-ptr)))))))))
224 (defmethod database-disconnect ((database mysql-database))
225 (mysql-close (database-mysql-ptr database))
226 (setf (database-mysql-ptr database) nil)
229 (defmethod database-execute-command (sql-expression (database mysql-database))
230 (uffi:with-cstring (sql-native sql-expression)
231 (let ((mysql-ptr (database-mysql-ptr database)))
232 (declare (type mysql-mysql-ptr-def mysql-ptr))
233 (if (zerop (mysql-real-query mysql-ptr sql-native
234 (uffi:foreign-encoded-octet-count
235 sql-expression :encoding (encoding database))))
237 (error 'sql-database-data-error
239 :expression sql-expression
240 :error-id (mysql-errno mysql-ptr)
241 :message (mysql-error-string mysql-ptr))))))
244 (defmethod database-query (query-expression (database mysql-database)
245 result-types field-names)
246 (declare (optimize (speed 3)))
247 (let ((mysql-ptr (database-mysql-ptr database))
248 (results nil) ;; all the results and column-names in reverse-order
249 res-ptr (num-fields 0))
250 (declare (type mysql-mysql-ptr-def mysql-ptr res-ptr)
252 (when (database-execute-command query-expression database)
254 ((get-row (row lengths)
255 "Pull a single row value from the results"
256 (loop for i from 0 below num-fields
259 (uffi:deref-array row '(:array (* :unsigned-char))
263 (uffi:deref-array lengths '(:array :unsigned-long)
265 :encoding (encoding database))))
267 "get all the rows out of the now valid results set"
268 (loop for row = (mysql-fetch-row res-ptr)
269 for lengths = (mysql-fetch-lengths res-ptr)
270 until (uffi:null-pointer-p row)
271 collect (get-row row lengths)))
273 "for a mysql-ptr, grab and return a results set"
274 (setf res-ptr (mysql-use-result mysql-ptr))
276 ((or (null res-ptr) (uffi:null-pointer-p res-ptr))
277 (unless (zerop (mysql-errno mysql-ptr))
278 ;;from http://dev.mysql.com/doc/refman/5.0/en/mysql-field-count.html
279 ;; if mysql_use_result or mysql_store_result return a null ptr,
280 ;; we use a mysql_errno check to see if it had a problem or just
281 ;; was a query without a result. If no error, just return nil.
282 (error 'sql-database-data-error
284 :expression query-expression
285 :error-id (mysql-errno mysql-ptr)
286 :message (mysql-error-string mysql-ptr))))
289 (progn (setf num-fields (mysql-num-fields res-ptr)
290 result-types (canonicalize-types
291 result-types res-ptr))
292 (push (get-result-rows) results)
293 (push (when field-names
294 (result-field-names res-ptr))
296 (mysql-free-result res-ptr))))))
300 while (let ((next (mysql-next-result mysql-ptr)))
302 (0 t) ;Successful and there are more results
303 (-1 nil) ;Successful and there are no more results
306 (values-list (nreverse results))))))
309 (defstruct mysql-result-set
310 (res-ptr (uffi:make-null-pointer 'mysql-mysql-res) :type mysql-mysql-res-ptr-def)
311 (types nil :type list)
312 (num-fields 0 :type fixnum)
313 (full-set nil :type boolean))
316 (defmethod database-query-result-set ((query-expression string)
317 (database mysql-database)
318 &key full-set result-types)
319 ;; TODO: REFACTOR THIS IN TERMS OF database-query or vice-versa
320 ;; This doesnt seem to free database results reliably, dont think
321 ;; that we should allow that particularly, OTOH, dont know how
322 ;; we support cursoring without it
323 (let ((mysql-ptr (database-mysql-ptr database)))
324 (declare (type mysql-mysql-ptr-def mysql-ptr))
325 (when (database-execute-command query-expression database)
326 (let ((res-ptr (if full-set
327 (mysql-store-result mysql-ptr)
328 (mysql-use-result mysql-ptr))))
329 (declare (type mysql-mysql-res-ptr-def res-ptr))
330 (if (not (uffi:null-pointer-p res-ptr))
331 (let* ((num-fields (mysql-num-fields res-ptr))
332 (result-set (make-mysql-result-set
334 :num-fields num-fields
338 result-types res-ptr))))
342 (mysql-num-rows res-ptr))
345 (error 'sql-database-data-error
347 :expression query-expression
348 :error-id (mysql-errno mysql-ptr)
349 :message (mysql-error-string mysql-ptr))))
352 (defmethod database-dump-result-set (result-set (database mysql-database))
353 (mysql-free-result (mysql-result-set-res-ptr result-set))
357 (defmethod database-store-next-row (result-set (database mysql-database) list)
358 (let* ((res-ptr (mysql-result-set-res-ptr result-set))
359 (row (mysql-fetch-row res-ptr))
360 (lengths (mysql-fetch-lengths res-ptr))
361 (types (mysql-result-set-types result-set)))
362 (declare (type mysql-mysql-res-ptr-def res-ptr)
363 (type mysql-row-def row))
364 (unless (uffi:null-pointer-p row)
365 (loop for i from 0 below (mysql-result-set-num-fields result-set)
370 (uffi:deref-array row '(:array (* :unsigned-char)) i)
373 (uffi:deref-array lengths '(:array :unsigned-long) i)
374 :encoding (encoding database))))
378 ;; Table and attribute introspection
380 (defmethod database-list-tables ((database mysql-database) &key (owner nil))
381 (declare (ignore owner))
383 ((eql #\5 (char (database-server-info database) 0))
384 (loop for (name type) in (database-query "SHOW FULL TABLES" database nil nil)
385 when (and (string-equal type "base table")
386 (not (and (>= (length name) 11)
387 (string-equal (subseq name 0 11) "_CLSQL_SEQ_"))))
390 (remove-if #'(lambda (s)
391 (and (>= (length s) 11)
392 (string-equal (subseq s 0 11) "_CLSQL_SEQ_")))
393 (mapcar #'car (database-query "SHOW TABLES" database nil nil))))))
395 (defmethod database-list-views ((database mysql-database)
397 (declare (ignore owner))
399 ((eql #\5 (char (database-server-info database) 0))
400 (loop for (name type) in (database-query "SHOW FULL TABLES" database nil nil)
401 when (string-equal type "view")
406 (defmethod database-list-indexes ((database mysql-database)
409 (dolist (table (database-list-tables database :owner owner) result)
411 (append (database-list-table-indexes table database :owner owner)
414 (defmethod database-list-table-indexes (table (database mysql-database)
416 (declare (ignore owner))
418 (rows (database-query
419 (format nil "SHOW INDEX FROM ~A" (escaped-database-identifier
423 ((null rows) (nreverse results))
424 (let ((col (nth 2 (car rows))))
425 (unless (find col results :test #'string-equal)
426 (push col results)))))
428 (defmethod database-list-attributes ((table clsql-sys::%database-identifier)
429 (database mysql-database)
431 &aux (table (unescaped-database-identifier table)))
432 (declare (ignore owner))
435 (format nil "SHOW COLUMNS FROM ~A" (escaped-database-identifier
439 (defmethod database-attribute-type ((attribute clsql-sys::%database-identifier)
440 (table clsql-sys::%database-identifier)
441 (database mysql-database)
443 &aux (table (unescaped-database-identifier table))
444 (attribute (unescaped-database-identifier attribute)))
445 (declare (ignore owner))
446 (let ((row (car (database-query
448 "SHOW COLUMNS FROM ~A LIKE '~A'"
449 (escaped-database-identifier
451 (unescaped-database-identifier
454 (let* ((raw-type (second row))
456 (start-length (position #\( raw-type))
457 (type (if start-length
458 (subseq raw-type 0 start-length)
460 (length (when start-length
461 (parse-integer (subseq raw-type (1+ start-length))
464 (values (ensure-keyword type) length nil (if (string-equal null "YES") 1 0))))))
466 ;;; Sequence functions
468 (defmethod database-create-sequence (sequence-name
469 (database mysql-database))
470 (let ((table-name (%sequence-name-to-table sequence-name database)))
471 (database-execute-command
472 (concatenate 'string "CREATE TABLE " table-name
473 " (id int NOT NULL PRIMARY KEY AUTO_INCREMENT)")
475 (database-execute-command
476 (concatenate 'string "INSERT INTO " table-name
480 (defmethod database-drop-sequence (sequence-name
481 (database mysql-database))
482 (database-execute-command
483 (concatenate 'string "DROP TABLE "
484 (%sequence-name-to-table sequence-name database))
487 (defmethod database-list-sequences ((database mysql-database)
489 (declare (ignore owner))
490 (mapcan #'(lambda (s)
491 (let ((sn (%table-name-to-sequence-name (car s))))
492 (and sn (list (car s) sn))))
493 (database-query "SHOW TABLES" database nil nil)))
495 (defmethod database-set-sequence-position (sequence-name
497 (database mysql-database))
498 (database-execute-command
499 (format nil "UPDATE ~A SET id=~A" (%sequence-name-to-table sequence-name database)
502 (mysql:mysql-insert-id (clsql-mysql::database-mysql-ptr database)))
504 (defmethod database-sequence-next (sequence-name (database mysql-database))
506 (database-execute-command
507 (concatenate 'string "UPDATE " (%sequence-name-to-table sequence-name database)
508 " SET id=LAST_INSERT_ID(id+1)")
510 (mysql:mysql-insert-id (clsql-mysql::database-mysql-ptr database))))
512 (defmethod database-sequence-last (sequence-name (database mysql-database))
514 (caar (database-query
515 (concatenate 'string "SELECT id from "
516 (%sequence-name-to-table sequence-name database))
517 database :auto nil))))
519 (defmethod database-last-auto-increment-id ((database mysql-database) table column)
520 (declare (ignore table column))
521 (car (query "SELECT LAST_INSERT_ID();"
522 :flatp t :field-names nil
523 :database database)))
525 (defmethod database-create (connection-spec (type (eql :mysql)))
526 (destructuring-bind (host name user password) connection-spec
527 (let ((database (database-connect (list host "" user password)
529 (setf (slot-value database 'clsql-sys::state) :open)
531 (database-execute-command (format nil "create database ~A" name) database)
532 (database-disconnect database)))))
534 (defmethod database-destroy (connection-spec (type (eql :mysql)))
535 (destructuring-bind (host name user password) connection-spec
536 (let ((database (database-connect (list host "" user password)
538 (setf (slot-value database 'clsql-sys::state) :open)
540 (database-execute-command (format nil "drop database ~A" name) database)
541 (database-disconnect database)))))
543 (defmethod database-probe (connection-spec (type (eql :mysql)))
544 (when (find (second connection-spec) (database-list connection-spec type)
545 :test #'string-equal)
548 (defmethod database-list (connection-spec (type (eql :mysql)))
549 (destructuring-bind (host name user password &optional port options) connection-spec
550 (declare (ignore options))
551 (let ((database (database-connect (list host (or name "mysql")
552 user password port) type)))
555 (setf (slot-value database 'clsql-sys::state) :open)
556 (mapcar #'car (database-query "show databases" database :auto nil)))
558 (database-disconnect database)
559 (setf (slot-value database 'clsql-sys::state) :closed))))))
562 ;;; Prepared statements
564 (defclass mysql-stmt ()
565 ((database :initarg :database :reader database)
566 (stmt :initarg :stmt :accessor stmt)
567 (input-bind :initarg :input-bind :reader input-bind)
568 (output-bind :initarg :output-bind :reader output-bind)
569 (types :initarg :types :reader types)
570 (result-set :initarg :result-set :reader result-set)
571 (num-fields :initarg :num-fields :reader num-fields)
572 (field-names :initarg :field-names :accessor stmt-field-names)
573 (length-ptr :initarg :length-ptr :reader length-ptr)
574 (is-null-ptr :initarg :is-null-ptr :reader is-null-ptr)
575 (result-types :initarg :result-types :reader result-types)))
577 (defun clsql-type->mysql-type (type)
579 ((in type :null) mysql-field-types#null)
580 ((in type :int :integer) mysql-field-types#long)
581 ((in type :short) mysql-field-types#short)
582 ((in type :bigint) mysql-field-types#longlong)
583 ((in type :float :double :number) mysql-field-types#double)
584 ((and (consp type) (in (car type) :char :string :varchar)) mysql-field-types#var-string)
585 ((or (eq type :blob) (and (consp type) (in (car type) :blob))) mysql-field-types#var-string)
587 (error 'sql-user-error
589 (format nil "Unknown clsql type ~A." type)))))
592 (defmethod database-prepare (sql-stmt types (database mysql-database) result-types field-names)
593 (let* ((mysql-ptr (database-mysql-ptr database))
594 (stmt (mysql-stmt-init mysql-ptr)))
595 (when (uffi:null-pointer-p stmt)
596 (error 'sql-database-error
597 :error-id (mysql-errno mysql-ptr)
598 :message (mysql-error-string mysql-ptr)))
600 (uffi:with-cstring (native-query sql-stmt)
601 (unless (zerop (mysql-stmt-prepare stmt native-query (uffi:foreign-encoded-octet-count
602 sql-stmt :encoding (encoding database))))
603 (mysql-stmt-close stmt)
604 (error 'sql-database-error
605 :error-id (mysql-errno mysql-ptr)
606 :message (mysql-error-string mysql-ptr))))
608 (unless (= (mysql-stmt-param-count stmt) (length types))
609 (mysql-stmt-close stmt)
610 (error 'sql-database-error
612 (format nil "Mysql param count (~D) does not match number of types (~D)"
613 (mysql-stmt-param-count stmt) (length types))))
615 (let ((rs (mysql-stmt-result-metadata stmt)))
616 (when (uffi:null-pointer-p rs)
617 (warn "mysql_stmt_result_metadata returned NULL")
619 (mysql-stmt-close stmt)
621 (error 'sql-database-error
622 :message "mysql_stmt_result_metadata returned NULL"))
624 (let ((input-bind (uffi:allocate-foreign-object 'mysql-bind (length types)))
625 (mysql-types (mapcar 'clsql-type->mysql-type types))
626 field-vec num-fields is-null-ptr output-bind length-ptr)
629 (dotimes (i (length types))
630 (let* ((binding (uffi:deref-array input-bind '(:array mysql-bind) i)))
631 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-type)
633 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-length) 0)))
636 (unless (uffi:null-pointer-p rs)
637 (setq field-vec (mysql-fetch-fields rs)
638 num-fields (mysql-num-fields rs)
639 is-null-ptr (uffi:allocate-foreign-object :byte num-fields)
640 output-bind (uffi:allocate-foreign-object 'mysql-bind num-fields)
641 length-ptr (uffi:allocate-foreign-object :unsigned-long num-fields))
642 (dotimes (i num-fields)
644 (let* ((field (uffi:deref-array field-vec '(:array mysql-field) i))
645 (type (uffi:get-slot-value field 'mysql-field 'type))
646 (binding (uffi:deref-array output-bind '(:array mysql-bind) i)))
647 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-type) type)
649 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-length) 0)
650 #+need-to-allocate-foreign-object-for-this
651 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::is-null)
652 (+ i (uffi:pointer-address is-null-ptr)))
653 #+need-to-allocate-foreign-object-for-this
654 (setf (uffi:get-slot-value binding 'mysql-bind 'length)
655 (+ (* i 8) (uffi:pointer-address length-ptr)))
658 ((#.mysql-field-types#var-string #.mysql-field-types#string
659 #.mysql-field-types#tiny-blob #.mysql-field-types#blob
660 #.mysql-field-types#medium-blob #.mysql-field-types#long-blob)
661 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-length) 1024)
662 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
663 (uffi:allocate-foreign-object :unsigned-char 1024)))
664 (#.mysql-field-types#tiny
665 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
666 (uffi:allocate-foreign-object :byte)))
667 (#.mysql-field-types#short
668 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
669 (uffi:allocate-foreign-object :short)))
670 (#.mysql-field-types#long
671 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
672 ;; segfaults if supply :int on amd64
673 (uffi:allocate-foreign-object :long)))
675 (#.mysql-field-types#longlong
676 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
677 (uffi:allocate-foreign-object :long)))
678 (#.mysql-field-types#float
679 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
680 (uffi:allocate-foreign-object :float)))
681 (#.mysql-field-types#double
682 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
683 (uffi:allocate-foreign-object :double)))
684 ((#.mysql-field-types#time #.mysql-field-types#date
685 #.mysql-field-types#datetime #.mysql-field-types#timestamp)
686 (uffi:allocate-foreign-object 'mysql-time))
688 (error "mysql type ~D not supported." type)))))
690 (unless (zerop (mysql-stmt-bind-result stmt output-bind))
691 (mysql-stmt-close stmt)
692 (error 'sql-database-error
693 :error-id (mysql-stmt-errno stmt)
694 :message (uffi:convert-from-cstring
695 (mysql-stmt-error stmt)))))
697 (make-instance 'mysql-stmt
700 :num-fields num-fields
701 :input-bind input-bind
702 :output-bind output-bind
704 :result-types result-types
705 :length-ptr length-ptr
706 :is-null-ptr is-null-ptr
708 :field-names field-names)))))
711 (defmethod database-bind-parameter ((stmt mysql-stmt) position value)
712 ;; FIXME: will need to allocate bind structure. This should probably be
713 ;; done in C since the API is not mature and may change
714 (let ((binding (uffi:deref-array (input-bind stmt) '(:array mysql-bind) (1- position)))
715 (type (nth (1- position) (types stmt))))
716 (setf (uffi:get-slot-value binding 'mysql-bind 'length) 0)
719 (when (is-null-ptr stmt)
720 (setf (uffi:deref-array (is-null-ptr stmt) '(:array :byte) (1- position)) 1)))
722 (when (is-null-ptr stmt)
723 (setf (uffi:deref-array (is-null-ptr stmt) '(:array :byte) (1- position)) 0))
725 (#.mysql-field-types#long
726 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer) value))
728 (warn "Unknown input bind type ~D." type))
732 (defmethod database-run-prepared ((stmt mysql-stmt))
734 (when (input-bind stmt)
735 (unless (zerop (mysql-stmt-bind-param (stmt stmt) (input-bind stmt)))
736 (error 'sql-database-error
737 :error-id (mysql-stmt-errno (stmt stmt))
738 :message (uffi:convert-from-cstring
739 (mysql-stmt-error (stmt stmt))))))
741 (unless (zerop (mysql-stmt-execute (stmt stmt)))
742 (error 'sql-database-error
743 :error-id (mysql-stmt-errno (stmt stmt))
744 :message (uffi:convert-from-cstring
745 (mysql-stmt-error (stmt stmt)))))
747 (unless (zerop (mysql-stmt-store-result (stmt stmt)))
748 (error 'sql-database-error
749 :error-id (mysql-stmt-errno (stmt stmt))
750 :message (uffi:convert-from-cstring
751 (mysql-stmt-error (stmt stmt)))))
752 (database-fetch-prepared-rows stmt))
755 (defun database-fetch-prepared-rows (stmt)
756 (do ((rc (mysql-stmt-fetch (stmt stmt)) (mysql-stmt-fetch (stmt stmt)))
757 (num-fields (num-fields stmt))
759 ((not (zerop rc)) (nreverse rows))
761 (loop for i from 0 below num-fields
764 (not (zerop (uffi:ensure-char-integer
765 (uffi:deref-array (is-null-ptr stmt) '(:array :byte) i))))))
767 (let* ((bind (uffi:deref-array (output-bind stmt) '(:array mysql-bind) i))
768 (type (uffi:get-slot-value bind 'mysql-bind 'mysql::buffer-type))
769 (buffer (uffi:get-slot-value bind 'mysql-bind 'mysql::buffer)))
771 ((#.mysql-field-types#var-string #.mysql-field-types#string
772 #.mysql-field-types#tiny-blob #.mysql-field-types#blob
773 #.mysql-field-types#medium-blob #.mysql-field-types#long-blob)
774 (uffi:convert-from-foreign-string buffer :encoding (encoding (database stmt))))
775 (#.mysql-field-types#tiny
776 (uffi:ensure-char-integer
777 (uffi:deref-pointer buffer :byte)))
778 (#.mysql-field-types#short
779 (uffi:deref-pointer buffer :short))
780 (#.mysql-field-types#long
781 (uffi:deref-pointer buffer :int))
783 (#.mysql-field-types#longlong
784 (uffi:deref-pointer buffer :long))
785 (#.mysql-field-types#float
786 (uffi:deref-pointer buffer :float))
787 (#.mysql-field-types#double
788 (uffi:deref-pointer buffer :double))
789 ((#.mysql-field-types#time #.mysql-field-types#date
790 #.mysql-field-types#datetime #.mysql-field-types#timestamp)
791 (let ((year (uffi:get-slot-value buffer 'mysql-time 'mysql::year))
792 (month (uffi:get-slot-value buffer 'mysql-time 'mysql::month))
793 (day (uffi:get-slot-value buffer 'mysql-time 'mysql::day))
794 (hour (uffi:get-slot-value buffer 'mysql-time 'mysql::hour))
795 (minute (uffi:get-slot-value buffer 'mysql-time 'mysql::minute))
796 (second (uffi:get-slot-value buffer 'mysql-time 'mysql::second)))
798 (make-time :year year :month month :day day :hour hour
799 :minute minute :second second))))
808 (defmethod database-free-prepared ((stmt mysql-stmt))
809 (with-slots (stmt) stmt
810 (mysql-stmt-close stmt))
814 ;;; Database capabilities
816 (defmethod db-type-use-column-on-drop-index? ((db-type (eql :mysql)))
819 (defmethod db-type-has-views? ((db-type (eql :mysql)))
821 #-mysql-client-v5 nil)
823 (defmethod db-type-has-subqueries? ((db-type (eql :mysql)))
824 #+(or mysql-client-v4.1 mysql-client-v5) t
825 #-(or mysql-client-v4.1 mysql-client-v5) nil)
827 (defmethod db-type-has-boolean-where? ((db-type (eql :mysql)))
828 #+(or mysql-client-v4.1 mysql-client-v5) t
829 #-(or mysql-client-v4.1 mysql-client-v5) nil)
831 (defmethod db-type-has-union? ((db-type (eql :mysql)))
832 (not (eql (schar mysql::*mysql-client-info* 0) #\3)))
834 (defmethod db-type-transaction-capable? ((db-type (eql :mysql)) database)
835 (let ((tuple (car (database-query "SHOW VARIABLES LIKE 'HAVE_INNODB'" database :auto nil))))
836 (and tuple (string-equal "YES" (second tuple)))))
838 (defmethod db-type-has-prepared-stmt? ((db-type (eql :mysql)))
839 #+(or mysql-client-v4.1 mysql-client-v5) t
840 #-(or mysql-client-v4.1 mysql-client-v5) nil)
842 (defmethod db-type-has-auto-increment? ((db-type (eql :mysql)))
845 (when (clsql-sys:database-type-library-loaded :mysql)
846 (clsql-sys:initialize-database-type :database-type :mysql))