]> gitweb.factorcode.org Git - factor.git/blob - basis/db/postgresql/ffi/ffi.factor
use radix literals
[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.c-types alien.syntax combinators system
5 alien.libraries ;
6 IN: db.postgresql.ffi
7
8 << "postgresql" {
9     { [ os windows? ]  [ "libpq.dll" ] }
10     { [ os macosx? ] [ "libpq.dylib" ] }
11     { [ os unix?  ]  [ "libpq.so" ] }
12 } cond cdecl add-library >>
13
14 ! ConnSatusType
15 CONSTANT: CONNECTION_OK                     0x0
16 CONSTANT: CONNECTION_BAD                    0x1
17 CONSTANT: CONNECTION_STARTED                0x2
18 CONSTANT: CONNECTION_MADE                   0x3
19 CONSTANT: CONNECTION_AWAITING_RESPONSE      0x4
20 CONSTANT: CONNECTION_AUTH_OK                0x5
21 CONSTANT: CONNECTION_SETENV                 0x6
22 CONSTANT: CONNECTION_SSL_STARTUP            0x7
23 CONSTANT: CONNECTION_NEEDED                 0x8
24
25 ! PostgresPollingStatusType
26 CONSTANT: PGRES_POLLING_FAILED              0x0
27 CONSTANT: PGRES_POLLING_READING             0x1
28 CONSTANT: PGRES_POLLING_WRITING             0x2
29 CONSTANT: PGRES_POLLING_OK                  0x3
30 CONSTANT: PGRES_POLLING_ACTIVE              0x4
31
32 ! ExecStatusType;
33 CONSTANT: PGRES_EMPTY_QUERY                 0x0
34 CONSTANT: PGRES_COMMAND_OK                  0x1
35 CONSTANT: PGRES_TUPLES_OK                   0x2
36 CONSTANT: PGRES_COPY_OUT                    0x3
37 CONSTANT: PGRES_COPY_IN                     0x4
38 CONSTANT: PGRES_BAD_RESPONSE                0x5
39 CONSTANT: PGRES_NONFATAL_ERROR              0x6
40 CONSTANT: PGRES_FATAL_ERROR                 0x7
41
42 ! PGTransactionStatusType;
43 CONSTANT: PQTRANS_IDLE                      0x0
44 CONSTANT: PQTRANS_ACTIVE                    0x1
45 CONSTANT: PQTRANS_INTRANS                   0x2
46 CONSTANT: PQTRANS_INERROR                   0x3
47 CONSTANT: PQTRANS_UNKNOWN                   0x4
48
49 ! PGVerbosity;
50 CONSTANT: PQERRORS_TERSE                    0x0
51 CONSTANT: PQERRORS_DEFAULT                  0x1
52 CONSTANT: PQERRORS_VERBOSE                  0x2
53
54 CONSTANT: InvalidOid 0
55
56 TYPEDEF: int ConnStatusType
57 TYPEDEF: int ExecStatusType 
58 TYPEDEF: int PostgresPollingStatusType
59 TYPEDEF: int PGTransactionStatusType 
60 TYPEDEF: int PGVerbosity 
61
62 C-TYPE: PGconn
63 C-TYPE: PGresult
64 C-TYPE: PGcancel
65 TYPEDEF: uint Oid
66 TYPEDEF: char pqbool
67 C-TYPE: PQconninfoOption
68 C-TYPE: PGnotify
69 C-TYPE: PQArgBlock
70 C-TYPE: PQprintOpt
71 C-TYPE: SSL
72 C-TYPE: FILE
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 ( c-string conninfo ) ;
81 FUNCTION: PostgresPollingStatusType PQconnectPoll ( PGconn* conn ) ;
82
83 ! Synchronous (blocking)
84 FUNCTION: PGconn* PQconnectdb ( c-string conninfo ) ;
85 FUNCTION: PGconn* PQsetdbLogin ( c-string pghost, c-string pgport,
86              c-string pgoptions, c-string pgtty,
87              c-string dbName,
88              c-string login, c-string 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: c-string PQdb ( PGconn* conn ) ;
120 FUNCTION: c-string PQuser ( PGconn* conn ) ;
121 FUNCTION: c-string PQpass ( PGconn* conn ) ;
122 FUNCTION: c-string PQhost ( PGconn* conn ) ;
123 FUNCTION: c-string PQport ( PGconn* conn ) ;
124 FUNCTION: c-string PQtty ( PGconn* conn ) ;
125 FUNCTION: c-string PQoptions ( PGconn* conn ) ;
126 FUNCTION: ConnStatusType PQstatus ( PGconn* conn ) ;
127 FUNCTION: PGTransactionStatusType PQtransactionStatus ( PGconn* conn ) ;
128 FUNCTION: c-string PQparameterStatus ( PGconn* conn,
129                   c-string paramName ) ;
130 FUNCTION: int PQprotocolVersion ( PGconn* conn ) ;
131 ! FUNCTION: int PQServerVersion ( PGconn* conn ) ;
132 FUNCTION: c-string 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, c-string 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, c-string 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, c-string query ) ;
173 FUNCTION: PGresult* PQexecParams ( PGconn* conn,
174              c-string command,
175              int nParams,
176              Oid* paramTypes,
177              c-string* paramValues,
178              int* paramLengths,
179              int* paramFormats,
180              int resultFormat ) ;
181 FUNCTION: PGresult* PQprepare ( PGconn* conn, c-string stmtName,
182         c-string query, int nParams,
183         Oid* paramTypes ) ;
184 FUNCTION: PGresult* PQexecPrepared ( PGconn* conn,
185              c-string stmtName,
186              int nParams,
187              c-string* 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, c-string query ) ;
194 FUNCTION: int PQsendQueryParams ( PGconn* conn,
195                   c-string command,
196                   int nParams,
197                   Oid* paramTypes,
198                   c-string* paramValues,
199                   int* paramLengths,
200                   int* paramFormats,
201                   int resultFormat ) ;
202 FUNCTION: PGresult* PQsendPrepare ( PGconn* conn, c-string stmtName,
203             c-string query, int nParams,
204             Oid* paramTypes ) ;
205 FUNCTION: int PQsendQueryPrepared ( PGconn* conn,
206                   c-string stmtName,
207                   int nParams,
208                   c-string* 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, c-string buffer, int nbytes ) ;
223 FUNCTION: int    PQputCopyEnd ( PGconn* conn, c-string errormsg ) ;
224 FUNCTION: int    PQgetCopyData ( PGconn* conn, c-string* buffer, int async ) ;
225
226 ! Deprecated routines for copy in/out
227 FUNCTION: int    PQgetline ( PGconn* conn, c-string string, int length ) ;
228 FUNCTION: int    PQputline ( PGconn* conn, c-string string ) ;
229 FUNCTION: int    PQgetlineAsync ( PGconn* conn, c-string buffer, int bufsize ) ;
230 FUNCTION: int    PQputnbytes ( PGconn* conn, c-string 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: c-string PQresStatus ( ExecStatusType status ) ;
255 FUNCTION: c-string PQresultErrorMessage ( PGresult* res ) ;
256 FUNCTION: c-string 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: c-string PQfname ( PGresult* res, int field_num ) ;
261 FUNCTION: int   PQfnumber ( PGresult* res, c-string 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: c-string PQcmdStatus ( PGresult* res ) ;
269 FUNCTION: c-string PQoidStatus ( PGresult* res ) ;
270 FUNCTION: Oid   PQoidValue ( PGresult* res ) ;
271 FUNCTION: c-string PQcmdTuples ( PGresult* res ) ;
272 ! FUNCTION: c-string 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                                     c-string to, c-string from, size_t length,
296                                     int* error ) ;
297 FUNCTION: c-string PQescapeByteaConn ( PGconn* conn,
298                                     c-string from, size_t length,
299                                     size_t* to_length ) ;
300 FUNCTION: void* PQunescapeBytea ( c-string strtext, size_t* retbuflen ) ;
301 ! FUNCTION: c-string PQunescapeBytea ( c-string strtext, size_t* retbuflen ) ;
302 ! These forms are deprecated!
303 FUNCTION: size_t PQescapeString ( void* to, c-string from, size_t length ) ;
304 FUNCTION: c-string PQescapeBytea ( c-string 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                                 c-string 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, c-string buf, size_t len ) ;
330 FUNCTION: int    lo_write ( PGconn* conn, int fd, c-string 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, c-string filename ) ;
337 FUNCTION: int    lo_export ( PGconn* conn, Oid lobjId, c-string filename ) ;
338
339 ! === in fe-misc.c ===
340
341 ! Determine length of multibyte encoded char at *s
342 FUNCTION: int    PQmblen ( c-string s, int encoding ) ;
343
344 ! Determine display length of multibyte encoded char at *s
345 FUNCTION: int    PQdsplen ( c-string 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 CONSTANT: BOOL-OID 16
352 CONSTANT: BYTEA-OID 17
353 CONSTANT: CHAR-OID 18
354 CONSTANT: NAME-OID 19
355 CONSTANT: INT8-OID 20
356 CONSTANT: INT2-OID 21
357 CONSTANT: INT4-OID 23
358 CONSTANT: TEXT-OID 23
359 CONSTANT: OID-OID 26
360 CONSTANT: FLOAT4-OID 700
361 CONSTANT: FLOAT8-OID 701
362 CONSTANT: VARCHAR-OID 1043
363 CONSTANT: DATE-OID 1082
364 CONSTANT: TIME-OID 1083
365 CONSTANT: TIMESTAMP-OID 1114
366 CONSTANT: TIMESTAMPTZ-OID 1184
367 CONSTANT: INTERVAL-OID 1186
368 CONSTANT: NUMERIC-OID 1700