(dotimes (i num-fields)
(declare (fixnum i))
(let* ((field (uffi:deref-array field-vec '(:array mysql-field) i))
- (type (uffi:get-slot-value field 'mysql-field 'type)))
+ (flags (uffi:get-slot-value field 'mysql-field 'mysql::flags))
+ (unsigned (plusp (logand flags 32)))
+ (type (uffi:get-slot-value field 'mysql-field 'type)))
(push
(case type
((#.mysql-field-types#tiny
#.mysql-field-types#short
- #.mysql-field-types#int24
- #.mysql-field-types#long)
- :int32)
- (#.mysql-field-types#longlong
- :int64)
+ #.mysql-field-types#int24)
+ (if unsigned
+ :uint32
+ :int32))
+ (#.mysql-field-types#long
+ (if unsigned
+ :uint
+ :int))
+ (#.mysql-field-types#longlong
+ (if unsigned
+ :uint64
+ :int64))
((#.mysql-field-types#double
#.mysql-field-types#float
#.mysql-field-types#decimal)
:mysql)
(defmethod database-name-from-spec (connection-spec (database-type (eql :mysql)))
- (check-connection-spec connection-spec database-type (host db user password))
- (destructuring-bind (host db user password) connection-spec
+ (check-connection-spec connection-spec database-type
+ (host db user password &optional port))
+ (destructuring-bind (host db user password &optional port) connection-spec
(declare (ignore password))
(concatenate 'string
- (if host host "localhost")
+ (etypecase host
+ (null "localhost")
+ (pathname (namestring host))
+ (string host))
+ (if port
+ (concatenate 'string
+ ":"
+ (etypecase port
+ (integer (write-to-string port))
+ (string port)))
+ "")
"/" db "/" user)))
(defmethod database-connect (connection-spec (database-type (eql :mysql)))
- (check-connection-spec connection-spec database-type (host db user password))
- (destructuring-bind (host db user password) connection-spec
+ (check-connection-spec connection-spec database-type
+ (host db user password &optional port))
+ (destructuring-bind (host db user password &optional port) connection-spec
(let ((mysql-ptr (mysql-init (uffi:make-null-pointer 'mysql-mysql)))
(socket nil))
(if (uffi:null-pointer-p mysql-ptr)
(if (uffi:null-pointer-p
(mysql-real-connect
mysql-ptr host-native user-native password-native
- db-native 0 socket-native 0))
+ db-native
+ (etypecase port
+ (null 0)
+ (integer port)
+ (string (parse-integer port)))
+ socket-native 0))
(progn
(setq error-occurred t)
(error 'sql-connection-error
(mysql:mysql-insert-id (clsql-mysql::database-mysql-ptr database))))
(defmethod database-sequence-last (sequence-name (database mysql-database))
- (declare (ignore sequence-name)))
+ (without-interrupts
+ (caar (database-query
+ (concatenate 'string "SELECT id from "
+ (%sequence-name-to-table sequence-name))
+ database :auto nil))))
(defmethod database-create (connection-spec (type (eql :mysql)))
- (destructuring-bind (host name user password) connection-spec
+ (destructuring-bind (host name user password &optional port) connection-spec
(multiple-value-bind (output status)
- (clsql-sys:command-output "mysqladmin create -u~A -p~A -h~A ~A"
+ (clsql-sys:command-output "mysqladmin create -u~A -p~A -h~A~@[ -P~A~] ~A"
user password
(if host host "localhost")
+ port name
name)
(if (or (not (eql 0 status))
(and (search "failed" output) (search "error" output)))
t))))
(defmethod database-destroy (connection-spec (type (eql :mysql)))
- (destructuring-bind (host name user password) connection-spec
+ (destructuring-bind (host name user password &optional port) connection-spec
(multiple-value-bind (output status)
- (clsql-sys:command-output "mysqladmin drop -f -u~A -p~A -h~A ~A"
+ (clsql-sys:command-output "mysqladmin drop -f -u~A -p~A -h~A~@[ -P~A~] ~A"
user password
(if host host "localhost")
- name)
+ port name)
(if (or (not (eql 0 status))
(and (search "failed" output) (search "error" output)))
(error 'sql-database-error
t))
(defmethod database-list (connection-spec (type (eql :mysql)))
- (destructuring-bind (host name user password) connection-spec
+ (destructuring-bind (host name user password &optional port) connection-spec
(declare (ignore name))
- (let ((database (database-connect (list host "mysql" user password) type)))
+ (let ((database (database-connect (list host "mysql" user password port) type)))
(unwind-protect
(progn
(setf (slot-value database 'clsql-sys::state) :open)
((in type :short) mysql-field-types#short)
((in type :bigint) mysql-field-types#longlong)
((in type :float :double :number) mysql-field-types#double)
- ((and (consp type) (in (car type) :char :varchar)) mysql-field-types#var-string)
+ ((and (consp type) (in (car type) :char :string :varchar)) mysql-field-types#var-string)
+ ((or (eq type :blob) (and (consp type) (in (car type) :blob))) mysql-field-types#var-string)
(t
(error 'sql-user-error
:message
(uffi:with-cstring (native-query sql-stmt)
(unless (zerop (mysql-stmt-prepare stmt native-query (length sql-stmt)))
+ (mysql-stmt-close stmt)
(error 'sql-database-error
:error-id (mysql-errno mysql-ptr)
:message (mysql-error-string mysql-ptr))))
(unless (= (mysql-stmt-param-count stmt) (length types))
+ (mysql-stmt-close stmt)
(error 'sql-database-error
:message
(format nil "Mysql param count (~D) does not match number of types (~D)"
(let ((rs (mysql-stmt-result-metadata stmt)))
(when (uffi:null-pointer-p rs)
+ (warn "mysql_stmt_result_metadata returned NULL")
+ #+nil
+ (mysql-stmt-close stmt)
+ #+nil
(error 'sql-database-error
- :message "NULL result metadata"))
+ :message "mysql_stmt_result_metadata returned NULL"))
- (let* ((field-vec (mysql-fetch-fields rs))
- (num-fields (mysql-num-fields rs))
- (output-bind (uffi:allocate-foreign-object mysql-bind num-fields))
- (length-ptr (uffi:allocate-foreign-object :unsigned-long num-fields))
- (is-null-ptr (uffi:allocate-foreign-object :byte num-fields)))
-
- (dotimes (i num-fields)
- (declare (fixnum i))
- (let* ((field (uffi:deref-array field-vec '(:array mysql-field) i))
- (type (uffi:get-slot-value field 'mysql-field 'type))
- (binding (uffi:deref-array output-bind '(:array mysql-bind) i)))
- (setf (uffi:get-slot-value binding 'mysql-bind 'buffer-type) type)
- (setf (uffi:get-slot-value binding 'mysql-bind 'buffer-length) 0)
- (setf (uffi:get-slot-value binding 'mysql-bind 'is-null)
- (uffi:pointer-address (uffi:deref-array is-null-ptr '(:array :byte) i)))
- (setf (uffi:get-slot-value binding 'mysql-bind 'length)
- (uffi:pointer-address (uffi:deref-array length-ptr '(:array :unsigned-long) i)))
-
- (case type
- ((#.mysql-field-types#var-string #.mysql-field-types#string
- #.mysql-field-types#tiny-blob #.mysql-field-types#blob
- #.mysql-field-types#medium-blob #.mysql-field-types#long-blob)
- (setf (uffi:get-slot-value binding 'mysql-bind 'buffer-length) 1024)
- (setf (uffi:get-slot-value binding 'mysql-bind 'buffer)
- (uffi:allocate-foreign-object :unsigned-char 1024)))
- (#.mysql-field-types#tiny
- (setf (uffi:get-slot-value binding 'mysql-bind 'buffer)
- (uffi:allocate-foreign-object :byte)))
- (#.mysql-field-types#short
- (setf (uffi:get-slot-value binding 'mysql-bind 'buffer)
- (uffi:allocate-foreign-object :short)))
- (#.mysql-field-types#long
- (setf (uffi:get-slot-value binding 'mysql-bind 'buffer)
- (uffi:allocate-foreign-object :int)))
- #+64bit
- (#.mysql-field-types#longlong
- (setf (uffi:get-slot-value binding 'mysql-bind 'buffer)
- (uffi:allocate-foreign-object :long)))
- (#.mysql-field-types#float
- (setf (uffi:get-slot-value binding 'mysql-bind 'buffer)
- (uffi:allocate-foreign-object :float)))
- (#.mysql-field-types#double
- (setf (uffi:get-slot-value binding 'mysql-bind 'buffer)
- (uffi:allocate-foreign-object :double)))
- ((#.mysql-field-types#time #.mysql-field-types#date
- #.mysql-field-types#datetime #.mysql-field-types#timestamp)
- (uffi:allocate-foreign-object 'mysql-time))
- (t
- (error "mysql type ~D not supported." type)))))
-
- (unless (zerop (mysql-stmt-bind-result stmt output-bind))
- (error 'sql-database-error
- :error-id (mysql-stmt-errno stmt)
- :message (uffi:convert-from-cstring
- (mysql-stmt-error stmt))))
-
+ (let ((input-bind (uffi:allocate-foreign-object 'mysql-bind (length types)))
+ (mysql-types (mapcar 'clsql-type->mysql-type types))
+ field-vec num-fields is-null-ptr output-bind length-ptr)
+
+ (print 'a)
+ (dotimes (i (length types))
+ (let* ((binding (uffi:deref-array input-bind '(:array mysql-bind) i)))
+ (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-type)
+ (nth i mysql-types))
+ (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-length) 0)))
+
+ (print 'b)
+ (unless (uffi:null-pointer-p rs)
+ (setq field-vec (mysql-fetch-fields rs)
+ num-fields (mysql-num-fields rs)
+ is-null-ptr (uffi:allocate-foreign-object :byte num-fields)
+ output-bind (uffi:allocate-foreign-object 'mysql-bind num-fields)
+ length-ptr (uffi:allocate-foreign-object :unsigned-long num-fields))
+ (dotimes (i num-fields)
+ (declare (fixnum i))
+ (let* ((field (uffi:deref-array field-vec '(:array mysql-field) i))
+ (type (uffi:get-slot-value field 'mysql-field 'type))
+ (binding (uffi:deref-array output-bind '(:array mysql-bind) i)))
+ (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-type) type)
+
+ (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-length) 0)
+ #+need-to-allocate-foreign-object-for-this
+ (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::is-null)
+ (+ i (uffi:pointer-address is-null-ptr)))
+ #+need-to-allocate-foreign-object-for-this
+ (setf (uffi:get-slot-value binding 'mysql-bind 'length)
+ (+ (* i 8) (uffi:pointer-address length-ptr)))
+
+ (case type
+ ((#.mysql-field-types#var-string #.mysql-field-types#string
+ #.mysql-field-types#tiny-blob #.mysql-field-types#blob
+ #.mysql-field-types#medium-blob #.mysql-field-types#long-blob)
+ (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer-length) 1024)
+ (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
+ (uffi:allocate-foreign-object :unsigned-char 1024)))
+ (#.mysql-field-types#tiny
+ (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
+ (uffi:allocate-foreign-object :byte)))
+ (#.mysql-field-types#short
+ (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
+ (uffi:allocate-foreign-object :short)))
+ (#.mysql-field-types#long
+ (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
+ ;; segfaults if supply :int on amd64
+ (uffi:allocate-foreign-object :long)))
+ #+64bit
+ (#.mysql-field-types#longlong
+ (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
+ (uffi:allocate-foreign-object :long)))
+ (#.mysql-field-types#float
+ (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
+ (uffi:allocate-foreign-object :float)))
+ (#.mysql-field-types#double
+ (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer)
+ (uffi:allocate-foreign-object :double)))
+ ((#.mysql-field-types#time #.mysql-field-types#date
+ #.mysql-field-types#datetime #.mysql-field-types#timestamp)
+ (uffi:allocate-foreign-object 'mysql-time))
+ (t
+ (error "mysql type ~D not supported." type)))))
+
+ (unless (zerop (mysql-stmt-bind-result stmt output-bind))
+ (mysql-stmt-close stmt)
+ (error 'sql-database-error
+ :error-id (mysql-stmt-errno stmt)
+ :message (uffi:convert-from-cstring
+ (mysql-stmt-error stmt)))))
+
(make-instance 'mysql-stmt
- :database database
- :stmt stmt
- :num-fields num-fields
- :input-bind (uffi:allocate-foreign-object mysql-bind (length types))
- :output-bind output-bind
- :result-set rs
- :result-types result-types
- :length-ptr length-ptr
- :is-null-ptr is-null-ptr
- :types (mapcar 'clsql-type->mysql-type types)
- :field-names field-names)))))
+ :database database
+ :stmt stmt
+ :num-fields num-fields
+ :input-bind input-bind
+ :output-bind output-bind
+ :result-set rs
+ :result-types result-types
+ :length-ptr length-ptr
+ :is-null-ptr is-null-ptr
+ :types mysql-types
+ :field-names field-names)))))
#+mysql-client-v4.1
(defmethod database-bind-parameter ((stmt mysql-stmt) position value)
(setf (uffi:get-slot-value binding 'mysql-bind 'length) 0)
(cond
((null value)
- (setf (uffi:get-slot-value binding 'mysql-bind 'is-null) 1))
+ (when (is-null-ptr stmt)
+ (setf (uffi:deref-array (is-null-ptr stmt) '(:array :byte) (1- position)) 1)))
(t
- (setf (uffi:get-slot-value binding 'mysql-bind 'is-null) 0)
- (setf (uffi:get-slot-value binding 'mysql-bind 'buffer-type) type)
+ (when (is-null-ptr stmt)
+ (setf (uffi:deref-array (is-null-ptr stmt) '(:array :byte) (1- position)) 0))
(case type
(#.mysql-field-types#long
- (let ((ptr (uffi:allocate-foreign-object :long)))
- (setf (uffi:deref-pointer ptr :long) value)
- (setf (uffi:get-slot-value binding 'mysql-bind 'buffer) ptr)))
+ (setf (uffi:get-slot-value binding 'mysql-bind 'mysql::buffer) value))
+ (t
+ (warn "Unknown input bind type ~D." type))
)))))
#+mysql-client-v4.1
(defmethod database-run-prepared ((stmt mysql-stmt))
- (unless (zerop (mysql-stmt-bind-param (stmt stmt) (input-bind stmt)))
- (error 'sql-database-error
- :error-id (mysql-stmt-errno (stmt stmt))
- :message (uffi:convert-from-cstring
- (mysql-stmt-error (stmt stmt)))))
+ (print 'a1)
+ (when (input-bind stmt)
+ (unless (zerop (mysql-stmt-bind-param (stmt stmt) (input-bind stmt)))
+ (error 'sql-database-error
+ :error-id (mysql-stmt-errno (stmt stmt))
+ :message (uffi:convert-from-cstring
+ (mysql-stmt-error (stmt stmt))))))
+ (print 'a2)
(unless (zerop (mysql-stmt-execute (stmt stmt)))
(error 'sql-database-error
:error-id (mysql-stmt-errno (stmt stmt))
:message (uffi:convert-from-cstring
(mysql-stmt-error (stmt stmt)))))
+ (print 'a3)
(unless (zerop (mysql-stmt-store-result (stmt stmt)))
(error 'sql-database-error
:error-id (mysql-stmt-errno (stmt stmt))
#+mysql-client-v4.1
(defun database-fetch-prepared-rows (stmt)
(do ((rc (mysql-stmt-fetch (stmt stmt)) (mysql-stmt-fetch (stmt stmt)))
+ (num-fields (num-fields stmt))
(rows '()))
((not (zerop rc)) (nreverse rows))
(push
- (loop for i from 0 below (num-fields stmt)
+ (loop for i from 0 below num-fields
collect
(let ((is-null
(not (zerop (uffi:ensure-char-integer
(uffi:deref-array (is-null-ptr stmt) '(:array :byte) i))))))
(unless is-null
(let* ((bind (uffi:deref-array (output-bind stmt) '(:array mysql-bind) i))
- (type (uffi:get-slot-value bind 'mysql-bind 'buffer-type))
- (buffer (uffi:get-slot-value bind 'mysql-bind 'buffer)))
+ (type (uffi:get-slot-value bind 'mysql-bind 'mysql::buffer-type))
+ (buffer (uffi:get-slot-value bind 'mysql-bind 'mysql::buffer)))
(case type
((#.mysql-field-types#var-string #.mysql-field-types#string
#.mysql-field-types#tiny-blob #.mysql-field-types#blob
(day (uffi:get-slot-value buffer 'mysql-time 'mysql::day))
(hour (uffi:get-slot-value buffer 'mysql-time 'mysql::hour))
(minute (uffi:get-slot-value buffer 'mysql-time 'mysql::minute))
- (second (uffi:get-slot-value buffer 'mysql-time 'mysql::second)))
+ (second (uffi:get-slot-value buffer 'mysql-time 'mysql::second)))
(db-timestring
(make-time :year year :month month :day day :hour hour
:minute minute :second second))))
#-mysql-client-v4.1 nil)
(defmethod db-type-has-boolean-where? ((db-type (eql :mysql)))
- nil)
+ #+mysql-client-v4.1 t
+ #-mysql-client-v4.1 nil)
(defmethod db-type-has-union? ((db-type (eql :mysql)))
(not (eql (schar mysql::*mysql-client-info* 0) #\3)))