1 /*-------------------------------------------------------------------------
4 * This file contains definitions for structures and
5 * externs for functions used by frontend postgres applications.
7 * Portions Copyright (c) 1996-2005, 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.123 2005/12/23 01:16:38 tgl 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 would
43 * break compatibility with existing code.
47 /* Non-blocking mode only below here */
50 * The existence of these should never be relied upon - they should only
51 * 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, PGresult
82 * 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 the
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 /* PGcancel encapsulates the information needed to cancel a running
120 * query on an existing connection.
121 * The contents of this struct are not supposed to be known to applications.
123 typedef struct pg_cancel PGcancel;
125 /* PGnotify represents the occurrence of a NOTIFY message.
126 * Ideally this would be an opaque typedef, but it's so simple that it's
127 * unlikely to change.
128 * NOTE: in Postgres 6.4 and later, the be_pid is the notifying backend's,
129 * whereas in earlier versions it was always your own backend's PID.
131 typedef struct pgNotify
133 char *relname; /* notification condition name */
134 int be_pid; /* process ID of server process */
135 char *extra; /* notification parameter */
136 /* Fields below here are private to libpq; apps should not use 'em */
137 struct pgNotify *next; /* list link */
140 /* Function types for notice-handling callbacks */
141 typedef void (*PQnoticeReceiver) (void *arg, const PGresult *res);
142 typedef void (*PQnoticeProcessor) (void *arg, const char *message);
144 /* Print options for PQprint() */
147 typedef struct _PQprintOpt
149 pqbool header; /* print output field headings and row count */
150 pqbool align; /* fill align the fields */
151 pqbool standard; /* old brain dead format */
152 pqbool html3; /* output html tables */
153 pqbool expanded; /* expand tables */
154 pqbool pager; /* use pager for output if needed */
155 char *fieldSep; /* field separator */
156 char *tableOpt; /* insert to HTML <table ...> */
157 char *caption; /* HTML <caption> */
158 char **fieldName; /* null terminated array of replacement field
163 * Structure for the conninfo parameter definitions returned by PQconndefaults
165 * All fields except "val" point at static strings which must not be altered.
166 * "val" is either NULL or a malloc'd current-value string. PQconninfoFree()
167 * will release both the val strings and the PQconninfoOption array itself.
170 typedef struct _PQconninfoOption
172 char *keyword; /* The keyword of the option */
173 char *envvar; /* Fallback environment variable name */
174 char *compiled; /* Fallback compiled in default value */
175 char *val; /* Option's current value, or NULL */
176 char *label; /* Label for field in connect dialog */
177 char *dispchar; /* Character to display for this field in a
178 * connect dialog. Values are: "" Display
179 * entered value as is "*" Password field -
180 * hide value "D" Debug option - don't show
182 int dispsize; /* Field size in characters for dialog */
186 * PQArgBlock -- structure for PQfn() arguments
195 int *ptr; /* can't use void (dec compiler barfs) */
201 * Exported functions of libpq
205 /* === in fe-connect.c === */
207 /* make a new client connection to the backend */
208 /* Asynchronous (non-blocking) */
209 extern PGconn *PQconnectStart(const char *conninfo);
210 extern PostgresPollingStatusType PQconnectPoll(PGconn *conn);
212 /* Synchronous (blocking) */
213 extern PGconn *PQconnectdb(const char *conninfo);
214 extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport,
215 const char *pgoptions, const char *pgtty,
217 const char *login, const char *pwd);
219 #define PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME) \
220 PQsetdbLogin(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME, NULL, NULL)
222 /* close the current connection and free the PGconn data structure */
223 extern void PQfinish(PGconn *conn);
225 /* get info about connection options known to PQconnectdb */
226 extern PQconninfoOption *PQconndefaults(void);
228 /* free the data structure returned by PQconndefaults() */
229 extern void PQconninfoFree(PQconninfoOption *connOptions);
232 * close the current connection and restablish a new one with the same
235 /* Asynchronous (non-blocking) */
236 extern int PQresetStart(PGconn *conn);
237 extern PostgresPollingStatusType PQresetPoll(PGconn *conn);
239 /* Synchronous (blocking) */
240 extern void PQreset(PGconn *conn);
242 /* request a cancel structure */
243 extern PGcancel *PQgetCancel(PGconn *conn);
245 /* free a cancel structure */
246 extern void PQfreeCancel(PGcancel *cancel);
248 /* issue a cancel request */
249 extern int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize);
251 /* backwards compatible version of PQcancel; not thread-safe */
252 extern int PQrequestCancel(PGconn *conn);
254 /* Accessor functions for PGconn objects */
255 extern char *PQdb(const PGconn *conn);
256 extern char *PQuser(const PGconn *conn);
257 extern char *PQpass(const PGconn *conn);
258 extern char *PQhost(const PGconn *conn);
259 extern char *PQport(const PGconn *conn);
260 extern char *PQtty(const PGconn *conn);
261 extern char *PQoptions(const PGconn *conn);
262 extern ConnStatusType PQstatus(const PGconn *conn);
263 extern PGTransactionStatusType PQtransactionStatus(const PGconn *conn);
264 extern const char *PQparameterStatus(const PGconn *conn,
265 const char *paramName);
266 extern int PQprotocolVersion(const PGconn *conn);
267 extern int PQserverVersion(const PGconn *conn);
268 extern char *PQerrorMessage(const PGconn *conn);
269 extern int PQsocket(const PGconn *conn);
270 extern int PQbackendPID(const PGconn *conn);
271 extern int PQclientEncoding(const PGconn *conn);
272 extern int PQsetClientEncoding(PGconn *conn, const char *encoding);
275 /* Get the SSL structure associated with a connection */
276 extern SSL *PQgetssl(PGconn *conn);
278 extern void *PQgetssl(PGconn *conn);
281 /* Tell libpq whether it needs to initialize OpenSSL */
282 extern void PQinitSSL(int do_init);
284 /* Set verbosity for PQerrorMessage and PQresultErrorMessage */
285 extern PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity);
287 /* Enable/disable tracing */
288 extern void PQtrace(PGconn *conn, FILE *debug_port);
289 extern void PQuntrace(PGconn *conn);
291 /* Override default notice handling routines */
292 extern PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn,
293 PQnoticeReceiver proc,
295 extern PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
296 PQnoticeProcessor proc,
300 * Used to set callback that prevents concurrent access to
301 * non-thread safe functions that libpq needs.
302 * The default implementation uses a libpq internal mutex.
303 * Only required for multithreaded apps that use kerberos
304 * both within their app and for postgresql connections.
306 typedef void (*pgthreadlock_t) (int acquire);
308 extern pgthreadlock_t PQregisterThreadLock(pgthreadlock_t newhandler);
310 /* === in fe-exec.c === */
312 /* Simple synchronous query */
313 extern PGresult *PQexec(PGconn *conn, const char *query);
314 extern PGresult *PQexecParams(PGconn *conn,
317 const Oid *paramTypes,
318 const char *const * paramValues,
319 const int *paramLengths,
320 const int *paramFormats,
322 extern PGresult *PQprepare(PGconn *conn, const char *stmtName,
323 const char *query, int nParams,
324 const Oid *paramTypes);
325 extern PGresult *PQexecPrepared(PGconn *conn,
326 const char *stmtName,
328 const char *const * paramValues,
329 const int *paramLengths,
330 const int *paramFormats,
333 /* Interface for multiple-result or asynchronous queries */
334 extern int PQsendQuery(PGconn *conn, const char *query);
335 extern int PQsendQueryParams(PGconn *conn,
338 const Oid *paramTypes,
339 const char *const * paramValues,
340 const int *paramLengths,
341 const int *paramFormats,
343 extern int PQsendPrepare(PGconn *conn, const char *stmtName,
344 const char *query, int nParams,
345 const Oid *paramTypes);
346 extern int PQsendQueryPrepared(PGconn *conn,
347 const char *stmtName,
349 const char *const * paramValues,
350 const int *paramLengths,
351 const int *paramFormats,
353 extern PGresult *PQgetResult(PGconn *conn);
355 /* Routines for managing an asynchronous query */
356 extern int PQisBusy(PGconn *conn);
357 extern int PQconsumeInput(PGconn *conn);
359 /* LISTEN/NOTIFY support */
360 extern PGnotify *PQnotifies(PGconn *conn);
362 /* Routines for copy in/out */
363 extern int PQputCopyData(PGconn *conn, const char *buffer, int nbytes);
364 extern int PQputCopyEnd(PGconn *conn, const char *errormsg);
365 extern int PQgetCopyData(PGconn *conn, char **buffer, int async);
367 /* Deprecated routines for copy in/out */
368 extern int PQgetline(PGconn *conn, char *string, int length);
369 extern int PQputline(PGconn *conn, const char *string);
370 extern int PQgetlineAsync(PGconn *conn, char *buffer, int bufsize);
371 extern int PQputnbytes(PGconn *conn, const char *buffer, int nbytes);
372 extern int PQendcopy(PGconn *conn);
374 /* Set blocking/nonblocking connection to the backend */
375 extern int PQsetnonblocking(PGconn *conn, int arg);
376 extern int PQisnonblocking(const PGconn *conn);
378 /* Force the write buffer to be written (or at least try) */
379 extern int PQflush(PGconn *conn);
382 * "Fast path" interface --- not really recommended for application
385 extern PGresult *PQfn(PGconn *conn,
390 const PQArgBlock *args,
393 /* Accessor functions for PGresult objects */
394 extern ExecStatusType PQresultStatus(const PGresult *res);
395 extern char *PQresStatus(ExecStatusType status);
396 extern char *PQresultErrorMessage(const PGresult *res);
397 extern char *PQresultErrorField(const PGresult *res, int fieldcode);
398 extern int PQntuples(const PGresult *res);
399 extern int PQnfields(const PGresult *res);
400 extern int PQbinaryTuples(const PGresult *res);
401 extern char *PQfname(const PGresult *res, int field_num);
402 extern int PQfnumber(const PGresult *res, const char *field_name);
403 extern Oid PQftable(const PGresult *res, int field_num);
404 extern int PQftablecol(const PGresult *res, int field_num);
405 extern int PQfformat(const PGresult *res, int field_num);
406 extern Oid PQftype(const PGresult *res, int field_num);
407 extern int PQfsize(const PGresult *res, int field_num);
408 extern int PQfmod(const PGresult *res, int field_num);
409 extern char *PQcmdStatus(PGresult *res);
410 extern char *PQoidStatus(const PGresult *res); /* old and ugly */
411 extern Oid PQoidValue(const PGresult *res); /* new and improved */
412 extern char *PQcmdTuples(PGresult *res);
413 extern char *PQgetvalue(const PGresult *res, int tup_num, int field_num);
414 extern int PQgetlength(const PGresult *res, int tup_num, int field_num);
415 extern int PQgetisnull(const PGresult *res, int tup_num, int field_num);
417 /* Delete a PGresult */
418 extern void PQclear(PGresult *res);
420 /* For freeing other alloc'd results, such as PGnotify structs */
421 extern void PQfreemem(void *ptr);
423 /* Exists for backward compatibility. bjm 2003-03-24 */
424 #define PQfreeNotify(ptr) PQfreemem(ptr)
426 /* Define the string so all uses are consistent. */
427 #define PQnoPasswordSupplied "fe_sendauth: no password supplied\n"
430 * Make an empty PGresult with given status (some apps find this
431 * useful). If conn is not NULL and status indicates an error, the
432 * conn's errorMessage is copied.
434 extern PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
437 /* Quoting strings before inclusion in queries. */
438 extern size_t PQescapeString(char *to, const char *from, size_t length);
439 extern unsigned char *PQescapeBytea(const unsigned char *bintext, size_t binlen,
441 extern unsigned char *PQunescapeBytea(const unsigned char *strtext,
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 *pg_make_encrypted_password(const char *passwd, const char *user);
506 #endif /* LIBPQ_FE_H */