+;; 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#int8
+ :longlong)
+ ((#.pgsql-ftype#float4
+ #.pgsql-ftype#float8)
+ :double)
+ (otherwise
+ t))))
+
+
+(defun canonicalize-type-list (types num-fields)
+ "Ensure a field type list meets expectations.
+Duplicated from clsql-uffi package so that this interface
+doesn't depend on UFFI."
+ (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 :longlong t)
+ (nth i types))
+ (t
+ t))
+ new-types)))
+ (nreverse new-types)))
+
+(defun canonicalize-types (types cursor)
+ (let* ((fields (postgresql-cursor-fields cursor))
+ (num-fields (length fields)))
+ (cond
+ ((listp types)
+ (canonicalize-type-list types num-fields))
+ ((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))))
+