]> gitweb.factorcode.org Git - factor.git/blob - basis/db/postgresql/ffi/ffi.factor
4358d7f3de6d5de9a14f618235b7ac24797e95be
[factor.git] / basis / db / postgresql / ffi / ffi.factor
1 ! Copyright (C) 2007, 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 ! tested on debian linux with postgresql 8.1
4 USING: alien alien.syntax combinators system ;
5 IN: db.postgresql.ffi
6
7 << "postgresql" {
8     { [ os winnt? ]  [ "libpq.dll" ] }
9     { [ os macosx? ] [ "libpq.dylib" ] }
10     { [ os unix?  ]  [ "libpq.so" ] }
11 } cond "cdecl" add-library >>
12
13 ! ConnSatusType
14 : CONNECTION_OK                     HEX: 0 ; inline
15 : CONNECTION_BAD                    HEX: 1 ; inline
16 : CONNECTION_STARTED                HEX: 2 ; inline
17 : CONNECTION_MADE                   HEX: 3 ; inline
18 : CONNECTION_AWAITING_RESPONSE      HEX: 4 ; inline
19 : CONNECTION_AUTH_OK                HEX: 5 ; inline
20 : CONNECTION_SETENV                 HEX: 6 ; inline
21 : CONNECTION_SSL_STARTUP            HEX: 7 ; inline
22 : CONNECTION_NEEDED                 HEX: 8 ; inline
23
24 ! PostgresPollingStatusType
25 : PGRES_POLLING_FAILED              HEX: 0 ; inline
26 : PGRES_POLLING_READING             HEX: 1 ; inline
27 : PGRES_POLLING_WRITING             HEX: 2 ; inline
28 : PGRES_POLLING_OK                  HEX: 3 ; inline
29 : PGRES_POLLING_ACTIVE              HEX: 4 ; inline
30
31 ! ExecStatusType;
32 : PGRES_EMPTY_QUERY                 HEX: 0 ; inline
33 : PGRES_COMMAND_OK                  HEX: 1 ; inline
34 : PGRES_TUPLES_OK                   HEX: 2 ; inline
35 : PGRES_COPY_OUT                    HEX: 3 ; inline
36 : PGRES_COPY_IN                     HEX: 4 ; inline
37 : PGRES_BAD_RESPONSE                HEX: 5 ; inline
38 : PGRES_NONFATAL_ERROR              HEX: 6 ; inline
39 : PGRES_FATAL_ERROR                 HEX: 7 ; inline
40
41 ! PGTransactionStatusType;
42 : PQTRANS_IDLE                      HEX: 0 ; inline
43 : PQTRANS_ACTIVE                    HEX: 1 ; inline
44 : PQTRANS_INTRANS                   HEX: 2 ; inline
45 : PQTRANS_INERROR                   HEX: 3 ; inline
46 : PQTRANS_UNKNOWN                   HEX: 4 ; inline
47
48 ! PGVerbosity;
49 : PQERRORS_TERSE                    HEX: 0 ; inline
50 : PQERRORS_DEFAULT                  HEX: 1 ; inline
51 : PQERRORS_VERBOSE                  HEX: 2 ; inline
52
53 : InvalidOid 0 ; inline
54
55 TYPEDEF: int ConnStatusType
56 TYPEDEF: int ExecStatusType 
57 TYPEDEF: int PostgresPollingStatusType
58 TYPEDEF: int PGTransactionStatusType 
59 TYPEDEF: int PGVerbosity 
60
61 TYPEDEF: void* PGconn*
62 TYPEDEF: void* PGresult*
63 TYPEDEF: void* PGcancel*
64 TYPEDEF: uint Oid
65 TYPEDEF: uint* Oid*
66 TYPEDEF: char pqbool
67 TYPEDEF: void* PQconninfoOption*
68 TYPEDEF: void* PGnotify*
69 TYPEDEF: void* PQArgBlock*
70 TYPEDEF: void* PQprintOpt*
71 TYPEDEF: void* FILE*
72 TYPEDEF: void* SSL*
73
74 LIBRARY: postgresql
75
76 ! Exported functions of libpq
77
78 ! make a new client connection to the backend
79 ! Asynchronous (non-blocking)
80 FUNCTION: PGconn* PQconnectStart ( char* conninfo ) ;
81 FUNCTION: PostgresPollingStatusType PQconnectPoll ( PGconn* conn ) ;
82
83 ! Synchronous (blocking)
84 FUNCTION: PGconn* PQconnectdb ( char* conninfo ) ;
85 FUNCTION: PGconn* PQsetdbLogin ( char* pghost, char* pgport,
86              char* pgoptions, char* pgtty,
87              char* dbName,
88              char* login, char* pwd ) ;
89
90 : PQsetdb ( M_PGHOST M_PGPORT M_PGOPT M_PGTTY M_DBNAME -- PGconn* )
91     f f PQsetdbLogin ;
92
93 ! close the current connection and free the PGconn data structure
94 FUNCTION: void PQfinish ( PGconn* conn ) ;
95
96 ! get info about connection options known to PQconnectdb
97 FUNCTION: PQconninfoOption* PQconndefaults ( ) ;
98
99 ! free the data structure returned by PQconndefaults()
100 FUNCTION: void PQconninfoFree ( PQconninfoOption* connOptions ) ;
101
102 ! Asynchronous (non-blocking)
103 FUNCTION: int    PQresetStart ( PGconn* conn ) ;
104 FUNCTION: PostgresPollingStatusType PQresetPoll ( PGconn* conn ) ;
105
106 ! Synchronous (blocking)
107 FUNCTION: void PQreset ( PGconn* conn ) ;
108
109 ! request a cancel structure
110 FUNCTION: PGcancel* PQgetCancel ( PGconn* conn ) ;
111
112 ! free a cancel structure
113 FUNCTION: void PQfreeCancel ( PGcancel* cancel ) ;
114
115 ! issue a cancel request
116 FUNCTION: int    PQrequestCancel ( PGconn* conn ) ;
117
118 ! Accessor functions for PGconn objects
119 FUNCTION: char* PQdb ( PGconn* conn ) ;
120 FUNCTION: char* PQuser ( PGconn* conn ) ;
121 FUNCTION: char* PQpass ( PGconn* conn ) ;
122 FUNCTION: char* PQhost ( PGconn* conn ) ;
123 FUNCTION: char* PQport ( PGconn* conn ) ;
124 FUNCTION: char* PQtty ( PGconn* conn ) ;
125 FUNCTION: char* PQoptions ( PGconn* conn ) ;
126 FUNCTION: ConnStatusType PQstatus ( PGconn* conn ) ;
127 FUNCTION: PGTransactionStatusType PQtransactionStatus ( PGconn* conn ) ;
128 FUNCTION: char* PQparameterStatus ( PGconn* conn,
129                   char* paramName ) ;
130 FUNCTION: int PQprotocolVersion ( PGconn* conn ) ;
131 ! FUNCTION: int PQServerVersion ( PGconn* conn ) ;
132 FUNCTION: char* PQerrorMessage ( PGconn* conn ) ;
133 FUNCTION: int PQsocket ( PGconn* conn ) ;
134 FUNCTION: int PQbackendPID ( PGconn* conn ) ;
135 FUNCTION: int PQclientEncoding ( PGconn* conn ) ;
136 FUNCTION: int PQsetClientEncoding ( PGconn* conn, char* encoding ) ;
137
138 ! May not be compiled into libpq
139 ! Get the SSL structure associated with a connection
140 FUNCTION: SSL* PQgetssl ( PGconn* conn ) ;
141
142 ! Tell libpq whether it needs to initialize OpenSSL
143 FUNCTION: void PQinitSSL ( int do_init ) ;
144
145 ! Set verbosity for PQerrorMessage and PQresultErrorMessage
146 FUNCTION: PGVerbosity PQsetErrorVerbosity ( PGconn* conn,
147     PGVerbosity verbosity ) ;
148
149 ! Enable/disable tracing
150 FUNCTION: void PQtrace ( PGconn* conn, FILE* debug_port ) ;
151 FUNCTION: void PQuntrace ( PGconn* conn ) ;
152
153 ! BROKEN
154 ! Function types for notice-handling callbacks
155 ! typedef void (*PQnoticeReceiver) (void *arg, PGresult *res);
156 ! typedef void (*PQnoticeProcessor) (void *arg, char* message);
157 ! ALIAS: void* PQnoticeReceiver
158 ! ALIAS: void* PQnoticeProcessor
159
160 ! Override default notice handling routines
161 ! FUNCTION: PQnoticeReceiver PQsetNoticeReceiver ( PGconn* conn,
162                     ! PQnoticeReceiver proc,
163                     ! void* arg ) ;
164 ! FUNCTION: PQnoticeProcessor PQsetNoticeProcessor ( PGconn* conn,
165                     ! PQnoticeProcessor proc,
166                     ! void* arg ) ;
167 ! END BROKEN
168
169 ! === in fe-exec.c ===
170
171 ! Simple synchronous query
172 FUNCTION: PGresult* PQexec ( PGconn* conn, char* query ) ;
173 FUNCTION: PGresult* PQexecParams ( PGconn* conn,
174              char* command,
175              int nParams,
176              Oid* paramTypes,
177              char** paramValues,
178              int* paramLengths,
179              int* paramFormats,
180              int resultFormat ) ;
181 FUNCTION: PGresult* PQprepare ( PGconn* conn, char* stmtName,
182         char* query, int nParams,
183         Oid* paramTypes ) ;
184 FUNCTION: PGresult* PQexecPrepared ( PGconn* conn,
185              char* stmtName,
186              int nParams,
187              char** paramValues,
188              int* paramLengths,
189              int* paramFormats,
190              int resultFormat ) ;
191
192 ! Interface for multiple-result or asynchronous queries
193 FUNCTION: int PQsendQuery ( PGconn* conn, char* query ) ;
194 FUNCTION: int PQsendQueryParams ( PGconn* conn,
195                   char* command,
196                   int nParams,
197                   Oid* paramTypes,
198                   char** paramValues,
199                   int* paramLengths,
200                   int* paramFormats,
201                   int resultFormat ) ;
202 FUNCTION: PGresult* PQsendPrepare ( PGconn* conn, char* stmtName,
203             char* query, int nParams,
204             Oid* paramTypes ) ;
205 FUNCTION: int PQsendQueryPrepared ( PGconn* conn,
206                   char* stmtName,
207                   int nParams,
208                   char** paramValues,
209                   int *paramLengths,
210                   int *paramFormats,
211                   int resultFormat ) ;
212 FUNCTION: PGresult* PQgetResult ( PGconn* conn ) ;
213
214 ! Routines for managing an asynchronous query
215 FUNCTION: int    PQisBusy ( PGconn* conn ) ;
216 FUNCTION: int    PQconsumeInput ( PGconn* conn ) ;
217
218 ! LISTEN/NOTIFY support
219 FUNCTION: PGnotify* PQnotifies ( PGconn* conn ) ;
220
221 ! Routines for copy in/out
222 FUNCTION: int    PQputCopyData ( PGconn* conn, char* buffer, int nbytes ) ;
223 FUNCTION: int    PQputCopyEnd ( PGconn* conn, char* errormsg ) ;
224 FUNCTION: int    PQgetCopyData ( PGconn* conn, char** buffer, int async ) ;
225
226 ! Deprecated routines for copy in/out
227 FUNCTION: int    PQgetline ( PGconn* conn, char* string, int length ) ;
228 FUNCTION: int    PQputline ( PGconn* conn, char* string ) ;
229 FUNCTION: int    PQgetlineAsync ( PGconn* conn, char* buffer, int bufsize ) ;
230 FUNCTION: int    PQputnbytes ( PGconn* conn, char* buffer, int nbytes ) ;
231 FUNCTION: int    PQendcopy ( PGconn* conn ) ;
232
233 ! Set blocking/nonblocking connection to the backend
234 FUNCTION: int    PQsetnonblocking ( PGconn* conn, int arg ) ;
235 FUNCTION: int    PQisnonblocking ( PGconn* conn ) ;
236
237 ! Force the write buffer to be written (or at least try)
238 FUNCTION: int    PQflush ( PGconn* conn ) ;
239
240
241 ! * "Fast path" interface --- not really recommended for application
242 ! * use
243 !
244 FUNCTION: PGresult* PQfn ( PGconn* conn,
245      int fnid,
246      int* result_buf,
247      int* result_len,
248      int result_is_int,
249      PQArgBlock* args,
250      int nargs ) ;
251
252 ! Accessor functions for PGresult objects
253 FUNCTION: ExecStatusType PQresultStatus ( PGresult* res ) ;
254 FUNCTION: char* PQresStatus ( ExecStatusType status ) ;
255 FUNCTION: char* PQresultErrorMessage ( PGresult* res ) ;
256 FUNCTION: char* PQresultErrorField ( PGresult* res, int fieldcode ) ;
257 FUNCTION: int   PQntuples ( PGresult* res ) ;
258 FUNCTION: int   PQnfields ( PGresult* res ) ;
259 FUNCTION: int   PQbinaryTuples ( PGresult* res ) ;
260 FUNCTION: char* PQfname ( PGresult* res, int field_num ) ;
261 FUNCTION: int   PQfnumber ( PGresult* res, char* field_name ) ;
262 FUNCTION: Oid   PQftable ( PGresult* res, int field_num ) ;
263 FUNCTION: int   PQftablecol ( PGresult* res, int field_num ) ;
264 FUNCTION: int   PQfformat ( PGresult* res, int field_num ) ;
265 FUNCTION: Oid   PQftype ( PGresult* res, int field_num ) ;
266 FUNCTION: int   PQfsize ( PGresult* res, int field_num ) ;
267 FUNCTION: int   PQfmod ( PGresult* res, int field_num ) ;
268 FUNCTION: char* PQcmdStatus ( PGresult* res ) ;
269 FUNCTION: char* PQoidStatus ( PGresult* res ) ;
270 FUNCTION: Oid   PQoidValue ( PGresult* res ) ;
271 FUNCTION: char* PQcmdTuples ( PGresult* res ) ;
272 ! FUNCTION: char* PQgetvalue ( PGresult* res, int tup_num, int field_num ) ;
273 FUNCTION: void* PQgetvalue ( PGresult* res, int tup_num, int field_num ) ;
274 FUNCTION: int   PQgetlength ( PGresult* res, int tup_num, int field_num ) ;
275 FUNCTION: int   PQgetisnull ( PGresult* res, int tup_num, int field_num ) ;
276
277 ! Delete a PGresult
278 FUNCTION: void PQclear ( PGresult* res ) ;
279
280 ! For freeing other alloc'd results, such as PGnotify structs
281 FUNCTION: void PQfreemem ( void* ptr ) ;
282
283 ! Exists for backward compatibility.
284 : PQfreeNotify ( ptr -- ) PQfreemem ;
285
286 !
287 ! Make an empty PGresult with given status (some apps find this
288 ! useful). If conn is not NULL and status indicates an error, the
289 ! conn's errorMessage is copied.
290 !
291 FUNCTION: PGresult* PQmakeEmptyPGresult ( PGconn* conn, ExecStatusType status ) ;
292
293 ! Quoting strings before inclusion in queries.
294 FUNCTION: size_t PQescapeStringConn ( PGconn* conn,
295                                     char* to, char* from, size_t length,
296                                     int* error ) ;
297 FUNCTION: uchar* PQescapeByteaConn ( PGconn* conn,
298                                     char* from, size_t length,
299                                     size_t* to_length ) ;
300 FUNCTION: void* PQunescapeBytea ( uchar* strtext, size_t* retbuflen ) ;
301 ! FUNCTION: uchar* PQunescapeBytea ( uchar* strtext, size_t* retbuflen ) ;
302 ! These forms are deprecated!
303 FUNCTION: size_t PQescapeString ( void* to, char* from, size_t length ) ;
304 FUNCTION: uchar* PQescapeBytea ( uchar* bintext, size_t binlen,
305               size_t* bytealen ) ;
306
307 ! === in fe-print.c ===
308
309 FUNCTION: void PQprint ( FILE* fout, PGresult* res, PQprintOpt* ps ) ;
310
311 ! really old printing routines
312 FUNCTION: void PQdisplayTuples ( PGresult* res,
313                                 FILE* fp,               
314                                 int fillAlign,
315                                 char* fieldSep,
316                                 int printHeader,
317                                 int quiet ) ;
318
319 FUNCTION: void PQprintTuples ( PGresult* res,
320                           FILE* fout,           
321                           int printAttName,
322                           int terseOutput,      
323                           int width ) ; 
324 ! === in fe-lobj.c ===
325
326 ! Large-object access routines
327 FUNCTION: int    lo_open ( PGconn* conn, Oid lobjId, int mode ) ;
328 FUNCTION: int    lo_close ( PGconn* conn, int fd ) ;
329 FUNCTION: int    lo_read ( PGconn* conn, int fd, char* buf, size_t len ) ;
330 FUNCTION: int    lo_write ( PGconn* conn, int fd, char* buf, size_t len ) ;
331 FUNCTION: int    lo_lseek ( PGconn* conn, int fd, int offset, int whence ) ;
332 FUNCTION: Oid    lo_creat ( PGconn* conn, int mode ) ;
333 ! FUNCTION: Oid    lo_creat ( PGconn* conn, Oid lobjId ) ;
334 FUNCTION: int    lo_tell ( PGconn* conn, int fd ) ;
335 FUNCTION: int    lo_unlink ( PGconn* conn, Oid lobjId ) ;
336 FUNCTION: Oid    lo_import ( PGconn* conn, char* filename ) ;
337 FUNCTION: int    lo_export ( PGconn* conn, Oid lobjId, char* filename ) ;
338
339 ! === in fe-misc.c ===
340
341 ! Determine length of multibyte encoded char at *s
342 FUNCTION: int    PQmblen ( uchar* s, int encoding ) ;
343
344 ! Determine display length of multibyte encoded char at *s
345 FUNCTION: int    PQdsplen ( uchar* s, int encoding ) ;
346
347 ! Get encoding id from environment variable PGCLIENTENCODING
348 FUNCTION: int    PQenv2encoding ( ) ;
349
350 ! From git, include/catalog/pg_type.h
351 : BOOL-OID 16 ; inline
352 : BYTEA-OID 17 ; inline
353 : CHAR-OID 18 ; inline
354 : NAME-OID 19 ; inline
355 : INT8-OID 20 ; inline
356 : INT2-OID 21 ; inline
357 : INT4-OID 23 ; inline
358 : TEXT-OID 23 ; inline
359 : OID-OID 26 ; inline
360 : FLOAT4-OID 700 ; inline
361 : FLOAT8-OID 701 ; inline
362 : VARCHAR-OID 1043 ; inline
363 : DATE-OID 1082 ; inline
364 : TIME-OID 1083 ; inline
365 : TIMESTAMP-OID 1114 ; inline
366 : TIMESTAMPTZ-OID 1184 ; inline
367 : INTERVAL-OID 1186 ; inline
368 : NUMERIC-OID 1700 ; inline