1 /*-------------------------------------------------------------------------
4 * This file contains definitions for structures and
5 * externs for functions used by frontend postgres applications.
7 * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-fe.h,v 1.107 2004/08/29 04:13:12 momjian Exp $
12 *-------------------------------------------------------------------------
26 * postgres_ext.h defines the backend's externally visible types,
29 #include "postgres_ext.h"
31 /* SSL type is needed here only to declare PQgetssl() */
33 #include <openssl/ssl.h>
36 /* Application-visible enum types */
41 * Although it is okay to add to this list, values which become unused
42 * should never be removed, nor should constants be redefined - that
43 * would break compatibility with existing code.
47 /* Non-blocking mode only below here */
50 * The existence of these should never be relied upon - they should
51 * only be used for user feedback or similar purposes.
53 CONNECTION_STARTED, /* Waiting for connection to be made. */
54 CONNECTION_MADE, /* Connection OK; waiting to send. */
55 CONNECTION_AWAITING_RESPONSE, /* Waiting for a response from the
57 CONNECTION_AUTH_OK, /* Received authentication; waiting for
59 CONNECTION_SETENV, /* Negotiating environment. */
60 CONNECTION_SSL_STARTUP, /* Negotiating SSL. */
61 CONNECTION_NEEDED /* Internal state: connect() needed */
66 PGRES_POLLING_FAILED = 0,
67 PGRES_POLLING_READING, /* These two indicate that one may */
68 PGRES_POLLING_WRITING, /* use select before polling again. */
70 PGRES_POLLING_ACTIVE /* unused; keep for awhile for backwards
72 } PostgresPollingStatusType;
76 PGRES_EMPTY_QUERY = 0, /* empty query string was executed */
77 PGRES_COMMAND_OK, /* a query command that doesn't return
78 * anything was executed properly by the
80 PGRES_TUPLES_OK, /* a query command that returns tuples was
81 * executed properly by the backend,
82 * PGresult contains the result tuples */
83 PGRES_COPY_OUT, /* Copy Out data transfer in progress */
84 PGRES_COPY_IN, /* Copy In data transfer in progress */
85 PGRES_BAD_RESPONSE, /* an unexpected response was recv'd from
87 PGRES_NONFATAL_ERROR, /* notice or warning message */
88 PGRES_FATAL_ERROR /* query failed */
93 PQTRANS_IDLE, /* connection idle */
94 PQTRANS_ACTIVE, /* command in progress */
95 PQTRANS_INTRANS, /* idle, within transaction block */
96 PQTRANS_INERROR, /* idle, within failed transaction */
97 PQTRANS_UNKNOWN /* cannot determine status */
98 } PGTransactionStatusType;
102 PQERRORS_TERSE, /* single-line error messages */
103 PQERRORS_DEFAULT, /* recommended style */
104 PQERRORS_VERBOSE /* all the facts, ma'am */
107 /* PGconn encapsulates a connection to the backend.
108 * The contents of this struct are not supposed to be known to applications.
110 typedef struct pg_conn PGconn;
112 /* PGresult encapsulates the result of a query (or more precisely, of a single
113 * SQL command --- a query string given to PQsendQuery can contain multiple
114 * commands and thus return multiple PGresult objects).
115 * The contents of this struct are not supposed to be known to applications.
117 typedef struct pg_result PGresult;
119 /* PGnotify represents the occurrence of a NOTIFY message.
120 * Ideally this would be an opaque typedef, but it's so simple that it's
121 * unlikely to change.
122 * NOTE: in Postgres 6.4 and later, the be_pid is the notifying backend's,
123 * whereas in earlier versions it was always your own backend's PID.
125 typedef struct pgNotify
127 char *relname; /* notification condition name */
128 int be_pid; /* process ID of server process */
129 char *extra; /* notification parameter */
132 /* Function types for notice-handling callbacks */
133 typedef void (*PQnoticeReceiver) (void *arg, const PGresult *res);
134 typedef void (*PQnoticeProcessor) (void *arg, const char *message);
136 /* Print options for PQprint() */
139 typedef struct _PQprintOpt
141 pqbool header; /* print output field headings and row
143 pqbool align; /* fill align the fields */
144 pqbool standard; /* old brain dead format */
145 pqbool html3; /* output html tables */
146 pqbool expanded; /* expand tables */
147 pqbool pager; /* use pager for output if needed */
148 char *fieldSep; /* field separator */
149 char *tableOpt; /* insert to HTML <table ...> */
150 char *caption; /* HTML <caption> */
151 char **fieldName; /* null terminated array of repalcement
156 * Structure for the conninfo parameter definitions returned by PQconndefaults
158 * All fields except "val" point at static strings which must not be altered.
159 * "val" is either NULL or a malloc'd current-value string. PQconninfoFree()
160 * will release both the val strings and the PQconninfoOption array itself.
163 typedef struct _PQconninfoOption
165 char *keyword; /* The keyword of the option */
166 char *envvar; /* Fallback environment variable name */
167 char *compiled; /* Fallback compiled in default value */
168 char *val; /* Option's current value, or NULL */
169 char *label; /* Label for field in connect dialog */
170 char *dispchar; /* Character to display for this field in
171 * a connect dialog. Values are: ""
172 * Display entered value as is "*"
173 * Password field - hide value "D" Debug
174 * option - don't show by default */
175 int dispsize; /* Field size in characters for dialog */
179 * PQArgBlock -- structure for PQfn() arguments
188 int *ptr; /* can't use void (dec compiler barfs) */
194 * Exported functions of libpq
198 /* === in fe-connect.c === */
200 /* make a new client connection to the backend */
201 /* Asynchronous (non-blocking) */
202 extern PGconn *PQconnectStart(const char *conninfo);
203 extern PostgresPollingStatusType PQconnectPoll(PGconn *conn);
205 /* Synchronous (blocking) */
206 extern PGconn *PQconnectdb(const char *conninfo);
207 extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport,
208 const char *pgoptions, const char *pgtty,
210 const char *login, const char *pwd);
212 #define PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME) \
213 PQsetdbLogin(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME, NULL, NULL)
215 /* close the current connection and free the PGconn data structure */
216 extern void PQfinish(PGconn *conn);
218 /* get info about connection options known to PQconnectdb */
219 extern PQconninfoOption *PQconndefaults(void);
221 /* free the data structure returned by PQconndefaults() */
222 extern void PQconninfoFree(PQconninfoOption *connOptions);
225 * close the current connection and restablish a new one with the same
228 /* Asynchronous (non-blocking) */
229 extern int PQresetStart(PGconn *conn);
230 extern PostgresPollingStatusType PQresetPoll(PGconn *conn);
232 /* Synchronous (blocking) */
233 extern void PQreset(PGconn *conn);
235 /* issue a cancel request */
236 extern int PQrequestCancel(PGconn *conn);
238 /* Accessor functions for PGconn objects */
239 extern char *PQdb(const PGconn *conn);
240 extern char *PQuser(const PGconn *conn);
241 extern char *PQpass(const PGconn *conn);
242 extern char *PQhost(const PGconn *conn);
243 extern char *PQport(const PGconn *conn);
244 extern char *PQtty(const PGconn *conn);
245 extern char *PQoptions(const PGconn *conn);
246 extern ConnStatusType PQstatus(const PGconn *conn);
247 extern PGTransactionStatusType PQtransactionStatus(const PGconn *conn);
248 extern const char *PQparameterStatus(const PGconn *conn,
249 const char *paramName);
250 extern int PQprotocolVersion(const PGconn *conn);
251 extern int PQserverVersion(const PGconn *conn);
252 extern char *PQerrorMessage(const PGconn *conn);
253 extern int PQsocket(const PGconn *conn);
254 extern int PQbackendPID(const PGconn *conn);
255 extern int PQclientEncoding(const PGconn *conn);
256 extern int PQsetClientEncoding(PGconn *conn, const char *encoding);
259 /* Get the SSL structure associated with a connection */
260 extern SSL *PQgetssl(PGconn *conn);
263 /* Set verbosity for PQerrorMessage and PQresultErrorMessage */
264 extern PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity);
266 /* Enable/disable tracing */
267 extern void PQtrace(PGconn *conn, FILE *debug_port);
268 extern void PQuntrace(PGconn *conn);
270 /* Override default notice handling routines */
271 extern PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn,
272 PQnoticeReceiver proc,
274 extern PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
275 PQnoticeProcessor proc,
279 * Used to set callback that prevents concurrent access to
280 * non-thread safe functions that libpq needs.
281 * The default implementation uses a libpq internal mutex.
282 * Only required for multithreaded apps that use kerberos
283 * both within their app and for postgresql connections.
285 typedef void (pgthreadlock_t)(int acquire);
287 extern pgthreadlock_t * PQregisterThreadLock(pgthreadlock_t *newhandler);
289 extern void PQinitSSL(int do_init);
291 /* === in fe-exec.c === */
293 /* Simple synchronous query */
294 extern PGresult *PQexec(PGconn *conn, const char *query);
295 extern PGresult *PQexecParams(PGconn *conn,
298 const Oid *paramTypes,
299 const char *const * paramValues,
300 const int *paramLengths,
301 const int *paramFormats,
303 extern PGresult *PQexecPrepared(PGconn *conn,
304 const char *stmtName,
306 const char *const * paramValues,
307 const int *paramLengths,
308 const int *paramFormats,
311 /* Interface for multiple-result or asynchronous queries */
312 extern int PQsendQuery(PGconn *conn, const char *query);
313 extern int PQsendQueryParams(PGconn *conn,
316 const Oid *paramTypes,
317 const char *const * paramValues,
318 const int *paramLengths,
319 const int *paramFormats,
321 extern int PQsendQueryPrepared(PGconn *conn,
322 const char *stmtName,
324 const char *const * paramValues,
325 const int *paramLengths,
326 const int *paramFormats,
328 extern PGresult *PQgetResult(PGconn *conn);
330 /* Routines for managing an asynchronous query */
331 extern int PQisBusy(PGconn *conn);
332 extern int PQconsumeInput(PGconn *conn);
334 /* LISTEN/NOTIFY support */
335 extern PGnotify *PQnotifies(PGconn *conn);
337 /* Routines for copy in/out */
338 extern int PQputCopyData(PGconn *conn, const char *buffer, int nbytes);
339 extern int PQputCopyEnd(PGconn *conn, const char *errormsg);
340 extern int PQgetCopyData(PGconn *conn, char **buffer, int async);
342 /* Deprecated routines for copy in/out */
343 extern int PQgetline(PGconn *conn, char *string, int length);
344 extern int PQputline(PGconn *conn, const char *string);
345 extern int PQgetlineAsync(PGconn *conn, char *buffer, int bufsize);
346 extern int PQputnbytes(PGconn *conn, const char *buffer, int nbytes);
347 extern int PQendcopy(PGconn *conn);
349 /* Set blocking/nonblocking connection to the backend */
350 extern int PQsetnonblocking(PGconn *conn, int arg);
351 extern int PQisnonblocking(const PGconn *conn);
353 /* Force the write buffer to be written (or at least try) */
354 extern int PQflush(PGconn *conn);
357 * "Fast path" interface --- not really recommended for application
360 extern PGresult *PQfn(PGconn *conn,
365 const PQArgBlock *args,
368 /* Accessor functions for PGresult objects */
369 extern ExecStatusType PQresultStatus(const PGresult *res);
370 extern char *PQresStatus(ExecStatusType status);
371 extern char *PQresultErrorMessage(const PGresult *res);
372 extern char *PQresultErrorField(const PGresult *res, int fieldcode);
373 extern int PQntuples(const PGresult *res);
374 extern int PQnfields(const PGresult *res);
375 extern int PQbinaryTuples(const PGresult *res);
376 extern char *PQfname(const PGresult *res, int field_num);
377 extern int PQfnumber(const PGresult *res, const char *field_name);
378 extern Oid PQftable(const PGresult *res, int field_num);
379 extern int PQftablecol(const PGresult *res, int field_num);
380 extern int PQfformat(const PGresult *res, int field_num);
381 extern Oid PQftype(const PGresult *res, int field_num);
382 extern int PQfsize(const PGresult *res, int field_num);
383 extern int PQfmod(const PGresult *res, int field_num);
384 extern char *PQcmdStatus(PGresult *res);
385 extern char *PQoidStatus(const PGresult *res); /* old and ugly */
386 extern Oid PQoidValue(const PGresult *res); /* new and improved */
387 extern char *PQcmdTuples(PGresult *res);
388 extern char *PQgetvalue(const PGresult *res, int tup_num, int field_num);
389 extern int PQgetlength(const PGresult *res, int tup_num, int field_num);
390 extern int PQgetisnull(const PGresult *res, int tup_num, int field_num);
392 /* Delete a PGresult */
393 extern void PQclear(PGresult *res);
395 /* For freeing other alloc'd results, such as PGnotify structs */
396 extern void PQfreemem(void *ptr);
398 /* Exists for backward compatibility. bjm 2003-03-24 */
399 #define PQfreeNotify(ptr) PQfreemem(ptr)
402 * Make an empty PGresult with given status (some apps find this
403 * useful). If conn is not NULL and status indicates an error, the
404 * conn's errorMessage is copied.
406 extern PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
409 /* Quoting strings before inclusion in queries. */
410 extern size_t PQescapeString(char *to, const char *from, size_t length);
411 extern unsigned char *PQescapeBytea(const unsigned char *bintext, size_t binlen,
413 extern unsigned char *PQunescapeBytea(const unsigned char *strtext,
418 /* === in fe-print.c === */
420 extern void PQprint(FILE *fout, /* output stream */
422 const PQprintOpt *ps); /* option structure */
425 * really old printing routines
427 extern void PQdisplayTuples(const PGresult *res,
428 FILE *fp, /* where to send the output */
429 int fillAlign, /* pad the fields with spaces */
430 const char *fieldSep, /* field separator */
431 int printHeader, /* display headers? */
434 extern void PQprintTuples(const PGresult *res,
435 FILE *fout, /* output stream */
436 int printAttName, /* print attribute names */
437 int terseOutput, /* delimiter bars */
438 int width); /* width of column, if 0, use variable
442 /* === in fe-lobj.c === */
444 /* Large-object access routines */
445 extern int lo_open(PGconn *conn, Oid lobjId, int mode);
446 extern int lo_close(PGconn *conn, int fd);
447 extern int lo_read(PGconn *conn, int fd, char *buf, size_t len);
448 extern int lo_write(PGconn *conn, int fd, char *buf, size_t len);
449 extern int lo_lseek(PGconn *conn, int fd, int offset, int whence);
450 extern Oid lo_creat(PGconn *conn, int mode);
451 extern int lo_tell(PGconn *conn, int fd);
452 extern int lo_unlink(PGconn *conn, Oid lobjId);
453 extern Oid lo_import(PGconn *conn, const char *filename);
454 extern int lo_export(PGconn *conn, Oid lobjId, const char *filename);
456 /* === in fe-misc.c === */
458 /* Determine length of multibyte encoded char at *s */
459 extern int PQmblen(const unsigned char *s, int encoding);
461 /* Determine display length of multibyte encoded char at *s */
462 extern int PQdsplen(const unsigned char *s, int encoding);
464 /* Get encoding id from environment variable PGCLIENTENCODING */
465 extern int PQenv2encoding(void);
467 /* === in fe-secure.c === */
470 * Indicates whether the libpq thread is in send().
471 * Used to ignore SIGPIPE if thread is in send().
473 extern pqbool PQinSend(void);
479 #endif /* LIBPQ_FE_H */