r8936: merged classic-tests into tests
[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-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   :returning pgsql-conn)
73
74 (declaim (inline PQfinish))
75 (uffi:def-function ("PQfinish" PQfinish)
76   ((conn pgsql-conn))
77   :module "postgresql"
78   :returning :void)
79
80 (declaim (inline PQstatus))
81 (uffi:def-function ("PQstatus" PQstatus)
82   ((conn pgsql-conn))
83   :module "postgresql"
84   :returning pgsql-conn-status-type)
85
86 (declaim (inline PQerrorMessage))
87 (uffi:def-function ("PQerrorMessage" PQerrorMessage)
88   ((conn pgsql-conn))
89   :module "postgresql"
90   :returning :cstring)
91
92 (declaim (inline PQexec))
93 (uffi:def-function ("PQexec" PQexec)
94   ((conn pgsql-conn)
95    (query :cstring))
96   :module "postgresql"
97   :returning pgsql-result)
98
99 (declaim (inline PQresultStatus))
100 (uffi:def-function ("PQresultStatus" PQresultStatus)
101   ((res pgsql-result))
102   :module "postgresql"
103   :returning pgsql-exec-status-type)
104
105 (declaim (inline PQresultErrorMessage))
106 (uffi:def-function ("PQresultErrorMessage" PQresultErrorMessage)
107   ((res pgsql-result))
108   :module "postgresql"
109   :returning :cstring)
110
111 (declaim (inline PQntuples))
112 (uffi:def-function ("PQntuples" PQntuples) 
113   ((res pgsql-result))
114   :module "postgresql"
115   :returning :int)
116
117 (declaim (inline PQnfields))
118 (uffi:def-function ("PQnfields" PQnfields)
119   ((res pgsql-result))
120   :module "postgresql"
121   :returning :int)
122
123 (declaim (inline PQfname))
124 (uffi:def-function ("PQfname" PQfname)
125   ((res pgsql-result)
126    (field-num :int))
127   :module "postgresql"
128   :returning :cstring)
129
130 (declaim (inline PQfnumber))
131 (uffi:def-function ("PQfnumber" PQfnumber)
132   ((res pgsql-result)
133   (field-name :cstring))
134   :module "postgresql"
135   :returning :int)
136
137 (declaim (inline PQftype))
138 (uffi:def-function ("PQftype" PQftype)
139   ((res pgsql-result)
140    (field-num :int))
141   :module "postgresql"
142   :returning pgsql-oid)
143
144 (declaim (inline PQfsize))
145 (uffi:def-function ("PQfsize" PQfsize)
146   ((res pgsql-result)
147    (field-num :int))
148   :module "postgresql"
149   :returning :short)
150
151 (declaim (inline PQcmdStatus))
152 (uffi:def-function ("PQcmdStatus" PQcmdStatus)
153   ((res pgsql-result))
154   :module "postgresql"
155   :returning :cstring)
156
157 (declaim (inline PQoidStatus))
158 (uffi:def-function ("PQoidStatus" PQoidStatus)
159   ((res pgsql-result))
160   :module "postgresql"
161   :returning :cstring)
162
163 (declaim (inline PQcmdTuples))
164 (uffi:def-function ("PQcmdTuples" PQcmdTuples)
165   ((res pgsql-result))
166   :module "postgresql"
167   :returning :cstring)
168
169 (declaim (inline PQgetvalue))
170 (uffi:def-function ("PQgetvalue" PQgetvalue)
171   ((res pgsql-result)
172    (tup-num :int)
173    (field-num :int))
174   :module "postgresql"
175   :returning (* :unsigned-char))
176
177 (declaim (inline PQgetlength))
178 (uffi:def-function ("PQgetlength" PQgetlength)
179   ((res pgsql-result)
180    (tup-num :int)
181    (field-num :int))
182   :module "postgresql"
183   :returning :int)
184
185 (declaim (inline PQgetisnull))
186 (uffi:def-function ("PQgetisnull" PQgetisnull)
187   ((res pgsql-result)
188    (tup-num :int)
189    (field-num :int))
190   :module "postgresql"
191   :returning :int)
192
193 (declaim (inline PQclear))
194 (uffi:def-function ("PQclear" PQclear)
195   ((res pgsql-result))
196   :module "postgresql"
197   :returning :void)
198
199 (declaim (inline PQisBusy))
200 (uffi:def-function ("PQisBusy" PQisBusy)
201   ((conn pgsql-conn))
202   :module "postgresql"
203   :returning :int)
204
205
206 ;;; Large objects support (MB)
207
208 (defconstant +INV_ARCHIVE+ 65536)         ; fe-lobj.c
209 (defconstant +INV_WRITE+   131072)
210 (defconstant +INV_READ+    262144)
211
212 (declaim (inline lo-creat))
213 (uffi:def-function ("lo_creat" lo-create)
214   ((conn pgsql-conn)
215    (mode :int))
216   :module "postgresql"
217   :returning pgsql-oid)
218
219 (declaim (inline lo-open))
220 (uffi:def-function ("lo_open" lo-open)
221   ((conn pgsql-conn)
222    (oid pgsql-oid)
223    (mode :int))
224   :module "postgresql"
225   :returning :int)
226
227 (declaim (inline lo-write))
228 (uffi:def-function ("lo_write" lo-write)
229   ((conn pgsql-conn)
230    (fd :int)
231    (data :cstring)
232    (size :int))
233   :module "postgresql"
234   :returning :int)
235
236 (declaim (inline lo-read))
237 (uffi:def-function ("lo_read" lo-read)
238   ((conn pgsql-conn)
239    (fd :int)
240    (data (* :unsigned-char))
241    (size :int))
242   :module "postgresql"
243   :returning :int)
244
245 (declaim (inline lo-lseek))
246 (uffi:def-function ("lo_lseek" lo-lseek)
247   ((conn pgsql-conn)
248    (fd :int)
249    (offset :int)
250    (whence :int))
251   :module "postgresql"
252   :returning :int)
253
254 (declaim (inline lo-close))
255 (uffi:def-function ("lo_close" lo-close)
256   ((conn pgsql-conn)
257    (fd :int))
258   :module "postgresql"
259   :returning :int)
260
261 (declaim (inline lo-unlink))
262 (uffi:def-function ("lo_unlink" lo-unlink)
263   ((conn pgsql-conn)
264    (oid pgsql-oid))
265   :module "postgresql"
266   :returning :int)