r9509: * sql/ooddl.lisp: Commit patch from Edi Weitz fixing return
[clsql.git] / db-oracle / oracle-sql.lisp
index dfc3a155ae59a333f8255a95b50e57ddaf2723ed..222e030c9b41881c05edfb5276d0327044a8a3f8 100644 (file)
@@ -139,8 +139,7 @@ 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))
@@ -196,36 +195,21 @@ the length of that format.")
   (setf debug::*debug-print-length* nil))
 
 
-;;;; the OCI library, part V: converting from OCI representations to Lisp
-;;;; representations
-
 ;; Return the INDEXth string of the OCI array, represented as Lisp
 ;; SIMPLE-STRING. SIZE is the size of the fixed-width fields used by
 ;; Oracle to store strings within the array.
 
-;; In the wild world of databases, trailing spaces aren't generally
-;; significant, since e.g. "LARRY " and "LARRY    " are the same string
-;; stored in different fixed-width fields. OCI drops trailing spaces
-;; for us in some cases but apparently not for fields of fixed
-;; character width, e.g.
-;;
-;;   (dbi:sql "create table employees (name char(15), job char(15), city
-;;            char(15), rate float)" :db orcl :types :auto)
-;; In order to map the "same string" property above onto Lisp equality,
-;; we drop trailing spaces in all cases:
-
 (uffi:def-type string-pointer (* :unsigned-char))
 
 (defun deref-oci-string (arrayptr string-index size)
   (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 
-              (uffi:make-pointer
-               (+ (uffi:pointer-address arrayptr) (* string-index size))
-               :unsigned-char)))
-        (trimmed (string-trim " " raw)))
-     (if (equal trimmed "NULL") nil trimmed)))
+  (let ((str (uffi:convert-from-foreign-string 
+             (uffi:make-pointer
+              (+ (uffi:pointer-address arrayptr) (* string-index size))
+              :unsigned-char))))
+    (if (string-equal str "NULL") nil str)))
 
 ;; the OCI library, part Z: no-longer used logic to convert from
 ;; Oracle's binary date representation to Common Lisp's native date
@@ -411,7 +395,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))
@@ -641,11 +627,11 @@ the length of that format.")
                             (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 (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))
@@ -916,17 +902,17 @@ the length of that format.")
                 )
     database :auto nil)))
 
-;; 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
-     (format nil "ALTER SEQUENCE ~A INCREMENT BY ~D" name incr)
-     database)
-    (database-sequence-next name database)
-    (database-execute-command
-     (format nil "ALTER SEQUENCE ~A INCREMENT BY 1" name)
-     database)))
+  (without-interrupts
+   (let* ((next (database-sequence-next name database))
+         (incr (- position next)))
+     (database-execute-command
+      (format nil "ALTER SEQUENCE ~A INCREMENT BY ~D" name incr)
+      database)
+     (database-sequence-next name database)
+     (database-execute-command
+      (format nil "ALTER SEQUENCE ~A INCREMENT BY 1" name)
+      database))))
 
 (defmethod database-list-sequences ((database oracle-database) &key owner)
   (let ((query
@@ -939,8 +925,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)
 
 
@@ -1008,27 +994,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))