1 /*-------------------------------------------------------------------------
4 * This file contains definitions for structures and
5 * externs for functions used by frontend postgres applications.
7 * Portions Copyright (c) 1996-2009, 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.147 2009/06/11 14:49:14 momjian Exp $
12 *-------------------------------------------------------------------------
26 * postgres_ext.h defines the backend's externally visible types,
29 #include "postgres_ext.h"
32 * Option flags for PQcopyResult
34 #define PG_COPYRES_ATTRS 0x01
35 #define PG_COPYRES_TUPLES 0x02 /* Implies PG_COPYRES_ATTRS */
36 #define PG_COPYRES_EVENTS 0x04
37 #define PG_COPYRES_NOTICEHOOKS 0x08
39 /* Application-visible enum types */
44 * Although it is okay to add to this list, values which become unused
45 * should never be removed, nor should constants be redefined - that would
46 * break compatibility with existing code.
50 /* Non-blocking mode only below here */
53 * The existence of these should never be relied upon - they should only
54 * be used for user feedback or similar purposes.
56 CONNECTION_STARTED, /* Waiting for connection to be made. */
57 CONNECTION_MADE, /* Connection OK; waiting to send. */
58 CONNECTION_AWAITING_RESPONSE, /* Waiting for a response from the
60 CONNECTION_AUTH_OK, /* Received authentication; waiting for
62 CONNECTION_SETENV, /* Negotiating environment. */
63 CONNECTION_SSL_STARTUP, /* Negotiating SSL. */
64 CONNECTION_NEEDED /* Internal state: connect() needed */
69 PGRES_POLLING_FAILED = 0,
70 PGRES_POLLING_READING, /* These two indicate that one may */
71 PGRES_POLLING_WRITING, /* use select before polling again. */
73 PGRES_POLLING_ACTIVE /* unused; keep for awhile for backwards
75 } PostgresPollingStatusType;
79 PGRES_EMPTY_QUERY = 0, /* empty query string was executed */
80 PGRES_COMMAND_OK, /* a query command that doesn't return
81 * anything was executed properly by the
83 PGRES_TUPLES_OK, /* a query command that returns tuples was
84 * executed properly by the backend, PGresult
85 * contains the result tuples */
86 PGRES_COPY_OUT, /* Copy Out data transfer in progress */
87 PGRES_COPY_IN, /* Copy In data transfer in progress */
88 PGRES_BAD_RESPONSE, /* an unexpected response was recv'd from the
90 PGRES_NONFATAL_ERROR, /* notice or warning message */
91 PGRES_FATAL_ERROR /* query failed */
96 PQTRANS_IDLE, /* connection idle */
97 PQTRANS_ACTIVE, /* command in progress */
98 PQTRANS_INTRANS, /* idle, within transaction block */
99 PQTRANS_INERROR, /* idle, within failed transaction */
100 PQTRANS_UNKNOWN /* cannot determine status */
101 } PGTransactionStatusType;
105 PQERRORS_TERSE, /* single-line error messages */
106 PQERRORS_DEFAULT, /* recommended style */
107 PQERRORS_VERBOSE /* all the facts, ma'am */
110 /* PGconn encapsulates a connection to the backend.
111 * The contents of this struct are not supposed to be known to applications.
113 typedef struct pg_conn PGconn;
115 /* PGresult encapsulates the result of a query (or more precisely, of a single
116 * SQL command --- a query string given to PQsendQuery can contain multiple
117 * commands and thus return multiple PGresult objects).
118 * The contents of this struct are not supposed to be known to applications.
120 typedef struct pg_result PGresult;
122 /* PGcancel encapsulates the information needed to cancel a running
123 * query on an existing connection.
124 * The contents of this struct are not supposed to be known to applications.
126 typedef struct pg_cancel PGcancel;
128 /* PGnotify represents the occurrence of a NOTIFY message.
129 * Ideally this would be an opaque typedef, but it's so simple that it's
130 * unlikely to change.
131 * NOTE: in Postgres 6.4 and later, the be_pid is the notifying backend's,
132 * whereas in earlier versions it was always your own backend's PID.
134 typedef struct pgNotify
136 char *relname; /* notification condition name */
137 int be_pid; /* process ID of notifying server process */
138 char *extra; /* notification parameter */
139 /* Fields below here are private to libpq; apps should not use 'em */
140 struct pgNotify *next; /* list link */
143 /* Function types for notice-handling callbacks */
144 typedef void (*PQnoticeReceiver) (void *arg, const PGresult *res);
145 typedef void (*PQnoticeProcessor) (void *arg, const char *message);
147 /* Print options for PQprint() */
150 typedef struct _PQprintOpt
152 pqbool header; /* print output field headings and row count */
153 pqbool align; /* fill align the fields */
154 pqbool standard; /* old brain dead format */
155 pqbool html3; /* output html tables */
156 pqbool expanded; /* expand tables */
157 pqbool pager; /* use pager for output if needed */
158 char *fieldSep; /* field separator */
159 char *tableOpt; /* insert to HTML <table ...> */
160 char *caption; /* HTML <caption> */
161 char **fieldName; /* null terminated array of replacement field
166 * Structure for the conninfo parameter definitions returned by PQconndefaults
167 * or PQconninfoParse.
169 * All fields except "val" point at static strings which must not be altered.
170 * "val" is either NULL or a malloc'd current-value string. PQconninfoFree()
171 * will release both the val strings and the PQconninfoOption array itself.
174 typedef struct _PQconninfoOption
176 char *keyword; /* The keyword of the option */
177 char *envvar; /* Fallback environment variable name */
178 char *compiled; /* Fallback compiled in default value */
179 char *val; /* Option's current value, or NULL */
180 char *label; /* Label for field in connect dialog */
181 char *dispchar; /* Indicates how to display this field in a
182 * connect dialog. Values are: "" Display
183 * entered value as is "*" Password field -
184 * hide value "D" Debug option - don't show
186 int dispsize; /* Field size in characters for dialog */
190 * PQArgBlock -- structure for PQfn() arguments
199 int *ptr; /* can't use void (dec compiler barfs) */
205 * PGresAttDesc -- Data about a single attribute (column) of a query result
208 typedef struct pgresAttDesc
210 char *name; /* column name */
211 Oid tableid; /* source table, if known */
212 int columnid; /* source column, if known */
213 int format; /* format code for value (text/binary) */
214 Oid typid; /* type id */
215 int typlen; /* type size */
216 int atttypmod; /* type-specific modifier info */
220 * Exported functions of libpq
224 /* === in fe-connect.c === */
226 /* make a new client connection to the backend */
227 /* Asynchronous (non-blocking) */
228 extern PGconn *PQconnectStart(const char *conninfo);
229 extern PostgresPollingStatusType PQconnectPoll(PGconn *conn);
231 /* Synchronous (blocking) */
232 extern PGconn *PQconnectdb(const char *conninfo);
233 extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport,
234 const char *pgoptions, const char *pgtty,
236 const char *login, const char *pwd);
238 #define PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME) \
239 PQsetdbLogin(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME, NULL, NULL)
241 /* close the current connection and free the PGconn data structure */
242 extern void PQfinish(PGconn *conn);
244 /* get info about connection options known to PQconnectdb */
245 extern PQconninfoOption *PQconndefaults(void);
247 /* parse connection options in same way as PQconnectdb */
248 extern PQconninfoOption *PQconninfoParse(const char *conninfo, char **errmsg);
250 /* free the data structure returned by PQconndefaults() or PQconninfoParse() */
251 extern void PQconninfoFree(PQconninfoOption *connOptions);
254 * close the current connection and restablish a new one with the same
257 /* Asynchronous (non-blocking) */
258 extern int PQresetStart(PGconn *conn);
259 extern PostgresPollingStatusType PQresetPoll(PGconn *conn);
261 /* Synchronous (blocking) */
262 extern void PQreset(PGconn *conn);
264 /* request a cancel structure */
265 extern PGcancel *PQgetCancel(PGconn *conn);
267 /* free a cancel structure */
268 extern void PQfreeCancel(PGcancel *cancel);
270 /* issue a cancel request */
271 extern int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize);
273 /* backwards compatible version of PQcancel; not thread-safe */
274 extern int PQrequestCancel(PGconn *conn);
276 /* Accessor functions for PGconn objects */
277 extern char *PQdb(const PGconn *conn);
278 extern char *PQuser(const PGconn *conn);
279 extern char *PQpass(const PGconn *conn);
280 extern char *PQhost(const PGconn *conn);
281 extern char *PQport(const PGconn *conn);
282 extern char *PQtty(const PGconn *conn);
283 extern char *PQoptions(const PGconn *conn);
284 extern ConnStatusType PQstatus(const PGconn *conn);
285 extern PGTransactionStatusType PQtransactionStatus(const PGconn *conn);
286 extern const char *PQparameterStatus(const PGconn *conn,
287 const char *paramName);
288 extern int PQprotocolVersion(const PGconn *conn);
289 extern int PQserverVersion(const PGconn *conn);
290 extern char *PQerrorMessage(const PGconn *conn);
291 extern int PQsocket(const PGconn *conn);
292 extern int PQbackendPID(const PGconn *conn);
293 extern int PQconnectionNeedsPassword(const PGconn *conn);
294 extern int PQconnectionUsedPassword(const PGconn *conn);
295 extern int PQclientEncoding(const PGconn *conn);
296 extern int PQsetClientEncoding(PGconn *conn, const char *encoding);
298 /* Get the OpenSSL structure associated with a connection. Returns NULL for
299 * unencrypted connections or if any other TLS library is in use. */
300 extern void *PQgetssl(PGconn *conn);
302 /* Tell libpq whether it needs to initialize OpenSSL */
303 extern void PQinitSSL(int do_init);
305 /* More detailed way to tell libpq whether it needs to initialize OpenSSL */
306 extern void PQinitOpenSSL(int do_ssl, int do_crypto);
308 /* Set verbosity for PQerrorMessage and PQresultErrorMessage */
309 extern PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity);
311 /* Enable/disable tracing */
312 extern void PQtrace(PGconn *conn, FILE *debug_port);
313 extern void PQuntrace(PGconn *conn);
315 /* Override default notice handling routines */
316 extern PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn,
317 PQnoticeReceiver proc,
319 extern PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
320 PQnoticeProcessor proc,
324 * Used to set callback that prevents concurrent access to
325 * non-thread safe functions that libpq needs.
326 * The default implementation uses a libpq internal mutex.
327 * Only required for multithreaded apps that use kerberos
328 * both within their app and for postgresql connections.
330 typedef void (*pgthreadlock_t) (int acquire);
332 extern pgthreadlock_t PQregisterThreadLock(pgthreadlock_t newhandler);
334 /* === in fe-exec.c === */
336 /* Simple synchronous query */
337 extern PGresult *PQexec(PGconn *conn, const char *query);
338 extern PGresult *PQexecParams(PGconn *conn,
341 const Oid *paramTypes,
342 const char *const * paramValues,
343 const int *paramLengths,
344 const int *paramFormats,
346 extern PGresult *PQprepare(PGconn *conn, const char *stmtName,
347 const char *query, int nParams,
348 const Oid *paramTypes);
349 extern PGresult *PQexecPrepared(PGconn *conn,
350 const char *stmtName,
352 const char *const * paramValues,
353 const int *paramLengths,
354 const int *paramFormats,
357 /* Interface for multiple-result or asynchronous queries */
358 extern int PQsendQuery(PGconn *conn, const char *query);
359 extern int PQsendQueryParams(PGconn *conn,
362 const Oid *paramTypes,
363 const char *const * paramValues,
364 const int *paramLengths,
365 const int *paramFormats,
367 extern int PQsendPrepare(PGconn *conn, const char *stmtName,
368 const char *query, int nParams,
369 const Oid *paramTypes);
370 extern int PQsendQueryPrepared(PGconn *conn,
371 const char *stmtName,
373 const char *const * paramValues,
374 const int *paramLengths,
375 const int *paramFormats,
377 extern PGresult *PQgetResult(PGconn *conn);
379 /* Routines for managing an asynchronous query */
380 extern int PQisBusy(PGconn *conn);
381 extern int PQconsumeInput(PGconn *conn);
383 /* LISTEN/NOTIFY support */
384 extern PGnotify *PQnotifies(PGconn *conn);
386 /* Routines for copy in/out */
387 extern int PQputCopyData(PGconn *conn, const char *buffer, int nbytes);
388 extern int PQputCopyEnd(PGconn *conn, const char *errormsg);
389 extern int PQgetCopyData(PGconn *conn, char **buffer, int async);
391 /* Deprecated routines for copy in/out */
392 extern int PQgetline(PGconn *conn, char *string, int length);
393 extern int PQputline(PGconn *conn, const char *string);
394 extern int PQgetlineAsync(PGconn *conn, char *buffer, int bufsize);
395 extern int PQputnbytes(PGconn *conn, const char *buffer, int nbytes);
396 extern int PQendcopy(PGconn *conn);
398 /* Set blocking/nonblocking connection to the backend */
399 extern int PQsetnonblocking(PGconn *conn, int arg);
400 extern int PQisnonblocking(const PGconn *conn);
401 extern int PQisthreadsafe(void);
403 /* Force the write buffer to be written (or at least try) */
404 extern int PQflush(PGconn *conn);
407 * "Fast path" interface --- not really recommended for application
410 extern PGresult *PQfn(PGconn *conn,
415 const PQArgBlock *args,
418 /* Accessor functions for PGresult objects */
419 extern ExecStatusType PQresultStatus(const PGresult *res);
420 extern char *PQresStatus(ExecStatusType status);
421 extern char *PQresultErrorMessage(const PGresult *res);
422 extern char *PQresultErrorField(const PGresult *res, int fieldcode);
423 extern int PQntuples(const PGresult *res);
424 extern int PQnfields(const PGresult *res);
425 extern int PQbinaryTuples(const PGresult *res);
426 extern char *PQfname(const PGresult *res, int field_num);
427 extern int PQfnumber(const PGresult *res, const char *field_name);
428 extern Oid PQftable(const PGresult *res, int field_num);
429 extern int PQftablecol(const PGresult *res, int field_num);
430 extern int PQfformat(const PGresult *res, int field_num);
431 extern Oid PQftype(const PGresult *res, int field_num);
432 extern int PQfsize(const PGresult *res, int field_num);
433 extern int PQfmod(const PGresult *res, int field_num);
434 extern char *PQcmdStatus(PGresult *res);
435 extern char *PQoidStatus(const PGresult *res); /* old and ugly */
436 extern Oid PQoidValue(const PGresult *res); /* new and improved */
437 extern char *PQcmdTuples(PGresult *res);
438 extern char *PQgetvalue(const PGresult *res, int tup_num, int field_num);
439 extern int PQgetlength(const PGresult *res, int tup_num, int field_num);
440 extern int PQgetisnull(const PGresult *res, int tup_num, int field_num);
441 extern int PQnparams(const PGresult *res);
442 extern Oid PQparamtype(const PGresult *res, int param_num);
444 /* Describe prepared statements and portals */
445 extern PGresult *PQdescribePrepared(PGconn *conn, const char *stmt);
446 extern PGresult *PQdescribePortal(PGconn *conn, const char *portal);
447 extern int PQsendDescribePrepared(PGconn *conn, const char *stmt);
448 extern int PQsendDescribePortal(PGconn *conn, const char *portal);
450 /* Delete a PGresult */
451 extern void PQclear(PGresult *res);
453 /* For freeing other alloc'd results, such as PGnotify structs */
454 extern void PQfreemem(void *ptr);
456 /* Exists for backward compatibility. bjm 2003-03-24 */
457 #define PQfreeNotify(ptr) PQfreemem(ptr)
459 /* Error when no password was given. */
460 /* Note: depending on this is deprecated; use PQconnectionNeedsPassword(). */
461 #define PQnoPasswordSupplied "fe_sendauth: no password supplied\n"
463 /* Create and manipulate PGresults */
464 extern PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
465 extern PGresult *PQcopyResult(const PGresult *src, int flags);
466 extern int PQsetResultAttrs(PGresult *res, int numAttributes, PGresAttDesc *attDescs);
467 extern void *PQresultAlloc(PGresult *res, size_t nBytes);
468 extern int PQsetvalue(PGresult *res, int tup_num, int field_num, char *value, int len);
470 /* Quoting strings before inclusion in queries. */
471 extern size_t PQescapeStringConn(PGconn *conn,
472 char *to, const char *from, size_t length,
474 extern unsigned char *PQescapeByteaConn(PGconn *conn,
475 const unsigned char *from, size_t from_length,
477 extern unsigned char *PQunescapeBytea(const unsigned char *strtext,
480 /* These forms are deprecated! */
481 extern size_t PQescapeString(char *to, const char *from, size_t length);
482 extern unsigned char *PQescapeBytea(const unsigned char *from, size_t from_length,
487 /* === in fe-print.c === */
490 PQprint(FILE *fout, /* output stream */
492 const PQprintOpt *ps); /* option structure */
495 * really old printing routines
498 PQdisplayTuples(const PGresult *res,
499 FILE *fp, /* where to send the output */
500 int fillAlign, /* pad the fields with spaces */
501 const char *fieldSep, /* field separator */
502 int printHeader, /* display headers? */
506 PQprintTuples(const PGresult *res,
507 FILE *fout, /* output stream */
508 int printAttName, /* print attribute names */
509 int terseOutput, /* delimiter bars */
510 int width); /* width of column, if 0, use variable width */
513 /* === in fe-lobj.c === */
515 /* Large-object access routines */
516 extern int lo_open(PGconn *conn, Oid lobjId, int mode);
517 extern int lo_close(PGconn *conn, int fd);
518 extern int lo_read(PGconn *conn, int fd, char *buf, size_t len);
519 extern int lo_write(PGconn *conn, int fd, const char *buf, size_t len);
520 extern int lo_lseek(PGconn *conn, int fd, int offset, int whence);
521 extern Oid lo_creat(PGconn *conn, int mode);
522 extern Oid lo_create(PGconn *conn, Oid lobjId);
523 extern int lo_tell(PGconn *conn, int fd);
524 extern int lo_truncate(PGconn *conn, int fd, size_t len);
525 extern int lo_unlink(PGconn *conn, Oid lobjId);
526 extern Oid lo_import(PGconn *conn, const char *filename);
527 extern Oid lo_import_with_oid(PGconn *conn, const char *filename, Oid lobjId);
528 extern int lo_export(PGconn *conn, Oid lobjId, const char *filename);
530 /* === in fe-misc.c === */
532 /* Determine length of multibyte encoded char at *s */
533 extern int PQmblen(const char *s, int encoding);
535 /* Determine display length of multibyte encoded char at *s */
536 extern int PQdsplen(const char *s, int encoding);
538 /* Get encoding id from environment variable PGCLIENTENCODING */
539 extern int PQenv2encoding(void);
541 /* === in fe-auth.c === */
543 extern char *PQencryptPassword(const char *passwd, const char *user);
545 /* === in encnames.c === */
547 extern int pg_char_to_encoding(const char *name);
548 extern const char *pg_encoding_to_char(int encoding);
549 extern int pg_valid_server_encoding_id(int encoding);
555 #endif /* LIBPQ_FE_H */