X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=interfaces%2Fpostgresql%2Fpostgresql-sql.cl;h=21cb247eed7b7defced65805d84835a814e59711;hb=2073ba6b66571d3da57523dbdcb30ed6ffb4b161;hp=f0a1ee1bd511a614a6b5002c6152278570738e2d;hpb=421ef23a5a01534ac4c4d7594d182023ed31b376;p=clsql.git diff --git a/interfaces/postgresql/postgresql-sql.cl b/interfaces/postgresql/postgresql-sql.cl index f0a1ee1..21cb247 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.7 2002/03/25 06:07:06 kevin Exp $ +;;;; $Id: postgresql-sql.cl,v 1.13 2002/04/19 20:25:20 marc.battyani Exp $ ;;;; ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg ;;;; and Copyright (c) 1999-2001 by Pierre R. Mai @@ -22,7 +22,7 @@ (in-package :cl-user) (defpackage :clsql-postgresql - (:use :common-lisp :clsql-sys :postgresql) + (:use :common-lisp :clsql-sys :postgresql :clsql-uffi) (:export #:postgresql-database) (:documentation "This is the CLSQL interface to PostgreSQL.")) @@ -30,52 +30,38 @@ ;;; 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 - 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)) - (if (eq types :auto) - :auto - 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 make-type-list-for-auto (num-fields res-ptr) + (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) + :int32) + (#.pgsql-ftype#int8 + :int64) + ((#.pgsql-ftype#float4 + #.pgsql-ftype#float8) + :double) + (otherwise + t)) + new-types))) + (nreverse new-types))) +(defun canonicalize-types (types num-fields res-ptr) + (if (null types) + nil + (let ((auto-list (make-type-list-for-auto num-fields res-ptr))) + (cond + ((listp types) + (canonicalize-type-list types auto-list)) + ((eq types :auto) + auto-list) + (t + nil))))) (defun tidy-error-message (message) (unless (stringp message) @@ -147,7 +133,7 @@ (setf (database-conn-ptr database) nil) t) -(defmethod database-query (query-expression (database postgresql-database) field-types) +(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) @@ -164,8 +150,9 @@ nil) (#.pgsql-exec-status-type#tuples-ok (let ((num-fields (PQnfields result))) - (setq field-types - (canonicalize-field-types field-types num-fields)) + (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 @@ -173,7 +160,7 @@ (if (zerop (PQgetisnull result tuple-index i)) (convert-raw-field (PQgetvalue result tuple-index i) - field-types i) + types i) nil))))) (t (error 'clsql-sql-error @@ -216,13 +203,13 @@ (defstruct postgresql-result-set (res-ptr (uffi:make-null-pointer 'pgsql-result) :type pgsql-result-def) - (field-types nil) + (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) - &key full-set field-types) + &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) @@ -240,9 +227,10 @@ :res-ptr result :num-fields (PQnfields result) :num-tuples (PQntuples result) - :field-types (canonicalize-field-types - field-types - (PQnfields result))))) + :types (canonicalize-types + types + (PQnfields result) + result)))) (if full-set (values result-set (PQnfields result) @@ -268,7 +256,7 @@ (defmethod database-store-next-row (result-set (database postgresql-database) list) (let ((result (postgresql-result-set-res-ptr result-set)) - (field-types (postgresql-result-set-field-types 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)) @@ -281,8 +269,61 @@ (if (zerop (PQgetisnull result tuple-index i)) (convert-raw-field (PQgetvalue result tuple-index i) - field-types i) + types i) nil)) finally (incf (postgresql-result-set-tuple-index result-set)) (return list))))) + +;;; Large objects support (Marc B) + +(defmethod database-create-large-object ((database postgresql-database)) + (lo-create (database-conn-ptr database) + (logior postgresql::+INV_WRITE+ postgresql::+INV_READ+))) + +;; (MB)the begin/commit/rollback stuff will be removed when with-transaction wil be implemented +(defmethod database-write-large-object ( object-id (data string) (database postgresql-database)) + (let ((ptr (database-conn-ptr database)) + (length (length data)) + (result nil) + (fd nil)) + (unwind-protect + (progn + (database-execute-command "begin" database) + (setf fd (lo-open ptr object-id postgresql::+INV_WRITE+)) + (when (>= fd 0) + (when (= (lo-write ptr fd data length) length) + (setf result t)))) + (progn + (when (and fd (>= fd 0)) + (lo-close ptr fd)) + (database-execute-command (if result "commit" "rollback") database))) + result)) + +;; (MB) the begin/commit/rollback stuff will be removed when with-transaction wil be implemented +(defmethod database-read-large-object (object-id (database postgresql-database)) + (let ((ptr (database-conn-ptr database)) + (buffer nil) + (result nil) + (length 0) + (fd nil)) + (unwind-protect + (progn + (database-execute-command "begin" database) + (setf fd (lo-open ptr object-id postgresql::+INV_READ+)) + (when (>= fd 0) + (setf length (lo-lseek ptr fd 0 2)) + (lo-lseek ptr fd 0 0) + (when (> length 0) + (setf buffer (uffi:allocate-foreign-string length :type '(:unsigned :byte))) + (when (= (lo-read ptr fd buffer length) length) + (setf result (uffi:convert-from-foreign-string + buffer :length length :null-terminated-p nil)))))) + (progn + (when buffer (uffi:free-foreign-object buffer)) + (when (and fd (>= fd 0)) (lo-close ptr fd)) + (database-execute-command (if result "commit" "rollback") database))) + result)) + +(defmethod database-delete-large-object (object-id (database postgresql-database)) + (lo-unlink (database-conn-ptr database) object-id))