1 /*-------------------------------------------------------------------------
4 * This file contains definitions for structures and
5 * externs for functions used by frontend postgres applications.
7 * Portions Copyright (c) 1996-2006, 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.129 2006/05/23 22:13:19 momjian Exp $
12 *-------------------------------------------------------------------------
26 * postgres_ext.h defines the backend's externally visible types,
29 #include "postgres_ext.h"
31 /* Application-visible enum types */
36 * Although it is okay to add to this list, values which become unused
37 * should never be removed, nor should constants be redefined - that would
38 * break compatibility with existing code.
42 /* Non-blocking mode only below here */
45 * The existence of these should never be relied upon - they should only
46 * be used for user feedback or similar purposes.
48 CONNECTION_STARTED, /* Waiting for connection to be made. */
49 CONNECTION_MADE, /* Connection OK; waiting to send. */
50 CONNECTION_AWAITING_RESPONSE, /* Waiting for a response from the
52 CONNECTION_AUTH_OK, /* Received authentication; waiting for
54 CONNECTION_SETENV, /* Negotiating environment. */
55 CONNECTION_SSL_STARTUP, /* Negotiating SSL. */
56 CONNECTION_NEEDED /* Internal state: connect() needed */
61 PGRES_POLLING_FAILED = 0,
62 PGRES_POLLING_READING, /* These two indicate that one may */
63 PGRES_POLLING_WRITING, /* use select before polling again. */
65 PGRES_POLLING_ACTIVE /* unused; keep for awhile for backwards
67 } PostgresPollingStatusType;
71 PGRES_EMPTY_QUERY = 0, /* empty query string was executed */
72 PGRES_COMMAND_OK, /* a query command that doesn't return
73 * anything was executed properly by the
75 PGRES_TUPLES_OK, /* a query command that returns tuples was
76 * executed properly by the backend, PGresult
77 * contains the result tuples */
78 PGRES_COPY_OUT, /* Copy Out data transfer in progress */
79 PGRES_COPY_IN, /* Copy In data transfer in progress */
80 PGRES_BAD_RESPONSE, /* an unexpected response was recv'd from the
82 PGRES_NONFATAL_ERROR, /* notice or warning message */
83 PGRES_FATAL_ERROR /* query failed */
88 PQTRANS_IDLE, /* connection idle */
89 PQTRANS_ACTIVE, /* command in progress */
90 PQTRANS_INTRANS, /* idle, within transaction block */
91 PQTRANS_INERROR, /* idle, within failed transaction */
92 PQTRANS_UNKNOWN /* cannot determine status */
93 } PGTransactionStatusType;
97 PQERRORS_TERSE, /* single-line error messages */
98 PQERRORS_DEFAULT, /* recommended style */
99 PQERRORS_VERBOSE /* all the facts, ma'am */
102 /* PGconn encapsulates a connection to the backend.
103 * The contents of this struct are not supposed to be known to applications.
105 typedef struct pg_conn PGconn;
107 /* PGresult encapsulates the result of a query (or more precisely, of a single
108 * SQL command --- a query string given to PQsendQuery can contain multiple
109 * commands and thus return multiple PGresult objects).
110 * The contents of this struct are not supposed to be known to applications.
112 typedef struct pg_result PGresult;
114 /* PGcancel encapsulates the information needed to cancel a running
115 * query on an existing connection.
116 * The contents of this struct are not supposed to be known to applications.
118 typedef struct pg_cancel PGcancel;
120 /* PGnotify represents the occurrence of a NOTIFY message.
121 * Ideally this would be an opaque typedef, but it's so simple that it's
122 * unlikely to change.
123 * NOTE: in Postgres 6.4 and later, the be_pid is the notifying backend's,
124 * whereas in earlier versions it was always your own backend's PID.
126 typedef struct pgNotify
128 char *relname; /* notification condition name */
129 int be_pid; /* process ID of notifying server process */
130 char *extra; /* notification parameter */
131 /* Fields below here are private to libpq; apps should not use 'em */
132 struct pgNotify *next; /* list link */
135 /* Function types for notice-handling callbacks */
136 typedef void (*PQnoticeReceiver) (void *arg, const PGresult *res);
137 typedef void (*PQnoticeProcessor) (void *arg, const char *message);
139 /* Print options for PQprint() */
142 typedef struct _PQprintOpt
144 pqbool header; /* print output field headings and row count */
145 pqbool align; /* fill align the fields */
146 pqbool standard; /* old brain dead format */
147 pqbool html3; /* output html tables */
148 pqbool expanded; /* expand tables */
149 pqbool pager; /* use pager for output if needed */
150 char *fieldSep; /* field separator */
151 char *tableOpt; /* insert to HTML <table ...> */
152 char *caption; /* HTML <caption> */
153 char **fieldName; /* null terminated array of replacement field
158 * Structure for the conninfo parameter definitions returned by PQconndefaults
160 * All fields except "val" point at static strings which must not be altered.
161 * "val" is either NULL or a malloc'd current-value string. PQconninfoFree()
162 * will release both the val strings and the PQconninfoOption array itself.
165 typedef struct _PQconninfoOption
167 char *keyword; /* The keyword of the option */
168 char *envvar; /* Fallback environment variable name */
169 char *compiled; /* Fallback compiled in default value */
170 char *val; /* Option's current value, or NULL */
171 char *label; /* Label for field in connect dialog */
172 char *dispchar; /* Character to display for this field in a
173 * connect dialog. Values are: "" Display
174 * entered value as is "*" Password field -
175 * hide value "D" Debug option - don't show
177 int dispsize; /* Field size in characters for dialog */
181 * PQArgBlock -- structure for PQfn() arguments
190 int *ptr; /* can't use void (dec compiler barfs) */
196 * Exported functions of libpq
200 /* === in fe-connect.c === */
202 /* make a new client connection to the backend */
203 /* Asynchronous (non-blocking) */
204 extern PGconn *PQconnectStart(const char *conninfo);
205 extern PostgresPollingStatusType PQconnectPoll(PGconn *conn);
207 /* Synchronous (blocking) */
208 extern PGconn *PQconnectdb(const char *conninfo);
209 extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport,
210 const char *pgoptions, const char *pgtty,
212 const char *login, const char *pwd);
214 #define PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME) \
215 PQsetdbLogin(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME, NULL, NULL)
217 /* close the current connection and free the PGconn data structure */
218 extern void PQfinish(PGconn *conn);
220 /* get info about connection options known to PQconnectdb */
221 extern PQconninfoOption *PQconndefaults(void);
223 /* free the data structure returned by PQconndefaults() */
224 extern void PQconninfoFree(PQconninfoOption *connOptions);
227 * close the current connection and restablish a new one with the same
230 /* Asynchronous (non-blocking) */
231 extern int PQresetStart(PGconn *conn);
232 extern PostgresPollingStatusType PQresetPoll(PGconn *conn);
234 /* Synchronous (blocking) */
235 extern void PQreset(PGconn *conn);
237 /* request a cancel structure */
238 extern PGcancel *PQgetCancel(PGconn *conn);
240 /* free a cancel structure */
241 extern void PQfreeCancel(PGcancel *cancel);
243 /* issue a cancel request */
244 extern int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize);
246 /* backwards compatible version of PQcancel; not thread-safe */
247 extern int PQrequestCancel(PGconn *conn);
249 /* Accessor functions for PGconn objects */
250 extern char *PQdb(const PGconn *conn);
251 extern char *PQuser(const PGconn *conn);
252 extern char *PQpass(const PGconn *conn);
253 extern char *PQhost(const PGconn *conn);
254 extern char *PQport(const PGconn *conn);
255 extern char *PQtty(const PGconn *conn);
256 extern char *PQoptions(const PGconn *conn);
257 extern ConnStatusType PQstatus(const PGconn *conn);
258 extern PGTransactionStatusType PQtransactionStatus(const PGconn *conn);
259 extern const char *PQparameterStatus(const PGconn *conn,
260 const char *paramName);
261 extern int PQprotocolVersion(const PGconn *conn);
262 extern int PQserverVersion(const PGconn *conn);
263 extern char *PQerrorMessage(const PGconn *conn);
264 extern int PQsocket(const PGconn *conn);
265 extern int PQbackendPID(const PGconn *conn);
266 extern int PQclientEncoding(const PGconn *conn);
267 extern int PQsetClientEncoding(PGconn *conn, const char *encoding);
269 /* Get the OpenSSL structure associated with a connection. Returns NULL for
270 * unencrypted connections or if any other TLS library is in use. */
271 extern void *PQgetssl(PGconn *conn);
273 /* Tell libpq whether it needs to initialize OpenSSL */
274 extern void PQinitSSL(int do_init);
276 /* Set verbosity for PQerrorMessage and PQresultErrorMessage */
277 extern PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity);
279 /* Enable/disable tracing */
280 extern void PQtrace(PGconn *conn, FILE *debug_port);
281 extern void PQuntrace(PGconn *conn);
283 /* Override default notice handling routines */
284 extern PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn,
285 PQnoticeReceiver proc,
287 extern PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
288 PQnoticeProcessor proc,
292 * Used to set callback that prevents concurrent access to
293 * non-thread safe functions that libpq needs.
294 * The default implementation uses a libpq internal mutex.
295 * Only required for multithreaded apps that use kerberos
296 * both within their app and for postgresql connections.
298 typedef void (*pgthreadlock_t) (int acquire);
300 extern pgthreadlock_t PQregisterThreadLock(pgthreadlock_t newhandler);
302 /* === in fe-exec.c === */
304 /* Simple synchronous query */
305 extern PGresult *PQexec(PGconn *conn, const char *query);
306 extern PGresult *PQexecParams(PGconn *conn,
309 const Oid *paramTypes,
310 const char *const * paramValues,
311 const int *paramLengths,
312 const int *paramFormats,
314 extern PGresult *PQprepare(PGconn *conn, const char *stmtName,
315 const char *query, int nParams,
316 const Oid *paramTypes);
317 extern PGresult *PQexecPrepared(PGconn *conn,
318 const char *stmtName,
320 const char *const * paramValues,
321 const int *paramLengths,
322 const int *paramFormats,
325 /* Interface for multiple-result or asynchronous queries */
326 extern int PQsendQuery(PGconn *conn, const char *query);
327 extern int PQsendQueryParams(PGconn *conn,
330 const Oid *paramTypes,
331 const char *const * paramValues,
332 const int *paramLengths,
333 const int *paramFormats,
335 extern int PQsendPrepare(PGconn *conn, const char *stmtName,
336 const char *query, int nParams,
337 const Oid *paramTypes);
338 extern int PQsendQueryPrepared(PGconn *conn,
339 const char *stmtName,
341 const char *const * paramValues,
342 const int *paramLengths,
343 const int *paramFormats,
345 extern PGresult *PQgetResult(PGconn *conn);
347 /* Routines for managing an asynchronous query */
348 extern int PQisBusy(PGconn *conn);
349 extern int PQconsumeInput(PGconn *conn);
351 /* LISTEN/NOTIFY support */
352 extern PGnotify *PQnotifies(PGconn *conn);
354 /* Routines for copy in/out */
355 extern int PQputCopyData(PGconn *conn, const char *buffer, int nbytes);
356 extern int PQputCopyEnd(PGconn *conn, const char *errormsg);
357 extern int PQgetCopyData(PGconn *conn, char **buffer, int async);
359 /* Deprecated routines for copy in/out */
360 extern int PQgetline(PGconn *conn, char *string, int length);
361 extern int PQputline(PGconn *conn, const char *string);
362 extern int PQgetlineAsync(PGconn *conn, char *buffer, int bufsize);
363 extern int PQputnbytes(PGconn *conn, const char *buffer, int nbytes);
364 extern int PQendcopy(PGconn *conn);
366 /* Set blocking/nonblocking connection to the backend */
367 extern int PQsetnonblocking(PGconn *conn, int arg);
368 extern int PQisnonblocking(const PGconn *conn);
369 extern int PQisthreadsafe(void);
371 /* Force the write buffer to be written (or at least try) */
372 extern int PQflush(PGconn *conn);
375 * "Fast path" interface --- not really recommended for application
378 extern PGresult *PQfn(PGconn *conn,
383 const PQArgBlock *args,
386 /* Accessor functions for PGresult objects */
387 extern ExecStatusType PQresultStatus(const PGresult *res);
388 extern char *PQresStatus(ExecStatusType status);
389 extern char *PQresultErrorMessage(const PGresult *res);
390 extern char *PQresultErrorField(const PGresult *res, int fieldcode);
391 extern int PQntuples(const PGresult *res);
392 extern int PQnfields(const PGresult *res);
393 extern int PQbinaryTuples(const PGresult *res);
394 extern char *PQfname(const PGresult *res, int field_num);
395 extern int PQfnumber(const PGresult *res, const char *field_name);
396 extern Oid PQftable(const PGresult *res, int field_num);
397 extern int PQftablecol(const PGresult *res, int field_num);
398 extern int PQfformat(const PGresult *res, int field_num);
399 extern Oid PQftype(const PGresult *res, int field_num);
400 extern int PQfsize(const PGresult *res, int field_num);
401 extern int PQfmod(const PGresult *res, int field_num);
402 extern char *PQcmdStatus(PGresult *res);
403 extern char *PQoidStatus(const PGresult *res); /* old and ugly */
404 extern Oid PQoidValue(const PGresult *res); /* new and improved */
405 extern char *PQcmdTuples(PGresult *res);
406 extern char *PQgetvalue(const PGresult *res, int tup_num, int field_num);
407 extern int PQgetlength(const PGresult *res, int tup_num, int field_num);
408 extern int PQgetisnull(const PGresult *res, int tup_num, int field_num);
410 /* Delete a PGresult */
411 extern void PQclear(PGresult *res);
413 /* For freeing other alloc'd results, such as PGnotify structs */
414 extern void PQfreemem(void *ptr);
416 /* Exists for backward compatibility. bjm 2003-03-24 */
417 #define PQfreeNotify(ptr) PQfreemem(ptr)
419 /* Define the string so all uses are consistent. */
420 #define PQnoPasswordSupplied "fe_sendauth: no password supplied\n"
423 * Make an empty PGresult with given status (some apps find this
424 * useful). If conn is not NULL and status indicates an error, the
425 * conn's errorMessage is copied.
427 extern PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
430 /* Quoting strings before inclusion in queries. */
431 extern size_t PQescapeStringConn(PGconn *conn,
432 char *to, const char *from, size_t length,
434 extern unsigned char *PQescapeByteaConn(PGconn *conn,
435 const unsigned char *from, size_t from_length,
437 extern unsigned char *PQunescapeBytea(const unsigned char *strtext,
439 /* These forms are deprecated! */
440 extern size_t PQescapeString(char *to, const char *from, size_t length);
441 extern unsigned char *PQescapeBytea(const unsigned char *from, size_t from_length,
446 /* === in fe-print.c === */
449 PQprint(FILE *fout, /* output stream */
451 const PQprintOpt *ps); /* option structure */
454 * really old printing routines
457 PQdisplayTuples(const PGresult *res,
458 FILE *fp, /* where to send the output */
459 int fillAlign, /* pad the fields with spaces */
460 const char *fieldSep, /* field separator */
461 int printHeader, /* display headers? */
465 PQprintTuples(const PGresult *res,
466 FILE *fout, /* output stream */
467 int printAttName, /* print attribute names */
468 int terseOutput, /* delimiter bars */
469 int width); /* width of column, if 0, use variable width */
472 /* === in fe-lobj.c === */
474 /* Large-object access routines */
475 extern int lo_open(PGconn *conn, Oid lobjId, int mode);
476 extern int lo_close(PGconn *conn, int fd);
477 extern int lo_read(PGconn *conn, int fd, char *buf, size_t len);
478 extern int lo_write(PGconn *conn, int fd, char *buf, size_t len);
479 extern int lo_lseek(PGconn *conn, int fd, int offset, int whence);
480 extern Oid lo_creat(PGconn *conn, int mode);
481 extern Oid lo_create(PGconn *conn, Oid lobjId);
482 extern int lo_tell(PGconn *conn, int fd);
483 extern int lo_unlink(PGconn *conn, Oid lobjId);
484 extern Oid lo_import(PGconn *conn, const char *filename);
485 extern int lo_export(PGconn *conn, Oid lobjId, const char *filename);
487 /* === in fe-misc.c === */
489 /* Determine length of multibyte encoded char at *s */
490 extern int PQmblen(const char *s, int encoding);
492 /* Determine display length of multibyte encoded char at *s */
493 extern int PQdsplen(const char *s, int encoding);
495 /* Get encoding id from environment variable PGCLIENTENCODING */
496 extern int PQenv2encoding(void);
498 /* === in fe-auth.c === */
500 extern char *PQencryptPassword(const char *passwd, const char *user);
506 #endif /* LIBPQ_FE_H */