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
13 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
14 ;;;; and Copyright (c) 1999-2001 by Pierre R. Mai
16 ;;;; CLSQL users are granted the rights to distribute and use this software
17 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
18 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
19 ;;;; *************************************************************************
24 ;;;; This file implements as little of the FFI bindings to the
25 ;;;; PostgreSQL client libraries as we could get away with.
26 ;;;; Especially all the PostgreSQL-specific goodies aren't there, and
27 ;;;; we just use void pointers where we can get away with it, which
28 ;;;; thanks to the design of the PostgreSQL client libraries is pretty
29 ;;;; much everywhere, in contrast to the MySQL client libraries for
36 (uffi:def-foreign-type pgsql-oid :unsigned-int)
38 (uffi:def-enum pgsql-conn-status-type
42 (uffi:def-enum pgsql-exec-status-type
52 (uffi:def-foreign-type pgsql-conn :pointer-void)
53 (uffi:def-foreign-type pgsql-result :pointer-void)
55 (uffi:def-type pgsql-conn-ptr :pointer-void)
57 (uffi:def-enum pgsql-ftype
65 ;;(declaim (inline PQsetdbLogin)) ;; causes compile error in LW 4.2.0
66 (uffi:def-function ("PQsetdbLogin" PQsetdbLogin)
75 :returning pgsql-conn)
77 (declaim (inline PQfinish))
78 (uffi:def-function ("PQfinish" PQfinish)
83 (declaim (inline PQstatus))
84 (uffi:def-function ("PQstatus" PQstatus)
87 :returning pgsql-conn-status-type)
89 (declaim (inline PQerrorMessage))
90 (uffi:def-function ("PQerrorMessage" PQerrorMessage)
95 (declaim (inline PQexec))
96 (uffi:def-function ("PQexec" PQexec)
100 :returning pgsql-result)
102 (declaim (inline PQresultStatus))
103 (uffi:def-function ("PQresultStatus" PQresultStatus)
106 :returning pgsql-exec-status-type)
108 ; From postgres_ext.h
110 ; #define PG_DIAG_SEVERITY 'S'
111 ; #define PG_DIAG_SQLSTATE 'C'
112 ; #define PG_DIAG_MESSAGE_PRIMARY 'M'
113 ; #define PG_DIAG_MESSAGE_DETAIL 'D'
114 ; #define PG_DIAG_MESSAGE_HINT 'H'
115 ; #define PG_DIAG_STATEMENT_POSITION 'P'
116 ; #define PG_DIAG_INTERNAL_POSITION 'p'
117 ; #define PG_DIAG_INTERNAL_QUERY 'q'
118 ; #define PG_DIAG_CONTEXT 'W'
119 ; #define PG_DIAG_SOURCE_FILE 'F'
120 ; #define PG_DIAG_SOURCE_LINE 'L'
121 ; #define PG_DIAG_SOURCE_FUNCTION 'R'
122 (defconstant +PG-DIAG-SEVERITY+ (char-code #\S))
123 (defconstant +PG-DIAG-SQLSTATE+ (char-code #\C))
124 (defconstant +PG-DIAG-MESSAGE-PRIMARY+ (char-code #\M))
125 (defconstant +PG-DIAG-MESSAGE-DETAIL+ (char-code #\D))
126 (defconstant +PG-DIAG-MESSAGE-HINT+ (char-code #\H))
127 (defconstant +PG-DIAG-STATEMENT-POSITION+ (char-code #\P))
128 (defconstant +PG-DIAG-INTERNAL-POSITION+ (char-code #\p))
129 (defconstant +PG-DIAG-INTERNAL-QUERY+ (char-code #\q))
130 (defconstant +PG-DIAG-CONTEXT+ (char-code #\W))
131 (defconstant +PG-DIAG-SOURCE-FILE+ (char-code #\F))
132 (defconstant +PG-DIAG-SOURCE-LINE+ (char-code #\L))
133 (defconstant +PG-DIAG-SOURCE-FUNCTION+ (char-code #\R))
135 ; PQresultErrorField can return diagnostic information about an error
136 (declaim (inline PQresultErrorField))
137 (uffi:def-function ("PQresultErrorField" PQresultErrorField)
143 (declaim (inline PQresultErrorMessage))
144 (uffi:def-function ("PQresultErrorMessage" PQresultErrorMessage)
149 (declaim (inline PQntuples))
150 (uffi:def-function ("PQntuples" PQntuples)
155 (declaim (inline PQnfields))
156 (uffi:def-function ("PQnfields" PQnfields)
161 (declaim (inline PQfname))
162 (uffi:def-function ("PQfname" PQfname)
168 (declaim (inline PQfnumber))
169 (uffi:def-function ("PQfnumber" PQfnumber)
171 (field-name :cstring))
175 (declaim (inline PQftype))
176 (uffi:def-function ("PQftype" PQftype)
180 :returning pgsql-oid)
182 (declaim (inline PQfsize))
183 (uffi:def-function ("PQfsize" PQfsize)
189 (declaim (inline PQcmdStatus))
190 (uffi:def-function ("PQcmdStatus" PQcmdStatus)
195 (declaim (inline PQoidStatus))
196 (uffi:def-function ("PQoidStatus" PQoidStatus)
201 (declaim (inline PQcmdTuples))
202 (uffi:def-function ("PQcmdTuples" PQcmdTuples)
207 (declaim (inline PQgetvalue))
208 (uffi:def-function ("PQgetvalue" PQgetvalue)
213 :returning (* :unsigned-char))
215 (declaim (inline PQgetlength))
216 (uffi:def-function ("PQgetlength" PQgetlength)
223 (declaim (inline PQgetisnull))
224 (uffi:def-function ("PQgetisnull" PQgetisnull)
231 (declaim (inline PQclear))
232 (uffi:def-function ("PQclear" PQclear)
237 (declaim (inline PQisBusy))
238 (uffi:def-function ("PQisBusy" PQisBusy)
244 ;;; Large objects support (MB)
246 (defconstant +INV_ARCHIVE+ 65536) ; fe-lobj.c
247 (defconstant +INV_WRITE+ 131072)
248 (defconstant +INV_READ+ 262144)
250 (declaim (inline lo-creat))
251 (uffi:def-function ("lo_creat" lo-create)
255 :returning pgsql-oid)
257 (declaim (inline lo-open))
258 (uffi:def-function ("lo_open" lo-open)
265 (declaim (inline lo-write))
266 (uffi:def-function ("lo_write" lo-write)
274 (declaim (inline lo-read))
275 (uffi:def-function ("lo_read" lo-read)
278 (data (* :unsigned-char))
283 (declaim (inline lo-lseek))
284 (uffi:def-function ("lo_lseek" lo-lseek)
292 (declaim (inline lo-close))
293 (uffi:def-function ("lo_close" lo-close)
299 (declaim (inline lo-unlink))
300 (uffi:def-function ("lo_unlink" lo-unlink)