r1657: Auto :types implemented for mysql,postgresql
[clsql.git] / interfaces / postgresql / postgresql-sql.cl
index f0a1ee1bd511a614a6b5002c6152278570738e2d..db4128bf96c4eab0469eda865be9e716a44d029d 100644 (file)
@@ -8,7 +8,7 @@
 ;;;;                Original code by Pierre R. Mai 
 ;;;; Date Started:  Feb 2002
 ;;;;
-;;;; $Id: postgresql-sql.cl,v 1.7 2002/03/25 06:07:06 kevin Exp $
+;;;; $Id: postgresql-sql.cl,v 1.8 2002/03/25 14:13:41 kevin Exp $
 ;;;;
 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
 ;;;; and Copyright (c) 1999-2001 by Pierre R. Mai
 
 ;;; Field conversion functions
 
-(defun canonicalize-field-types  (types num-fields)
-  (if (listp types)
-      (let ((length-types (length types))
-           new-types)
-       (loop for i from 0 below num-fields
+(defun canonicalize-field-types (types num-fields res-ptr)
+  (cond
+   ((if (listp types)
+       (let ((length-types (length types))
+             (new-types '()))
+         (loop for i from 0 below num-fields
              do
-             (if (>= i length-types)
-                 (push t new-types) ;; types is shorted than num-fields
+               (if (>= i length-types)
+                   (push t new-types) ;; types is shorted than num-fields
                  (push
                   (case (nth i types)
                     ((:int :long :double t)
                     (t
                      t))
                   new-types)))
-       (nreverse new-types))
-      (if (eq types :auto)
-         :auto
-         nil)))
+         (nreverse new-types))))
+   ((eq types :auto)
+    (let ((new-types '()))
+      (dotimes (i num-fields)
+       (declare (fixnum i))
+       (let* ((type (PQftype res-ptr i)))
+         (push
+          (case type
+            ((#.pgsql-ftype#bytea
+              #.pgsql-ftype#int2
+              #.pgsql-ftype#int4)
+             :int)
+            ((#.pgsql-ftype#float4
+              #.pgsql-ftype#float8)
+             :double)
+            (otherwise
+             t))
+          new-types)))
+      (nreverse new-types)))
+   (t
+    nil)))
+
 
 (uffi:def-function "atoi"
     ((str :cstring))
               (#.pgsql-exec-status-type#tuples-ok
               (let ((num-fields (PQnfields result)))
                 (setq field-types
-                      (canonicalize-field-types field-types num-fields))
+                  (canonicalize-field-types field-types num-fields
+                                            result))
                 (loop for tuple-index from 0 below (PQntuples result)
                       collect
                       (loop for i from 0 below num-fields
                         :num-tuples (PQntuples result)
                        :field-types (canonicalize-field-types 
                                      field-types
-                                     (PQnfields result)))))
+                                     (PQnfields result)
+                                     result))))
             (if full-set
                 (values result-set
                         (PQnfields result)