1 /*-------------------------------------------------------------------------
4 * This file contains definitions for structures and
5 * externs for functions used by frontend postgres applications.
7 * Portions Copyright (c) 1996-2007, 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.137 2007/07/08 17:11:51 joe Exp $
12 *-------------------------------------------------------------------------
26 * defines the backend's externally visible types,
29 #include "postgres_ext.h"
30 #include "postgres_fe.h"
32 /* Application-visible enum types */
37 * Although it is okay to add to this list, values which become unused
38 * should never be removed, nor should constants be redefined - that would
39 * break compatibility with existing code.
43 /* Non-blocking mode only below here */
46 * The existence of these should never be relied upon - they should only
47 * be used for user feedback or similar purposes.
49 CONNECTION_STARTED, /* Waiting for connection to be made. */
50 CONNECTION_MADE, /* Connection OK; waiting to send. */
51 CONNECTION_AWAITING_RESPONSE, /* Waiting for a response from the
53 CONNECTION_AUTH_OK, /* Received authentication; waiting for
55 CONNECTION_SETENV, /* Negotiating environment. */
56 CONNECTION_SSL_STARTUP, /* Negotiating SSL. */
57 CONNECTION_NEEDED /* Internal state: connect() needed */
62 PGRES_POLLING_FAILED = 0,
63 PGRES_POLLING_READING, /* These two indicate that one may */
64 PGRES_POLLING_WRITING, /* use select before polling again. */
66 PGRES_POLLING_ACTIVE /* unused; keep for awhile for backwards
68 } PostgresPollingStatusType;
72 PGRES_EMPTY_QUERY = 0, /* empty query string was executed */
73 PGRES_COMMAND_OK, /* a query command that doesn't return
74 * anything was executed properly by the
76 PGRES_TUPLES_OK, /* a query command that returns tuples was
77 * executed properly by the backend, PGresult
78 * contains the result tuples */
79 PGRES_COPY_OUT, /* Copy Out data transfer in progress */
80 PGRES_COPY_IN, /* Copy In data transfer in progress */
81 PGRES_BAD_RESPONSE, /* an unexpected response was recv'd from the
83 PGRES_NONFATAL_ERROR, /* notice or warning message */
84 PGRES_FATAL_ERROR /* query failed */
89 PQTRANS_IDLE, /* connection idle */
90 PQTRANS_ACTIVE, /* command in progress */
91 PQTRANS_INTRANS, /* idle, within transaction block */
92 PQTRANS_INERROR, /* idle, within failed transaction */
93 PQTRANS_UNKNOWN /* cannot determine status */
94 } PGTransactionStatusType;
98 PQERRORS_TERSE, /* single-line error messages */
99 PQERRORS_DEFAULT, /* recommended style */
100 PQERRORS_VERBOSE /* all the facts, ma'am */
103 /* PGconn encapsulates a connection to the backend.
104 * The contents of this struct are not supposed to be known to applications.
106 typedef struct pg_conn PGconn;
108 /* PGresult encapsulates the result of a query (or more precisely, of a single
109 * SQL command --- a query string given to PQsendQuery can contain multiple
110 * commands and thus return multiple PGresult objects).
111 * The contents of this struct are not supposed to be known to applications.
113 typedef struct pg_result PGresult;
115 /* PGcancel encapsulates the information needed to cancel a running
116 * query on an existing connection.
117 * The contents of this struct are not supposed to be known to applications.
119 typedef struct pg_cancel PGcancel;
121 /* PGnotify represents the occurrence of a NOTIFY message.
122 * Ideally this would be an opaque typedef, but it's so simple that it's
123 * unlikely to change.
124 * NOTE: in Postgres 6.4 and later, the be_pid is the notifying backend's,
125 * whereas in earlier versions it was always your own backend's PID.
127 typedef struct pgNotify
129 char *relname; /* notification condition name */
130 int be_pid; /* process ID of notifying server process */
131 char *extra; /* notification parameter */
132 /* Fields below here are private to libpq; apps should not use 'em */
133 struct pgNotify *next; /* list link */
136 /* Function types for notice-handling callbacks */
137 typedef void (*PQnoticeReceiver) (void *arg, const PGresult *res);
138 typedef void (*PQnoticeProcessor) (void *arg, const char *message);
140 /* Print options for PQprint() */
143 typedef struct _PQprintOpt
145 pqbool header; /* print output field headings and row count */
146 pqbool align; /* fill align the fields */
147 pqbool standard; /* old brain dead format */
148 pqbool html3; /* output html tables */
149 pqbool expanded; /* expand tables */
150 pqbool pager; /* use pager for output if needed */
151 char *fieldSep; /* field separator */
152 char *tableOpt; /* insert to HTML <table ...> */
153 char *caption; /* HTML <caption> */
154 char **fieldName; /* null terminated array of replacement field
159 * Structure for the conninfo parameter definitions returned by PQconndefaults
161 * All fields except "val" point at static strings which must not be altered.
162 * "val" is either NULL or a malloc'd current-value string. PQconninfoFree()
163 * will release both the val strings and the PQconninfoOption array itself.
166 typedef struct _PQconninfoOption
168 char *keyword; /* The keyword of the option */
169 char *envvar; /* Fallback environment variable name */
170 char *compiled; /* Fallback compiled in default value */
171 char *val; /* Option's current value, or NULL */
172 char *label; /* Label for field in connect dialog */
173 char *dispchar; /* Character to display for this field in a
174 * connect dialog. Values are: "" Display
175 * entered value as is "*" Password field -
176 * hide value "D" Debug option - don't show
178 int dispsize; /* Field size in characters for dialog */
182 * PQArgBlock -- structure for PQfn() arguments
191 int *ptr; /* can't use void (dec compiler barfs) */
197 * Exported functions of libpq
201 /* === in fe-connect.c === */
203 /* make a new client connection to the backend */
204 /* Asynchronous (non-blocking) */
205 extern PGconn *PQconnectStart(const char *conninfo);
206 extern PostgresPollingStatusType PQconnectPoll(PGconn *conn);
208 /* Synchronous (blocking) */
209 extern PGconn *PQconnectdb(const char *conninfo);
210 extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport,
211 const char *pgoptions, const char *pgtty,
213 const char *login, const char *pwd);
215 #define PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME) \
216 PQsetdbLogin(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME, NULL, NULL)
218 /* close the current connection and free the PGconn data structure */
219 extern void PQfinish(PGconn *conn);
221 /* get info about connection options known to PQconnectdb */
222 extern PQconninfoOption *PQconndefaults(void);
224 /* free the data structure returned by PQconndefaults() */
225 extern void PQconninfoFree(PQconninfoOption *connOptions);
228 * close the current connection and restablish a new one with the same
231 /* Asynchronous (non-blocking) */
232 extern int PQresetStart(PGconn *conn);
233 extern PostgresPollingStatusType PQresetPoll(PGconn *conn);
235 /* Synchronous (blocking) */
236 extern void PQreset(PGconn *conn);
238 /* request a cancel structure */
239 extern PGcancel *PQgetCancel(PGconn *conn);
241 /* free a cancel structure */
242 extern void PQfreeCancel(PGcancel *cancel);
244 /* issue a cancel request */
245 extern int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize);
247 /* backwards compatible version of PQcancel; not thread-safe */
248 extern int PQrequestCancel(PGconn *conn);
250 /* Accessor functions for PGconn objects */
251 extern char *PQdb(const PGconn *conn);
252 extern char *PQuser(const PGconn *conn);
253 extern char *PQpass(const PGconn *conn);
254 extern char *PQhost(const PGconn *conn);
255 extern char *PQport(const PGconn *conn);
256 extern char *PQtty(const PGconn *conn);
257 extern char *PQoptions(const PGconn *conn);
258 extern ConnStatusType PQstatus(const PGconn *conn);
259 extern PGTransactionStatusType PQtransactionStatus(const PGconn *conn);
260 extern const char *PQparameterStatus(const PGconn *conn,
261 const char *paramName);
262 extern int PQprotocolVersion(const PGconn *conn);
263 extern int PQserverVersion(const PGconn *conn);
264 extern char *PQerrorMessage(const PGconn *conn);
265 extern int PQsocket(const PGconn *conn);
266 extern int PQbackendPID(const PGconn *conn);
267 extern int PQclientEncoding(const PGconn *conn);
268 extern int PQsetClientEncoding(PGconn *conn, const char *encoding);
269 extern bool PQconnectionUsedPassword(const PGconn *conn);
271 /* Get the OpenSSL structure associated with a connection. Returns NULL for
272 * unencrypted connections or if any other TLS library is in use. */
273 extern void *PQgetssl(PGconn *conn);
275 /* Tell libpq whether it needs to initialize OpenSSL */
276 extern void PQinitSSL(int do_init);
278 /* Set verbosity for PQerrorMessage and PQresultErrorMessage */
279 extern PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity);
281 /* Enable/disable tracing */
282 extern void PQtrace(PGconn *conn, FILE *debug_port);
283 extern void PQuntrace(PGconn *conn);
285 /* Override default notice handling routines */
286 extern PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn,
287 PQnoticeReceiver proc,
289 extern PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
290 PQnoticeProcessor proc,
294 * Used to set callback that prevents concurrent access to
295 * non-thread safe functions that libpq needs.
296 * The default implementation uses a libpq internal mutex.
297 * Only required for multithreaded apps that use kerberos
298 * both within their app and for postgresql connections.
300 typedef void (*pgthreadlock_t) (int acquire);
302 extern pgthreadlock_t PQregisterThreadLock(pgthreadlock_t newhandler);
304 /* === in fe-exec.c === */
306 /* Simple synchronous query */
307 extern PGresult *PQexec(PGconn *conn, const char *query);
308 extern PGresult *PQexecParams(PGconn *conn,
311 const Oid *paramTypes,
312 const char *const * paramValues,
313 const int *paramLengths,
314 const int *paramFormats,
316 extern PGresult *PQprepare(PGconn *conn, const char *stmtName,
317 const char *query, int nParams,
318 const Oid *paramTypes);
319 extern PGresult *PQexecPrepared(PGconn *conn,
320 const char *stmtName,
322 const char *const * paramValues,
323 const int *paramLengths,
324 const int *paramFormats,
327 /* Interface for multiple-result or asynchronous queries */
328 extern int PQsendQuery(PGconn *conn, const char *query);
329 extern int PQsendQueryParams(PGconn *conn,
332 const Oid *paramTypes,
333 const char *const * paramValues,
334 const int *paramLengths,
335 const int *paramFormats,
337 extern int PQsendPrepare(PGconn *conn, const char *stmtName,
338 const char *query, int nParams,
339 const Oid *paramTypes);
340 extern int PQsendQueryPrepared(PGconn *conn,
341 const char *stmtName,
343 const char *const * paramValues,
344 const int *paramLengths,
345 const int *paramFormats,
347 extern PGresult *PQgetResult(PGconn *conn);
349 /* Routines for managing an asynchronous query */
350 extern int PQisBusy(PGconn *conn);
351 extern int PQconsumeInput(PGconn *conn);
353 /* LISTEN/NOTIFY support */
354 extern PGnotify *PQnotifies(PGconn *conn);
356 /* Routines for copy in/out */
357 extern int PQputCopyData(PGconn *conn, const char *buffer, int nbytes);
358 extern int PQputCopyEnd(PGconn *conn, const char *errormsg);
359 extern int PQgetCopyData(PGconn *conn, char **buffer, int async);
361 /* Deprecated routines for copy in/out */
362 extern int PQgetline(PGconn *conn, char *string, int length);
363 extern int PQputline(PGconn *conn, const char *string);
364 extern int PQgetlineAsync(PGconn *conn, char *buffer, int bufsize);
365 extern int PQputnbytes(PGconn *conn, const char *buffer, int nbytes);
366 extern int PQendcopy(PGconn *conn);
368 /* Set blocking/nonblocking connection to the backend */
369 extern int PQsetnonblocking(PGconn *conn, int arg);
370 extern int PQisnonblocking(const PGconn *conn);
371 extern int PQisthreadsafe(void);
373 /* Force the write buffer to be written (or at least try) */
374 extern int PQflush(PGconn *conn);
377 * "Fast path" interface --- not really recommended for application
380 extern PGresult *PQfn(PGconn *conn,
385 const PQArgBlock *args,
388 /* Accessor functions for PGresult objects */
389 extern ExecStatusType PQresultStatus(const PGresult *res);
390 extern char *PQresStatus(ExecStatusType status);
391 extern char *PQresultErrorMessage(const PGresult *res);
392 extern char *PQresultErrorField(const PGresult *res, int fieldcode);
393 extern int PQntuples(const PGresult *res);
394 extern int PQnfields(const PGresult *res);
395 extern int PQbinaryTuples(const PGresult *res);
396 extern char *PQfname(const PGresult *res, int field_num);
397 extern int PQfnumber(const PGresult *res, const char *field_name);
398 extern Oid PQftable(const PGresult *res, int field_num);
399 extern int PQftablecol(const PGresult *res, int field_num);
400 extern int PQfformat(const PGresult *res, int field_num);
401 extern Oid PQftype(const PGresult *res, int field_num);
402 extern int PQfsize(const PGresult *res, int field_num);
403 extern int PQfmod(const PGresult *res, int field_num);
404 extern char *PQcmdStatus(PGresult *res);
405 extern char *PQoidStatus(const PGresult *res); /* old and ugly */
406 extern Oid PQoidValue(const PGresult *res); /* new and improved */
407 extern char *PQcmdTuples(PGresult *res);
408 extern char *PQgetvalue(const PGresult *res, int tup_num, int field_num);
409 extern int PQgetlength(const PGresult *res, int tup_num, int field_num);
410 extern int PQgetisnull(const PGresult *res, int tup_num, int field_num);
411 extern int PQnparams(const PGresult *res);
412 extern Oid PQparamtype(const PGresult *res, int param_num);
414 /* Describe prepared statements and portals */
415 extern PGresult *PQdescribePrepared(PGconn *conn, const char *stmt);
416 extern PGresult *PQdescribePortal(PGconn *conn, const char *portal);
417 extern int PQsendDescribePrepared(PGconn *conn, const char *stmt);
418 extern int PQsendDescribePortal(PGconn *conn, const char *portal);
420 /* Delete a PGresult */
421 extern void PQclear(PGresult *res);
423 /* For freeing other alloc'd results, such as PGnotify structs */
424 extern void PQfreemem(void *ptr);
426 /* Exists for backward compatibility. bjm 2003-03-24 */
427 #define PQfreeNotify(ptr) PQfreemem(ptr)
429 /* Define the string so all uses are consistent. */
430 #define PQnoPasswordSupplied "fe_sendauth: no password supplied\n"
433 * Make an empty PGresult with given status (some apps find this
434 * useful). If conn is not NULL and status indicates an error, the
435 * conn's errorMessage is copied.
437 extern PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
440 /* Quoting strings before inclusion in queries. */
441 extern size_t PQescapeStringConn(PGconn *conn,
442 char *to, const char *from, size_t length,
444 extern unsigned char *PQescapeByteaConn(PGconn *conn,
445 const unsigned char *from, size_t from_length,
447 extern unsigned char *PQunescapeBytea(const unsigned char *strtext,
450 /* These forms are deprecated! */
451 extern size_t PQescapeString(char *to, const char *from, size_t length);
452 extern unsigned char *PQescapeBytea(const unsigned char *from, size_t from_length,
457 /* === in fe-print.c === */
460 PQprint(FILE *fout, /* output stream */
462 const PQprintOpt *ps); /* option structure */
465 * really old printing routines
468 PQdisplayTuples(const PGresult *res,
469 FILE *fp, /* where to send the output */
470 int fillAlign, /* pad the fields with spaces */
471 const char *fieldSep, /* field separator */
472 int printHeader, /* display headers? */
476 PQprintTuples(const PGresult *res,
477 FILE *fout, /* output stream */
478 int printAttName, /* print attribute names */
479 int terseOutput, /* delimiter bars */
480 int width); /* width of column, if 0, use variable width */
483 /* === in fe-lobj.c === */
485 /* Large-object access routines */
486 extern int lo_open(PGconn *conn, Oid lobjId, int mode);
487 extern int lo_close(PGconn *conn, int fd);
488 extern int lo_read(PGconn *conn, int fd, char *buf, size_t len);
489 extern int lo_write(PGconn *conn, int fd, const char *buf, size_t len);
490 extern int lo_lseek(PGconn *conn, int fd, int offset, int whence);
491 extern Oid lo_creat(PGconn *conn, int mode);
492 extern Oid lo_create(PGconn *conn, Oid lobjId);
493 extern int lo_tell(PGconn *conn, int fd);
494 extern int lo_truncate(PGconn *conn, int fd, size_t len);
495 extern int lo_unlink(PGconn *conn, Oid lobjId);
496 extern Oid lo_import(PGconn *conn, const char *filename);
497 extern int lo_export(PGconn *conn, Oid lobjId, const char *filename);
499 /* === in fe-misc.c === */
501 /* Determine length of multibyte encoded char at *s */
502 extern int PQmblen(const char *s, int encoding);
504 /* Determine display length of multibyte encoded char at *s */
505 extern int PQdsplen(const char *s, int encoding);
507 /* Get encoding id from environment variable PGCLIENTENCODING */
508 extern int PQenv2encoding(void);
510 /* === in fe-auth.c === */
512 extern char *PQencryptPassword(const char *passwd, const char *user);
518 #endif /* LIBPQ_FE_H */