X-Git-Url: http://git.kpe.io/?p=clsql.git;a=blobdiff_plain;f=db-mysql%2Fmysql-sql.lisp;h=1f54d44c430e6f261a6f7a7e509c470e035b0f79;hp=8f39471257c2a38ee6feaeea0faf371ecc48ca09;hb=eb6238bfcd8e0359c813c24626cc0b435c7a04bf;hpb=a7525d77bc9ddecb56749efb62f82e9422804d15 diff --git a/db-mysql/mysql-sql.lisp b/db-mysql/mysql-sql.lisp index 8f39471..1f54d44 100644 --- a/db-mysql/mysql-sql.lisp +++ b/db-mysql/mysql-sql.lisp @@ -6,7 +6,7 @@ ;;;; Purpose: High-level MySQL interface using UFFI ;;;; Date Started: Feb 2002 ;;;; -;;;; This file, part of CLSQL, is Copyright (c) 2002-2009 by Kevin M. Rosenberg +;;;; This file, part of CLSQL, is Copyright (c) 2002-2010 by Kevin M. Rosenberg ;;;; ;;;; CLSQL users are granted the rights to distribute and use this software ;;;; as governed by the terms of the Lisp Lesser GNU Public License @@ -16,16 +16,14 @@ (defpackage #:clsql-mysql (:use #:common-lisp #:clsql-sys #:mysql #:clsql-uffi) (:export #:mysql-database) + (:import-from :clsql-sys + :escaped :unescaped :combine-database-identifiers + :escaped-database-identifier :unescaped-database-identifier :database-identifier + :%sequence-name-to-table :%table-name-to-sequence-name) (:documentation "This is the CLSQL interface to MySQL.")) (in-package #:clsql-mysql) -;; if we have :sb-unicode, UFFI will treat :cstring as a UTF-8 string -(defun expression-length (query-expression) - (length #+sb-unicode (sb-ext:string-to-octets query-expression - :external-format :utf8) - #-sb-unicode query-expression)) - ;;; Field conversion functions (defun result-field-names (res-ptr) @@ -102,9 +100,9 @@ (defmethod database-name-from-spec (connection-spec (database-type (eql :mysql))) (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)) + (host db user password &optional port options)) + (destructuring-bind (host db user password &optional port options) connection-spec + (declare (ignore password options)) (concatenate 'string (etypecase host (null "localhost") @@ -119,10 +117,57 @@ "") "/" db "/" user))) +(defun lookup-option-code (option) + (if (assoc option +mysql-option-parameter-map+) + (symbol-value (intern + (concatenate 'string (symbol-name-default-case "mysql-option#") + (symbol-name option)) + (symbol-name '#:mysql))) + (progn + (warn "Unknown mysql option name ~A - ignoring.~%" option) + nil))) + +(defun set-mysql-options (mysql-ptr options) + (flet ((lookup-option-type (option) + (cdr (assoc option +mysql-option-parameter-map+)))) + (dolist (option options) + (if (atom option) + (let ((option-code (lookup-option-code option))) + (when option-code + (mysql-options mysql-ptr option-code uffi:+null-cstring-pointer+))) + (destructuring-bind (name . value) option + (let ((option-code (lookup-option-code name))) + (when option-code + (case (lookup-option-type name) + (:none + (mysql-options mysql-ptr option-code uffi:+null-cstring-pointer+)) + (:char-ptr + (if (stringp value) + (uffi:with-foreign-string (fs value) + (mysql-options mysql-ptr option-code fs)) + (warn "Expecting string argument for mysql option ~A, got ~A ~ +- ignoring.~%" + name value))) + (:uint-ptr + (if (integerp value) + (uffi:with-foreign-object (fo :unsigned-int) + (setf (uffi:deref-pointer fo :unsigned-int) value) + (mysql-options mysql-ptr option-code fo)) + (warn "Expecting integer argument for mysql option ~A, got ~A ~ +- ignoring.~%" + name value))) + (:boolean-ptr + (uffi:with-foreign-object (fo :byte) + (setf (uffi:deref-pointer fo :byte) + (case value + ((nil 0) 0) + (t 1))) + (mysql-options mysql-ptr option-code fo))))))))))) + (defmethod database-connect (connection-spec (database-type (eql :mysql))) (check-connection-spec connection-spec database-type - (host db user password &optional port)) - (destructuring-bind (host db user password &optional port) connection-spec + (host db user password &optional port options)) + (destructuring-bind (host db user password &optional port options) connection-spec (let ((mysql-ptr (mysql-init (uffi:make-null-pointer 'mysql-mysql))) (socket nil)) (if (uffi:null-pointer-p mysql-ptr) @@ -136,6 +181,8 @@ (password-native password) (db-native db) (socket-native socket)) + (when options + (set-mysql-options mysql-ptr options)) (let ((error-occurred nil)) (unwind-protect (if (uffi:null-pointer-p @@ -165,7 +212,8 @@ :mysql-ptr mysql-ptr)) (cmd "SET SESSION sql_mode='ANSI'")) (uffi:with-cstring (cmd-cs cmd) - (if (zerop (mysql-real-query mysql-ptr cmd-cs (expression-length cmd))) + (if (zerop (mysql-real-query mysql-ptr cmd-cs (uffi:foreign-encoded-octet-count + cmd :encoding (encoding db)))) db (progn (warn "Error setting ANSI mode for MySQL.") @@ -178,59 +226,13 @@ (setf (database-mysql-ptr database) nil) t) - -(defmethod database-query (query-expression (database mysql-database) - result-types field-names) - (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-real-query mysql-ptr query-native - (expression-length query-expression))) - (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 res-ptr)) - (values - (loop for row = (mysql-fetch-row res-ptr) - for lengths = (mysql-fetch-lengths res-ptr) - until (uffi:null-pointer-p row) - collect - (do* ((rlist (make-list num-fields)) - (i 0 (1+ i)) - (pos rlist (cdr pos))) - ((= i num-fields) rlist) - (declare (fixnum i)) - (setf (car pos) - (convert-raw-field - (uffi:deref-array row '(:array - (* :unsigned-char)) - i) - result-types i - (uffi:deref-array lengths '(:array :unsigned-long) - i))))) - (when field-names - (result-field-names res-ptr)))) - (mysql-free-result res-ptr)) - (error 'sql-database-data-error - :database database - :expression query-expression - :error-id (mysql-errno mysql-ptr) - :message (mysql-error-string mysql-ptr)))) - (error 'sql-database-data-error - :database database - :expression query-expression - :error-id (mysql-errno mysql-ptr) - :message (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))) (declare (type mysql-mysql-ptr-def mysql-ptr)) (if (zerop (mysql-real-query mysql-ptr sql-native - (expression-length sql-expression))) + (uffi:foreign-encoded-octet-count + sql-expression :encoding (encoding database)))) t (error 'sql-database-data-error :database database @@ -239,6 +241,73 @@ :message (mysql-error-string mysql-ptr)))))) +(defmethod database-query (query-expression (database mysql-database) + result-types field-names) + (declare (optimize (speed 3))) + (let ((mysql-ptr (database-mysql-ptr database)) + (results nil) ;; all the results and column-names in reverse-order + (res-ptr nil) + (num-fields 0)) + (declare (type mysql-mysql-ptr-def mysql-ptr) + (type (or null mysql-mysql-res-ptr-def) res-ptr) + (fixnum num-fields)) + (when (database-execute-command query-expression database) + (labels + ((get-row (row lengths) + "Pull a single row value from the results" + (loop for i from 0 below num-fields + collect + (convert-raw-field + (uffi:deref-array row '(:array (* :unsigned-char)) + (the fixnum i)) + (nth i result-types) + :length + (uffi:deref-array lengths '(:array :unsigned-long) + (the fixnum i)) + :encoding (encoding database)))) + (get-result-rows () + "get all the rows out of the now valid results set" + (loop for row = (mysql-fetch-row res-ptr) + for lengths = (mysql-fetch-lengths res-ptr) + until (uffi:null-pointer-p row) + collect (get-row row lengths))) + (do-result-set () + "for a mysql-ptr, grab and return a results set" + (setf res-ptr (mysql-use-result mysql-ptr)) + (cond + ((or (null res-ptr) (uffi:null-pointer-p res-ptr)) + (unless (zerop (mysql-errno mysql-ptr)) + ;;from http://dev.mysql.com/doc/refman/5.0/en/mysql-field-count.html + ;; if mysql_use_result or mysql_store_result return a null ptr, + ;; we use a mysql_errno check to see if it had a problem or just + ;; was a query without a result. If no error, just return nil. + (error 'sql-database-data-error + :database database + :expression query-expression + :error-id (mysql-errno mysql-ptr) + :message (mysql-error-string mysql-ptr)))) + (t + (unwind-protect + (progn (setf num-fields (mysql-num-fields res-ptr) + result-types (canonicalize-types + result-types res-ptr)) + (push (get-result-rows) results) + (push (when field-names + (result-field-names res-ptr)) + results)) + (mysql-free-result res-ptr)))))) + + (loop + do (do-result-set) + while (let ((next (mysql-next-result mysql-ptr))) + (case next + (0 t) ;Successful and there are more results + (-1 nil) ;Successful and there are no more results + (t nil) ;errors + ))) + (values-list (nreverse results)))))) + + (defstruct mysql-result-set (res-ptr (uffi:make-null-pointer 'mysql-mysql-res) :type mysql-mysql-res-ptr-def) (types nil :type list) @@ -249,40 +318,38 @@ (defmethod database-query-result-set ((query-expression string) (database mysql-database) &key full-set result-types) - (uffi:with-cstring (query-native query-expression) - (let ((mysql-ptr (database-mysql-ptr database))) - (declare (type mysql-mysql-ptr-def mysql-ptr)) - (if (zerop (mysql-real-query mysql-ptr query-native - (expression-length query-expression))) - (let ((res-ptr (if full-set - (mysql-store-result mysql-ptr) - (mysql-use-result mysql-ptr)))) - (declare (type mysql-mysql-res-ptr-def res-ptr)) - (if (not (uffi:null-pointer-p res-ptr)) - (let* ((num-fields (mysql-num-fields res-ptr)) - (result-set (make-mysql-result-set - :res-ptr res-ptr - :num-fields num-fields - :full-set full-set - :types - (canonicalize-types - result-types res-ptr)))) - (if full-set - (values result-set - num-fields - (mysql-num-rows res-ptr)) - (values result-set - num-fields))) - (error 'sql-database-data-error - :database database - :expression query-expression - :error-id (mysql-errno mysql-ptr) - :message (mysql-error-string mysql-ptr)))) - (error 'sql-database-data-error - :database database - :expression query-expression - :error-id (mysql-errno mysql-ptr) - :message (mysql-error-string mysql-ptr)))))) + ;; TODO: REFACTOR THIS IN TERMS OF database-query or vice-versa + ;; This doesnt seem to free database results reliably, dont think + ;; that we should allow that particularly, OTOH, dont know how + ;; we support cursoring without it + (let ((mysql-ptr (database-mysql-ptr database))) + (declare (type mysql-mysql-ptr-def mysql-ptr)) + (when (database-execute-command query-expression database) + (let ((res-ptr (if full-set + (mysql-store-result mysql-ptr) + (mysql-use-result mysql-ptr)))) + (declare (type mysql-mysql-res-ptr-def res-ptr)) + (if (not (uffi:null-pointer-p res-ptr)) + (let* ((num-fields (mysql-num-fields res-ptr)) + (result-set (make-mysql-result-set + :res-ptr res-ptr + :num-fields num-fields + :full-set full-set + :types + (canonicalize-types + result-types res-ptr)))) + (if full-set + (values result-set + num-fields + (mysql-num-rows res-ptr)) + (values result-set + num-fields))) + (error 'sql-database-data-error + :database database + :expression query-expression + :error-id (mysql-errno mysql-ptr) + :message (mysql-error-string mysql-ptr)))) + ))) (defmethod database-dump-result-set (result-set (database mysql-database)) (mysql-free-result (mysql-result-set-res-ptr result-set)) @@ -303,9 +370,10 @@ (setf (car rest) (convert-raw-field (uffi:deref-array row '(:array (* :unsigned-char)) i) - types - i - (uffi:deref-array lengths '(:array :unsigned-long) i)))) + (nth i types) + :length + (uffi:deref-array lengths '(:array :unsigned-long) i) + :encoding (encoding database)))) list))) @@ -350,7 +418,8 @@ (declare (ignore owner)) (do ((results nil) (rows (database-query - (format nil "SHOW INDEX FROM ~A" table) + (format nil "SHOW INDEX FROM ~A" (escaped-database-identifier + table database)) database nil nil) (cdr rows))) ((null rows) (nreverse results)) @@ -358,21 +427,31 @@ (unless (find col results :test #'string-equal) (push col results))))) -(defmethod database-list-attributes ((table string) (database mysql-database) - &key (owner nil)) +(defmethod database-list-attributes ((table clsql-sys::%database-identifier) + (database mysql-database) + &key (owner nil) + &aux (table (unescaped-database-identifier table))) (declare (ignore owner)) (mapcar #'car (database-query - (format nil "SHOW COLUMNS FROM ~A" table) + (format nil "SHOW COLUMNS FROM ~A" (escaped-database-identifier + table database)) database nil nil))) -(defmethod database-attribute-type (attribute (table string) +(defmethod database-attribute-type ((attribute clsql-sys::%database-identifier) + (table clsql-sys::%database-identifier) (database mysql-database) - &key (owner nil)) + &key (owner nil) + &aux (table (unescaped-database-identifier table)) + (attribute (unescaped-database-identifier attribute))) (declare (ignore owner)) (let ((row (car (database-query (format nil - "SHOW COLUMNS FROM ~A LIKE '~A'" table attribute) + "SHOW COLUMNS FROM ~A LIKE '~A'" + (escaped-database-identifier + table database) + (unescaped-database-identifier + attribute database)) database nil nil)))) (let* ((raw-type (second row)) (null (third row)) @@ -388,17 +467,9 @@ ;;; Sequence functions -(defun %sequence-name-to-table (sequence-name) - (concatenate 'string "_CLSQL_SEQ_" (sql-escape sequence-name))) - -(defun %table-name-to-sequence-name (table-name) - (and (>= (length table-name) 11) - (string-equal (subseq table-name 0 11) "_CLSQL_SEQ_") - (subseq table-name 11))) - (defmethod database-create-sequence (sequence-name (database mysql-database)) - (let ((table-name (%sequence-name-to-table sequence-name))) + (let ((table-name (%sequence-name-to-table sequence-name database))) (database-execute-command (concatenate 'string "CREATE TABLE " table-name " (id int NOT NULL PRIMARY KEY AUTO_INCREMENT)") @@ -411,7 +482,8 @@ (defmethod database-drop-sequence (sequence-name (database mysql-database)) (database-execute-command - (concatenate 'string "DROP TABLE " (%sequence-name-to-table sequence-name)) + (concatenate 'string "DROP TABLE " + (%sequence-name-to-table sequence-name database)) database)) (defmethod database-list-sequences ((database mysql-database) @@ -419,14 +491,14 @@ (declare (ignore owner)) (mapcan #'(lambda (s) (let ((sn (%table-name-to-sequence-name (car s)))) - (and sn (list sn)))) + (and sn (list (car s) sn)))) (database-query "SHOW TABLES" database nil nil))) (defmethod database-set-sequence-position (sequence-name (position integer) (database mysql-database)) (database-execute-command - (format nil "UPDATE ~A SET id=~A" (%sequence-name-to-table sequence-name) + (format nil "UPDATE ~A SET id=~A" (%sequence-name-to-table sequence-name database) position) database) (mysql:mysql-insert-id (clsql-mysql::database-mysql-ptr database))) @@ -434,7 +506,7 @@ (defmethod database-sequence-next (sequence-name (database mysql-database)) (without-interrupts (database-execute-command - (concatenate 'string "UPDATE " (%sequence-name-to-table sequence-name) + (concatenate 'string "UPDATE " (%sequence-name-to-table sequence-name database) " SET id=LAST_INSERT_ID(id+1)") database) (mysql:mysql-insert-id (clsql-mysql::database-mysql-ptr database)))) @@ -443,9 +515,15 @@ (without-interrupts (caar (database-query (concatenate 'string "SELECT id from " - (%sequence-name-to-table sequence-name)) + (%sequence-name-to-table sequence-name database)) database :auto nil)))) +(defmethod database-last-auto-increment-id ((database mysql-database) table column) + (declare (ignore table column)) + (car (query "SELECT LAST_INSERT_ID();" + :flatp t :field-names nil + :database database))) + (defmethod database-create (connection-spec (type (eql :mysql))) (destructuring-bind (host name user password) connection-spec (let ((database (database-connect (list host "" user password) @@ -470,7 +548,8 @@ t)) (defmethod database-list (connection-spec (type (eql :mysql))) - (destructuring-bind (host name user password &optional port) connection-spec + (destructuring-bind (host name user password &optional port options) connection-spec + (declare (ignore options)) (let ((database (database-connect (list host (or name "mysql") user password port) type))) (unwind-protect @@ -521,7 +600,8 @@ :message (mysql-error-string mysql-ptr))) (uffi:with-cstring (native-query sql-stmt) - (unless (zerop (mysql-stmt-prepare stmt native-query (expression-length sql-stmt))) + (unless (zerop (mysql-stmt-prepare stmt native-query (uffi:foreign-encoded-octet-count + sql-stmt :encoding (encoding database)))) (mysql-stmt-close stmt) (error 'sql-database-error :error-id (mysql-errno mysql-ptr) @@ -693,21 +773,21 @@ ((#.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) - (uffi:convert-from-foreign-string buffer)) - (#.mysql-field-types#tiny - (uffi:ensure-char-integer - (uffi:deref-pointer buffer :byte))) - (#.mysql-field-types#short - (uffi:deref-pointer buffer :short)) - (#.mysql-field-types#long - (uffi:deref-pointer buffer :int)) - #+64bit - (#.mysql-field-types#longlong + (uffi:convert-from-foreign-string buffer :encoding (encoding (database stmt)))) + (#.mysql-field-types#tiny + (uffi:ensure-char-integer + (uffi:deref-pointer buffer :byte))) + (#.mysql-field-types#short + (uffi:deref-pointer buffer :short)) + (#.mysql-field-types#long + (uffi:deref-pointer buffer :int)) + #+64bit + (#.mysql-field-types#longlong (uffi:deref-pointer buffer :long)) - (#.mysql-field-types#float - (uffi:deref-pointer buffer :float)) - (#.mysql-field-types#double - (uffi:deref-pointer buffer :double)) + (#.mysql-field-types#float + (uffi:deref-pointer buffer :float)) + (#.mysql-field-types#double + (uffi:deref-pointer buffer :double)) ((#.mysql-field-types#time #.mysql-field-types#date #.mysql-field-types#datetime #.mysql-field-types#timestamp) (let ((year (uffi:get-slot-value buffer 'mysql-time 'mysql::year)) @@ -715,7 +795,7 @@ (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)))) @@ -761,5 +841,8 @@ #+(or mysql-client-v4.1 mysql-client-v5) t #-(or mysql-client-v4.1 mysql-client-v5) nil) +(defmethod db-type-has-auto-increment? ((db-type (eql :mysql))) + t) + (when (clsql-sys:database-type-library-loaded :mysql) (clsql-sys:initialize-database-type :database-type :mysql))