1 /*-------------------------------------------------------------------------
4 * This file contains definitions for structures and
5 * externs for functions used by frontend postgres applications.
7 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $Id: libpq-fe.h,v 1.94 2003/06/21 21:51:34 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
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
71 * backwards compatibility */
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 /* PGnotify represents the occurrence of a NOTIFY message.
120 * Ideally this would be an opaque typedef, but it's so simple that it's
121 * unlikely to change.
122 * NOTE: in Postgres 6.4 and later, the be_pid is the notifying backend's,
123 * whereas in earlier versions it was always your own backend's PID.
125 typedef struct pgNotify
127 char *relname; /* notification condition name */
128 int be_pid; /* process ID of server process */
129 char *extra; /* notification parameter */
132 /* Function types for notice-handling callbacks */
133 typedef void (*PQnoticeReceiver) (void *arg, const PGresult *res);
134 typedef void (*PQnoticeProcessor) (void *arg, const char *message);
136 /* Print options for PQprint() */
139 typedef struct _PQprintOpt
141 pqbool header; /* print output field headings and row
143 pqbool align; /* fill align the fields */
144 pqbool standard; /* old brain dead format */
145 pqbool html3; /* output html tables */
146 pqbool expanded; /* expand tables */
147 pqbool pager; /* use pager for output if needed */
148 char *fieldSep; /* field separator */
149 char *tableOpt; /* insert to HTML <table ...> */
150 char *caption; /* HTML <caption> */
151 char **fieldName; /* null terminated array of repalcement
156 * Structure for the conninfo parameter definitions returned by PQconndefaults
158 * All fields except "val" point at static strings which must not be altered.
159 * "val" is either NULL or a malloc'd current-value string. PQconninfoFree()
160 * will release both the val strings and the PQconninfoOption array itself.
163 typedef struct _PQconninfoOption
165 char *keyword; /* The keyword of the option */
166 char *envvar; /* Fallback environment variable name */
167 char *compiled; /* Fallback compiled in default value */
168 char *val; /* Option's current value, or NULL */
169 char *label; /* Label for field in connect dialog */
170 char *dispchar; /* Character to display for this field in
171 * a connect dialog. Values are: ""
172 * Display entered value as is "*"
173 * Password field - hide value "D" Debug
174 * option - don't show by default */
175 int dispsize; /* Field size in characters for dialog */
179 * PQArgBlock -- structure for PQfn() arguments
188 int *ptr; /* can't use void (dec compiler barfs) */
194 * Exported functions of libpq
198 /* === in fe-connect.c === */
200 /* make a new client connection to the backend */
201 /* Asynchronous (non-blocking) */
202 extern PGconn *PQconnectStart(const char *conninfo);
203 extern PostgresPollingStatusType PQconnectPoll(PGconn *conn);
205 /* Synchronous (blocking) */
206 extern PGconn *PQconnectdb(const char *conninfo);
207 extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport,
208 const char *pgoptions, const char *pgtty,
210 const char *login, const char *pwd);
212 #define PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME) \
213 PQsetdbLogin(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME, NULL, NULL)
215 /* close the current connection and free the PGconn data structure */
216 extern void PQfinish(PGconn *conn);
218 /* get info about connection options known to PQconnectdb */
219 extern PQconninfoOption *PQconndefaults(void);
221 /* free the data structure returned by PQconndefaults() */
222 extern void PQconninfoFree(PQconninfoOption *connOptions);
225 * close the current connection and restablish a new one with the same
228 /* Asynchronous (non-blocking) */
229 extern int PQresetStart(PGconn *conn);
230 extern PostgresPollingStatusType PQresetPoll(PGconn *conn);
232 /* Synchronous (blocking) */
233 extern void PQreset(PGconn *conn);
235 /* issue a cancel request */
236 extern int PQrequestCancel(PGconn *conn);
238 /* Accessor functions for PGconn objects */
239 extern char *PQdb(const PGconn *conn);
240 extern char *PQuser(const PGconn *conn);
241 extern char *PQpass(const PGconn *conn);
242 extern char *PQhost(const PGconn *conn);
243 extern char *PQport(const PGconn *conn);
244 extern char *PQtty(const PGconn *conn);
245 extern char *PQoptions(const PGconn *conn);
246 extern ConnStatusType PQstatus(const PGconn *conn);
247 extern PGTransactionStatusType PQtransactionStatus(const PGconn *conn);
248 extern const char *PQparameterStatus(const PGconn *conn,
249 const char *paramName);
250 extern int PQprotocolVersion(const PGconn *conn);
251 extern char *PQerrorMessage(const PGconn *conn);
252 extern int PQsocket(const PGconn *conn);
253 extern int PQbackendPID(const PGconn *conn);
254 extern int PQclientEncoding(const PGconn *conn);
255 extern int PQsetClientEncoding(PGconn *conn, const char *encoding);
258 /* Get the SSL structure associated with a connection */
259 extern SSL *PQgetssl(PGconn *conn);
262 /* Set verbosity for PQerrorMessage and PQresultErrorMessage */
263 extern PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity);
265 /* Enable/disable tracing */
266 extern void PQtrace(PGconn *conn, FILE *debug_port);
267 extern void PQuntrace(PGconn *conn);
269 /* Override default notice handling routines */
270 extern PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn,
271 PQnoticeReceiver proc,
273 extern PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
274 PQnoticeProcessor proc,
277 /* === in fe-exec.c === */
279 /* Simple synchronous query */
280 extern PGresult *PQexec(PGconn *conn, const char *query);
281 extern PGresult *PQexecParams(PGconn *conn,
284 const Oid *paramTypes,
285 const char * const *paramValues,
286 const int *paramLengths,
287 const int *paramFormats,
290 /* Interface for multiple-result or asynchronous queries */
291 extern int PQsendQuery(PGconn *conn, const char *query);
292 extern int PQsendQueryParams(PGconn *conn,
295 const Oid *paramTypes,
296 const char * const *paramValues,
297 const int *paramLengths,
298 const int *paramFormats,
300 extern PGresult *PQgetResult(PGconn *conn);
302 /* Routines for managing an asynchronous query */
303 extern int PQisBusy(PGconn *conn);
304 extern int PQconsumeInput(PGconn *conn);
306 /* LISTEN/NOTIFY support */
307 extern PGnotify *PQnotifies(PGconn *conn);
309 /* Routines for copy in/out */
310 extern int PQputCopyData(PGconn *conn, const char *buffer, int nbytes);
311 extern int PQputCopyEnd(PGconn *conn, const char *errormsg);
312 extern int PQgetCopyData(PGconn *conn, char **buffer, int async);
313 /* Deprecated routines for copy in/out */
314 extern int PQgetline(PGconn *conn, char *string, int length);
315 extern int PQputline(PGconn *conn, const char *string);
316 extern int PQgetlineAsync(PGconn *conn, char *buffer, int bufsize);
317 extern int PQputnbytes(PGconn *conn, const char *buffer, int nbytes);
318 extern int PQendcopy(PGconn *conn);
320 /* Set blocking/nonblocking connection to the backend */
321 extern int PQsetnonblocking(PGconn *conn, int arg);
322 extern int PQisnonblocking(const PGconn *conn);
324 /* Force the write buffer to be written (or at least try) */
325 extern int PQflush(PGconn *conn);
328 * "Fast path" interface --- not really recommended for application
331 extern PGresult *PQfn(PGconn *conn,
336 const PQArgBlock *args,
339 /* Accessor functions for PGresult objects */
340 extern ExecStatusType PQresultStatus(const PGresult *res);
341 extern char *PQresStatus(ExecStatusType status);
342 extern char *PQresultErrorMessage(const PGresult *res);
343 extern char *PQresultErrorField(const PGresult *res, int fieldcode);
344 extern int PQntuples(const PGresult *res);
345 extern int PQnfields(const PGresult *res);
346 extern int PQbinaryTuples(const PGresult *res);
347 extern char *PQfname(const PGresult *res, int field_num);
348 extern int PQfnumber(const PGresult *res, const char *field_name);
349 extern Oid PQftable(const PGresult *res, int field_num);
350 extern int PQftablecol(const PGresult *res, int field_num);
351 extern int PQfformat(const PGresult *res, int field_num);
352 extern Oid PQftype(const PGresult *res, int field_num);
353 extern int PQfsize(const PGresult *res, int field_num);
354 extern int PQfmod(const PGresult *res, int field_num);
355 extern char *PQcmdStatus(PGresult *res);
356 extern char *PQoidStatus(const PGresult *res); /* old and ugly */
357 extern Oid PQoidValue(const PGresult *res); /* new and improved */
358 extern char *PQcmdTuples(PGresult *res);
359 extern char *PQgetvalue(const PGresult *res, int tup_num, int field_num);
360 extern int PQgetlength(const PGresult *res, int tup_num, int field_num);
361 extern int PQgetisnull(const PGresult *res, int tup_num, int field_num);
363 /* Delete a PGresult */
364 extern void PQclear(PGresult *res);
366 /* For freeing other alloc'd results, such as PGnotify structs */
367 extern void PQfreemem(void *ptr);
369 /* Exists for backward compatibility. bjm 2003-03-24 */
370 #define PQfreeNotify(ptr) PQfreemem(ptr)
373 * Make an empty PGresult with given status (some apps find this
374 * useful). If conn is not NULL and status indicates an error, the
375 * conn's errorMessage is copied.
377 extern PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
380 /* Quoting strings before inclusion in queries. */
381 extern size_t PQescapeString(char *to, const char *from, size_t length);
382 extern unsigned char *PQescapeBytea(const unsigned char *bintext, size_t binlen,
384 extern unsigned char *PQunescapeBytea(const unsigned char *strtext,
389 /* === in fe-print.c === */
391 extern void PQprint(FILE *fout, /* output stream */
393 const PQprintOpt *ps); /* option structure */
396 * really old printing routines
398 extern void PQdisplayTuples(const PGresult *res,
399 FILE *fp, /* where to send the output */
400 int fillAlign, /* pad the fields with spaces */
401 const char *fieldSep, /* field separator */
402 int printHeader, /* display headers? */
405 extern void PQprintTuples(const PGresult *res,
406 FILE *fout, /* output stream */
407 int printAttName, /* print attribute names */
408 int terseOutput, /* delimiter bars */
409 int width); /* width of column, if 0, use variable
413 /* === in fe-lobj.c === */
415 /* Large-object access routines */
416 extern int lo_open(PGconn *conn, Oid lobjId, int mode);
417 extern int lo_close(PGconn *conn, int fd);
418 extern int lo_read(PGconn *conn, int fd, char *buf, size_t len);
419 extern int lo_write(PGconn *conn, int fd, char *buf, size_t len);
420 extern int lo_lseek(PGconn *conn, int fd, int offset, int whence);
421 extern Oid lo_creat(PGconn *conn, int mode);
422 extern int lo_tell(PGconn *conn, int fd);
423 extern int lo_unlink(PGconn *conn, Oid lobjId);
424 extern Oid lo_import(PGconn *conn, const char *filename);
425 extern int lo_export(PGconn *conn, Oid lobjId, const char *filename);
427 /* === in fe-misc.c === */
429 /* Determine length of multibyte encoded char at *s */
430 extern int PQmblen(const unsigned char *s, int encoding);
432 /* Get encoding id from environment variable PGCLIENTENCODING */
433 extern int PQenv2encoding(void);
439 #endif /* LIBPQ_FE_H */