(make-instance 'mysql-database
:name (database-name-from-spec connection-spec
database-type)
+ :database-type :mysql
:connection-spec connection-spec
:mysql-ptr mysql-ptr))
(when error-occurred (mysql-close mysql-ptr)))))))))
:errno (mysql-errno mysql-ptr)
:error (mysql-error-string mysql-ptr))))))
-#+ignore
-(defmethod database-query (query-expression (database mysql-database)
- result-types)
- (declare (optimize (speed 3) (safety 0) (debug 0) (space 0)))
- (let ((mysql-ptr (database-mysql-ptr database)))
- (uffi:with-cstring (query-native query-expression)
- (if (zerop (mysql-query mysql-ptr query-native))
- (let ((res-ptr (mysql-use-result mysql-ptr)))
- (if res-ptr
- (unwind-protect
- (let ((num-fields (mysql-num-fields res-ptr)))
- (declare (fixnum num-fields))
- (setq result-types (canonicalize-types
- result-types num-fields
- res-ptr))
- (loop for row = (mysql-fetch-row res-ptr)
- until (uffi:null-pointer-p row)
- collect
- (loop for i fixnum from 0 below num-fields
- collect
- (convert-raw-field
- (uffi:deref-array row '(:array
- (* :unsigned-char))
- i)
- result-types i))))
- (mysql-free-result res-ptr))
- (error 'clsql-sql-error
- :database database
- :expression query-expression
- :errno (mysql-errno mysql-ptr)
- :error (mysql-error-string mysql-ptr))))
- (error 'clsql-sql-error
- :database database
- :expression query-expression
- :errno (mysql-errno mysql-ptr)
- :error (mysql-error-string mysql-ptr))))))
-
(defmethod database-execute-command (sql-expression (database mysql-database))
(uffi:with-cstring (sql-native sql-expression)
(let ((mysql-ptr (database-mysql-ptr database)))
(mysql:mysql-insert-id (clsql-mysql::database-mysql-ptr database)))
(defmethod database-sequence-next (sequence-name (database mysql-database))
- (database-execute-command
- (concatenate 'string "UPDATE " (%sequence-name-to-table sequence-name)
- " SET id=LAST_INSERT_ID(id+1)")
- database)
- (mysql:mysql-insert-id (clsql-mysql::database-mysql-ptr database)))
+ (without-interrupts
+ (database-execute-command
+ (concatenate 'string "UPDATE " (%sequence-name-to-table sequence-name)
+ " SET id=LAST_INSERT_ID(id+1)")
+ database)
+ (mysql:mysql-insert-id (clsql-mysql::database-mysql-ptr database))))
(defmethod database-sequence-last (sequence-name (database mysql-database))
(declare (ignore sequence-name)))
-
-
(defmethod database-create (connection-spec (type (eql :mysql)))
(destructuring-bind (host name user password) connection-spec
- (let ((asdf::*verbose-out* (make-string-output-stream)))
- (unwind-protect
- (let* ((status (asdf:run-shell-command
- "mysqladmin create -u~A -p~A -h~A ~A"
- user password
- (if host host "localhost")
- name))
- (result (get-output-stream-string asdf::*verbose-out*)))
-
- (if (search "CREATE DATABASE failed;" result)
- (error 'clsql-access-error
- :connection-spec connection-spec
- :database-type type
- :error
- (format nil "database-create failed: ~s" result))
- t))
- (close asdf::*verbose-out*)))))
-
-(defmethod database-destory (connection-spec (type (eql :mysql)))
+ (multiple-value-bind (output status)
+ (clsql-base-sys:command-output "mysqladmin create -u~A -p~A -h~A ~A"
+ user password
+ (if host host "localhost")
+ name)
+ (if (or (not (eql 0 status))
+ (and (search "failed" output) (search "error" output)))
+ (error 'clsql-access-error
+ :connection-spec connection-spec
+ :database-type type
+ :error
+ (format nil "database-create failed: ~A" output))
+ t))))
+
+(defmethod database-destroy (connection-spec (type (eql :mysql)))
(destructuring-bind (host name user password) connection-spec
- (let ((asdf::*verbose-out* (make-string-output-stream)))
- (unwind-protect
- (let* ((status (asdf:run-shell-command
- "mysqladmin drop -f -u~A -p~A -h~A ~A"
- user password
- (if host host "localhost")
- name))
- (result (get-output-stream-string asdf::*verbose-out*)))
-
- (if (search "DROP DATABASE failed;" result)
- (error 'clsql-access-error
- :connection-spec connection-spec
- :database-type type
- :error
- (format nil "database-destory failed: ~s" result))
- t))
- (close asdf::*verbose-out*)))))
+ (multiple-value-bind (output status)
+ (clsql-base-sys:command-output "mysqladmin drop -f -u~A -p~A -h~A ~A"
+ user password
+ (if host host "localhost")
+ name)
+ (if (or (not (eql 0 status))
+ (and (search "failed" output) (search "error" output)))
+ (error 'clsql-access-error
+ :connection-spec connection-spec
+ :database-type type
+ :error
+ (format nil "database-destroy failed: ~A" output))
+ t))))
(defmethod database-probe (connection-spec (type (eql :mysql)))
- (destructuring-bind (name user password) connection-spec
- (error "not-yet-implemented")))
+ (destructuring-bind (host name user password) connection-spec
+ (let ((database (database-connect (list host "mysql" user password) type)))
+ (unwind-protect
+ (when
+ (find name (database-query "select db from db"
+ database :auto)
+ :key #'car :test #'string-equal)
+ t)
+ (database-disconnect database)))))
+
(when (clsql-base-sys:database-type-library-loaded :mysql)
(clsql-base-sys:initialize-database-type :database-type :mysql))