]> granicus.if.org Git - postgresql/blob - src/interfaces/libpq/libpq-fe.h
Fix dblink_connect() so that it verifies that a password is supplied in the
[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-2008, 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.144 2008/09/22 13:55:14 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 /*
32  * Option flags for PQcopyResult
33  */
34 #define PG_COPYRES_ATTRS          0x01
35 #define PG_COPYRES_TUPLES         0x02          /* Implies PG_COPYRES_ATTRS */
36 #define PG_COPYRES_EVENTS         0x04
37 #define PG_COPYRES_NOTICEHOOKS    0x08
38
39 /* Application-visible enum types */
40
41 typedef enum
42 {
43         /*
44          * Although it is okay to add to this list, values which become unused
45          * should never be removed, nor should constants be redefined - that would
46          * break compatibility with existing code.
47          */
48         CONNECTION_OK,
49         CONNECTION_BAD,
50         /* Non-blocking mode only below here */
51
52         /*
53          * The existence of these should never be relied upon - they should only
54          * be used for user feedback or similar purposes.
55          */
56         CONNECTION_STARTED,                     /* Waiting for connection to be made.  */
57         CONNECTION_MADE,                        /* Connection OK; waiting to send.         */
58         CONNECTION_AWAITING_RESPONSE,           /* Waiting for a response from the
59                                                                                  * postmaster.            */
60         CONNECTION_AUTH_OK,                     /* Received authentication; waiting for
61                                                                  * backend startup. */
62         CONNECTION_SETENV,                      /* Negotiating environment. */
63         CONNECTION_SSL_STARTUP,         /* Negotiating SSL. */
64         CONNECTION_NEEDED                       /* Internal state: connect() needed */
65 } ConnStatusType;
66
67 typedef enum
68 {
69         PGRES_POLLING_FAILED = 0,
70         PGRES_POLLING_READING,          /* These two indicate that one may        */
71         PGRES_POLLING_WRITING,          /* use select before polling again.   */
72         PGRES_POLLING_OK,
73         PGRES_POLLING_ACTIVE            /* unused; keep for awhile for backwards
74                                                                  * compatibility */
75 } PostgresPollingStatusType;
76
77 typedef enum
78 {
79         PGRES_EMPTY_QUERY = 0,          /* empty query string was executed */
80         PGRES_COMMAND_OK,                       /* a query command that doesn't return
81                                                                  * anything was executed properly by the
82                                                                  * backend */
83         PGRES_TUPLES_OK,                        /* a query command that returns tuples was
84                                                                  * executed properly by the backend, PGresult
85                                                                  * contains the result tuples */
86         PGRES_COPY_OUT,                         /* Copy Out data transfer in progress */
87         PGRES_COPY_IN,                          /* Copy In data transfer in progress */
88         PGRES_BAD_RESPONSE,                     /* an unexpected response was recv'd from the
89                                                                  * backend */
90         PGRES_NONFATAL_ERROR,           /* notice or warning message */
91         PGRES_FATAL_ERROR                       /* query failed */
92 } ExecStatusType;
93
94 typedef enum
95 {
96         PQTRANS_IDLE,                           /* connection idle */
97         PQTRANS_ACTIVE,                         /* command in progress */
98         PQTRANS_INTRANS,                        /* idle, within transaction block */
99         PQTRANS_INERROR,                        /* idle, within failed transaction */
100         PQTRANS_UNKNOWN                         /* cannot determine status */
101 } PGTransactionStatusType;
102
103 typedef enum
104 {
105         PQERRORS_TERSE,                         /* single-line error messages */
106         PQERRORS_DEFAULT,                       /* recommended style */
107         PQERRORS_VERBOSE                        /* all the facts, ma'am */
108 } PGVerbosity;
109
110 /* PGconn encapsulates a connection to the backend.
111  * The contents of this struct are not supposed to be known to applications.
112  */
113 typedef struct pg_conn PGconn;
114
115 /* PGresult encapsulates the result of a query (or more precisely, of a single
116  * SQL command --- a query string given to PQsendQuery can contain multiple
117  * commands and thus return multiple PGresult objects).
118  * The contents of this struct are not supposed to be known to applications.
119  */
120 typedef struct pg_result PGresult;
121
122 /* PGcancel encapsulates the information needed to cancel a running
123  * query on an existing connection.
124  * The contents of this struct are not supposed to be known to applications.
125  */
126 typedef struct pg_cancel PGcancel;
127
128 /* PGnotify represents the occurrence of a NOTIFY message.
129  * Ideally this would be an opaque typedef, but it's so simple that it's
130  * unlikely to change.
131  * NOTE: in Postgres 6.4 and later, the be_pid is the notifying backend's,
132  * whereas in earlier versions it was always your own backend's PID.
133  */
134 typedef struct pgNotify
135 {
136         char       *relname;            /* notification condition name */
137         int                     be_pid;                 /* process ID of notifying server process */
138         char       *extra;                      /* notification parameter */
139         /* Fields below here are private to libpq; apps should not use 'em */
140         struct pgNotify *next;          /* list link */
141 } PGnotify;
142
143 /* Function types for notice-handling callbacks */
144 typedef void (*PQnoticeReceiver) (void *arg, const PGresult *res);
145 typedef void (*PQnoticeProcessor) (void *arg, const char *message);
146
147 /* Print options for PQprint() */
148 typedef char pqbool;
149
150 typedef struct _PQprintOpt
151 {
152         pqbool          header;                 /* print output field headings and row count */
153         pqbool          align;                  /* fill align the fields */
154         pqbool          standard;               /* old brain dead format */
155         pqbool          html3;                  /* output html tables */
156         pqbool          expanded;               /* expand tables */
157         pqbool          pager;                  /* use pager for output if needed */
158         char       *fieldSep;           /* field separator */
159         char       *tableOpt;           /* insert to HTML <table ...> */
160         char       *caption;            /* HTML <caption> */
161         char      **fieldName;          /* null terminated array of replacement field
162                                                                  * names */
163 } PQprintOpt;
164
165 /* ----------------
166  * Structure for the conninfo parameter definitions returned by PQconndefaults
167  * or PQconninfoParse.
168  *
169  * All fields except "val" point at static strings which must not be altered.
170  * "val" is either NULL or a malloc'd current-value string.  PQconninfoFree()
171  * will release both the val strings and the PQconninfoOption array itself.
172  * ----------------
173  */
174 typedef struct _PQconninfoOption
175 {
176         char       *keyword;            /* The keyword of the option                    */
177         char       *envvar;                     /* Fallback environment variable name   */
178         char       *compiled;           /* Fallback compiled in default value   */
179         char       *val;                        /* Option's current value, or NULL               */
180         char       *label;                      /* Label for field in connect dialog    */
181         char       *dispchar;           /* Indicates how to display this field in a
182                                                                  * connect dialog. Values are: "" Display
183                                                                  * entered value as is "*" Password field -
184                                                                  * hide value "D"  Debug option - don't show
185                                                                  * by default */
186         int                     dispsize;               /* Field size in characters for dialog  */
187 } PQconninfoOption;
188
189 /* ----------------
190  * PQArgBlock -- structure for PQfn() arguments
191  * ----------------
192  */
193 typedef struct
194 {
195         int                     len;
196         int                     isint;
197         union
198         {
199                 int                *ptr;                /* can't use void (dec compiler barfs)   */
200                 int                     integer;
201         }                       u;
202 } PQArgBlock;
203
204 /* ----------------
205  * PGresAttDesc -- Data about a single attribute (column) of a query result
206  * ----------------
207  */
208 typedef struct pgresAttDesc
209 {
210         char       *name;                       /* column name */
211         Oid                     tableid;                /* source table, if known */
212         int                     columnid;               /* source column, if known */
213         int                     format;                 /* format code for value (text/binary) */
214         Oid                     typid;                  /* type id */
215         int                     typlen;                 /* type size */
216         int                     atttypmod;              /* type-specific modifier info */
217 } PGresAttDesc;
218
219 /* ----------------
220  * Exported functions of libpq
221  * ----------------
222  */
223
224 /* ===  in fe-connect.c === */
225
226 /* make a new client connection to the backend */
227 /* Asynchronous (non-blocking) */
228 extern PGconn *PQconnectStart(const char *conninfo);
229 extern PostgresPollingStatusType PQconnectPoll(PGconn *conn);
230
231 /* Synchronous (blocking) */
232 extern PGconn *PQconnectdb(const char *conninfo);
233 extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport,
234                          const char *pgoptions, const char *pgtty,
235                          const char *dbName,
236                          const char *login, const char *pwd);
237
238 #define PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME)  \
239         PQsetdbLogin(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME, NULL, NULL)
240
241 /* close the current connection and free the PGconn data structure */
242 extern void PQfinish(PGconn *conn);
243
244 /* get info about connection options known to PQconnectdb */
245 extern PQconninfoOption *PQconndefaults(void);
246
247 /* parse connection options in same way as PQconnectdb */
248 extern PQconninfoOption *PQconninfoParse(const char *conninfo, char **errmsg);
249
250 /* free the data structure returned by PQconndefaults() or PQconninfoParse() */
251 extern void PQconninfoFree(PQconninfoOption *connOptions);
252
253 /*
254  * close the current connection and restablish a new one with the same
255  * parameters
256  */
257 /* Asynchronous (non-blocking) */
258 extern int      PQresetStart(PGconn *conn);
259 extern PostgresPollingStatusType PQresetPoll(PGconn *conn);
260
261 /* Synchronous (blocking) */
262 extern void PQreset(PGconn *conn);
263
264 /* request a cancel structure */
265 extern PGcancel *PQgetCancel(PGconn *conn);
266
267 /* free a cancel structure */
268 extern void PQfreeCancel(PGcancel *cancel);
269
270 /* issue a cancel request */
271 extern int      PQcancel(PGcancel *cancel, char *errbuf, int errbufsize);
272
273 /* backwards compatible version of PQcancel; not thread-safe */
274 extern int      PQrequestCancel(PGconn *conn);
275
276 /* Accessor functions for PGconn objects */
277 extern char *PQdb(const PGconn *conn);
278 extern char *PQuser(const PGconn *conn);
279 extern char *PQpass(const PGconn *conn);
280 extern char *PQhost(const PGconn *conn);
281 extern char *PQport(const PGconn *conn);
282 extern char *PQtty(const PGconn *conn);
283 extern char *PQoptions(const PGconn *conn);
284 extern ConnStatusType PQstatus(const PGconn *conn);
285 extern PGTransactionStatusType PQtransactionStatus(const PGconn *conn);
286 extern const char *PQparameterStatus(const PGconn *conn,
287                                   const char *paramName);
288 extern int      PQprotocolVersion(const PGconn *conn);
289 extern int      PQserverVersion(const PGconn *conn);
290 extern char *PQerrorMessage(const PGconn *conn);
291 extern int      PQsocket(const PGconn *conn);
292 extern int      PQbackendPID(const PGconn *conn);
293 extern int      PQconnectionNeedsPassword(const PGconn *conn);
294 extern int      PQconnectionUsedPassword(const PGconn *conn);
295 extern int      PQclientEncoding(const PGconn *conn);
296 extern int      PQsetClientEncoding(PGconn *conn, const char *encoding);
297
298 /* Get the OpenSSL structure associated with a connection. Returns NULL for
299  * unencrypted connections or if any other TLS library is in use. */
300 extern void *PQgetssl(PGconn *conn);
301
302 /* Tell libpq whether it needs to initialize OpenSSL */
303 extern void PQinitSSL(int do_init);
304
305 /* Set verbosity for PQerrorMessage and PQresultErrorMessage */
306 extern PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity);
307
308 /* Enable/disable tracing */
309 extern void PQtrace(PGconn *conn, FILE *debug_port);
310 extern void PQuntrace(PGconn *conn);
311
312 /* Override default notice handling routines */
313 extern PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn,
314                                         PQnoticeReceiver proc,
315                                         void *arg);
316 extern PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
317                                          PQnoticeProcessor proc,
318                                          void *arg);
319
320 /*
321  *         Used to set callback that prevents concurrent access to
322  *         non-thread safe functions that libpq needs.
323  *         The default implementation uses a libpq internal mutex.
324  *         Only required for multithreaded apps that use kerberos
325  *         both within their app and for postgresql connections.
326  */
327 typedef void (*pgthreadlock_t) (int acquire);
328
329 extern pgthreadlock_t PQregisterThreadLock(pgthreadlock_t newhandler);
330
331 /* === in fe-exec.c === */
332
333 /* Simple synchronous query */
334 extern PGresult *PQexec(PGconn *conn, const char *query);
335 extern PGresult *PQexecParams(PGconn *conn,
336                          const char *command,
337                          int nParams,
338                          const Oid *paramTypes,
339                          const char *const * paramValues,
340                          const int *paramLengths,
341                          const int *paramFormats,
342                          int resultFormat);
343 extern PGresult *PQprepare(PGconn *conn, const char *stmtName,
344                   const char *query, int nParams,
345                   const Oid *paramTypes);
346 extern PGresult *PQexecPrepared(PGconn *conn,
347                            const char *stmtName,
348                            int nParams,
349                            const char *const * paramValues,
350                            const int *paramLengths,
351                            const int *paramFormats,
352                            int resultFormat);
353
354 /* Interface for multiple-result or asynchronous queries */
355 extern int      PQsendQuery(PGconn *conn, const char *query);
356 extern int PQsendQueryParams(PGconn *conn,
357                                   const char *command,
358                                   int nParams,
359                                   const Oid *paramTypes,
360                                   const char *const * paramValues,
361                                   const int *paramLengths,
362                                   const int *paramFormats,
363                                   int resultFormat);
364 extern int PQsendPrepare(PGconn *conn, const char *stmtName,
365                           const char *query, int nParams,
366                           const Oid *paramTypes);
367 extern int PQsendQueryPrepared(PGconn *conn,
368                                         const char *stmtName,
369                                         int nParams,
370                                         const char *const * paramValues,
371                                         const int *paramLengths,
372                                         const int *paramFormats,
373                                         int resultFormat);
374 extern PGresult *PQgetResult(PGconn *conn);
375
376 /* Routines for managing an asynchronous query */
377 extern int      PQisBusy(PGconn *conn);
378 extern int      PQconsumeInput(PGconn *conn);
379
380 /* LISTEN/NOTIFY support */
381 extern PGnotify *PQnotifies(PGconn *conn);
382
383 /* Routines for copy in/out */
384 extern int      PQputCopyData(PGconn *conn, const char *buffer, int nbytes);
385 extern int      PQputCopyEnd(PGconn *conn, const char *errormsg);
386 extern int      PQgetCopyData(PGconn *conn, char **buffer, int async);
387
388 /* Deprecated routines for copy in/out */
389 extern int      PQgetline(PGconn *conn, char *string, int length);
390 extern int      PQputline(PGconn *conn, const char *string);
391 extern int      PQgetlineAsync(PGconn *conn, char *buffer, int bufsize);
392 extern int      PQputnbytes(PGconn *conn, const char *buffer, int nbytes);
393 extern int      PQendcopy(PGconn *conn);
394
395 /* Set blocking/nonblocking connection to the backend */
396 extern int      PQsetnonblocking(PGconn *conn, int arg);
397 extern int      PQisnonblocking(const PGconn *conn);
398 extern int      PQisthreadsafe(void);
399
400 /* Force the write buffer to be written (or at least try) */
401 extern int      PQflush(PGconn *conn);
402
403 /*
404  * "Fast path" interface --- not really recommended for application
405  * use
406  */
407 extern PGresult *PQfn(PGconn *conn,
408          int fnid,
409          int *result_buf,
410          int *result_len,
411          int result_is_int,
412          const PQArgBlock *args,
413          int nargs);
414
415 /* Accessor functions for PGresult objects */
416 extern ExecStatusType PQresultStatus(const PGresult *res);
417 extern char *PQresStatus(ExecStatusType status);
418 extern char *PQresultErrorMessage(const PGresult *res);
419 extern char *PQresultErrorField(const PGresult *res, int fieldcode);
420 extern int      PQntuples(const PGresult *res);
421 extern int      PQnfields(const PGresult *res);
422 extern int      PQbinaryTuples(const PGresult *res);
423 extern char *PQfname(const PGresult *res, int field_num);
424 extern int      PQfnumber(const PGresult *res, const char *field_name);
425 extern Oid      PQftable(const PGresult *res, int field_num);
426 extern int      PQftablecol(const PGresult *res, int field_num);
427 extern int      PQfformat(const PGresult *res, int field_num);
428 extern Oid      PQftype(const PGresult *res, int field_num);
429 extern int      PQfsize(const PGresult *res, int field_num);
430 extern int      PQfmod(const PGresult *res, int field_num);
431 extern char *PQcmdStatus(PGresult *res);
432 extern char *PQoidStatus(const PGresult *res);  /* old and ugly */
433 extern Oid      PQoidValue(const PGresult *res);        /* new and improved */
434 extern char *PQcmdTuples(PGresult *res);
435 extern char *PQgetvalue(const PGresult *res, int tup_num, int field_num);
436 extern int      PQgetlength(const PGresult *res, int tup_num, int field_num);
437 extern int      PQgetisnull(const PGresult *res, int tup_num, int field_num);
438 extern int      PQnparams(const PGresult *res);
439 extern Oid      PQparamtype(const PGresult *res, int param_num);
440
441 /* Describe prepared statements and portals */
442 extern PGresult *PQdescribePrepared(PGconn *conn, const char *stmt);
443 extern PGresult *PQdescribePortal(PGconn *conn, const char *portal);
444 extern int      PQsendDescribePrepared(PGconn *conn, const char *stmt);
445 extern int      PQsendDescribePortal(PGconn *conn, const char *portal);
446
447 /* Delete a PGresult */
448 extern void PQclear(PGresult *res);
449
450 /* For freeing other alloc'd results, such as PGnotify structs */
451 extern void PQfreemem(void *ptr);
452
453 /* Exists for backward compatibility.  bjm 2003-03-24 */
454 #define PQfreeNotify(ptr) PQfreemem(ptr)
455
456 /* Error when no password was given. */
457 /* Note: depending on this is deprecated; use PQconnectionNeedsPassword(). */
458 #define PQnoPasswordSupplied    "fe_sendauth: no password supplied\n"
459
460 /* Create and manipulate PGresults */
461 extern PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
462 extern PGresult *PQcopyResult(const PGresult *src, int flags);
463 extern int PQsetResultAttrs(PGresult *res, int numAttributes, PGresAttDesc *attDescs);
464 extern void *PQresultAlloc(PGresult *res, size_t nBytes);
465 extern int PQsetvalue(PGresult *res, int tup_num, int field_num, char *value, int len);
466
467 /* Quoting strings before inclusion in queries. */
468 extern size_t PQescapeStringConn(PGconn *conn,
469                                    char *to, const char *from, size_t length,
470                                    int *error);
471 extern unsigned char *PQescapeByteaConn(PGconn *conn,
472                                   const unsigned char *from, size_t from_length,
473                                   size_t *to_length);
474 extern unsigned char *PQunescapeBytea(const unsigned char *strtext,
475                                 size_t *retbuflen);
476
477 /* These forms are deprecated! */
478 extern size_t PQescapeString(char *to, const char *from, size_t length);
479 extern unsigned char *PQescapeBytea(const unsigned char *from, size_t from_length,
480                           size_t *to_length);
481
482
483
484 /* === in fe-print.c === */
485
486 extern void
487 PQprint(FILE *fout,                             /* output stream */
488                 const PGresult *res,
489                 const PQprintOpt *ps);  /* option structure */
490
491 /*
492  * really old printing routines
493  */
494 extern void
495 PQdisplayTuples(const PGresult *res,
496                                 FILE *fp,               /* where to send the output */
497                                 int fillAlign,  /* pad the fields with spaces */
498                                 const char *fieldSep,   /* field separator */
499                                 int printHeader,        /* display headers? */
500                                 int quiet);
501
502 extern void
503 PQprintTuples(const PGresult *res,
504                           FILE *fout,           /* output stream */
505                           int printAttName, /* print attribute names */
506                           int terseOutput,      /* delimiter bars */
507                           int width);           /* width of column, if 0, use variable width */
508
509
510 /* === in fe-lobj.c === */
511
512 /* Large-object access routines */
513 extern int      lo_open(PGconn *conn, Oid lobjId, int mode);
514 extern int      lo_close(PGconn *conn, int fd);
515 extern int      lo_read(PGconn *conn, int fd, char *buf, size_t len);
516 extern int      lo_write(PGconn *conn, int fd, const char *buf, size_t len);
517 extern int      lo_lseek(PGconn *conn, int fd, int offset, int whence);
518 extern Oid      lo_creat(PGconn *conn, int mode);
519 extern Oid      lo_create(PGconn *conn, Oid lobjId);
520 extern int      lo_tell(PGconn *conn, int fd);
521 extern int      lo_truncate(PGconn *conn, int fd, size_t len);
522 extern int      lo_unlink(PGconn *conn, Oid lobjId);
523 extern Oid      lo_import(PGconn *conn, const char *filename);
524 extern Oid      lo_import_with_oid(PGconn *conn, const char *filename, Oid lobjId);
525 extern int      lo_export(PGconn *conn, Oid lobjId, const char *filename);
526
527 /* === in fe-misc.c === */
528
529 /* Determine length of multibyte encoded char at *s */
530 extern int      PQmblen(const char *s, int encoding);
531
532 /* Determine display length of multibyte encoded char at *s */
533 extern int      PQdsplen(const char *s, int encoding);
534
535 /* Get encoding id from environment variable PGCLIENTENCODING */
536 extern int      PQenv2encoding(void);
537
538 /* === in fe-auth.c === */
539
540 extern char *PQencryptPassword(const char *passwd, const char *user);
541
542 /* === in encnames.c === */
543
544 extern int      pg_char_to_encoding(const char *name);
545 extern const char *pg_encoding_to_char(int encoding);
546 extern int      pg_valid_server_encoding_id(int encoding);
547
548 #ifdef __cplusplus
549 }
550 #endif
551
552 #endif   /* LIBPQ_FE_H */