r9457: Reworked CLSQL file structure.
[clsql.git] / db-oracle / oracle-sql.lisp
index 79ba68e4bb30e20d71621266aca2c10103711550..f71b7f772836820ea7cb0b30938de3792e3a22e3 100644 (file)
@@ -36,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)
@@ -44,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
@@ -98,7 +105,7 @@ 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
@@ -110,7 +117,7 @@ the length of that format.")
     :documentation
     "The major version number of the Oracle server, should be 8, 9, or 10")
    (client-version 
-    :type string
+    :type (or null string)
     :initarg :client-version
     :reader client-version
     :documentation
@@ -139,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
@@ -201,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 
@@ -414,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  
@@ -454,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)
@@ -498,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)
@@ -623,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)))
@@ -636,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)
@@ -665,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))))
@@ -703,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.
@@ -903,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))
@@ -926,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
@@ -1018,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)