1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: postgresql.cl
6 ;;;; Purpose: Low-level PostgreSQL interface using UFFI
7 ;;;; Programmers: Kevin M. Rosenberg based on
8 ;;;; Original code by Pierre R. Mai
9 ;;;; Date Started: Feb 2002
11 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
12 ;;;; and Copyright (c) 1999-2001 by Pierre R. Mai
14 ;;;; CLSQL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
22 ;;;; This file implements as little of the FFI bindings to the
23 ;;;; PostgreSQL client libraries as we could get away with.
24 ;;;; Especially all the PostgreSQL-specific goodies aren't there, and
25 ;;;; we just use void pointers where we can get away with it, which
26 ;;;; thanks to the design of the PostgreSQL client libraries is pretty
27 ;;;; much everywhere, in contrast to the MySQL client libraries for
34 (uffi:def-foreign-type pgsql-oid :unsigned-int)
36 (uffi:def-enum pgsql-conn-status-type
40 (uffi:def-enum pgsql-exec-status-type
50 (uffi:def-foreign-type pgsql-conn :pointer-void)
51 (uffi:def-foreign-type pgsql-result :pointer-void)
53 (uffi:def-type pgsql-conn-ptr :pointer-void)
55 (uffi:def-enum pgsql-ftype
63 ;;(declaim (inline PQsetdbLogin)) ;; causes compile error in LW 4.2.0
64 (uffi:def-function ("PQsetdbLogin" PQsetdbLogin)
73 :returning pgsql-conn)
75 (declaim (inline PQfinish))
76 (uffi:def-function ("PQfinish" PQfinish)
81 (declaim (inline PQstatus))
82 (uffi:def-function ("PQstatus" PQstatus)
85 :returning pgsql-conn-status-type)
87 (declaim (inline PQerrorMessage))
88 (uffi:def-function ("PQerrorMessage" PQerrorMessage)
93 (declaim (inline PQexec))
94 (uffi:def-function ("PQexec" PQexec)
98 :returning pgsql-result)
100 (declaim (inline PQresultStatus))
101 (uffi:def-function ("PQresultStatus" PQresultStatus)
104 :returning pgsql-exec-status-type)
106 ; From postgres_ext.h
108 ; #define PG_DIAG_SEVERITY 'S'
109 ; #define PG_DIAG_SQLSTATE 'C'
110 ; #define PG_DIAG_MESSAGE_PRIMARY 'M'
111 ; #define PG_DIAG_MESSAGE_DETAIL 'D'
112 ; #define PG_DIAG_MESSAGE_HINT 'H'
113 ; #define PG_DIAG_STATEMENT_POSITION 'P'
114 ; #define PG_DIAG_INTERNAL_POSITION 'p'
115 ; #define PG_DIAG_INTERNAL_QUERY 'q'
116 ; #define PG_DIAG_CONTEXT 'W'
117 ; #define PG_DIAG_SOURCE_FILE 'F'
118 ; #define PG_DIAG_SOURCE_LINE 'L'
119 ; #define PG_DIAG_SOURCE_FUNCTION 'R'
120 (defconstant +PG-DIAG-SEVERITY+ (char-code #\S))
121 (defconstant +PG-DIAG-SQLSTATE+ (char-code #\C))
122 (defconstant +PG-DIAG-MESSAGE-PRIMARY+ (char-code #\M))
123 (defconstant +PG-DIAG-MESSAGE-DETAIL+ (char-code #\D))
124 (defconstant +PG-DIAG-MESSAGE-HINT+ (char-code #\H))
125 (defconstant +PG-DIAG-STATEMENT-POSITION+ (char-code #\P))
126 (defconstant +PG-DIAG-INTERNAL-POSITION+ (char-code #\p))
127 (defconstant +PG-DIAG-INTERNAL-QUERY+ (char-code #\q))
128 (defconstant +PG-DIAG-CONTEXT+ (char-code #\W))
129 (defconstant +PG-DIAG-SOURCE-FILE+ (char-code #\F))
130 (defconstant +PG-DIAG-SOURCE-LINE+ (char-code #\L))
131 (defconstant +PG-DIAG-SOURCE-FUNCTION+ (char-code #\R))
133 ; PQresultErrorField can return diagnostic information about an error
134 (declaim (inline PQresultErrorField))
135 (uffi:def-function ("PQresultErrorField" PQresultErrorField)
141 (declaim (inline PQresultErrorMessage))
142 (uffi:def-function ("PQresultErrorMessage" PQresultErrorMessage)
147 (declaim (inline PQntuples))
148 (uffi:def-function ("PQntuples" PQntuples)
153 (declaim (inline PQnfields))
154 (uffi:def-function ("PQnfields" PQnfields)
159 (declaim (inline PQfname))
160 (uffi:def-function ("PQfname" PQfname)
166 (declaim (inline PQfnumber))
167 (uffi:def-function ("PQfnumber" PQfnumber)
169 (field-name :cstring))
173 (declaim (inline PQftype))
174 (uffi:def-function ("PQftype" PQftype)
178 :returning pgsql-oid)
180 (declaim (inline PQfsize))
181 (uffi:def-function ("PQfsize" PQfsize)
187 (declaim (inline PQcmdStatus))
188 (uffi:def-function ("PQcmdStatus" PQcmdStatus)
193 (declaim (inline PQoidStatus))
194 (uffi:def-function ("PQoidStatus" PQoidStatus)
199 (declaim (inline PQcmdTuples))
200 (uffi:def-function ("PQcmdTuples" PQcmdTuples)
205 (declaim (inline PQgetvalue))
206 (uffi:def-function ("PQgetvalue" PQgetvalue)
211 :returning (* :unsigned-char))
213 (declaim (inline PQgetlength))
214 (uffi:def-function ("PQgetlength" PQgetlength)
221 (declaim (inline PQgetisnull))
222 (uffi:def-function ("PQgetisnull" PQgetisnull)
229 (declaim (inline PQclear))
230 (uffi:def-function ("PQclear" PQclear)
235 (declaim (inline PQisBusy))
236 (uffi:def-function ("PQisBusy" PQisBusy)
242 ;;; Large objects support (MB)
244 (defconstant +INV_ARCHIVE+ 65536) ; fe-lobj.c
245 (defconstant +INV_WRITE+ 131072)
246 (defconstant +INV_READ+ 262144)
248 (declaim (inline lo-creat))
249 (uffi:def-function ("lo_creat" lo-create)
253 :returning pgsql-oid)
255 (declaim (inline lo-open))
256 (uffi:def-function ("lo_open" lo-open)
263 (declaim (inline lo-write))
264 (uffi:def-function ("lo_write" lo-write)
272 (declaim (inline lo-read))
273 (uffi:def-function ("lo_read" lo-read)
276 (data (* :unsigned-char))
281 (declaim (inline lo-lseek))
282 (uffi:def-function ("lo_lseek" lo-lseek)
290 (declaim (inline lo-close))
291 (uffi:def-function ("lo_close" lo-close)
297 (declaim (inline lo-unlink))
298 (uffi:def-function ("lo_unlink" lo-unlink)