X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=interfaces%2Fpostgresql%2Fpostgresql-sql.cl;h=ce4641960b753dd5101ea5ee44f5dd2322535554;hb=225df4af8fb41057b7a0e80c3cf1912880ca620b;hp=eca4b293f4440dd3dcb2563ecbaa790e6c945eb0;hpb=b06cb6d32e2a334f7dc72e8fb583a5b9609136b7;p=clsql.git diff --git a/interfaces/postgresql/postgresql-sql.cl b/interfaces/postgresql/postgresql-sql.cl index eca4b29..ce46419 100644 --- a/interfaces/postgresql/postgresql-sql.cl +++ b/interfaces/postgresql/postgresql-sql.cl @@ -8,7 +8,7 @@ ;;;; Original code by Pierre R. Mai ;;;; Date Started: Feb 2002 ;;;; -;;;; $Id: postgresql-sql.cl,v 1.2 2002/03/23 17:07:40 kevin Exp $ +;;;; $Id: postgresql-sql.cl,v 1.9 2002/03/25 23:48:46 kevin Exp $ ;;;; ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg ;;;; and Copyright (c) 1999-2001 by Pierre R. Mai @@ -28,6 +28,73 @@ (in-package :clsql-postgresql) +;;; Field conversion functions + +(defun canonicalize-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 + (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)) + (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)) + :returning :int) + +(uffi:def-function "atol" + ((str :cstring)) + :returning :long) + +(uffi:def-function "atof" + ((str :cstring)) + :returning :double) + +(defun convert-raw-field (char-ptr types index) + (let ((type (if (listp types) + (nth index types) + types))) + (case type + (:int + (atoi char-ptr)) + (:long + (atol char-ptr)) + (:double + (atof char-ptr)) + (otherwise + (uffi:convert-from-foreign-string char-ptr))))) + (defun tidy-error-message (message) (unless (stringp message) @@ -99,7 +166,7 @@ (setf (database-conn-ptr database) nil) t) -(defmethod database-query (query-expression (database postgresql-database)) +(defmethod database-query (query-expression (database postgresql-database) types) (let ((conn-ptr (database-conn-ptr database))) (declare (type pgsql-conn-def conn-ptr)) (uffi:with-cstring (query-native query-expression) @@ -115,14 +182,19 @@ (#.pgsql-exec-status-type#empty-query nil) (#.pgsql-exec-status-type#tuples-ok - (loop for tuple-index from 0 below (PQntuples result) - collect - (loop for i from 0 below (PQnfields result) - collect - (if (zerop (PQgetisnull result tuple-index i)) - (uffi:convert-from-cstring - (PQgetvalue result tuple-index i)) - nil)))) + (let ((num-fields (PQnfields result))) + (setq types + (canonicalize-types types num-fields + result)) + (loop for tuple-index from 0 below (PQntuples result) + collect + (loop for i from 0 below num-fields + collect + (if (zerop (PQgetisnull result tuple-index i)) + (convert-raw-field + (PQgetvalue result tuple-index i) + types i) + nil))))) (t (error 'clsql-sql-error :database database @@ -164,13 +236,13 @@ (defstruct postgresql-result-set (res-ptr (uffi:make-null-pointer 'pgsql-result) :type pgsql-result-def) - (num-tuples 0) - (num-fields 0) - (tuple-index 0)) + (types nil) + (num-tuples 0 :type integer) + (num-fields 0 :type integer) + (tuple-index 0 :type integer)) -(defmethod database-query-result-set (query-expression - (database postgresql-database) - &optional full-set) +(defmethod database-query-result-set (query-expression (database postgresql-database) + &key full-set types) (let ((conn-ptr (database-conn-ptr database))) (declare (type pgsql-conn-def conn-ptr)) (uffi:with-cstring (query-native query-expression) @@ -184,20 +256,22 @@ (case (PQresultStatus result) ((#.pgsql-exec-status-type#empty-query #.pgsql-exec-status-type#tuples-ok) - (if full-set - (values (make-postgresql-result-set + (let ((result-set (make-postgresql-result-set :res-ptr result :num-fields (PQnfields result) - :num-tuples (PQntuples result)) - (PQnfields result) - (PQntuples result)) - (values (make-postgresql-result-set - :res-ptr result - :num-fields (PQnfields result) - :num-tuples (PQntuples result)) - (PQnfields result)))) - (t - (unwind-protect + :num-tuples (PQntuples result) + :types (canonicalize-types + types + (PQnfields result) + result)))) + (if full-set + (values result-set + (PQnfields result) + (PQntuples result)) + (values result-set + (PQnfields result))))) + (t + (unwind-protect (error 'clsql-sql-error :database database :expression query-expression @@ -214,7 +288,8 @@ (defmethod database-store-next-row (result-set (database postgresql-database) list) - (let ((result (postgresql-result-set-res-ptr result-set))) + (let ((result (postgresql-result-set-res-ptr result-set)) + (types (postgresql-result-set-types result-set))) (declare (type pgsql-result-def result)) (if (>= (postgresql-result-set-tuple-index result-set) (postgresql-result-set-num-tuples result-set)) @@ -225,8 +300,9 @@ do (setf (car rest) (if (zerop (PQgetisnull result tuple-index i)) - (uffi:convert-from-cstring - (PQgetvalue result tuple-index i)) + (convert-raw-field + (PQgetvalue result tuple-index i) + types i) nil)) finally (incf (postgresql-result-set-tuple-index result-set))