Automated commit for debian release 6.7.2-1
[clsql.git] / db-postgresql / postgresql-api.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
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
10 ;;;;
11 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
12 ;;;; and Copyright (c) 1999-2001 by Pierre R. Mai
13 ;;;;
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 ;;;; *************************************************************************
18
19 (in-package #:pgsql)
20
21
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
28 ;;;; example.
29
30 ;;;; Type definitions
31
32 ;;; Basic Types
33
34 (uffi:def-foreign-type pgsql-oid :unsigned-int)
35
36 (uffi:def-enum pgsql-conn-status-type
37     (:connection-ok
38      :connection-bad))
39
40 (uffi:def-enum pgsql-exec-status-type
41     (:empty-query
42      :command-ok
43      :tuples-ok
44      :copy-out
45      :copy-in
46      :bad-response
47      :nonfatal-error
48      :fatal-error))
49
50 (uffi:def-foreign-type pgsql-conn :pointer-void)
51 (uffi:def-foreign-type pgsql-result :pointer-void)
52
53 (uffi:def-type pgsql-conn-ptr :pointer-void)
54
55 (uffi:def-enum pgsql-ftype
56     ((:bytea 17)
57      (:int2 21)
58      (:int4 23)
59      (:int8 20)
60      (:float4 700)
61      (:float8 701)))
62
63 ;;(declaim (inline PQsetdbLogin)) ;; causes compile error in LW 4.2.0
64 (uffi:def-function ("PQsetdbLogin" PQsetdbLogin)
65   ((pghost :cstring)
66    (pgport :cstring)
67    (pgoptions :cstring)
68    (pgtty :cstring)
69    (dbName :cstring)
70    (login :cstring)
71    (pwd :cstring))
72   :module "postgresql"
73   :returning pgsql-conn)
74
75 (declaim (inline PQfinish))
76 (uffi:def-function ("PQfinish" PQfinish)
77   ((conn pgsql-conn))
78   :module "postgresql"
79   :returning :void)
80
81 (declaim (inline PQstatus))
82 (uffi:def-function ("PQstatus" PQstatus)
83   ((conn pgsql-conn))
84   :module "postgresql"
85   :returning pgsql-conn-status-type)
86
87 (declaim (inline PQerrorMessage))
88 (uffi:def-function ("PQerrorMessage" PQerrorMessage)
89   ((conn pgsql-conn))
90   :module "postgresql"
91   :returning :cstring)
92
93 (declaim (inline PQexec))
94 (uffi:def-function ("PQexec" PQexec)
95   ((conn pgsql-conn)
96    (query :cstring))
97   :module "postgresql"
98   :returning pgsql-result)
99
100 (declaim (inline PQresultStatus))
101 (uffi:def-function ("PQresultStatus" PQresultStatus)
102   ((res pgsql-result))
103   :module "postgresql"
104   :returning pgsql-exec-status-type)
105
106 ; From postgres_ext.h
107
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))
132
133 ; PQresultErrorField can return diagnostic information about an error
134 (declaim (inline PQresultErrorField))
135 (uffi:def-function ("PQresultErrorField" PQresultErrorField)
136     ((res pgsql-result)
137      (field-code :int))
138   :module "postgresql"
139   :returning :cstring)
140
141 (declaim (inline PQresultErrorMessage))
142 (uffi:def-function ("PQresultErrorMessage" PQresultErrorMessage)
143   ((res pgsql-result))
144   :module "postgresql"
145   :returning :cstring)
146
147 (declaim (inline PQntuples))
148 (uffi:def-function ("PQntuples" PQntuples)
149   ((res pgsql-result))
150   :module "postgresql"
151   :returning :int)
152
153 (declaim (inline PQnfields))
154 (uffi:def-function ("PQnfields" PQnfields)
155   ((res pgsql-result))
156   :module "postgresql"
157   :returning :int)
158
159 (declaim (inline PQfname))
160 (uffi:def-function ("PQfname" PQfname)
161   ((res pgsql-result)
162    (field-num :int))
163   :module "postgresql"
164   :returning :cstring)
165
166 (declaim (inline PQfnumber))
167 (uffi:def-function ("PQfnumber" PQfnumber)
168   ((res pgsql-result)
169   (field-name :cstring))
170   :module "postgresql"
171   :returning :int)
172
173 (declaim (inline PQftype))
174 (uffi:def-function ("PQftype" PQftype)
175   ((res pgsql-result)
176    (field-num :int))
177   :module "postgresql"
178   :returning pgsql-oid)
179
180 (declaim (inline PQfsize))
181 (uffi:def-function ("PQfsize" PQfsize)
182   ((res pgsql-result)
183    (field-num :int))
184   :module "postgresql"
185   :returning :short)
186
187 (declaim (inline PQcmdStatus))
188 (uffi:def-function ("PQcmdStatus" PQcmdStatus)
189   ((res pgsql-result))
190   :module "postgresql"
191   :returning :cstring)
192
193 (declaim (inline PQoidStatus))
194 (uffi:def-function ("PQoidStatus" PQoidStatus)
195   ((res pgsql-result))
196   :module "postgresql"
197   :returning :cstring)
198
199 (declaim (inline PQcmdTuples))
200 (uffi:def-function ("PQcmdTuples" PQcmdTuples)
201   ((res pgsql-result))
202   :module "postgresql"
203   :returning :cstring)
204
205 (declaim (inline PQgetvalue))
206 (uffi:def-function ("PQgetvalue" PQgetvalue)
207   ((res pgsql-result)
208    (tup-num :int)
209    (field-num :int))
210   :module "postgresql"
211   :returning (* :unsigned-char))
212
213 (declaim (inline PQgetlength))
214 (uffi:def-function ("PQgetlength" PQgetlength)
215   ((res pgsql-result)
216    (tup-num :int)
217    (field-num :int))
218   :module "postgresql"
219   :returning :int)
220
221 (declaim (inline PQgetisnull))
222 (uffi:def-function ("PQgetisnull" PQgetisnull)
223   ((res pgsql-result)
224    (tup-num :int)
225    (field-num :int))
226   :module "postgresql"
227   :returning :int)
228
229 (declaim (inline PQclear))
230 (uffi:def-function ("PQclear" PQclear)
231   ((res pgsql-result))
232   :module "postgresql"
233   :returning :void)
234
235 (declaim (inline PQisBusy))
236 (uffi:def-function ("PQisBusy" PQisBusy)
237   ((conn pgsql-conn))
238   :module "postgresql"
239   :returning :int)
240
241
242 ;;; Large objects support (MB)
243
244 (defconstant +INV_ARCHIVE+ 65536)         ; fe-lobj.c
245 (defconstant +INV_WRITE+   131072)
246 (defconstant +INV_READ+    262144)
247
248 (declaim (inline lo-creat))
249 (uffi:def-function ("lo_creat" lo-create)
250   ((conn pgsql-conn)
251    (mode :int))
252   :module "postgresql"
253   :returning pgsql-oid)
254
255 (declaim (inline lo-open))
256 (uffi:def-function ("lo_open" lo-open)
257   ((conn pgsql-conn)
258    (oid pgsql-oid)
259    (mode :int))
260   :module "postgresql"
261   :returning :int)
262
263 (declaim (inline lo-write))
264 (uffi:def-function ("lo_write" lo-write)
265   ((conn pgsql-conn)
266    (fd :int)
267    (data :cstring)
268    (size :int))
269   :module "postgresql"
270   :returning :int)
271
272 (declaim (inline lo-read))
273 (uffi:def-function ("lo_read" lo-read)
274   ((conn pgsql-conn)
275    (fd :int)
276    (data (* :unsigned-char))
277    (size :int))
278   :module "postgresql"
279   :returning :int)
280
281 (declaim (inline lo-lseek))
282 (uffi:def-function ("lo_lseek" lo-lseek)
283   ((conn pgsql-conn)
284    (fd :int)
285    (offset :int)
286    (whence :int))
287   :module "postgresql"
288   :returning :int)
289
290 (declaim (inline lo-close))
291 (uffi:def-function ("lo_close" lo-close)
292   ((conn pgsql-conn)
293    (fd :int))
294   :module "postgresql"
295   :returning :int)
296
297 (declaim (inline lo-unlink))
298 (uffi:def-function ("lo_unlink" lo-unlink)
299   ((conn pgsql-conn)
300    (oid pgsql-oid))
301   :module "postgresql"
302   :returning :int)