]> granicus.if.org Git - postgresql/blob - src/interfaces/libpq/libpq-fe.h
Add an officially exported libpq function to encrypt passwords, and
[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-2005, 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.123 2005/12/23 01:16:38 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 only
51          * 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 backwards
71                                                                  * 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, PGresult
82                                                                  * 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 the
86                                                                  * 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 /* PGcancel encapsulates the information needed to cancel a running
120  * query on an existing connection.
121  * The contents of this struct are not supposed to be known to applications.
122  */
123 typedef struct pg_cancel PGcancel;
124
125 /* PGnotify represents the occurrence of a NOTIFY message.
126  * Ideally this would be an opaque typedef, but it's so simple that it's
127  * unlikely to change.
128  * NOTE: in Postgres 6.4 and later, the be_pid is the notifying backend's,
129  * whereas in earlier versions it was always your own backend's PID.
130  */
131 typedef struct pgNotify
132 {
133         char       *relname;            /* notification condition name */
134         int                     be_pid;                 /* process ID of server process */
135         char       *extra;                      /* notification parameter */
136         /* Fields below here are private to libpq; apps should not use 'em */
137         struct pgNotify *next;          /* list link */
138 } PGnotify;
139
140 /* Function types for notice-handling callbacks */
141 typedef void (*PQnoticeReceiver) (void *arg, const PGresult *res);
142 typedef void (*PQnoticeProcessor) (void *arg, const char *message);
143
144 /* Print options for PQprint() */
145 typedef char pqbool;
146
147 typedef struct _PQprintOpt
148 {
149         pqbool          header;                 /* print output field headings and row count */
150         pqbool          align;                  /* fill align the fields */
151         pqbool          standard;               /* old brain dead format */
152         pqbool          html3;                  /* output html tables */
153         pqbool          expanded;               /* expand tables */
154         pqbool          pager;                  /* use pager for output if needed */
155         char       *fieldSep;           /* field separator */
156         char       *tableOpt;           /* insert to HTML <table ...> */
157         char       *caption;            /* HTML <caption> */
158         char      **fieldName;          /* null terminated array of replacement field
159                                                                  * names */
160 } PQprintOpt;
161
162 /* ----------------
163  * Structure for the conninfo parameter definitions returned by PQconndefaults
164  *
165  * All fields except "val" point at static strings which must not be altered.
166  * "val" is either NULL or a malloc'd current-value string.  PQconninfoFree()
167  * will release both the val strings and the PQconninfoOption array itself.
168  * ----------------
169  */
170 typedef struct _PQconninfoOption
171 {
172         char       *keyword;            /* The keyword of the option                    */
173         char       *envvar;                     /* Fallback environment variable name   */
174         char       *compiled;           /* Fallback compiled in default value   */
175         char       *val;                        /* Option's current value, or NULL               */
176         char       *label;                      /* Label for field in connect dialog    */
177         char       *dispchar;           /* Character to display for this field in a
178                                                                  * connect dialog. Values are: "" Display
179                                                                  * entered value as is "*" Password field -
180                                                                  * hide value "D"  Debug option - don't show
181                                                                  * by default */
182         int                     dispsize;               /* Field size in characters for dialog  */
183 } PQconninfoOption;
184
185 /* ----------------
186  * PQArgBlock -- structure for PQfn() arguments
187  * ----------------
188  */
189 typedef struct
190 {
191         int                     len;
192         int                     isint;
193         union
194         {
195                 int                *ptr;                /* can't use void (dec compiler barfs)   */
196                 int                     integer;
197         }                       u;
198 } PQArgBlock;
199
200 /* ----------------
201  * Exported functions of libpq
202  * ----------------
203  */
204
205 /* ===  in fe-connect.c === */
206
207 /* make a new client connection to the backend */
208 /* Asynchronous (non-blocking) */
209 extern PGconn *PQconnectStart(const char *conninfo);
210 extern PostgresPollingStatusType PQconnectPoll(PGconn *conn);
211
212 /* Synchronous (blocking) */
213 extern PGconn *PQconnectdb(const char *conninfo);
214 extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport,
215                          const char *pgoptions, const char *pgtty,
216                          const char *dbName,
217                          const char *login, const char *pwd);
218
219 #define PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME)  \
220         PQsetdbLogin(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME, NULL, NULL)
221
222 /* close the current connection and free the PGconn data structure */
223 extern void PQfinish(PGconn *conn);
224
225 /* get info about connection options known to PQconnectdb */
226 extern PQconninfoOption *PQconndefaults(void);
227
228 /* free the data structure returned by PQconndefaults() */
229 extern void PQconninfoFree(PQconninfoOption *connOptions);
230
231 /*
232  * close the current connection and restablish a new one with the same
233  * parameters
234  */
235 /* Asynchronous (non-blocking) */
236 extern int      PQresetStart(PGconn *conn);
237 extern PostgresPollingStatusType PQresetPoll(PGconn *conn);
238
239 /* Synchronous (blocking) */
240 extern void PQreset(PGconn *conn);
241
242 /* request a cancel structure */
243 extern PGcancel *PQgetCancel(PGconn *conn);
244
245 /* free a cancel structure */
246 extern void PQfreeCancel(PGcancel *cancel);
247
248 /* issue a cancel request */
249 extern int      PQcancel(PGcancel *cancel, char *errbuf, int errbufsize);
250
251 /* backwards compatible version of PQcancel; not thread-safe */
252 extern int      PQrequestCancel(PGconn *conn);
253
254 /* Accessor functions for PGconn objects */
255 extern char *PQdb(const PGconn *conn);
256 extern char *PQuser(const PGconn *conn);
257 extern char *PQpass(const PGconn *conn);
258 extern char *PQhost(const PGconn *conn);
259 extern char *PQport(const PGconn *conn);
260 extern char *PQtty(const PGconn *conn);
261 extern char *PQoptions(const PGconn *conn);
262 extern ConnStatusType PQstatus(const PGconn *conn);
263 extern PGTransactionStatusType PQtransactionStatus(const PGconn *conn);
264 extern const char *PQparameterStatus(const PGconn *conn,
265                                   const char *paramName);
266 extern int      PQprotocolVersion(const PGconn *conn);
267 extern int      PQserverVersion(const PGconn *conn);
268 extern char *PQerrorMessage(const PGconn *conn);
269 extern int      PQsocket(const PGconn *conn);
270 extern int      PQbackendPID(const PGconn *conn);
271 extern int      PQclientEncoding(const PGconn *conn);
272 extern int      PQsetClientEncoding(PGconn *conn, const char *encoding);
273
274 #ifdef USE_SSL
275 /* Get the SSL structure associated with a connection */
276 extern SSL *PQgetssl(PGconn *conn);
277 #else
278 extern void *PQgetssl(PGconn *conn);
279 #endif
280
281 /* Tell libpq whether it needs to initialize OpenSSL */
282 extern void PQinitSSL(int do_init);
283
284 /* Set verbosity for PQerrorMessage and PQresultErrorMessage */
285 extern PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity);
286
287 /* Enable/disable tracing */
288 extern void PQtrace(PGconn *conn, FILE *debug_port);
289 extern void PQuntrace(PGconn *conn);
290
291 /* Override default notice handling routines */
292 extern PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn,
293                                         PQnoticeReceiver proc,
294                                         void *arg);
295 extern PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
296                                          PQnoticeProcessor proc,
297                                          void *arg);
298
299 /*
300  *         Used to set callback that prevents concurrent access to
301  *         non-thread safe functions that libpq needs.
302  *         The default implementation uses a libpq internal mutex.
303  *         Only required for multithreaded apps that use kerberos
304  *         both within their app and for postgresql connections.
305  */
306 typedef void (*pgthreadlock_t) (int acquire);
307
308 extern pgthreadlock_t PQregisterThreadLock(pgthreadlock_t newhandler);
309
310 /* === in fe-exec.c === */
311
312 /* Simple synchronous query */
313 extern PGresult *PQexec(PGconn *conn, const char *query);
314 extern PGresult *PQexecParams(PGconn *conn,
315                          const char *command,
316                          int nParams,
317                          const Oid *paramTypes,
318                          const char *const * paramValues,
319                          const int *paramLengths,
320                          const int *paramFormats,
321                          int resultFormat);
322 extern PGresult *PQprepare(PGconn *conn, const char *stmtName,
323                   const char *query, int nParams,
324                   const Oid *paramTypes);
325 extern PGresult *PQexecPrepared(PGconn *conn,
326                            const char *stmtName,
327                            int nParams,
328                            const char *const * paramValues,
329                            const int *paramLengths,
330                            const int *paramFormats,
331                            int resultFormat);
332
333 /* Interface for multiple-result or asynchronous queries */
334 extern int      PQsendQuery(PGconn *conn, const char *query);
335 extern int PQsendQueryParams(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 int PQsendPrepare(PGconn *conn, const char *stmtName,
344                           const char *query, int nParams,
345                           const Oid *paramTypes);
346 extern int PQsendQueryPrepared(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 extern PGresult *PQgetResult(PGconn *conn);
354
355 /* Routines for managing an asynchronous query */
356 extern int      PQisBusy(PGconn *conn);
357 extern int      PQconsumeInput(PGconn *conn);
358
359 /* LISTEN/NOTIFY support */
360 extern PGnotify *PQnotifies(PGconn *conn);
361
362 /* Routines for copy in/out */
363 extern int      PQputCopyData(PGconn *conn, const char *buffer, int nbytes);
364 extern int      PQputCopyEnd(PGconn *conn, const char *errormsg);
365 extern int      PQgetCopyData(PGconn *conn, char **buffer, int async);
366
367 /* Deprecated routines for copy in/out */
368 extern int      PQgetline(PGconn *conn, char *string, int length);
369 extern int      PQputline(PGconn *conn, const char *string);
370 extern int      PQgetlineAsync(PGconn *conn, char *buffer, int bufsize);
371 extern int      PQputnbytes(PGconn *conn, const char *buffer, int nbytes);
372 extern int      PQendcopy(PGconn *conn);
373
374 /* Set blocking/nonblocking connection to the backend */
375 extern int      PQsetnonblocking(PGconn *conn, int arg);
376 extern int      PQisnonblocking(const PGconn *conn);
377
378 /* Force the write buffer to be written (or at least try) */
379 extern int      PQflush(PGconn *conn);
380
381 /*
382  * "Fast path" interface --- not really recommended for application
383  * use
384  */
385 extern PGresult *PQfn(PGconn *conn,
386          int fnid,
387          int *result_buf,
388          int *result_len,
389          int result_is_int,
390          const PQArgBlock *args,
391          int nargs);
392
393 /* Accessor functions for PGresult objects */
394 extern ExecStatusType PQresultStatus(const PGresult *res);
395 extern char *PQresStatus(ExecStatusType status);
396 extern char *PQresultErrorMessage(const PGresult *res);
397 extern char *PQresultErrorField(const PGresult *res, int fieldcode);
398 extern int      PQntuples(const PGresult *res);
399 extern int      PQnfields(const PGresult *res);
400 extern int      PQbinaryTuples(const PGresult *res);
401 extern char *PQfname(const PGresult *res, int field_num);
402 extern int      PQfnumber(const PGresult *res, const char *field_name);
403 extern Oid      PQftable(const PGresult *res, int field_num);
404 extern int      PQftablecol(const PGresult *res, int field_num);
405 extern int      PQfformat(const PGresult *res, int field_num);
406 extern Oid      PQftype(const PGresult *res, int field_num);
407 extern int      PQfsize(const PGresult *res, int field_num);
408 extern int      PQfmod(const PGresult *res, int field_num);
409 extern char *PQcmdStatus(PGresult *res);
410 extern char *PQoidStatus(const PGresult *res);  /* old and ugly */
411 extern Oid      PQoidValue(const PGresult *res);        /* new and improved */
412 extern char *PQcmdTuples(PGresult *res);
413 extern char *PQgetvalue(const PGresult *res, int tup_num, int field_num);
414 extern int      PQgetlength(const PGresult *res, int tup_num, int field_num);
415 extern int      PQgetisnull(const PGresult *res, int tup_num, int field_num);
416
417 /* Delete a PGresult */
418 extern void PQclear(PGresult *res);
419
420 /* For freeing other alloc'd results, such as PGnotify structs */
421 extern void PQfreemem(void *ptr);
422
423 /* Exists for backward compatibility.  bjm 2003-03-24 */
424 #define PQfreeNotify(ptr) PQfreemem(ptr)
425
426 /* Define the string so all uses are consistent. */
427 #define PQnoPasswordSupplied    "fe_sendauth: no password supplied\n"
428
429 /*
430  * Make an empty PGresult with given status (some apps find this
431  * useful). If conn is not NULL and status indicates an error, the
432  * conn's errorMessage is copied.
433  */
434 extern PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
435
436
437 /* Quoting strings before inclusion in queries. */
438 extern size_t PQescapeString(char *to, const char *from, size_t length);
439 extern unsigned char *PQescapeBytea(const unsigned char *bintext, size_t binlen,
440                           size_t *bytealen);
441 extern unsigned char *PQunescapeBytea(const unsigned char *strtext,
442                                 size_t *retbuflen);
443
444
445
446 /* === in fe-print.c === */
447
448 extern void
449 PQprint(FILE *fout,                             /* output stream */
450                 const PGresult *res,
451                 const PQprintOpt *ps);  /* option structure */
452
453 /*
454  * really old printing routines
455  */
456 extern void
457 PQdisplayTuples(const PGresult *res,
458                                 FILE *fp,               /* where to send the output */
459                                 int fillAlign,  /* pad the fields with spaces */
460                                 const char *fieldSep,   /* field separator */
461                                 int printHeader,        /* display headers? */
462                                 int quiet);
463
464 extern void
465 PQprintTuples(const PGresult *res,
466                           FILE *fout,           /* output stream */
467                           int printAttName, /* print attribute names */
468                           int terseOutput,      /* delimiter bars */
469                           int width);           /* width of column, if 0, use variable width */
470
471
472 /* === in fe-lobj.c === */
473
474 /* Large-object access routines */
475 extern int      lo_open(PGconn *conn, Oid lobjId, int mode);
476 extern int      lo_close(PGconn *conn, int fd);
477 extern int      lo_read(PGconn *conn, int fd, char *buf, size_t len);
478 extern int      lo_write(PGconn *conn, int fd, char *buf, size_t len);
479 extern int      lo_lseek(PGconn *conn, int fd, int offset, int whence);
480 extern Oid      lo_creat(PGconn *conn, int mode);
481 extern Oid      lo_create(PGconn *conn, Oid lobjId);
482 extern int      lo_tell(PGconn *conn, int fd);
483 extern int      lo_unlink(PGconn *conn, Oid lobjId);
484 extern Oid      lo_import(PGconn *conn, const char *filename);
485 extern int      lo_export(PGconn *conn, Oid lobjId, const char *filename);
486
487 /* === in fe-misc.c === */
488
489 /* Determine length of multibyte encoded char at *s */
490 extern int      PQmblen(const char *s, int encoding);
491
492 /* Determine display length of multibyte encoded char at *s */
493 extern int      PQdsplen(const char *s, int encoding);
494
495 /* Get encoding id from environment variable PGCLIENTENCODING */
496 extern int      PQenv2encoding(void);
497
498 /* === in fe-auth.c === */
499
500 extern char *pg_make_encrypted_password(const char *passwd, const char *user);
501
502 #ifdef __cplusplus
503 }
504 #endif
505
506 #endif   /* LIBPQ_FE_H */