X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=db-oracle%2Foracle-sql.lisp;h=5ee5ce4e1290f99fd7baa588d7f8d305ce20fea7;hb=d0695ffb828519fade3aa5166236812e6144975b;hp=b5876d44cbff9d96a6358d544af5e1728d65a43c;hpb=1df8353cc15bcbf98078605cb6955aafa622ecea;p=clsql.git diff --git a/db-oracle/oracle-sql.lisp b/db-oracle/oracle-sql.lisp index b5876d4..5ee5ce4 100644 --- a/db-oracle/oracle-sql.lisp +++ b/db-oracle/oracle-sql.lisp @@ -37,8 +37,6 @@ likely that we'll have to worry about the CMUCL limit.")) (defmacro deref-vp (foreign-object) `(the vp-type (uffi:deref-pointer (the vpp-type ,foreign-object) :pointer-void))) -;; constants - from OCI? - (defvar +unsigned-char-null-pointer+ (uffi:make-null-pointer :unsigned-char)) (defvar +unsigned-short-null-pointer+ @@ -46,6 +44,8 @@ likely that we'll have to worry about the CMUCL limit.")) (defvar +unsigned-int-null-pointer+ (uffi:make-null-pointer :unsigned-int)) +;; constants - from OCI? + (defconstant +var-not-in-list+ 1007) (defconstant +no-data-found+ 1403) (defconstant +null-value-returned+ 1405) @@ -118,19 +118,7 @@ the length of that format.") :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-client-version - :reader major-client-version - :documentation - "The major version number of the Oracle client, should be 8, 9, or 10"))) + "The major version number of the Oracle server, should be 8, 9, or 10"))) ;;; Handle the messy case of return code=+oci-error+, querying the @@ -139,16 +127,15 @@ the length of that format.") (defun handle-oci-error (&key database nulls-ok) (cond (database - (with-slots (errhp) - database + (with-slots (errhp) database (uffi:with-foreign-objects ((errbuf '(:array :unsigned-char #.+errbuf-len+)) (errcode :long)) ;; ensure errbuf empty string (setf (uffi:deref-array errbuf '(:array :unsigned-char) 0) - (uffi:ensure-char-storable (code-char 0))) - + (uffi:ensure-char-storable (code-char 0))) (setf (uffi:deref-pointer errcode :long) 0) + (uffi:with-cstring (sqlstate nil) (oci-error-get (deref-vp errhp) 1 sqlstate @@ -190,7 +177,7 @@ the length of that format.") ;;; Enabling this can be handy for low-level debugging. #+nil (progn - (trace oci-initialize #+oci-8-1-5 oci-env-create oci-handle-alloc oci-logon + (trace #-oci7 oci-env-create oci-initialize oci-handle-alloc oci-logon oci-error-get oci-stmt-prepare oci-stmt-execute oci-param-get oci-logon oci-attr-get oci-define-by-pos oci-stmt-fetch) (setf debug::*debug-print-length* nil)) @@ -396,7 +383,9 @@ the length of that format.") (#.SQLT-FLT (uffi:deref-array b '(:array :double) irow)) (#.SQLT-INT - (uffi:deref-array b '(:array :int) irow)) + (ecase (cd-sizeof cd) + (4 + (uffi:deref-array b '(:array :int) irow)))) (#.SQLT-DATE (deref-oci-string b irow (cd-sizeof cd)))))))) (when (and (eq :string (cd-result-type cd)) @@ -519,6 +508,12 @@ the length of that format.") ;; 21 bytes. See pp. 3-10, 3-26, and 6-13 of OCI documentation ;; for more details. +;; Mac OS X Note: According to table 6-8 in the Oracle 9i OCI +;; documentation, PRECISION may actually be an sb2 instead of a +;; single byte if performing an "implicit describe". Using a +;; signed short instead of an unsigned byte fixes a Mac OS X bug +;; where PRECISION is always zero. -- JJB 20040713 + ;; When calling OCI C code to handle the conversion, we have ;; only two numeric types available to pass the return value: ;; double-float and signed-long. It would be possible to @@ -565,7 +560,10 @@ the length of that format.") ;; below, beware!) try setting this value into COLSIZE, calling OCI, ;; then looking at the value in COLSIZE. (setf colsize #x12345678) ;; debugging only - + +;; Mac OS X Note: This workaround fails on a bigendian platform so +;; I've changed the data type of COLNAME to :unsigned-short as per +;; the Oracle 9i OCI documentation. -- JJB 20040713 (uffi:def-type byte-pointer (* :byte)) (uffi:def-type ulong-pointer (* :unsigned-long)) @@ -578,11 +576,11 @@ the length of that format.") (with-slots (errhp) database (uffi:with-foreign-objects ((dtype-foreign :unsigned-short) (parmdp :pointer-void) - (precision :byte) + (precision :short) (scale :byte) (colname '(* :unsigned-char)) (colnamelen :unsigned-long) - (colsize :unsigned-long) + (colsize :unsigned-short) (colsizesize :unsigned-long) (defnp ':pointer-void)) (let ((buffer nil) @@ -625,12 +623,12 @@ the length of that format.") +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) + (*precision (uffi:deref-pointer precision :short))) + + ;;(format t "scale=~d, precision=~d~%" *scale *precision) (cond - ((or (and (zerop *scale) (not (zerop *precision))) - (and (minusp *scale) (< *precision 10))) + ((or (and (minusp *scale) (zerop *precision)) + (and (zerop *scale) (plusp *precision))) (setf buffer (acquire-foreign-resource :int +n-buf-rows+) sizeof 4 ;; sizeof(int) dtype #.SQLT-INT)) @@ -640,7 +638,7 @@ the length of that format.") dtype #.SQLT-FLT))))) ;; Default to SQL-STR (t - (setf (uffi:deref-pointer colsize :unsigned-long) 0) + (setf (uffi:deref-pointer colsize :unsigned-short) 0) (setf dtype #.SQLT-STR) (oci-attr-get (deref-vp parmdp) +oci-dtype-param+ @@ -648,7 +646,7 @@ the length of that format.") +unsigned-int-null-pointer+ +oci-attr-data-size+ (deref-vp errhp)) - (let ((colsize-including-null (1+ (uffi:deref-pointer colsize :unsigned-long)))) + (let ((colsize-including-null (1+ (uffi:deref-pointer colsize :unsigned-short)))) (setf buffer (acquire-foreign-resource :unsigned-char (* +n-buf-rows+ colsize-including-null))) (setf sizeof colsize-including-null)))) @@ -730,76 +728,52 @@ the length of that format.") ;; handle errors very gracefully (since they're part of the ;; error-handling mechanism themselves) so we just assert they ;; work. + (setf (deref-vp envhp) +null-void-pointer+) - #+oci-8-1-5 - (progn - (oci-env-create envhp +oci-default+ +null-void-pointer+ - +null-void-pointer+ +null-void-pointer+ - +null-void-pointer+ 0 +null-void-pointer-pointer+) - (oci-handle-alloc envhp - (deref-vp errhp) - +oci-htype-error+ 0 - +null-void-pointer-pointer+)) - #-oci-8-1-5 + + #-oci7 + (oci-env-create envhp +oci-default+ +null-void-pointer+ + +null-void-pointer+ +null-void-pointer+ + +null-void-pointer+ 0 +null-void-pointer-pointer+) + #+oci7 (progn (oci-initialize +oci-object+ +null-void-pointer+ +null-void-pointer+ +null-void-pointer+ +null-void-pointer-pointer+) (ignore-errors (oci-handle-alloc +null-void-pointer+ envhp +oci-htype-env+ 0 +null-void-pointer-pointer+)) ;no testing return - (oci-env-init envhp +oci-default+ 0 +null-void-pointer-pointer+) - (oci-handle-alloc (deref-vp envhp) errhp - +oci-htype-error+ 0 +null-void-pointer-pointer+) - (oci-handle-alloc (deref-vp envhp) srvhp - +oci-htype-server+ 0 +null-void-pointer-pointer+) - (uffi:with-cstring (dblink nil) - (oci-server-attach (deref-vp srvhp) - (deref-vp errhp) - dblink - 0 +oci-default+)) - (oci-handle-alloc (deref-vp envhp) svchp - +oci-htype-svcctx+ 0 +null-void-pointer-pointer+) - (oci-attr-set (deref-vp svchp) - +oci-htype-svcctx+ - (deref-vp srvhp) 0 +oci-attr-server+ - (deref-vp errhp)) - ;; oci-handle-alloc((dvoid *)encvhp, (dvoid **)&stmthp, OCI_HTYPE_STMT, 0, 0); - ;;#+nil - ) - ;; 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 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-client-version (major-client-version-from-string - client-version) - :major-server-version (major-client-version-from-string - server-version))) + (oci-env-init envhp +oci-default+ 0 +null-void-pointer-pointer+)) + (oci-handle-alloc (deref-vp envhp) errhp + +oci-htype-error+ 0 +null-void-pointer-pointer+) + (oci-handle-alloc (deref-vp envhp) srvhp + +oci-htype-server+ 0 +null-void-pointer-pointer+) + + #+ignore ;; not used since CLSQL uses the OCILogon function instead + (uffi:with-cstring (dblink nil) + (oci-server-attach (deref-vp srvhp) + (deref-vp errhp) + dblink + 0 +oci-default+)) + + (oci-handle-alloc (deref-vp envhp) svchp + +oci-htype-svcctx+ 0 +null-void-pointer-pointer+) + (oci-attr-set (deref-vp svchp) + +oci-htype-svcctx+ + (deref-vp srvhp) 0 +oci-attr-server+ + (deref-vp errhp)) + ;; oci-handle-alloc((dvoid *)encvhp, (dvoid **)&stmthp, OCI_HTYPE_STMT, 0, 0); + ;;#+nil + + (let ((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))) (oci-logon (deref-vp envhp) (deref-vp errhp) svchp @@ -924,8 +898,8 @@ the length of that format.") (defmethod database-execute-command (sql-expression (database oracle-database)) (database-query sql-expression database nil nil) - ;; HACK HACK HACK - (database-query "commit" database nil nil) + (when (database-autocommit database) + (oracle-commit database)) t) @@ -993,27 +967,30 @@ the length of that format.") do (setf (nth i list) (nth i row))) list))) -(defmethod clsql-sys:database-start-transaction ((database oracle-database)) +(defmethod database-start-transaction ((database oracle-database)) (call-next-method) - ) - -;;(with-slots (svchp errhp) database -;; (osucc (oci-trans-start (uffi:deref-pointer svchp) -;; (uffi:deref-pointer errhp) -;; 60 -;; +oci-trans-new+))) -;; t) - + ;; Not needed with simple transaction + #+ignore + (with-slots (svchp errhp) database + (oci-trans-start (deref-vp svchp) + (deref-vp errhp) + 60 + +oci-trans-new+)) + t) -(defmethod clsql-sys:database-commit-transaction ((database oracle-database)) - (call-next-method) + +(defun oracle-commit (database) (with-slots (svchp errhp) database - (osucc (oci-trans-commit (deref-vp svchp) - (deref-vp errhp) - 0))) + (osucc (oci-trans-commit (deref-vp svchp) + (deref-vp errhp) + 0)))) + +(defmethod database-commit-transaction ((database oracle-database)) + (call-next-method) + (oracle-commit database) t) -(defmethod clsql-sys:database-abort-transaction ((database oracle-database)) +(defmethod database-abort-transaction ((database oracle-database)) (call-next-method) (osucc (oci-trans-rollback (deref-vp (svchp database)) (deref-vp (errhp database))