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.119 2005/09/24 17:53:28 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
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 /* 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
151 pqbool align; /* fill align the fields */
152 pqbool standard; /* old brain dead format */
153 pqbool html3; /* output html tables */
154 pqbool expanded; /* expand tables */
155 pqbool pager; /* use pager for output if needed */
156 char *fieldSep; /* field separator */
157 char *tableOpt; /* insert to HTML <table ...> */
158 char *caption; /* HTML <caption> */
159 char **fieldName; /* null terminated array of replacement
164 * Structure for the conninfo parameter definitions returned by PQconndefaults
166 * All fields except "val" point at static strings which must not be altered.
167 * "val" is either NULL or a malloc'd current-value string. PQconninfoFree()
168 * will release both the val strings and the PQconninfoOption array itself.
171 typedef struct _PQconninfoOption
173 char *keyword; /* The keyword of the option */
174 char *envvar; /* Fallback environment variable name */
175 char *compiled; /* Fallback compiled in default value */
176 char *val; /* Option's current value, or NULL */
177 char *label; /* Label for field in connect dialog */
178 char *dispchar; /* Character to display for this field in
179 * a connect dialog. Values are: ""
180 * Display entered value as is "*"
181 * Password field - hide value "D" Debug
182 * option - don't show by default */
183 int dispsize; /* Field size in characters for dialog */
187 * PQArgBlock -- structure for PQfn() arguments
196 int *ptr; /* can't use void (dec compiler barfs) */
202 * Exported functions of libpq
206 /* === in fe-connect.c === */
208 /* make a new client connection to the backend */
209 /* Asynchronous (non-blocking) */
210 extern PGconn *PQconnectStart(const char *conninfo);
211 extern PostgresPollingStatusType PQconnectPoll(PGconn *conn);
213 /* Synchronous (blocking) */
214 extern PGconn *PQconnectdb(const char *conninfo);
215 extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport,
216 const char *pgoptions, const char *pgtty,
218 const char *login, const char *pwd);
220 #define PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME) \
221 PQsetdbLogin(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME, NULL, NULL)
223 /* close the current connection and free the PGconn data structure */
224 extern void PQfinish(PGconn *conn);
226 /* get info about connection options known to PQconnectdb */
227 extern PQconninfoOption *PQconndefaults(void);
229 /* free the data structure returned by PQconndefaults() */
230 extern void PQconninfoFree(PQconninfoOption *connOptions);
233 * close the current connection and restablish a new one with the same
236 /* Asynchronous (non-blocking) */
237 extern int PQresetStart(PGconn *conn);
238 extern PostgresPollingStatusType PQresetPoll(PGconn *conn);
240 /* Synchronous (blocking) */
241 extern void PQreset(PGconn *conn);
243 /* request a cancel structure */
244 extern PGcancel *PQgetCancel(PGconn *conn);
246 /* free a cancel structure */
247 extern void PQfreeCancel(PGcancel *cancel);
249 /* issue a cancel request */
250 extern int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize);
252 /* backwards compatible version of PQcancel; not thread-safe */
253 extern int PQrequestCancel(PGconn *conn);
255 /* Accessor functions for PGconn objects */
256 extern char *PQdb(const PGconn *conn);
257 extern char *PQuser(const PGconn *conn);
258 extern char *PQpass(const PGconn *conn);
259 extern char *PQhost(const PGconn *conn);
260 extern char *PQport(const PGconn *conn);
261 extern char *PQtty(const PGconn *conn);
262 extern char *PQoptions(const PGconn *conn);
263 extern ConnStatusType PQstatus(const PGconn *conn);
264 extern PGTransactionStatusType PQtransactionStatus(const PGconn *conn);
265 extern const char *PQparameterStatus(const PGconn *conn,
266 const char *paramName);
267 extern int PQprotocolVersion(const PGconn *conn);
268 extern int PQserverVersion(const PGconn *conn);
269 extern char *PQerrorMessage(const PGconn *conn);
270 extern int PQsocket(const PGconn *conn);
271 extern int PQbackendPID(const PGconn *conn);
272 extern int PQclientEncoding(const PGconn *conn);
273 extern int PQsetClientEncoding(PGconn *conn, const char *encoding);
276 /* Get the SSL structure associated with a connection */
277 extern SSL *PQgetssl(PGconn *conn);
279 extern void *PQgetssl(PGconn *conn);
282 /* Tell libpq whether it needs to initialize OpenSSL */
283 extern void PQinitSSL(int do_init);
285 /* Set verbosity for PQerrorMessage and PQresultErrorMessage */
286 extern PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity);
288 /* Enable/disable tracing */
289 extern void PQtrace(PGconn *conn, FILE *debug_port);
290 extern void PQuntrace(PGconn *conn);
292 /* Override default notice handling routines */
293 extern PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn,
294 PQnoticeReceiver proc,
296 extern PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
297 PQnoticeProcessor proc,
301 * Used to set callback that prevents concurrent access to
302 * non-thread safe functions that libpq needs.
303 * The default implementation uses a libpq internal mutex.
304 * Only required for multithreaded apps that use kerberos
305 * both within their app and for postgresql connections.
307 typedef void (*pgthreadlock_t) (int acquire);
309 extern pgthreadlock_t PQregisterThreadLock(pgthreadlock_t newhandler);
311 /* === in fe-exec.c === */
313 /* Simple synchronous query */
314 extern PGresult *PQexec(PGconn *conn, const char *query);
315 extern PGresult *PQexecParams(PGconn *conn,
318 const Oid *paramTypes,
319 const char *const * paramValues,
320 const int *paramLengths,
321 const int *paramFormats,
323 extern PGresult *PQprepare(PGconn *conn, const char *stmtName,
324 const char *query, int nParams,
325 const Oid *paramTypes);
326 extern PGresult *PQexecPrepared(PGconn *conn,
327 const char *stmtName,
329 const char *const * paramValues,
330 const int *paramLengths,
331 const int *paramFormats,
334 /* Interface for multiple-result or asynchronous queries */
335 extern int PQsendQuery(PGconn *conn, const char *query);
336 extern int PQsendQueryParams(PGconn *conn,
339 const Oid *paramTypes,
340 const char *const * paramValues,
341 const int *paramLengths,
342 const int *paramFormats,
344 extern int PQsendPrepare(PGconn *conn, const char *stmtName,
345 const char *query, int nParams,
346 const Oid *paramTypes);
347 extern int PQsendQueryPrepared(PGconn *conn,
348 const char *stmtName,
350 const char *const * paramValues,
351 const int *paramLengths,
352 const int *paramFormats,
354 extern PGresult *PQgetResult(PGconn *conn);
356 /* Routines for managing an asynchronous query */
357 extern int PQisBusy(PGconn *conn);
358 extern int PQconsumeInput(PGconn *conn);
360 /* LISTEN/NOTIFY support */
361 extern PGnotify *PQnotifies(PGconn *conn);
363 /* Routines for copy in/out */
364 extern int PQputCopyData(PGconn *conn, const char *buffer, int nbytes);
365 extern int PQputCopyEnd(PGconn *conn, const char *errormsg);
366 extern int PQgetCopyData(PGconn *conn, char **buffer, int async);
368 /* Deprecated routines for copy in/out */
369 extern int PQgetline(PGconn *conn, char *string, int length);
370 extern int PQputline(PGconn *conn, const char *string);
371 extern int PQgetlineAsync(PGconn *conn, char *buffer, int bufsize);
372 extern int PQputnbytes(PGconn *conn, const char *buffer, int nbytes);
373 extern int PQendcopy(PGconn *conn);
375 /* Set blocking/nonblocking connection to the backend */
376 extern int PQsetnonblocking(PGconn *conn, int arg);
377 extern int PQisnonblocking(const PGconn *conn);
379 /* Force the write buffer to be written (or at least try) */
380 extern int PQflush(PGconn *conn);
383 * "Fast path" interface --- not really recommended for application
386 extern PGresult *PQfn(PGconn *conn,
391 const PQArgBlock *args,
394 /* Accessor functions for PGresult objects */
395 extern ExecStatusType PQresultStatus(const PGresult *res);
396 extern char *PQresStatus(ExecStatusType status);
397 extern char *PQresultErrorMessage(const PGresult *res);
398 extern char *PQresultErrorField(const PGresult *res, int fieldcode);
399 extern int PQntuples(const PGresult *res);
400 extern int PQnfields(const PGresult *res);
401 extern int PQbinaryTuples(const PGresult *res);
402 extern char *PQfname(const PGresult *res, int field_num);
403 extern int PQfnumber(const PGresult *res, const char *field_name);
404 extern Oid PQftable(const PGresult *res, int field_num);
405 extern int PQftablecol(const PGresult *res, int field_num);
406 extern int PQfformat(const PGresult *res, int field_num);
407 extern Oid PQftype(const PGresult *res, int field_num);
408 extern int PQfsize(const PGresult *res, int field_num);
409 extern int PQfmod(const PGresult *res, int field_num);
410 extern char *PQcmdStatus(PGresult *res);
411 extern char *PQoidStatus(const PGresult *res); /* old and ugly */
412 extern Oid PQoidValue(const PGresult *res); /* new and improved */
413 extern char *PQcmdTuples(PGresult *res);
414 extern char *PQgetvalue(const PGresult *res, int tup_num, int field_num);
415 extern int PQgetlength(const PGresult *res, int tup_num, int field_num);
416 extern int PQgetisnull(const PGresult *res, int tup_num, int field_num);
418 /* Delete a PGresult */
419 extern void PQclear(PGresult *res);
421 /* For freeing other alloc'd results, such as PGnotify structs */
422 extern void PQfreemem(void *ptr);
424 /* Exists for backward compatibility. bjm 2003-03-24 */
425 #define PQfreeNotify(ptr) PQfreemem(ptr)
427 /* Define the string so all uses are consistent. */
428 #define PQnoPasswordSupplied "fe_sendauth: no password supplied\n"
431 * Make an empty PGresult with given status (some apps find this
432 * useful). If conn is not NULL and status indicates an error, the
433 * conn's errorMessage is copied.
435 extern PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
438 /* Quoting strings before inclusion in queries. */
439 extern size_t PQescapeString(char *to, const char *from, size_t length);
440 extern unsigned char *PQescapeBytea(const unsigned char *bintext, size_t binlen,
442 extern unsigned char *PQunescapeBytea(const unsigned char *strtext,
447 /* === in fe-print.c === */
450 PQprint(FILE *fout, /* output stream */
452 const PQprintOpt *ps); /* option structure */
455 * really old printing routines
458 PQdisplayTuples(const PGresult *res,
459 FILE *fp, /* where to send the output */
460 int fillAlign, /* pad the fields with spaces */
461 const char *fieldSep, /* field separator */
462 int printHeader, /* display headers? */
466 PQprintTuples(const PGresult *res,
467 FILE *fout, /* output stream */
468 int printAttName, /* print attribute names */
469 int terseOutput, /* delimiter bars */
470 int width); /* width of column, if 0, use variable
474 /* === in fe-lobj.c === */
476 /* Large-object access routines */
477 extern int lo_open(PGconn *conn, Oid lobjId, int mode);
478 extern int lo_close(PGconn *conn, int fd);
479 extern int lo_read(PGconn *conn, int fd, char *buf, size_t len);
480 extern int lo_write(PGconn *conn, int fd, char *buf, size_t len);
481 extern int lo_lseek(PGconn *conn, int fd, int offset, int whence);
482 extern Oid lo_creat(PGconn *conn, int mode);
483 extern Oid lo_create(PGconn *conn, Oid lobjId);
484 extern int lo_tell(PGconn *conn, int fd);
485 extern int lo_unlink(PGconn *conn, Oid lobjId);
486 extern Oid lo_import(PGconn *conn, const char *filename);
487 extern int lo_export(PGconn *conn, Oid lobjId, const char *filename);
489 /* === in fe-misc.c === */
491 /* Determine length of multibyte encoded char at *s */
492 extern int PQmblen(const char *s, int encoding);
494 /* Determine display length of multibyte encoded char at *s */
495 extern int PQdsplen(const char *s, int encoding);
497 /* Get encoding id from environment variable PGCLIENTENCODING */
498 extern int PQenv2encoding(void);
504 #endif /* LIBPQ_FE_H */