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
9 { [ os winnt? ] [ "libpq.dll" ] }
10 { [ os macosx? ] [ "libpq.dylib" ] }
11 { [ os unix? ] [ "libpq.so" ] }
12 } cond "cdecl" add-library >>
15 CONSTANT: CONNECTION_OK HEX: 0
16 CONSTANT: CONNECTION_BAD HEX: 1
17 CONSTANT: CONNECTION_STARTED HEX: 2
18 CONSTANT: CONNECTION_MADE HEX: 3
19 CONSTANT: CONNECTION_AWAITING_RESPONSE HEX: 4
20 CONSTANT: CONNECTION_AUTH_OK HEX: 5
21 CONSTANT: CONNECTION_SETENV HEX: 6
22 CONSTANT: CONNECTION_SSL_STARTUP HEX: 7
23 CONSTANT: CONNECTION_NEEDED HEX: 8
25 ! PostgresPollingStatusType
26 CONSTANT: PGRES_POLLING_FAILED HEX: 0
27 CONSTANT: PGRES_POLLING_READING HEX: 1
28 CONSTANT: PGRES_POLLING_WRITING HEX: 2
29 CONSTANT: PGRES_POLLING_OK HEX: 3
30 CONSTANT: PGRES_POLLING_ACTIVE HEX: 4
33 CONSTANT: PGRES_EMPTY_QUERY HEX: 0
34 CONSTANT: PGRES_COMMAND_OK HEX: 1
35 CONSTANT: PGRES_TUPLES_OK HEX: 2
36 CONSTANT: PGRES_COPY_OUT HEX: 3
37 CONSTANT: PGRES_COPY_IN HEX: 4
38 CONSTANT: PGRES_BAD_RESPONSE HEX: 5
39 CONSTANT: PGRES_NONFATAL_ERROR HEX: 6
40 CONSTANT: PGRES_FATAL_ERROR HEX: 7
42 ! PGTransactionStatusType;
43 CONSTANT: PQTRANS_IDLE HEX: 0
44 CONSTANT: PQTRANS_ACTIVE HEX: 1
45 CONSTANT: PQTRANS_INTRANS HEX: 2
46 CONSTANT: PQTRANS_INERROR HEX: 3
47 CONSTANT: PQTRANS_UNKNOWN HEX: 4
50 CONSTANT: PQERRORS_TERSE HEX: 0
51 CONSTANT: PQERRORS_DEFAULT HEX: 1
52 CONSTANT: PQERRORS_VERBOSE HEX: 2
54 CONSTANT: InvalidOid 0
56 TYPEDEF: int ConnStatusType
57 TYPEDEF: int ExecStatusType
58 TYPEDEF: int PostgresPollingStatusType
59 TYPEDEF: int PGTransactionStatusType
60 TYPEDEF: int PGVerbosity
68 C-TYPE: PQconninfoOption
77 ! Exported functions of libpq
79 ! make a new client connection to the backend
80 ! Asynchronous (non-blocking)
81 FUNCTION: PGconn* PQconnectStart ( char* conninfo ) ;
82 FUNCTION: PostgresPollingStatusType PQconnectPoll ( PGconn* conn ) ;
84 ! Synchronous (blocking)
85 FUNCTION: PGconn* PQconnectdb ( char* conninfo ) ;
86 FUNCTION: PGconn* PQsetdbLogin ( char* pghost, char* pgport,
87 char* pgoptions, char* pgtty,
89 char* login, char* pwd ) ;
91 : PQsetdb ( M_PGHOST M_PGPORT M_PGOPT M_PGTTY M_DBNAME -- PGconn* )
94 ! close the current connection and free the PGconn data structure
95 FUNCTION: void PQfinish ( PGconn* conn ) ;
97 ! get info about connection options known to PQconnectdb
98 FUNCTION: PQconninfoOption* PQconndefaults ( ) ;
100 ! free the data structure returned by PQconndefaults()
101 FUNCTION: void PQconninfoFree ( PQconninfoOption* connOptions ) ;
103 ! Asynchronous (non-blocking)
104 FUNCTION: int PQresetStart ( PGconn* conn ) ;
105 FUNCTION: PostgresPollingStatusType PQresetPoll ( PGconn* conn ) ;
107 ! Synchronous (blocking)
108 FUNCTION: void PQreset ( PGconn* conn ) ;
110 ! request a cancel structure
111 FUNCTION: PGcancel* PQgetCancel ( PGconn* conn ) ;
113 ! free a cancel structure
114 FUNCTION: void PQfreeCancel ( PGcancel* cancel ) ;
116 ! issue a cancel request
117 FUNCTION: int PQrequestCancel ( PGconn* conn ) ;
119 ! Accessor functions for PGconn objects
120 FUNCTION: char* PQdb ( PGconn* conn ) ;
121 FUNCTION: char* PQuser ( PGconn* conn ) ;
122 FUNCTION: char* PQpass ( PGconn* conn ) ;
123 FUNCTION: char* PQhost ( PGconn* conn ) ;
124 FUNCTION: char* PQport ( PGconn* conn ) ;
125 FUNCTION: char* PQtty ( PGconn* conn ) ;
126 FUNCTION: char* PQoptions ( PGconn* conn ) ;
127 FUNCTION: ConnStatusType PQstatus ( PGconn* conn ) ;
128 FUNCTION: PGTransactionStatusType PQtransactionStatus ( PGconn* conn ) ;
129 FUNCTION: char* PQparameterStatus ( PGconn* conn,
131 FUNCTION: int PQprotocolVersion ( PGconn* conn ) ;
132 ! FUNCTION: int PQServerVersion ( PGconn* conn ) ;
133 FUNCTION: char* PQerrorMessage ( PGconn* conn ) ;
134 FUNCTION: int PQsocket ( PGconn* conn ) ;
135 FUNCTION: int PQbackendPID ( PGconn* conn ) ;
136 FUNCTION: int PQclientEncoding ( PGconn* conn ) ;
137 FUNCTION: int PQsetClientEncoding ( PGconn* conn, char* encoding ) ;
139 ! May not be compiled into libpq
140 ! Get the SSL structure associated with a connection
141 FUNCTION: SSL* PQgetssl ( PGconn* conn ) ;
143 ! Tell libpq whether it needs to initialize OpenSSL
144 FUNCTION: void PQinitSSL ( int do_init ) ;
146 ! Set verbosity for PQerrorMessage and PQresultErrorMessage
147 FUNCTION: PGVerbosity PQsetErrorVerbosity ( PGconn* conn,
148 PGVerbosity verbosity ) ;
150 ! Enable/disable tracing
151 FUNCTION: void PQtrace ( PGconn* conn, FILE* debug_port ) ;
152 FUNCTION: void PQuntrace ( PGconn* conn ) ;
155 ! Function types for notice-handling callbacks
156 ! typedef void (*PQnoticeReceiver) (void *arg, PGresult *res);
157 ! typedef void (*PQnoticeProcessor) (void *arg, char* message);
158 ! ALIAS: void* PQnoticeReceiver
159 ! ALIAS: void* PQnoticeProcessor
161 ! Override default notice handling routines
162 ! FUNCTION: PQnoticeReceiver PQsetNoticeReceiver ( PGconn* conn,
163 ! PQnoticeReceiver proc,
165 ! FUNCTION: PQnoticeProcessor PQsetNoticeProcessor ( PGconn* conn,
166 ! PQnoticeProcessor proc,
170 ! === in fe-exec.c ===
172 ! Simple synchronous query
173 FUNCTION: PGresult* PQexec ( PGconn* conn, char* query ) ;
174 FUNCTION: PGresult* PQexecParams ( PGconn* conn,
182 FUNCTION: PGresult* PQprepare ( PGconn* conn, char* stmtName,
183 char* query, int nParams,
185 FUNCTION: PGresult* PQexecPrepared ( PGconn* conn,
193 ! Interface for multiple-result or asynchronous queries
194 FUNCTION: int PQsendQuery ( PGconn* conn, char* query ) ;
195 FUNCTION: int PQsendQueryParams ( PGconn* conn,
203 FUNCTION: PGresult* PQsendPrepare ( PGconn* conn, char* stmtName,
204 char* query, int nParams,
206 FUNCTION: int PQsendQueryPrepared ( PGconn* conn,
213 FUNCTION: PGresult* PQgetResult ( PGconn* conn ) ;
215 ! Routines for managing an asynchronous query
216 FUNCTION: int PQisBusy ( PGconn* conn ) ;
217 FUNCTION: int PQconsumeInput ( PGconn* conn ) ;
219 ! LISTEN/NOTIFY support
220 FUNCTION: PGnotify* PQnotifies ( PGconn* conn ) ;
222 ! Routines for copy in/out
223 FUNCTION: int PQputCopyData ( PGconn* conn, char* buffer, int nbytes ) ;
224 FUNCTION: int PQputCopyEnd ( PGconn* conn, char* errormsg ) ;
225 FUNCTION: int PQgetCopyData ( PGconn* conn, char** buffer, int async ) ;
227 ! Deprecated routines for copy in/out
228 FUNCTION: int PQgetline ( PGconn* conn, char* string, int length ) ;
229 FUNCTION: int PQputline ( PGconn* conn, char* string ) ;
230 FUNCTION: int PQgetlineAsync ( PGconn* conn, char* buffer, int bufsize ) ;
231 FUNCTION: int PQputnbytes ( PGconn* conn, char* buffer, int nbytes ) ;
232 FUNCTION: int PQendcopy ( PGconn* conn ) ;
234 ! Set blocking/nonblocking connection to the backend
235 FUNCTION: int PQsetnonblocking ( PGconn* conn, int arg ) ;
236 FUNCTION: int PQisnonblocking ( PGconn* conn ) ;
238 ! Force the write buffer to be written (or at least try)
239 FUNCTION: int PQflush ( PGconn* conn ) ;
242 ! * "Fast path" interface --- not really recommended for application
245 FUNCTION: PGresult* PQfn ( PGconn* conn,
253 ! Accessor functions for PGresult objects
254 FUNCTION: ExecStatusType PQresultStatus ( PGresult* res ) ;
255 FUNCTION: char* PQresStatus ( ExecStatusType status ) ;
256 FUNCTION: char* PQresultErrorMessage ( PGresult* res ) ;
257 FUNCTION: char* PQresultErrorField ( PGresult* res, int fieldcode ) ;
258 FUNCTION: int PQntuples ( PGresult* res ) ;
259 FUNCTION: int PQnfields ( PGresult* res ) ;
260 FUNCTION: int PQbinaryTuples ( PGresult* res ) ;
261 FUNCTION: char* PQfname ( PGresult* res, int field_num ) ;
262 FUNCTION: int PQfnumber ( PGresult* res, char* field_name ) ;
263 FUNCTION: Oid PQftable ( PGresult* res, int field_num ) ;
264 FUNCTION: int PQftablecol ( PGresult* res, int field_num ) ;
265 FUNCTION: int PQfformat ( PGresult* res, int field_num ) ;
266 FUNCTION: Oid PQftype ( PGresult* res, int field_num ) ;
267 FUNCTION: int PQfsize ( PGresult* res, int field_num ) ;
268 FUNCTION: int PQfmod ( PGresult* res, int field_num ) ;
269 FUNCTION: char* PQcmdStatus ( PGresult* res ) ;
270 FUNCTION: char* PQoidStatus ( PGresult* res ) ;
271 FUNCTION: Oid PQoidValue ( PGresult* res ) ;
272 FUNCTION: char* PQcmdTuples ( PGresult* res ) ;
273 ! FUNCTION: char* PQgetvalue ( PGresult* res, int tup_num, int field_num ) ;
274 FUNCTION: void* PQgetvalue ( PGresult* res, int tup_num, int field_num ) ;
275 FUNCTION: int PQgetlength ( PGresult* res, int tup_num, int field_num ) ;
276 FUNCTION: int PQgetisnull ( PGresult* res, int tup_num, int field_num ) ;
279 FUNCTION: void PQclear ( PGresult* res ) ;
281 ! For freeing other alloc'd results, such as PGnotify structs
282 FUNCTION: void PQfreemem ( void* ptr ) ;
284 ! Exists for backward compatibility.
285 : PQfreeNotify ( ptr -- ) PQfreemem ;
288 ! Make an empty PGresult with given status (some apps find this
289 ! useful). If conn is not NULL and status indicates an error, the
290 ! conn's errorMessage is copied.
292 FUNCTION: PGresult* PQmakeEmptyPGresult ( PGconn* conn, ExecStatusType status ) ;
294 ! Quoting strings before inclusion in queries.
295 FUNCTION: size_t PQescapeStringConn ( PGconn* conn,
296 char* to, char* from, size_t length,
298 FUNCTION: uchar* PQescapeByteaConn ( PGconn* conn,
299 char* from, size_t length,
300 size_t* to_length ) ;
301 FUNCTION: void* PQunescapeBytea ( uchar* strtext, size_t* retbuflen ) ;
302 ! FUNCTION: uchar* PQunescapeBytea ( uchar* strtext, size_t* retbuflen ) ;
303 ! These forms are deprecated!
304 FUNCTION: size_t PQescapeString ( void* to, char* from, size_t length ) ;
305 FUNCTION: uchar* PQescapeBytea ( uchar* bintext, size_t binlen,
308 ! === in fe-print.c ===
310 FUNCTION: void PQprint ( FILE* fout, PGresult* res, PQprintOpt* ps ) ;
312 ! really old printing routines
313 FUNCTION: void PQdisplayTuples ( PGresult* res,
320 FUNCTION: void PQprintTuples ( PGresult* res,
325 ! === in fe-lobj.c ===
327 ! Large-object access routines
328 FUNCTION: int lo_open ( PGconn* conn, Oid lobjId, int mode ) ;
329 FUNCTION: int lo_close ( PGconn* conn, int fd ) ;
330 FUNCTION: int lo_read ( PGconn* conn, int fd, char* buf, size_t len ) ;
331 FUNCTION: int lo_write ( PGconn* conn, int fd, char* buf, size_t len ) ;
332 FUNCTION: int lo_lseek ( PGconn* conn, int fd, int offset, int whence ) ;
333 FUNCTION: Oid lo_creat ( PGconn* conn, int mode ) ;
334 ! FUNCTION: Oid lo_creat ( PGconn* conn, Oid lobjId ) ;
335 FUNCTION: int lo_tell ( PGconn* conn, int fd ) ;
336 FUNCTION: int lo_unlink ( PGconn* conn, Oid lobjId ) ;
337 FUNCTION: Oid lo_import ( PGconn* conn, char* filename ) ;
338 FUNCTION: int lo_export ( PGconn* conn, Oid lobjId, char* filename ) ;
340 ! === in fe-misc.c ===
342 ! Determine length of multibyte encoded char at *s
343 FUNCTION: int PQmblen ( uchar* s, int encoding ) ;
345 ! Determine display length of multibyte encoded char at *s
346 FUNCTION: int PQdsplen ( uchar* s, int encoding ) ;
348 ! Get encoding id from environment variable PGCLIENTENCODING
349 FUNCTION: int PQenv2encoding ( ) ;
351 ! From git, include/catalog/pg_type.h
352 CONSTANT: BOOL-OID 16
353 CONSTANT: BYTEA-OID 17
354 CONSTANT: CHAR-OID 18
355 CONSTANT: NAME-OID 19
356 CONSTANT: INT8-OID 20
357 CONSTANT: INT2-OID 21
358 CONSTANT: INT4-OID 23
359 CONSTANT: TEXT-OID 23
361 CONSTANT: FLOAT4-OID 700
362 CONSTANT: FLOAT8-OID 701
363 CONSTANT: VARCHAR-OID 1043
364 CONSTANT: DATE-OID 1082
365 CONSTANT: TIME-OID 1083
366 CONSTANT: TIMESTAMP-OID 1114
367 CONSTANT: TIMESTAMPTZ-OID 1184
368 CONSTANT: INTERVAL-OID 1186
369 CONSTANT: NUMERIC-OID 1700