r10077: * multiple: Apply patch from Joerg Hoehle with multiple
[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 ;;;; $Id$
12 ;;;;
13 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
14 ;;;; and Copyright (c) 1999-2001 by Pierre R. Mai
15 ;;;;
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 ;;;; *************************************************************************
20
21 (in-package #:postgresql)
22
23
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
30 ;;;; example.
31
32 ;;;; Type definitions
33
34 ;;; Basic Types
35
36 (uffi:def-foreign-type pgsql-oid :unsigned-int)
37
38 (uffi:def-enum pgsql-conn-status-type 
39     (:connection-ok
40      :connection-bad))
41
42 (uffi:def-enum pgsql-exec-status-type
43     (:empty-query
44      :command-ok
45      :tuples-ok
46      :copy-out
47      :copy-in
48      :bad-response
49      :nonfatal-error
50      :fatal-error))
51
52 (uffi:def-foreign-type pgsql-conn :pointer-void)
53 (uffi:def-foreign-type pgsql-result :pointer-void)
54
55 (uffi:def-type pgsql-conn-ptr :pointer-void)
56
57 (uffi:def-enum pgsql-ftype
58     ((:bytea 17)
59      (:int2 21)
60      (:int4 23)
61      (:int8 20)
62      (:float4 700)
63      (:float8 701)))
64   
65 ;;(declaim (inline PQsetdbLogin)) ;; causes compile error in LW 4.2.0
66 (uffi:def-function ("PQsetdbLogin" PQsetdbLogin)
67   ((pghost :cstring)
68    (pgport :cstring)
69    (pgoptions :cstring)
70    (pgtty :cstring)
71    (dbName :cstring)
72    (login :cstring)
73    (pwd :cstring))
74   :module "postgresql"
75   :returning pgsql-conn)
76
77 (declaim (inline PQfinish))
78 (uffi:def-function ("PQfinish" PQfinish)
79   ((conn pgsql-conn))
80   :module "postgresql"
81   :returning :void)
82
83 (declaim (inline PQstatus))
84 (uffi:def-function ("PQstatus" PQstatus)
85   ((conn pgsql-conn))
86   :module "postgresql"
87   :returning pgsql-conn-status-type)
88
89 (declaim (inline PQerrorMessage))
90 (uffi:def-function ("PQerrorMessage" PQerrorMessage)
91   ((conn pgsql-conn))
92   :module "postgresql"
93   :returning :cstring)
94
95 (declaim (inline PQexec))
96 (uffi:def-function ("PQexec" PQexec)
97   ((conn pgsql-conn)
98    (query :cstring))
99   :module "postgresql"
100   :returning pgsql-result)
101
102 (declaim (inline PQresultStatus))
103 (uffi:def-function ("PQresultStatus" PQresultStatus)
104   ((res pgsql-result))
105   :module "postgresql"
106   :returning pgsql-exec-status-type)
107
108 (declaim (inline PQresultErrorMessage))
109 (uffi:def-function ("PQresultErrorMessage" PQresultErrorMessage)
110   ((res pgsql-result))
111   :module "postgresql"
112   :returning :cstring)
113
114 (declaim (inline PQntuples))
115 (uffi:def-function ("PQntuples" PQntuples) 
116   ((res pgsql-result))
117   :module "postgresql"
118   :returning :int)
119
120 (declaim (inline PQnfields))
121 (uffi:def-function ("PQnfields" PQnfields)
122   ((res pgsql-result))
123   :module "postgresql"
124   :returning :int)
125
126 (declaim (inline PQfname))
127 (uffi:def-function ("PQfname" PQfname)
128   ((res pgsql-result)
129    (field-num :int))
130   :module "postgresql"
131   :returning :cstring)
132
133 (declaim (inline PQfnumber))
134 (uffi:def-function ("PQfnumber" PQfnumber)
135   ((res pgsql-result)
136   (field-name :cstring))
137   :module "postgresql"
138   :returning :int)
139
140 (declaim (inline PQftype))
141 (uffi:def-function ("PQftype" PQftype)
142   ((res pgsql-result)
143    (field-num :int))
144   :module "postgresql"
145   :returning pgsql-oid)
146
147 (declaim (inline PQfsize))
148 (uffi:def-function ("PQfsize" PQfsize)
149   ((res pgsql-result)
150    (field-num :int))
151   :module "postgresql"
152   :returning :short)
153
154 (declaim (inline PQcmdStatus))
155 (uffi:def-function ("PQcmdStatus" PQcmdStatus)
156   ((res pgsql-result))
157   :module "postgresql"
158   :returning :cstring)
159
160 (declaim (inline PQoidStatus))
161 (uffi:def-function ("PQoidStatus" PQoidStatus)
162   ((res pgsql-result))
163   :module "postgresql"
164   :returning :cstring)
165
166 (declaim (inline PQcmdTuples))
167 (uffi:def-function ("PQcmdTuples" PQcmdTuples)
168   ((res pgsql-result))
169   :module "postgresql"
170   :returning :cstring)
171
172 (declaim (inline PQgetvalue))
173 (uffi:def-function ("PQgetvalue" PQgetvalue)
174   ((res pgsql-result)
175    (tup-num :int)
176    (field-num :int))
177   :module "postgresql"
178   :returning (* :unsigned-char))
179
180 (declaim (inline PQgetlength))
181 (uffi:def-function ("PQgetlength" PQgetlength)
182   ((res pgsql-result)
183    (tup-num :int)
184    (field-num :int))
185   :module "postgresql"
186   :returning :int)
187
188 (declaim (inline PQgetisnull))
189 (uffi:def-function ("PQgetisnull" PQgetisnull)
190   ((res pgsql-result)
191    (tup-num :int)
192    (field-num :int))
193   :module "postgresql"
194   :returning :int)
195
196 (declaim (inline PQclear))
197 (uffi:def-function ("PQclear" PQclear)
198   ((res pgsql-result))
199   :module "postgresql"
200   :returning :void)
201
202 (declaim (inline PQisBusy))
203 (uffi:def-function ("PQisBusy" PQisBusy)
204   ((conn pgsql-conn))
205   :module "postgresql"
206   :returning :int)
207
208
209 ;;; Large objects support (MB)
210
211 (defconstant +INV_ARCHIVE+ 65536)         ; fe-lobj.c
212 (defconstant +INV_WRITE+   131072)
213 (defconstant +INV_READ+    262144)
214
215 (declaim (inline lo-creat))
216 (uffi:def-function ("lo_creat" lo-create)
217   ((conn pgsql-conn)
218    (mode :int))
219   :module "postgresql"
220   :returning pgsql-oid)
221
222 (declaim (inline lo-open))
223 (uffi:def-function ("lo_open" lo-open)
224   ((conn pgsql-conn)
225    (oid pgsql-oid)
226    (mode :int))
227   :module "postgresql"
228   :returning :int)
229
230 (declaim (inline lo-write))
231 (uffi:def-function ("lo_write" lo-write)
232   ((conn pgsql-conn)
233    (fd :int)
234    (data :cstring)
235    (size :int))
236   :module "postgresql"
237   :returning :int)
238
239 (declaim (inline lo-read))
240 (uffi:def-function ("lo_read" lo-read)
241   ((conn pgsql-conn)
242    (fd :int)
243    (data (* :unsigned-char))
244    (size :int))
245   :module "postgresql"
246   :returning :int)
247
248 (declaim (inline lo-lseek))
249 (uffi:def-function ("lo_lseek" lo-lseek)
250   ((conn pgsql-conn)
251    (fd :int)
252    (offset :int)
253    (whence :int))
254   :module "postgresql"
255   :returning :int)
256
257 (declaim (inline lo-close))
258 (uffi:def-function ("lo_close" lo-close)
259   ((conn pgsql-conn)
260    (fd :int))
261   :module "postgresql"
262   :returning :int)
263
264 (declaim (inline lo-unlink))
265 (uffi:def-function ("lo_unlink" lo-unlink)
266   ((conn pgsql-conn)
267    (oid pgsql-oid))
268   :module "postgresql"
269   :returning :int)