r1683: *** empty log message ***
[clsql.git] / interfaces / postgresql-socket / postgresql-socket-api.cl
index c33bb131f1144198cdd6694a183515e5fd95c002..0ee7d8a8fe0ee2911714260dee5288631cd67b4c 100644 (file)
@@ -9,7 +9,7 @@
 ;;;;                
 ;;;; Date Started:  Feb 2002
 ;;;;
-;;;; $Id: postgresql-socket-api.cl,v 1.5 2002/03/25 23:48:46 kevin Exp $
+;;;; $Id: postgresql-socket-api.cl,v 1.12 2002/03/27 12:09:39 kevin Exp $
 ;;;;
 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
 ;;;; and Copyright (c) 1999-2001 by Pierre R. Mai
@@ -36,6 +36,7 @@
     ((:bytea 17)
      (:int2 21)
      (:int4 23)
+     (:int8 20)
      (:float4 700)
      (:float8 701)))
 
@@ -208,7 +209,8 @@ socket interface"
 
 (defun read-socket-sequence (string stream)
 "KMR -- Added to support reading from binary stream into a string"
-  (declare (optimize (speed 3) (safety 0)))
+  (declare (optimize (speed 3) (safety 0))
+          (string string))
   (dotimes (i (length string))
     (declare (fixnum i))
     (setf (char string i) (code-char (read-byte stream))))
@@ -563,47 +565,122 @@ connection, if it is still open."
            do (setf (aref result index) (ldb (byte 1 weight) byte))))
     result))
 
-(defun read-field (socket type)
-  (let* ((length (read-socket-value 'int32 socket))
-        (result (make-string (- length 4))))
-    (read-socket-sequence result socket)
-    (case type
-      (:int
-       (parse-integer result))
-      (:double
-       (let ((*read-default-float-format* 'double-float))
-        (read-from-string result)))
-      (t
-       result))))
 
-(defun read-field2 (socket type)
-  (let* ((length (read-socket-value 'int32 socket)))
+(defun read-field (socket type)
+  (let ((length (- (read-socket-value 'int32 socket) 4)))
     (case type
-      (:int
+      ((:int32 :int64)
        (read-integer-from-socket socket length))
       (:double
        (read-double-from-socket socket length))
       (t
-       (let ((result (make-string (- length 4))))
+       (let ((result (make-string length)))
         (read-socket-sequence result socket)
         result)))))
 
+(uffi:def-constant +char-code-zero+ (char-code #\0))
+(uffi:def-constant +char-code-minus+ (char-code #\-))
+(uffi:def-constant +char-code-plus+ (char-code #\+))
+(uffi:def-constant +char-code-period+ (char-code #\.))
+(uffi:def-constant +char-code-lower-e+ (char-code #\e))
+(uffi:def-constant +char-code-upper-e+ (char-code #\E))
+
 (defun read-integer-from-socket (socket length)
-  (let ((val 0)
-       (first-char (read-byte socket))
-       (negative nil))
-    (if (eql first-char (char-code #\-))
-       (setq negative t)
-       (setq val (- first-char (char-code #\0))))
-    (dotimes (i (1- length))
-      (setq val (+
-                (* 10 val)
-                (- (read-byte socket) (char-code #\0)))))
-    (if negative
-       (- 0 val)
-       val)))
-
-           
+  (declare (fixnum length))
+  (if (zerop length)
+      nil
+    (let ((val 0)
+         (first-char (read-byte socket))
+         (minusp nil))
+      (declare (fixnum first-char))
+      (decf length) ;; read first char
+      (cond
+       ((= first-char +char-code-minus+)
+       (setq minusp t))
+       ((= first-char +char-code-plus+)
+       )               ;; nothing to do
+       (t
+       (setq val (- first-char +char-code-zero+))))
+      
+      (dotimes (i length)
+       (declare (fixnum i))
+       (setq val (+
+                  (* 10 val)
+                  (- (read-byte socket) +char-code-zero+))))
+      (if minusp
+         (- val)
+       val))))
+
+(defmacro ascii-digit (int)
+  (let ((offset (gensym)))
+    `(let ((,offset (- ,int +char-code-zero+)))
+      (declare (fixnum ,int ,offset))
+      (if (and (>= ,offset 0)
+              (< ,offset 10))
+         ,offset
+         nil))))
+      
+(defun read-double-from-socket (socket length)
+  (declare (fixnum length))
+  (let ((before-decimal 0)
+       (after-decimal 0)
+       (decimal-count 0)
+       (exponent 0)
+       (decimalp nil)
+       (minusp nil)
+       (result nil)
+       (char (read-byte socket)))
+    (declare (fixnum char exponent decimal-count))
+    (decf length) ;; already read first character
+    (cond
+      ((= char +char-code-minus+)
+       (setq minusp t))
+      ((= char +char-code-plus+)
+       )
+      ((= char +char-code-period+)
+       (setq decimalp t))
+      (t
+       (setq before-decimal (ascii-digit char))
+       (unless before-decimal
+        (error "Unexpected value"))))
+    
+    (block loop
+      (dotimes (i length)
+       (setq char (read-byte socket))
+       ;;      (format t "~&len:~D, i:~D, char:~D, minusp:~A, decimalp:~A" length i char minusp decimalp)
+       (let ((weight (ascii-digit char)))
+         (cond 
+          ((and weight (not decimalp)) ;; before decimal point
+           (setq before-decimal (+ weight (* 10 before-decimal))))
+          ((and weight decimalp) ;; after decimal point
+           (setq after-decimal (+ weight (* 10 after-decimal)))
+           (incf decimal-count))
+          ((and (= char +char-code-period+))
+           (setq decimalp t))
+          ((or (= char +char-code-lower-e+)          ;; E is for exponent
+               (= char +char-code-upper-e+))
+           (setq exponent (read-integer-from-socket socket (- length i 1)))
+           (setq exponent (or exponent 0))
+           (return-from loop))
+         (t 
+          (break "Unexpected value"))
+         )
+       )))
+    (setq result (* (+ (coerce before-decimal 'double-float)
+                      (* after-decimal 
+                         (expt 10 (- decimal-count))))
+                   (expt 10 exponent)))
+    (if minusp
+       (- result)
+       result)))
+       
+      
+#+ignore
+(defun read-double-from-socket (socket length)
+  (let ((result (make-string length)))
+    (read-socket-sequence result socket)
+    (let ((*read-default-float-format* 'double-float))
+      (read-from-string result))))
 
 (defun read-cursor-row (cursor types)
   (let* ((connection (postgresql-cursor-connection cursor))