r7061: initial property settings
[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 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
22 (in-package :postgresql)
23
24
25 ;;;; This file implements as little of the FFI bindings to the
26 ;;;; PostgreSQL client libraries as we could get away with.
27 ;;;; Especially all the PostgreSQL-specific goodies aren't there, and
28 ;;;; we just use void pointers where we can get away with it, which
29 ;;;; thanks to the design of the PostgreSQL client libraries is pretty
30 ;;;; much everywhere, in contrast to the MySQL client libraries for
31 ;;;; example.
32
33 ;;;; Type definitions
34
35 ;;; Basic Types
36
37 (uffi:def-foreign-type pgsql-oid :unsigned-int)
38
39 (uffi:def-enum pgsql-conn-status-type 
40     (:connection-ok
41      :connection-bad))
42
43 (uffi:def-enum pgsql-exec-status-type
44     (:empty-query
45      :command-ok
46      :tuples-ok
47      :copy-out
48      :copy-in
49      :bad-response
50      :nonfatal-error
51      :fatal-error))
52
53 (uffi:def-foreign-type pgsql-conn :pointer-void)
54 (uffi:def-foreign-type pgsql-result :pointer-void)
55
56 (uffi:def-enum pgsql-ftype
57     ((:bytea 17)
58      (:int2 21)
59      (:int4 23)
60      (:int8 20)
61      (:float4 700)
62      (:float8 701)))
63   
64 ;;(declaim (inline PQsetdbLogin)) ;; causes compile error in LW 4.2.0
65 (uffi:def-function ("PQsetdbLogin" PQsetdbLogin)
66   ((pghost :cstring)
67    (pgport :cstring)
68    (pgoptions :cstring)
69    (pgtty :cstring)
70    (dbName :cstring)
71    (login :cstring)
72    (pwd :cstring))
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 (declaim (inline PQresultErrorMessage))
107 (uffi:def-function ("PQresultErrorMessage" PQresultErrorMessage)
108   ((res pgsql-result))
109   :module "postgresql"
110   :returning :cstring)
111
112 (declaim (inline PQntuples))
113 (uffi:def-function ("PQntuples" PQntuples) 
114   ((res pgsql-result))
115   :module "postgresql"
116   :returning :int)
117
118 (declaim (inline PQnfields))
119 (uffi:def-function ("PQnfields" PQnfields)
120   ((res pgsql-result))
121   :module "postgresql"
122   :returning :int)
123
124 (declaim (inline PQfname))
125 (uffi:def-function ("PQfname" PQfname)
126   ((res pgsql-result)
127    (field-num :int))
128   :module "postgresql"
129   :returning :cstring)
130
131 (declaim (inline PQfnumber))
132 (uffi:def-function ("PQfnumber" PQfnumber)
133   ((res pgsql-result)
134   (field-name :cstring))
135   :module "postgresql"
136   :returning :int)
137
138 (declaim (inline PQftype))
139 (uffi:def-function ("PQftype" PQftype)
140   ((res pgsql-result)
141    (field-num :int))
142   :module "postgresql"
143   :returning pgsql-oid)
144
145 (declaim (inline PQfsize))
146 (uffi:def-function ("PQfsize" PQfsize)
147   ((res pgsql-result)
148    (field-num :int))
149   :module "postgresql"
150   :returning :short)
151
152 (declaim (inline PQcmdStatus))
153 (uffi:def-function ("PQcmdStatus" PQcmdStatus)
154   ((res pgsql-result))
155   :module "postgresql"
156   :returning :cstring)
157
158 (declaim (inline PQoidStatus))
159 (uffi:def-function ("PQoidStatus" PQoidStatus)
160   ((res pgsql-result))
161   :module "postgresql"
162   :returning :cstring)
163
164 (declaim (inline PQcmdTuples))
165 (uffi:def-function ("PQcmdTuples" PQcmdTuples)
166   ((res pgsql-result))
167   :module "postgresql"
168   :returning :cstring)
169
170 (declaim (inline PQgetvalue))
171 (uffi:def-function ("PQgetvalue" PQgetvalue)
172   ((res pgsql-result)
173    (tup-num :int)
174    (field-num :int))
175   :module "postgresql"
176   :returning (* :unsigned-char))
177
178 (declaim (inline PQgetlength))
179 (uffi:def-function ("PQgetlength" PQgetlength)
180   ((res pgsql-result)
181    (tup-num :int)
182    (field-num :int))
183   :module "postgresql"
184   :returning :int)
185
186 (declaim (inline PQgetisnull))
187 (uffi:def-function ("PQgetisnull" PQgetisnull)
188   ((res pgsql-result)
189    (tup-num :int)
190    (field-num :int))
191   :module "postgresql"
192   :returning :int)
193
194 (declaim (inline PQclear))
195 (uffi:def-function ("PQclear" PQclear)
196   ((res pgsql-result))
197   :module "postgresql"
198   :returning :void)
199
200 (declaim (inline PQisBusy))
201 (uffi:def-function ("PQisBusy" PQisBusy)
202   ((conn pgsql-conn))
203   :module "postgresql"
204   :returning :int)
205
206
207 ;;; Large objects support (MB)
208
209 (defconstant +INV_ARCHIVE+ 65536)         ; fe-lobj.c
210 (defconstant +INV_WRITE+   131072)
211 (defconstant +INV_READ+    262144)
212
213 (declaim (inline lo-creat))
214 (uffi:def-function ("lo_creat" lo-create)
215   ((conn pgsql-conn)
216    (mode :int))
217   :module "postgresql"
218   :returning pgsql-oid)
219
220 (declaim (inline lo-open))
221 (uffi:def-function ("lo_open" lo-open)
222   ((conn pgsql-conn)
223    (oid pgsql-oid)
224    (mode :int))
225   :module "postgresql"
226   :returning :int)
227
228 (declaim (inline lo-write))
229 (uffi:def-function ("lo_write" lo-write)
230   ((conn pgsql-conn)
231    (fd :int)
232    (data :cstring)
233    (size :int))
234   :module "postgresql"
235   :returning :int)
236
237 (declaim (inline lo-read))
238 (uffi:def-function ("lo_read" lo-read)
239   ((conn pgsql-conn)
240    (fd :int)
241    (data (* :unsigned-char))
242    (size :int))
243   :module "postgresql"
244   :returning :int)
245
246 (declaim (inline lo-lseek))
247 (uffi:def-function ("lo_lseek" lo-lseek)
248   ((conn pgsql-conn)
249    (fd :int)
250    (offset :int)
251    (whence :int))
252   :module "postgresql"
253   :returning :int)
254
255 (declaim (inline lo-close))
256 (uffi:def-function ("lo_close" lo-close)
257   ((conn pgsql-conn)
258    (fd :int))
259   :module "postgresql"
260   :returning :int)
261
262 (declaim (inline lo-unlink))
263 (uffi:def-function ("lo_unlink" lo-unlink)
264   ((conn pgsql-conn)
265    (oid pgsql-oid))
266   :module "postgresql"
267   :returning :int)