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