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