]> granicus.if.org Git - postgresql/blob - src/interfaces/libpq/libpq-fe.h
Update libpq to make new features of FE/BE protocol available to
[postgresql] / src / interfaces / libpq / libpq-fe.h
1 /*-------------------------------------------------------------------------
2  *
3  * libpq-fe.h
4  *        This file contains definitions for structures and
5  *        externs for functions used by frontend postgres applications.
6  *
7  * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * $Id: libpq-fe.h,v 1.94 2003/06/21 21:51:34 tgl Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14
15 #ifndef LIBPQ_FE_H
16 #define LIBPQ_FE_H
17
18 #ifdef __cplusplus
19 extern          "C"
20 {
21 #endif
22
23 #include <stdio.h>
24
25 /*
26  * postgres_ext.h defines the backend's externally visible types,
27  * such as Oid.
28  */
29 #include "postgres_ext.h"
30
31 /* SSL type is needed here only to declare PQgetssl() */
32 #ifdef USE_SSL
33 #include <openssl/ssl.h>
34 #endif
35
36 /* Application-visible enum types */
37
38 typedef enum
39 {
40         /*
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.
44          */
45         CONNECTION_OK,
46         CONNECTION_BAD,
47         /* Non-blocking mode only below here */
48
49         /*
50          * The existence of these should never be relied upon - they should
51          * only be used for user feedback or similar purposes.
52          */
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
56                                                                                  * postmaster.            */
57         CONNECTION_AUTH_OK,                     /* Received authentication; waiting for
58                                                                  * backend startup. */
59         CONNECTION_SETENV,                      /* Negotiating environment. */
60         CONNECTION_SSL_STARTUP,         /* Negotiating SSL. */
61         CONNECTION_NEEDED                       /* Internal state: connect() needed */
62 } ConnStatusType;
63
64 typedef enum
65 {
66         PGRES_POLLING_FAILED = 0,
67         PGRES_POLLING_READING,          /* These two indicate that one may        */
68         PGRES_POLLING_WRITING,          /* use select before polling again.   */
69         PGRES_POLLING_OK,
70         PGRES_POLLING_ACTIVE            /*  unused; keep for awhile for
71                                  *  backwards compatibility */
72 } PostgresPollingStatusType;
73
74 typedef enum
75 {
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
79                                                                  * backend */
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
86                                                                  * the backend */
87         PGRES_NONFATAL_ERROR,           /* notice or warning message */
88         PGRES_FATAL_ERROR                       /* query failed */
89 } ExecStatusType;
90
91 typedef enum
92 {
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;
99
100 typedef enum
101 {
102         PQERRORS_TERSE,                         /* single-line error messages */
103         PQERRORS_DEFAULT,                       /* recommended style */
104         PQERRORS_VERBOSE                        /* all the facts, ma'am */
105 } PGVerbosity;
106
107 /* PGconn encapsulates a connection to the backend.
108  * The contents of this struct are not supposed to be known to applications.
109  */
110 typedef struct pg_conn PGconn;
111
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.
116  */
117 typedef struct pg_result PGresult;
118
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.
124  */
125 typedef struct pgNotify
126 {
127         char       *relname;            /* notification condition name */
128         int                     be_pid;                 /* process ID of server process */
129         char       *extra;                      /* notification parameter */
130 } PGnotify;
131
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);
135
136 /* Print options for PQprint() */
137 typedef char pqbool;
138
139 typedef struct _PQprintOpt
140 {
141         pqbool          header;                 /* print output field headings and row
142                                                                  * count */
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
152                                                                  * field names */
153 } PQprintOpt;
154
155 /* ----------------
156  * Structure for the conninfo parameter definitions returned by PQconndefaults
157  *
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.
161  * ----------------
162  */
163 typedef struct _PQconninfoOption
164 {
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  */
176 } PQconninfoOption;
177
178 /* ----------------
179  * PQArgBlock -- structure for PQfn() arguments
180  * ----------------
181  */
182 typedef struct
183 {
184         int                     len;
185         int                     isint;
186         union
187         {
188                 int                *ptr;                /* can't use void (dec compiler barfs)   */
189                 int                     integer;
190         }                       u;
191 } PQArgBlock;
192
193 /* ----------------
194  * Exported functions of libpq
195  * ----------------
196  */
197
198 /* ===  in fe-connect.c === */
199
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);
204
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,
209                          const char *dbName,
210                          const char *login, const char *pwd);
211
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)
214
215 /* close the current connection and free the PGconn data structure */
216 extern void PQfinish(PGconn *conn);
217
218 /* get info about connection options known to PQconnectdb */
219 extern PQconninfoOption *PQconndefaults(void);
220
221 /* free the data structure returned by PQconndefaults() */
222 extern void PQconninfoFree(PQconninfoOption *connOptions);
223
224 /*
225  * close the current connection and restablish a new one with the same
226  * parameters
227  */
228 /* Asynchronous (non-blocking) */
229 extern int      PQresetStart(PGconn *conn);
230 extern PostgresPollingStatusType PQresetPoll(PGconn *conn);
231
232 /* Synchronous (blocking) */
233 extern void PQreset(PGconn *conn);
234
235 /* issue a cancel request */
236 extern int      PQrequestCancel(PGconn *conn);
237
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);
256
257 #ifdef USE_SSL
258 /* Get the SSL structure associated with a connection */
259 extern SSL *PQgetssl(PGconn *conn);
260 #endif
261
262 /* Set verbosity for PQerrorMessage and PQresultErrorMessage */
263 extern PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity);
264
265 /* Enable/disable tracing */
266 extern void PQtrace(PGconn *conn, FILE *debug_port);
267 extern void PQuntrace(PGconn *conn);
268
269 /* Override default notice handling routines */
270 extern PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn,
271                                                                                         PQnoticeReceiver proc,
272                                                                                         void *arg);
273 extern PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
274                                          PQnoticeProcessor proc,
275                                          void *arg);
276
277 /* === in fe-exec.c === */
278
279 /* Simple synchronous query */
280 extern PGresult *PQexec(PGconn *conn, const char *query);
281 extern PGresult *PQexecParams(PGconn *conn,
282                                                           const char *command,
283                                                           int nParams,
284                                                           const Oid *paramTypes,
285                                                           const char * const *paramValues,
286                                                           const int *paramLengths,
287                                                           const int *paramFormats,
288                                                           int resultFormat);
289
290 /* Interface for multiple-result or asynchronous queries */
291 extern int      PQsendQuery(PGconn *conn, const char *query);
292 extern int      PQsendQueryParams(PGconn *conn,
293                                                           const char *command,
294                                                           int nParams,
295                                                           const Oid *paramTypes,
296                                                           const char * const *paramValues,
297                                                           const int *paramLengths,
298                                                           const int *paramFormats,
299                                                           int resultFormat);
300 extern PGresult *PQgetResult(PGconn *conn);
301
302 /* Routines for managing an asynchronous query */
303 extern int      PQisBusy(PGconn *conn);
304 extern int      PQconsumeInput(PGconn *conn);
305
306 /* LISTEN/NOTIFY support */
307 extern PGnotify *PQnotifies(PGconn *conn);
308
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);
319
320 /* Set blocking/nonblocking connection to the backend */
321 extern int      PQsetnonblocking(PGconn *conn, int arg);
322 extern int      PQisnonblocking(const PGconn *conn);
323
324 /* Force the write buffer to be written (or at least try) */
325 extern int      PQflush(PGconn *conn);
326
327 /*
328  * "Fast path" interface --- not really recommended for application
329  * use
330  */
331 extern PGresult *PQfn(PGconn *conn,
332          int fnid,
333          int *result_buf,
334          int *result_len,
335          int result_is_int,
336          const PQArgBlock *args,
337          int nargs);
338
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);
362
363 /* Delete a PGresult */
364 extern void PQclear(PGresult *res);
365
366 /* For freeing other alloc'd results, such as PGnotify structs */
367 extern void PQfreemem(void *ptr);
368
369 /* Exists for backward compatibility.  bjm 2003-03-24 */
370 #define PQfreeNotify(ptr) PQfreemem(ptr)
371
372 /*
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.
376  */
377 extern PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
378
379
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,
383                           size_t *bytealen);
384 extern unsigned char *PQunescapeBytea(const unsigned char *strtext,
385                                 size_t *retbuflen);
386
387
388
389 /* === in fe-print.c === */
390
391 extern void PQprint(FILE *fout,                         /* output stream */
392                                         const PGresult *res,
393                                         const PQprintOpt *ps);  /* option structure */
394
395 /*
396  * really old printing routines
397  */
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? */
403                                 int quiet);
404
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
410                                                                  * width */
411
412
413 /* === in fe-lobj.c === */
414
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);
426
427 /* === in fe-misc.c === */
428
429 /* Determine length of multibyte encoded char at *s */
430 extern int      PQmblen(const unsigned char *s, int encoding);
431
432 /* Get encoding id from environment variable PGCLIENTENCODING */
433 extern int      PQenv2encoding(void);
434
435 #ifdef __cplusplus
436 }
437 #endif
438
439 #endif   /* LIBPQ_FE_H */