X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=db-oracle%2Foracle-sql.lisp;h=f71b7f772836820ea7cb0b30938de3792e3a22e3;hb=e622ee6f4bf2b9fe81af59d566e651c983a4833b;hp=d5fdb8797d3f529ff8ebca8de76ea0a06e125ffc;hpb=ca8b8ab72493dfe86309bf489d4a7ccf897adf08;p=clsql.git diff --git a/db-oracle/oracle-sql.lisp b/db-oracle/oracle-sql.lisp index d5fdb87..f71b7f7 100644 --- a/db-oracle/oracle-sql.lisp +++ b/db-oracle/oracle-sql.lisp @@ -15,8 +15,7 @@ (in-package #:clsql-oracle) -(defmethod database-initialize-database-type - ((database-type (eql :oracle))) +(defmethod database-initialize-database-type ((database-type (eql :oracle))) t) ;;;; arbitrary parameters, tunable for performance or other reasons @@ -37,6 +36,13 @@ likely that we'll have to worry about the CMUCL limit.")) ;; constants - from OCI? +(defvar +unsigned-char-null-pointer+ + (uffi:make-null-pointer :unsigned-char)) +(defvar +unsigned-short-null-pointer+ + (uffi:make-null-pointer :unsigned-short)) +(defvar +unsigned-int-null-pointer+ + (uffi:make-null-pointer :unsigned-int)) + (defconstant +var-not-in-list+ 1007) (defconstant +no-data-found+ 1403) (defconstant +null-value-returned+ 1405) @@ -45,8 +51,8 @@ likely that we'll have to worry about the CMUCL limit.")) (eval-when (:compile-toplevel :load-toplevel :execute) (defconstant SQLT-NUMBER 2) (defconstant SQLT-INT 3) - (defconstant SQLT-STR 5) (defconstant SQLT-FLT 4) + (defconstant SQLT-STR 5) (defconstant SQLT-DATE 12)) ;;; Note that despite the suggestive class name (and the way that the @@ -99,17 +105,29 @@ output format. In order to extract date strings from output buffers holding multiple date strings in fixed-width fields, we need to know the length of that format.") (server-version - :type string + :type (or null string) :initarg :server-version :reader server-version :documentation "Version string of Oracle server.") - (major-version-number + (major-server-version + :type (or null fixnum) + :initarg :major-server-version + :reader major-server-version + :documentation + "The major version number of the Oracle server, should be 8, 9, or 10") + (client-version + :type (or null string) + :initarg :client-version + :reader client-version + :documentation + "Version string of Oracle client.") + (major-client-version :type (or null fixnum) - :initarg :major-version-number - :reader major-version-number + :initarg :major-client-version + :reader major-client-version :documentation - "The major version number of Oracle, should be 8, 9, or 10"))) + "The major version number of the Oracle client, should be 8, 9, or 10"))) ;;; Handle the messy case of return code=+oci-error+, querying the @@ -128,9 +146,12 @@ the length of that format.") (uffi:ensure-char-storable (code-char 0))) (setf (uffi:deref-pointer errcode :long) 0) - (oci-error-get (deref-vp errhp) 1 - (uffi:make-null-pointer :unsigned-char) - errcode errbuf +errbuf-len+ +oci-htype-error+) + (uffi:with-cstring (sqlstate nil) + (oci-error-get (deref-vp errhp) 1 + sqlstate + errcode + (uffi:char-array-to-pointer errbuf) + +errbuf-len+ +oci-htype-error+)) (let ((subcode (uffi:deref-pointer errcode :long))) (unless (and nulls-ok (= subcode +null-value-returned+)) (error 'sql-database-error @@ -190,10 +211,10 @@ the length of that format.") ;; In order to map the "same string" property above onto Lisp equality, ;; we drop trailing spaces in all cases: -(uffi:def-type string-array (:array :unsigned-char)) +(uffi:def-type string-pointer (* :unsigned-char)) (defun deref-oci-string (arrayptr string-index size) - (declare (type string-array arrayptr)) + (declare (type string-pointer arrayptr)) (declare (type (mod #.+n-buf-rows+) string-index)) (declare (type (and unsigned-byte fixnum) size)) (let* ((raw (uffi:convert-from-foreign-string @@ -403,7 +424,7 @@ the length of that format.") (value (let* ((arb (foreign-resource-buffer (cd-indicators cd))) (indicator (uffi:deref-array arb '(:array :short) irow))) - (declare (type short-array arb)) + ;;(declare (type short-array arb)) (unless (= indicator -1) (ecase (cd-oci-data-type cd) (#.SQLT-STR @@ -443,7 +464,7 @@ the length of that format.") (oci-attr-get (deref-vp (qc-stmthp qc)) +oci-htype-stmt+ rowcount - (uffi:make-null-pointer :unsigned-long) + +unsigned-int-null-pointer+ +oci-attr-row-count+ (deref-vp errhp)) (setf (qc-n-from-oci qc) @@ -487,7 +508,7 @@ the length of that format.") (oci-attr-get (deref-vp stmthp) +oci-htype-stmt+ stmttype - (uffi:make-null-pointer :unsigned-int) + +unsigned-int-null-pointer+ +oci-attr-stmt-type+ (deref-vp errhp) :database db) @@ -612,7 +633,7 @@ the length of that format.") (oci-attr-get (deref-vp parmdp) +oci-dtype-param+ dtype-foreign - (uffi:make-null-pointer :unsigned-int) + +unsigned-int-null-pointer+ +oci-attr-data-type+ (deref-vp errhp)) (let ((dtype (uffi:deref-pointer dtype-foreign :unsigned-short))) @@ -625,20 +646,21 @@ the length of that format.") (oci-attr-get (deref-vp parmdp) +oci-dtype-param+ precision - (uffi:make-null-pointer :unsigned-int) + +unsigned-int-null-pointer+ +oci-attr-precision+ (deref-vp errhp)) (oci-attr-get (deref-vp parmdp) +oci-dtype-param+ scale - (uffi:make-null-pointer :unsigned-int) + +unsigned-int-null-pointer+ +oci-attr-scale+ (deref-vp errhp)) (let ((*scale (uffi:deref-pointer scale :byte)) (*precision (uffi:deref-pointer precision :byte))) - ;;(format t "scale=~d, precision=~d~%" *scale *precision) + + ;; (format t "scale=~d, precision=~d~%" *scale *precision) (cond - ((or (zerop *scale) + ((or (and (zerop *scale) (not (zerop *precision))) (and (minusp *scale) (< *precision 10))) (setf buffer (acquire-foreign-resource :int +n-buf-rows+) sizeof 4 ;; sizeof(int) @@ -654,7 +676,7 @@ the length of that format.") (oci-attr-get (deref-vp parmdp) +oci-dtype-param+ colsize - (uffi:make-null-pointer :unsigned-int) ;; (uffi:pointer-address colsizesize) + +unsigned-int-null-pointer+ +oci-attr-data-size+ (deref-vp errhp)) (let ((colsize-including-null (1+ (uffi:deref-pointer colsize :unsigned-long)))) @@ -692,15 +714,12 @@ the length of that format.") defnp (deref-vp errhp) (1+ icolumn) ; OCI 1-based indexing again - (uffi:with-cast-pointer (vp (foreign-resource-buffer buffer) :void) - vp) + (foreign-resource-buffer buffer) sizeof dtype - (uffi:with-cast-pointer (vp (foreign-resource-buffer indicators) :void) - vp) - (uffi:make-null-pointer :unsigned-short) - (uffi:with-cast-pointer (vp (foreign-resource-buffer retcodes) :unsigned-short) - vp) + (foreign-resource-buffer indicators) + +unsigned-short-null-pointer+ + (foreign-resource-buffer retcodes) +oci-default+)))))))) ;; Release the resources associated with a QUERY-CURSOR. @@ -780,26 +799,38 @@ the length of that format.") ) ;; Actually, oci-server-version returns the client version, not the server versions ;; will use "SELECT VERSION FROM V$INSTANCE" to get actual server version. - (let (db server-version) + (let (db server-version client-version) + (declare (ignorable server-version)) (uffi:with-foreign-object (buf '(:array :unsigned-char #.+errbuf-len+)) (oci-server-version (deref-vp svchp) (deref-vp errhp) (uffi:char-array-to-pointer buf) +errbuf-len+ +oci-htype-svcctx+) + (setf client-version (uffi:convert-from-foreign-string buf)) + ;; This returns the client version, not the server version, so diable it + #+ignore + (oci-server-version (deref-vp srvhp) + (deref-vp errhp) + (uffi:char-array-to-pointer buf) + +errbuf-len+ +oci-htype-server+) + #+ignore (setf server-version (uffi:convert-from-foreign-string buf))) (setq db (make-instance 'oracle-database :name (database-name-from-spec connection-spec database-type) + :connection-spec connection-spec :envhp envhp :errhp errhp :database-type :oracle :svchp svchp :dsn data-source-name :user user + :client-version client-version :server-version server-version - :major-version-number (major-version-from-string + :major-client-version (major-client-version-from-string + client-version) + :major-server-version (major-client-version-from-string server-version))) - (oci-logon (deref-vp envhp) (deref-vp errhp) svchp @@ -810,16 +841,32 @@ the length of that format.") ;; :date-format-length (1+ (length date-format))))) (setf (slot-value db 'clsql-sys::state) :open) (database-execute-command - (format nil "alter session set NLS_DATE_FORMAT='~A'" (date-format db)) db) + (format nil "ALTER SESSION SET NLS_DATE_FORMAT='~A'" (date-format db)) db) + (let ((server-version (caar (database-query "SELECT BANNER FROM V$VERSION WHERE BANNER LIKE '%Oracle%'" db nil nil)))) + (setf (slot-value db 'server-version) server-version + (slot-value db 'major-server-version) (major-client-version-from-string + server-version))) db)))) -(defun major-version-from-string (str) +(defun major-client-version-from-string (str) (cond ((search " 10g " str) 10) - ((search "Oracle9i " str) - 10))) + ((search "Oracle9i " str) + 9) + ((search "Oracle8" str) + 8))) + +(defun major-server-version-from-string (str) + (when (> (length str) 2) + (cond + ((string= "10." (subseq str 0 3)) + 10) + ((string= "9." (subseq str 0 2)) + 9) + ((string= "8." (subseq str 0 2)) + 8)))) ;; Close a database connection. @@ -864,18 +911,14 @@ the length of that format.") (push row reversed-result)))))) -(defmethod database-create-sequence - (sequence-name (database oracle-database)) +(defmethod database-create-sequence (sequence-name (database oracle-database)) (execute-command - (concatenate 'string "CREATE SEQUENCE " - (sql-escape sequence-name)) + (concatenate 'string "CREATE SEQUENCE " (sql-escape sequence-name)) :database database)) -(defmethod database-drop-sequence - (sequence-name (database oracle-database)) +(defmethod database-drop-sequence (sequence-name (database oracle-database)) (execute-command - (concatenate 'string "DROP SEQUENCE " - (sql-escape sequence-name)) + (concatenate 'string "DROP SEQUENCE " (sql-escape sequence-name)) :database database)) (defmethod database-sequence-next (sequence-name (database oracle-database)) @@ -887,7 +930,8 @@ the length of that format.") ) database :auto nil))) -(defmethod database-set-sequence-position (name position database) +;; FIXME: use lock +(defmethod database-set-sequence-position (name position (database oracle-database)) (let* ((next (database-sequence-next name database)) (incr (- position next))) (database-execute-command @@ -979,7 +1023,8 @@ the length of that format.") list))) (defmethod clsql-sys:database-start-transaction ((database oracle-database)) - (call-next-method)) + (call-next-method) + ) ;;(with-slots (svchp errhp) database ;; (osucc (oci-trans-start (uffi:deref-pointer svchp)