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
251 (declare (type mysql-mysql-ptr-def mysql-ptr)
252 (type (or null mysql-mysql-res-ptr-def) res-ptr)
254 (when (database-execute-command query-expression database)
256 ((get-row (row lengths)
257 "Pull a single row value from the results"
258 (loop for i from 0 below num-fields
261 (uffi:deref-array row '(:array (* :unsigned-char))
265 (uffi:deref-array lengths '(:array :unsigned-long)
267 :encoding (encoding database))))
269 "get all the rows out of the now valid results set"
270 (loop for row = (mysql-fetch-row res-ptr)
271 for lengths = (mysql-fetch-lengths res-ptr)
272 until (uffi:null-pointer-p row)
273 collect (get-row row lengths)))
275 "for a mysql-ptr, grab and return a results set"
276 (setf res-ptr (mysql-use-result mysql-ptr))
278 ((or (null res-ptr) (uffi:null-pointer-p res-ptr))
279 (unless (zerop (mysql-errno mysql-ptr))
280 ;;from http://dev.mysql.com/doc/refman/5.0/en/mysql-field-count.html
281 ;; if mysql_use_result or mysql_store_result return a null ptr,
282 ;; we use a mysql_errno check to see if it had a problem or just
283 ;; was a query without a result. If no error, just return nil.
284 (error 'sql-database-data-error
286 :expression query-expression
287 :error-id (mysql-errno mysql-ptr)
288 :message (mysql-error-string mysql-ptr))))
291 (progn (setf num-fields (mysql-num-fields res-ptr)
292 result-types (canonicalize-types
293 result-types res-ptr))
294 (push (get-result-rows) results)
295 (push (when field-names
296 (result-field-names res-ptr))
298 (mysql-free-result res-ptr))))))
302 while (let ((next (mysql-next-result mysql-ptr)))
304 (0 t) ;Successful and there are more results
305 (-1 nil) ;Successful and there are no more results
308 (values-list (nreverse results))))))
311 (defstruct mysql-result-set
312 (res-ptr (uffi:make-null-pointer 'mysql-mysql-res) :type mysql-mysql-res-ptr-def)
313 (types nil :type list)
314 (num-fields 0 :type fixnum)
315 (full-set nil :type boolean))
318 (defmethod database-query-result-set ((query-expression string)
319 (database mysql-database)
320 &key full-set result-types)
321 ;; TODO: REFACTOR THIS IN TERMS OF database-query or vice-versa
322 ;; This doesnt seem to free database results reliably, dont think
323 ;; that we should allow that particularly, OTOH, dont know how
324 ;; we support cursoring without it
325 (let ((mysql-ptr (database-mysql-ptr database)))
326 (declare (type mysql-mysql-ptr-def mysql-ptr))
327 (when (database-execute-command query-expression database)
328 (let ((res-ptr (if full-set
329 (mysql-store-result mysql-ptr)
330 (mysql-use-result mysql-ptr))))
331 (declare (type mysql-mysql-res-ptr-def res-ptr))
332 (if (not (uffi:null-pointer-p res-ptr))
333 (let* ((num-fields (mysql-num-fields res-ptr))
334 (result-set (make-mysql-result-set
336 :num-fields num-fields
340 result-types res-ptr))))
344 (mysql-num-rows res-ptr))
347 (error 'sql-database-data-error
349 :expression query-expression
350 :error-id (mysql-errno mysql-ptr)
351 :message (mysql-error-string mysql-ptr))))
354 (defmethod database-dump-result-set (result-set (database mysql-database))
355 (mysql-free-result (mysql-result-set-res-ptr result-set))
359 (defmethod database-store-next-row (result-set (database mysql-database) list)
360 (let* ((res-ptr (mysql-result-set-res-ptr result-set))
361 (row (mysql-fetch-row res-ptr))
362 (lengths (mysql-fetch-lengths res-ptr))
363 (types (mysql-result-set-types result-set)))
364 (declare (type mysql-mysql-res-ptr-def res-ptr)
365 (type mysql-row-def row))
366 (unless (uffi:null-pointer-p row)
367 (loop for i from 0 below (mysql-result-set-num-fields result-set)
372 (uffi:deref-array row '(:array (* :unsigned-char)) i)
375 (uffi:deref-array lengths '(:array :unsigned-long) i)
376 :encoding (encoding database))))
380 ;; Table and attribute introspection
382 (defmethod database-list-tables ((database mysql-database) &key (owner nil))
383 (declare (ignore owner))
385 ((eql #\5 (char (database-server-info database) 0))
386 (loop for (name type) in (database-query "SHOW FULL TABLES" database nil nil)
387 when (and (string-equal type "base table")
388 (not (and (>= (length name) 11)
389 (string-equal (subseq name 0 11) "_CLSQL_SEQ_"))))
392 (remove-if #'(lambda (s)
393 (and (>= (length s) 11)
394 (string-equal (subseq s 0 11) "_CLSQL_SEQ_")))
395 (mapcar #'car (database-query "SHOW TABLES" database nil nil))))))
397 (defmethod database-list-views ((database mysql-database)
399 (declare (ignore owner))
401 ((eql #\5 (char (database-server-info database) 0))
402 (loop for (name type) in (database-query "SHOW FULL TABLES" database nil nil)
403 when (string-equal type "view")
408 (defmethod database-list-indexes ((database mysql-database)
411 (dolist (table (database-list-tables database :owner owner) result)
413 (append (database-list-table-indexes table database :owner owner)
416 (defmethod database-list-table-indexes (table (database mysql-database)
418 (declare (ignore owner))
420 (rows (database-query
421 (format nil "SHOW INDEX FROM ~A" (escaped-database-identifier
425 ((null rows) (nreverse results))
426 (let ((col (nth 2 (car rows))))
427 (unless (find col results :test #'string-equal)
428 (push col results)))))
430 (defmethod database-list-attributes ((table clsql-sys::%database-identifier)
431 (database mysql-database)
433 &aux (table (unescaped-database-identifier table)))
434 (declare (ignore owner))
437 (format nil "SHOW COLUMNS FROM ~A" (escaped-database-identifier
441 (defmethod database-attribute-type ((attribute clsql-sys::%database-identifier)
442 (table clsql-sys::%database-identifier)
443 (database mysql-database)
445 &aux (table (unescaped-database-identifier table))
446 (attribute (unescaped-database-identifier attribute)))
447 (declare (ignore owner))
448 (let ((row (car (database-query
450 "SHOW COLUMNS FROM ~A LIKE '~A'"
451 (escaped-database-identifier
453 (unescaped-database-identifier
456 (let* ((raw-type (second row))
458 (start-length (position #\( raw-type))
459 (type (if start-length
460 (subseq raw-type 0 start-length)
462 (length (when start-length
463 (parse-integer (subseq raw-type (1+ start-length))
466 (values (ensure-keyword type) length nil (if (string-equal null "YES") 1 0))))))
468 ;;; Sequence functions
470 (defmethod database-create-sequence (sequence-name
471 (database mysql-database))
472 (let ((table-name (%sequence-name-to-table sequence-name database)))
473 (database-execute-command
474 (concatenate 'string "CREATE TABLE " table-name
475 " (id int NOT NULL PRIMARY KEY AUTO_INCREMENT)")
477 (database-execute-command
478 (concatenate 'string "INSERT INTO " table-name
482 (defmethod database-drop-sequence (sequence-name
483 (database mysql-database))
484 (database-execute-command
485 (concatenate 'string "DROP TABLE "
486 (%sequence-name-to-table sequence-name database))
489 (defmethod database-list-sequences ((database mysql-database)
491 (declare (ignore owner))
492 (mapcan #'(lambda (s)
493 (let ((sn (%table-name-to-sequence-name (car s))))
494 (and sn (list (car s) sn))))
495 (database-query "SHOW TABLES" database nil nil)))
497 (defmethod database-set-sequence-position (sequence-name
499 (database mysql-database))
500 (database-execute-command
501 (format nil "UPDATE ~A SET id=~A" (%sequence-name-to-table sequence-name database)
504 (mysql:mysql-insert-id (clsql-mysql::database-mysql-ptr database)))
506 (defmethod database-sequence-next (sequence-name (database mysql-database))
508 (database-execute-command
509 (concatenate 'string "UPDATE " (%sequence-name-to-table sequence-name database)
510 " SET id=LAST_INSERT_ID(id+1)")
512 (mysql:mysql-insert-id (clsql-mysql::database-mysql-ptr database))))
514 (defmethod database-sequence-last (sequence-name (database mysql-database))
516 (caar (database-query
517 (concatenate 'string "SELECT id from "
518 (%sequence-name-to-table sequence-name database))
519 database :auto nil))))
521 (defmethod database-last-auto-increment-id ((database mysql-database) table column)
522 (declare (ignore table column))
523 (car (query "SELECT LAST_INSERT_ID();"
524 :flatp t :field-names nil
525 :database database)))
527 (defmethod database-create (connection-spec (type (eql :mysql)))
528 (destructuring-bind (host name user password) connection-spec
529 (let ((database (database-connect (list host "" user password)
531 (setf (slot-value database 'clsql-sys::state) :open)
533 (database-execute-command (format nil "create database ~A" name) database)
534 (database-disconnect database)))))
536 (defmethod database-destroy (connection-spec (type (eql :mysql)))
537 (destructuring-bind (host name user password) connection-spec
538 (let ((database (database-connect (list host "" user password)
540 (setf (slot-value database 'clsql-sys::state) :open)
542 (database-execute-command (format nil "drop database ~A" name) database)
543 (database-disconnect database)))))
545 (defmethod database-probe (connection-spec (type (eql :mysql)))
546 (when (find (second connection-spec) (database-list connection-spec type)
547 :test #'string-equal)
550 (defmethod database-list (connection-spec (type (eql :mysql)))
551 (destructuring-bind (host name user password &optional port options) connection-spec
552 (declare (ignore options))
553 (let ((database (database-connect (list host (or name "mysql")
554 user password port) type)))
557 (setf (slot-value database 'clsql-sys::state) :open)
558 (mapcar #'car (database-query "show databases" database :auto nil)))
560 (database-disconnect database)
561 (setf (slot-value database 'clsql-sys::state) :closed))))))
564 ;;; Prepared statements
566 (defclass mysql-stmt ()
567 ((database :initarg :database :reader database)
568 (stmt :initarg :stmt :accessor stmt)
569 (input-bind :initarg :input-bind :reader input-bind)
570 (output-bind :initarg :output-bind :reader output-bind)
571 (types :initarg :types :reader types)
572 (result-set :initarg :result-set :reader result-set)
573 (num-fields :initarg :num-fields :reader num-fields)
574 (field-names :initarg :field-names :accessor stmt-field-names)
575 (length-ptr :initarg :length-ptr :reader length-ptr)
576 (is-null-ptr :initarg :is-null-ptr :reader is-null-ptr)
577 (result-types :initarg :result-types :reader result-types)))
579 (defun clsql-type->mysql-type (type)
581 ((in type :null) mysql-field-types#null)
582 ((in type :int :integer) mysql-field-types#long)
583 ((in type :short) mysql-field-types#short)
584 ((in type :bigint) mysql-field-types#longlong)
585 ((in type :float :double :number) mysql-field-types#double)
586 ((and (consp type) (in (car type) :char :string :varchar)) mysql-field-types#var-string)
587 ((or (eq type :blob) (and (consp type) (in (car type) :blob))) mysql-field-types#var-string)
589 (error 'sql-user-error
591 (format nil "Unknown clsql type ~A." type)))))
594 (defmethod database-prepare (sql-stmt types (database mysql-database) result-types field-names)
595 (let* ((mysql-ptr (database-mysql-ptr database))
596 (stmt (mysql-stmt-init mysql-ptr)))
597 (when (uffi:null-pointer-p stmt)
598 (error 'sql-database-error
599 :error-id (mysql-errno mysql-ptr)
600 :message (mysql-error-string mysql-ptr)))
602 (uffi:with-cstring (native-query sql-stmt)
603 (unless (zerop (mysql-stmt-prepare stmt native-query (uffi:foreign-encoded-octet-count
604 sql-stmt :encoding (encoding database))))
605 (mysql-stmt-close stmt)
606 (error 'sql-database-error
607 :error-id (mysql-errno mysql-ptr)
608 :message (mysql-error-string mysql-ptr))))
610 (unless (= (mysql-stmt-param-count stmt) (length types))
611 (mysql-stmt-close stmt)
612 (error 'sql-database-error
614 (format nil "Mysql param count (~D) does not match number of types (~D)"
615 (mysql-stmt-param-count stmt) (length types))))
617 (let ((rs (mysql-stmt-result-metadata stmt)))
618 (when (uffi:null-pointer-p rs)
619 (warn "mysql_stmt_result_metadata returned NULL")
621 (mysql-stmt-close stmt)
623 (error 'sql-database-error
624 :message "mysql_stmt_result_metadata returned NULL"))
626 (let ((input-bind (uffi:allocate-foreign-object 'mysql-bind (length types)))
627 (mysql-types (mapcar 'clsql-type->mysql-type types))
628 field-vec num-fields is-null-ptr output-bind length-ptr)
631 (dotimes (i (length types))
632 (let* ((binding (uffi:deref-array input-bind '(:array mysql-bind) i)))
633 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-type)
635 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-length) 0)))
638 (unless (uffi:null-pointer-p rs)
639 (setq field-vec (mysql-fetch-fields rs)
640 num-fields (mysql-num-fields rs)
641 is-null-ptr (uffi:allocate-foreign-object :byte num-fields)
642 output-bind (uffi:allocate-foreign-object 'mysql-bind num-fields)
643 length-ptr (uffi:allocate-foreign-object :unsigned-long num-fields))
644 (dotimes (i num-fields)
646 (let* ((field (uffi:deref-array field-vec '(:array mysql-field) i))
647 (type (uffi:get-slot-value field 'mysql-field 'type))
648 (binding (uffi:deref-array output-bind '(:array mysql-bind) i)))
649 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-type) type)
651 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-length) 0)
652 #+need-to-allocate-foreign-object-for-this
653 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::is-null)
654 (+ i (uffi:pointer-address is-null-ptr)))
655 #+need-to-allocate-foreign-object-for-this
656 (setf (uffi:get-slot-value binding 'mysql-bind 'length)
657 (+ (* i 8) (uffi:pointer-address length-ptr)))
660 ((#.mysql-field-types#var-string #.mysql-field-types#string
661 #.mysql-field-types#tiny-blob #.mysql-field-types#blob
662 #.mysql-field-types#medium-blob #.mysql-field-types#long-blob)
663 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-length) 1024)
664 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
665 (uffi:allocate-foreign-object :unsigned-char 1024)))
666 (#.mysql-field-types#tiny
667 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
668 (uffi:allocate-foreign-object :byte)))
669 (#.mysql-field-types#short
670 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
671 (uffi:allocate-foreign-object :short)))
672 (#.mysql-field-types#long
673 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
674 ;; segfaults if supply :int on amd64
675 (uffi:allocate-foreign-object :long)))
677 (#.mysql-field-types#longlong
678 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
679 (uffi:allocate-foreign-object :long)))
680 (#.mysql-field-types#float
681 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
682 (uffi:allocate-foreign-object :float)))
683 (#.mysql-field-types#double
684 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
685 (uffi:allocate-foreign-object :double)))
686 ((#.mysql-field-types#time #.mysql-field-types#date
687 #.mysql-field-types#datetime #.mysql-field-types#timestamp)
688 (uffi:allocate-foreign-object 'mysql-time))
690 (error "mysql type ~D not supported." type)))))
692 (unless (zerop (mysql-stmt-bind-result stmt output-bind))
693 (mysql-stmt-close stmt)
694 (error 'sql-database-error
695 :error-id (mysql-stmt-errno stmt)
696 :message (uffi:convert-from-cstring
697 (mysql-stmt-error stmt)))))
699 (make-instance 'mysql-stmt
702 :num-fields num-fields
703 :input-bind input-bind
704 :output-bind output-bind
706 :result-types result-types
707 :length-ptr length-ptr
708 :is-null-ptr is-null-ptr
710 :field-names field-names)))))
713 (defmethod database-bind-parameter ((stmt mysql-stmt) position value)
714 ;; FIXME: will need to allocate bind structure. This should probably be
715 ;; done in C since the API is not mature and may change
716 (let ((binding (uffi:deref-array (input-bind stmt) '(:array mysql-bind) (1- position)))
717 (type (nth (1- position) (types stmt))))
718 (setf (uffi:get-slot-value binding 'mysql-bind 'length) 0)
721 (when (is-null-ptr stmt)
722 (setf (uffi:deref-array (is-null-ptr stmt) '(:array :byte) (1- position)) 1)))
724 (when (is-null-ptr stmt)
725 (setf (uffi:deref-array (is-null-ptr stmt) '(:array :byte) (1- position)) 0))
727 (#.mysql-field-types#long
728 (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer) value))
730 (warn "Unknown input bind type ~D." type))
734 (defmethod database-run-prepared ((stmt mysql-stmt))
736 (when (input-bind stmt)
737 (unless (zerop (mysql-stmt-bind-param (stmt stmt) (input-bind stmt)))
738 (error 'sql-database-error
739 :error-id (mysql-stmt-errno (stmt stmt))
740 :message (uffi:convert-from-cstring
741 (mysql-stmt-error (stmt stmt))))))
743 (unless (zerop (mysql-stmt-execute (stmt stmt)))
744 (error 'sql-database-error
745 :error-id (mysql-stmt-errno (stmt stmt))
746 :message (uffi:convert-from-cstring
747 (mysql-stmt-error (stmt stmt)))))
749 (unless (zerop (mysql-stmt-store-result (stmt stmt)))
750 (error 'sql-database-error
751 :error-id (mysql-stmt-errno (stmt stmt))
752 :message (uffi:convert-from-cstring
753 (mysql-stmt-error (stmt stmt)))))
754 (database-fetch-prepared-rows stmt))
757 (defun database-fetch-prepared-rows (stmt)
758 (do ((rc (mysql-stmt-fetch (stmt stmt)) (mysql-stmt-fetch (stmt stmt)))
759 (num-fields (num-fields stmt))
761 ((not (zerop rc)) (nreverse rows))
763 (loop for i from 0 below num-fields
766 (not (zerop (uffi:ensure-char-integer
767 (uffi:deref-array (is-null-ptr stmt) '(:array :byte) i))))))
769 (let* ((bind (uffi:deref-array (output-bind stmt) '(:array mysql-bind) i))
770 (type (uffi:get-slot-value bind 'mysql-bind 'mysql::buffer-type))
771 (buffer (uffi:get-slot-value bind 'mysql-bind 'mysql::buffer)))
773 ((#.mysql-field-types#var-string #.mysql-field-types#string
774 #.mysql-field-types#tiny-blob #.mysql-field-types#blob
775 #.mysql-field-types#medium-blob #.mysql-field-types#long-blob)
776 (uffi:convert-from-foreign-string buffer :encoding (encoding (database stmt))))
777 (#.mysql-field-types#tiny
778 (uffi:ensure-char-integer
779 (uffi:deref-pointer buffer :byte)))
780 (#.mysql-field-types#short
781 (uffi:deref-pointer buffer :short))
782 (#.mysql-field-types#long
783 (uffi:deref-pointer buffer :int))
785 (#.mysql-field-types#longlong
786 (uffi:deref-pointer buffer :long))
787 (#.mysql-field-types#float
788 (uffi:deref-pointer buffer :float))
789 (#.mysql-field-types#double
790 (uffi:deref-pointer buffer :double))
791 ((#.mysql-field-types#time #.mysql-field-types#date
792 #.mysql-field-types#datetime #.mysql-field-types#timestamp)
793 (let ((year (uffi:get-slot-value buffer 'mysql-time 'mysql::year))
794 (month (uffi:get-slot-value buffer 'mysql-time 'mysql::month))
795 (day (uffi:get-slot-value buffer 'mysql-time 'mysql::day))
796 (hour (uffi:get-slot-value buffer 'mysql-time 'mysql::hour))
797 (minute (uffi:get-slot-value buffer 'mysql-time 'mysql::minute))
798 (second (uffi:get-slot-value buffer 'mysql-time 'mysql::second)))
800 (make-time :year year :month month :day day :hour hour
801 :minute minute :second second))))
810 (defmethod database-free-prepared ((stmt mysql-stmt))
811 (with-slots (stmt) stmt
812 (mysql-stmt-close stmt))
816 ;;; Database capabilities
818 (defmethod db-type-use-column-on-drop-index? ((db-type (eql :mysql)))
821 (defmethod db-type-has-views? ((db-type (eql :mysql)))
823 #-mysql-client-v5 nil)
825 (defmethod db-type-has-subqueries? ((db-type (eql :mysql)))
826 #+(or mysql-client-v4.1 mysql-client-v5) t
827 #-(or mysql-client-v4.1 mysql-client-v5) nil)
829 (defmethod db-type-has-boolean-where? ((db-type (eql :mysql)))
830 #+(or mysql-client-v4.1 mysql-client-v5) t
831 #-(or mysql-client-v4.1 mysql-client-v5) nil)
833 (defmethod db-type-has-union? ((db-type (eql :mysql)))
834 (not (eql (schar mysql::*mysql-client-info* 0) #\3)))
836 (defmethod db-type-transaction-capable? ((db-type (eql :mysql)) database)
837 (let ((tuple (car (database-query "SHOW VARIABLES LIKE 'HAVE_INNODB'" database :auto nil))))
838 (and tuple (string-equal "YES" (second tuple)))))
840 (defmethod db-type-has-prepared-stmt? ((db-type (eql :mysql)))
841 #+(or mysql-client-v4.1 mysql-client-v5) t
842 #-(or mysql-client-v4.1 mysql-client-v5) nil)
844 (defmethod db-type-has-auto-increment? ((db-type (eql :mysql)))
847 (when (clsql-sys:database-type-library-loaded :mysql)
848 (clsql-sys:initialize-database-type :database-type :mysql))