]> granicus.if.org Git - postgresql/blob - src/interfaces/libpq/libpq-fe.h
Invent a new, more thread-safe version of PQrequestCancel, called PQcancel.
[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-2004, 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.113 2004/10/30 23:11:27 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
43          * would 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 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,
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 /* 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
150                                                                  * count */
151         pqbool          align;                  /* fill align the fields */
152         pqbool          standard;               /* old brain dead format */
153         pqbool          html3;                  /* output html tables */
154         pqbool          expanded;               /* expand tables */
155         pqbool          pager;                  /* use pager for output if needed */
156         char       *fieldSep;           /* field separator */
157         char       *tableOpt;           /* insert to HTML <table ...> */
158         char       *caption;            /* HTML <caption> */
159         char      **fieldName;          /* null terminated array of repalcement
160                                                                  * field names */
161 } PQprintOpt;
162
163 /* ----------------
164  * Structure for the conninfo parameter definitions returned by PQconndefaults
165  *
166  * All fields except "val" point at static strings which must not be altered.
167  * "val" is either NULL or a malloc'd current-value string.  PQconninfoFree()
168  * will release both the val strings and the PQconninfoOption array itself.
169  * ----------------
170  */
171 typedef struct _PQconninfoOption
172 {
173         char       *keyword;            /* The keyword of the option                    */
174         char       *envvar;                     /* Fallback environment variable name   */
175         char       *compiled;           /* Fallback compiled in default value   */
176         char       *val;                        /* Option's current value, or NULL               */
177         char       *label;                      /* Label for field in connect dialog    */
178         char       *dispchar;           /* Character to display for this field in
179                                                                  * a connect dialog. Values are: ""
180                                                                  * Display entered value as is "*"
181                                                                  * Password field - hide value "D"      Debug
182                                                                  * option - don't show by default */
183         int                     dispsize;               /* Field size in characters for dialog  */
184 } PQconninfoOption;
185
186 /* ----------------
187  * PQArgBlock -- structure for PQfn() arguments
188  * ----------------
189  */
190 typedef struct
191 {
192         int                     len;
193         int                     isint;
194         union
195         {
196                 int                *ptr;                /* can't use void (dec compiler barfs)   */
197                 int                     integer;
198         }                       u;
199 } PQArgBlock;
200
201 /* ----------------
202  * Exported functions of libpq
203  * ----------------
204  */
205
206 /* ===  in fe-connect.c === */
207
208 /* make a new client connection to the backend */
209 /* Asynchronous (non-blocking) */
210 extern PGconn *PQconnectStart(const char *conninfo);
211 extern PostgresPollingStatusType PQconnectPoll(PGconn *conn);
212
213 /* Synchronous (blocking) */
214 extern PGconn *PQconnectdb(const char *conninfo);
215 extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport,
216                          const char *pgoptions, const char *pgtty,
217                          const char *dbName,
218                          const char *login, const char *pwd);
219
220 #define PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME)  \
221         PQsetdbLogin(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME, NULL, NULL)
222
223 /* close the current connection and free the PGconn data structure */
224 extern void PQfinish(PGconn *conn);
225
226 /* get info about connection options known to PQconnectdb */
227 extern PQconninfoOption *PQconndefaults(void);
228
229 /* free the data structure returned by PQconndefaults() */
230 extern void PQconninfoFree(PQconninfoOption *connOptions);
231
232 /*
233  * close the current connection and restablish a new one with the same
234  * parameters
235  */
236 /* Asynchronous (non-blocking) */
237 extern int      PQresetStart(PGconn *conn);
238 extern PostgresPollingStatusType PQresetPoll(PGconn *conn);
239
240 /* Synchronous (blocking) */
241 extern void PQreset(PGconn *conn);
242
243 /* request a cancel structure */
244 extern PGcancel *PQgetCancel(PGconn *conn);
245
246 /* free a cancel structure */
247 extern void PQfreeCancel(PGcancel *cancel);
248
249 /* issue a cancel request */
250 extern int      PQcancel(PGcancel *cancel, char *errbuf, int errbufsize);
251
252 /* backwards compatible version of PQcancel; not thread-safe */
253 extern int      PQrequestCancel(PGconn *conn);
254
255 /* Accessor functions for PGconn objects */
256 extern char *PQdb(const PGconn *conn);
257 extern char *PQuser(const PGconn *conn);
258 extern char *PQpass(const PGconn *conn);
259 extern char *PQhost(const PGconn *conn);
260 extern char *PQport(const PGconn *conn);
261 extern char *PQtty(const PGconn *conn);
262 extern char *PQoptions(const PGconn *conn);
263 extern ConnStatusType PQstatus(const PGconn *conn);
264 extern PGTransactionStatusType PQtransactionStatus(const PGconn *conn);
265 extern const char *PQparameterStatus(const PGconn *conn,
266                                   const char *paramName);
267 extern int      PQprotocolVersion(const PGconn *conn);
268 extern int      PQserverVersion(const PGconn *conn);
269 extern char *PQerrorMessage(const PGconn *conn);
270 extern int      PQsocket(const PGconn *conn);
271 extern int      PQbackendPID(const PGconn *conn);
272 extern int      PQclientEncoding(const PGconn *conn);
273 extern int      PQsetClientEncoding(PGconn *conn, const char *encoding);
274
275 #ifdef USE_SSL
276 /* Get the SSL structure associated with a connection */
277 extern SSL *PQgetssl(PGconn *conn);
278 #else
279 extern void *PQgetssl(PGconn *conn);
280 #endif
281
282 /* Set verbosity for PQerrorMessage and PQresultErrorMessage */
283 extern PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity);
284
285 /* Enable/disable tracing */
286 extern void PQtrace(PGconn *conn, FILE *debug_port);
287 extern void PQuntrace(PGconn *conn);
288
289 /* Override default notice handling routines */
290 extern PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn,
291                                         PQnoticeReceiver proc,
292                                         void *arg);
293 extern PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
294                                          PQnoticeProcessor proc,
295                                          void *arg);
296
297 /*
298  *         Used to set callback that prevents concurrent access to
299  *         non-thread safe functions that libpq needs.
300  *         The default implementation uses a libpq internal mutex.
301  *         Only required for multithreaded apps that use kerberos
302  *         both within their app and for postgresql connections.
303  */
304 typedef void (pgthreadlock_t) (int acquire);
305
306 extern pgthreadlock_t *PQregisterThreadLock(pgthreadlock_t *newhandler);
307
308 extern void PQinitSSL(int do_init);
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
470                                                                  * width */
471
472
473 /* === in fe-lobj.c === */
474
475 /* Large-object access routines */
476 extern int      lo_open(PGconn *conn, Oid lobjId, int mode);
477 extern int      lo_close(PGconn *conn, int fd);
478 extern int      lo_read(PGconn *conn, int fd, char *buf, size_t len);
479 extern int      lo_write(PGconn *conn, int fd, char *buf, size_t len);
480 extern int      lo_lseek(PGconn *conn, int fd, int offset, int whence);
481 extern Oid      lo_creat(PGconn *conn, int mode);
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 unsigned char *s, int encoding);
491
492 /* Determine display length of multibyte encoded char at *s */
493 extern int      PQdsplen(const unsigned char *s, int encoding);
494
495 /* Get encoding id from environment variable PGCLIENTENCODING */
496 extern int      PQenv2encoding(void);
497
498 /* === in fe-secure.c === */
499
500 /*
501  *      Indicates whether the libpq thread is in send().
502  *      Used to ignore SIGPIPE if thread is in send().
503  */
504 extern pqbool PQinSend(void);
505
506 #ifdef __cplusplus
507 }
508 #endif
509
510 #endif   /* LIBPQ_FE_H */