+;; Field type conversion
+
+(defun canonical-field-type (fields index)
+ "Extracts canonical field type from fields list"
+ (let ((oid (cadr (nth index fields))))
+ (case oid
+ ((#.pgsql-ftype#bytea
+ #.pgsql-ftype#int2
+ #.pgsql-ftype#int4)
+ :int)
+ ((#.pgsql-ftype#float4
+ #.pgsql-ftype#float8)
+ :double)
+ (otherwise
+ t))))
+
+(defun canonicalize-field-types (types cursor)
+ (let* ((fields (postgresql-cursor-fields cursor))
+ (num-fields (length fields)))
+ (cond
+ ((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
+ (push
+ (case (nth i types)
+ ((:int :long :double t)
+ (nth i types))
+ (t
+ t))
+ new-types)))
+ (nreverse new-types)))
+ ((eq types :auto)
+ (let ((new-types '()))
+ (dotimes (i num-fields)
+ (declare (fixnum i))
+ (push (canonical-field-type fields i) new-types))
+ (nreverse new-types)))
+ (t
+ nil))))
+