]> granicus.if.org Git - postgresql/blob - src/interfaces/libpq/libpq-fe.h
Arrange for the authentication request type to be preserved in
[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-2007, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * $PostgreSQL: pgsql/src/interfaces/libpq/libpq-fe.h,v 1.137 2007/07/08 17:11:51 joe 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  * defines the backend's externally visible types,
27  * such as Oid.
28  */
29 #include "postgres_ext.h"
30 #include "postgres_fe.h"
31
32 /* Application-visible enum types */
33
34 typedef enum
35 {
36         /*
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.
40          */
41         CONNECTION_OK,
42         CONNECTION_BAD,
43         /* Non-blocking mode only below here */
44
45         /*
46          * The existence of these should never be relied upon - they should only
47          * be used for user feedback or similar purposes.
48          */
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
52                                                                                  * postmaster.            */
53         CONNECTION_AUTH_OK,                     /* Received authentication; waiting for
54                                                                  * backend startup. */
55         CONNECTION_SETENV,                      /* Negotiating environment. */
56         CONNECTION_SSL_STARTUP,         /* Negotiating SSL. */
57         CONNECTION_NEEDED                       /* Internal state: connect() needed */
58 } ConnStatusType;
59
60 typedef enum
61 {
62         PGRES_POLLING_FAILED = 0,
63         PGRES_POLLING_READING,          /* These two indicate that one may        */
64         PGRES_POLLING_WRITING,          /* use select before polling again.   */
65         PGRES_POLLING_OK,
66         PGRES_POLLING_ACTIVE            /* unused; keep for awhile for backwards
67                                                                  * compatibility */
68 } PostgresPollingStatusType;
69
70 typedef enum
71 {
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
75                                                                  * backend */
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
82                                                                  * backend */
83         PGRES_NONFATAL_ERROR,           /* notice or warning message */
84         PGRES_FATAL_ERROR                       /* query failed */
85 } ExecStatusType;
86
87 typedef enum
88 {
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;
95
96 typedef enum
97 {
98         PQERRORS_TERSE,                         /* single-line error messages */
99         PQERRORS_DEFAULT,                       /* recommended style */
100         PQERRORS_VERBOSE                        /* all the facts, ma'am */
101 } PGVerbosity;
102
103 /* PGconn encapsulates a connection to the backend.
104  * The contents of this struct are not supposed to be known to applications.
105  */
106 typedef struct pg_conn PGconn;
107
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.
112  */
113 typedef struct pg_result PGresult;
114
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.
118  */
119 typedef struct pg_cancel PGcancel;
120
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.
126  */
127 typedef struct pgNotify
128 {
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 */
134 } PGnotify;
135
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);
139
140 /* Print options for PQprint() */
141 typedef char pqbool;
142
143 typedef struct _PQprintOpt
144 {
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
155                                                                  * names */
156 } PQprintOpt;
157
158 /* ----------------
159  * Structure for the conninfo parameter definitions returned by PQconndefaults
160  *
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.
164  * ----------------
165  */
166 typedef struct _PQconninfoOption
167 {
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
177                                                                  * by default */
178         int                     dispsize;               /* Field size in characters for dialog  */
179 } PQconninfoOption;
180
181 /* ----------------
182  * PQArgBlock -- structure for PQfn() arguments
183  * ----------------
184  */
185 typedef struct
186 {
187         int                     len;
188         int                     isint;
189         union
190         {
191                 int                *ptr;                /* can't use void (dec compiler barfs)   */
192                 int                     integer;
193         }                       u;
194 } PQArgBlock;
195
196 /* ----------------
197  * Exported functions of libpq
198  * ----------------
199  */
200
201 /* ===  in fe-connect.c === */
202
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);
207
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,
212                          const char *dbName,
213                          const char *login, const char *pwd);
214
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)
217
218 /* close the current connection and free the PGconn data structure */
219 extern void PQfinish(PGconn *conn);
220
221 /* get info about connection options known to PQconnectdb */
222 extern PQconninfoOption *PQconndefaults(void);
223
224 /* free the data structure returned by PQconndefaults() */
225 extern void PQconninfoFree(PQconninfoOption *connOptions);
226
227 /*
228  * close the current connection and restablish a new one with the same
229  * parameters
230  */
231 /* Asynchronous (non-blocking) */
232 extern int      PQresetStart(PGconn *conn);
233 extern PostgresPollingStatusType PQresetPoll(PGconn *conn);
234
235 /* Synchronous (blocking) */
236 extern void PQreset(PGconn *conn);
237
238 /* request a cancel structure */
239 extern PGcancel *PQgetCancel(PGconn *conn);
240
241 /* free a cancel structure */
242 extern void PQfreeCancel(PGcancel *cancel);
243
244 /* issue a cancel request */
245 extern int      PQcancel(PGcancel *cancel, char *errbuf, int errbufsize);
246
247 /* backwards compatible version of PQcancel; not thread-safe */
248 extern int      PQrequestCancel(PGconn *conn);
249
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);
270
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);
274
275 /* Tell libpq whether it needs to initialize OpenSSL */
276 extern void PQinitSSL(int do_init);
277
278 /* Set verbosity for PQerrorMessage and PQresultErrorMessage */
279 extern PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity);
280
281 /* Enable/disable tracing */
282 extern void PQtrace(PGconn *conn, FILE *debug_port);
283 extern void PQuntrace(PGconn *conn);
284
285 /* Override default notice handling routines */
286 extern PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn,
287                                         PQnoticeReceiver proc,
288                                         void *arg);
289 extern PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
290                                          PQnoticeProcessor proc,
291                                          void *arg);
292
293 /*
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.
299  */
300 typedef void (*pgthreadlock_t) (int acquire);
301
302 extern pgthreadlock_t PQregisterThreadLock(pgthreadlock_t newhandler);
303
304 /* === in fe-exec.c === */
305
306 /* Simple synchronous query */
307 extern PGresult *PQexec(PGconn *conn, const char *query);
308 extern PGresult *PQexecParams(PGconn *conn,
309                          const char *command,
310                          int nParams,
311                          const Oid *paramTypes,
312                          const char *const * paramValues,
313                          const int *paramLengths,
314                          const int *paramFormats,
315                          int resultFormat);
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,
321                            int nParams,
322                            const char *const * paramValues,
323                            const int *paramLengths,
324                            const int *paramFormats,
325                            int resultFormat);
326
327 /* Interface for multiple-result or asynchronous queries */
328 extern int      PQsendQuery(PGconn *conn, const char *query);
329 extern int PQsendQueryParams(PGconn *conn,
330                                   const char *command,
331                                   int nParams,
332                                   const Oid *paramTypes,
333                                   const char *const * paramValues,
334                                   const int *paramLengths,
335                                   const int *paramFormats,
336                                   int resultFormat);
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,
342                                         int nParams,
343                                         const char *const * paramValues,
344                                         const int *paramLengths,
345                                         const int *paramFormats,
346                                         int resultFormat);
347 extern PGresult *PQgetResult(PGconn *conn);
348
349 /* Routines for managing an asynchronous query */
350 extern int      PQisBusy(PGconn *conn);
351 extern int      PQconsumeInput(PGconn *conn);
352
353 /* LISTEN/NOTIFY support */
354 extern PGnotify *PQnotifies(PGconn *conn);
355
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);
360
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);
367
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);
372
373 /* Force the write buffer to be written (or at least try) */
374 extern int      PQflush(PGconn *conn);
375
376 /*
377  * "Fast path" interface --- not really recommended for application
378  * use
379  */
380 extern PGresult *PQfn(PGconn *conn,
381          int fnid,
382          int *result_buf,
383          int *result_len,
384          int result_is_int,
385          const PQArgBlock *args,
386          int nargs);
387
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);
413
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);
419
420 /* Delete a PGresult */
421 extern void PQclear(PGresult *res);
422
423 /* For freeing other alloc'd results, such as PGnotify structs */
424 extern void PQfreemem(void *ptr);
425
426 /* Exists for backward compatibility.  bjm 2003-03-24 */
427 #define PQfreeNotify(ptr) PQfreemem(ptr)
428
429 /* Define the string so all uses are consistent. */
430 #define PQnoPasswordSupplied    "fe_sendauth: no password supplied\n"
431
432 /*
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.
436  */
437 extern PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
438
439
440 /* Quoting strings before inclusion in queries. */
441 extern size_t PQescapeStringConn(PGconn *conn,
442                                    char *to, const char *from, size_t length,
443                                    int *error);
444 extern unsigned char *PQescapeByteaConn(PGconn *conn,
445                                   const unsigned char *from, size_t from_length,
446                                   size_t *to_length);
447 extern unsigned char *PQunescapeBytea(const unsigned char *strtext,
448                                 size_t *retbuflen);
449
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,
453                           size_t *to_length);
454
455
456
457 /* === in fe-print.c === */
458
459 extern void
460 PQprint(FILE *fout,                             /* output stream */
461                 const PGresult *res,
462                 const PQprintOpt *ps);  /* option structure */
463
464 /*
465  * really old printing routines
466  */
467 extern void
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? */
473                                 int quiet);
474
475 extern void
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 */
481
482
483 /* === in fe-lobj.c === */
484
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);
498
499 /* === in fe-misc.c === */
500
501 /* Determine length of multibyte encoded char at *s */
502 extern int      PQmblen(const char *s, int encoding);
503
504 /* Determine display length of multibyte encoded char at *s */
505 extern int      PQdsplen(const char *s, int encoding);
506
507 /* Get encoding id from environment variable PGCLIENTENCODING */
508 extern int      PQenv2encoding(void);
509
510 /* === in fe-auth.c === */
511
512 extern char *PQencryptPassword(const char *passwd, const char *user);
513
514 #ifdef __cplusplus
515 }
516 #endif
517
518 #endif   /* LIBPQ_FE_H */