libpq-fe.h 12.6 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * libpq-fe.h
4 5
 *	  This file contains definitions for structures and
 *	  externs for functions used by frontend postgres applications.
6 7 8
 *
 * Copyright (c) 1994, Regents of the University of California
 *
9
 * $Id: libpq-fe.h,v 1.53 1999/11/30 03:08:19 momjian Exp $
10 11 12 13 14 15 16 17
 *
 *-------------------------------------------------------------------------
 */

#ifndef LIBPQ_FE_H
#define LIBPQ_FE_H

#ifdef __cplusplus
18
extern		"C"
19
{
20 21
#endif

22
#include <stdio.h>
23 24
/* postgres_ext.h defines the backend's externally visible types,
 * such as Oid.
25
 */
26
#include "postgres_ext.h"
27

B
Bruce Momjian 已提交
28 29
/* Application-visible enum types */

30 31
	typedef enum
	{
32 33 34 35
		/* Although you may decide to change this list in some way,
		   values which become unused should never be removed, nor
           should constants be redefined - that would break 
           compatibility with existing code.                           */
36
		CONNECTION_OK,
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
		CONNECTION_BAD,
		/* Non-blocking mode only below here */
		/* The existence of these should never be relied upon - they
		   should only be used for user feedback or similar purposes.  */
		CONNECTION_STARTED,     /* Waiting for connection to be made.  */
		CONNECTION_MADE,        /* Connection OK; waiting to send.     */
		CONNECTION_AWAITING_RESPONSE,   /* Waiting for a response
										   from the backend.           */
		CONNECTION_AUTH_RESPONSE,       /* Got an authentication
										   response; about to deal
										   with it.                    */
		CONNECTION_ERROR_RESPONSE,      /* Got an error
										   response; about to deal 
										   with it.                    */
		CONNECTION_AUTH_OK,             /* Received authentication;
										   waiting for ReadyForQuery
										   etc.                        */
		CONNECTION_SETENV               /* Negotiating environment.    */
55
	} ConnStatusType;
56

57 58 59 60 61 62 63 64 65
	typedef enum
	{
		PGRES_POLLING_FAILED = 0,
		PGRES_POLLING_READING,     /* These two indicate that one may    */
		PGRES_POLLING_WRITING,     /* use select before polling again.   */
		PGRES_POLLING_OK,
		PGRES_POLLING_ACTIVE       /* Can call poll function immediately.*/
	} PostgresPollingStatusType;

66 67 68
	typedef enum
	{
		PGRES_EMPTY_QUERY = 0,
B
Bruce Momjian 已提交
69 70 71 72 73
		PGRES_COMMAND_OK,		/* a query command that doesn't return
								 * anything was executed properly by the
								 * backend */
		PGRES_TUPLES_OK,		/* a query command that returns tuples was
								 * executed properly by the backend,
74
								 * PGresult contains the result tuples */
B
Bruce Momjian 已提交
75 76
		PGRES_COPY_OUT,			/* Copy Out data transfer in progress */
		PGRES_COPY_IN,			/* Copy In data transfer in progress */
77 78 79 80
		PGRES_BAD_RESPONSE,		/* an unexpected response was recv'd from
								 * the backend */
		PGRES_NONFATAL_ERROR,
		PGRES_FATAL_ERROR
81
	} ExecStatusType;
82

83 84 85
/* String descriptions of the ExecStatusTypes.
 * NB: direct use of this array is now deprecated; call PQresStatus() instead.
 */
B
Bruce Momjian 已提交
86
	extern const char *const pgresStatus[];
87

88 89
/* PGconn encapsulates a connection to the backend.
 * The contents of this struct are not supposed to be known to applications.
90
 */
91
	typedef struct pg_conn PGconn;
92

93 94 95 96
/* PGresult encapsulates the result of a query (or more precisely, of a single
 * SQL command --- a query string given to PQsendQuery can contain multiple
 * commands and thus return multiple PGresult objects).
 * The contents of this struct are not supposed to be known to applications.
97
 */
98
	typedef struct pg_result PGresult;
99

100 101 102 103 104 105
/* PGsetenvHandle is an opaque handle which is returned by PQsetenvStart and
 * which should be passed to PQsetenvPoll or PQsetenvAbort in order to refer
 * to the particular process being performed.
 */
	typedef struct pg_setenv_state *PGsetenvHandle;

106 107 108
/* PGnotify represents the occurrence of a NOTIFY message.
 * Ideally this would be an opaque typedef, but it's so simple that it's
 * unlikely to change.
109 110
 * NOTE: in Postgres 6.4 and later, the be_pid is the notifying backend's,
 * whereas in earlier versions it was always your own backend's PID.
M
 
Marc G. Fournier 已提交
111
 */
112 113
	typedef struct pgNotify
	{
114
		char		relname[NAMEDATALEN];		/* name of relation
115
												 * containing data */
B
Bruce Momjian 已提交
116
		int			be_pid;		/* process id of backend */
117
	} PGnotify;
118

119
/* PQnoticeProcessor is the function type for the notice-message callback.
B
Bruce Momjian 已提交
120
 */
B
Bruce Momjian 已提交
121
	typedef void (*PQnoticeProcessor) (void *arg, const char *message);
M
 
Marc G. Fournier 已提交
122

B
Bruce Momjian 已提交
123
/* Print options for PQprint() */
B
Bruce Momjian 已提交
124 125 126 127 128 129 130

	/*
	 * We can't use the conventional "bool", because we are designed to be
	 * included in a user's program, and user may already have that type
	 * defined.  Pqbool, on the other hand, is unlikely to be used.
	 */
	typedef char pqbool;
B
Bruce Momjian 已提交
131 132

	typedef struct _PQprintOpt
133
	{
134
		pqbool		header;		/* print output field headings and row
135
								 * count */
136 137 138 139 140
		pqbool		align;		/* fill align the fields */
		pqbool		standard;	/* old brain dead format */
		pqbool		html3;		/* output html tables */
		pqbool		expanded;	/* expand tables */
		pqbool		pager;		/* use pager for output if needed */
141 142 143
		char	   *fieldSep;	/* field separator */
		char	   *tableOpt;	/* insert to HTML <table ...> */
		char	   *caption;	/* HTML <caption> */
144 145
		char	  **fieldName;	/* null terminated array of repalcement
								 * field names */
B
Bruce Momjian 已提交
146
	} PQprintOpt;
147

M
Marc G. Fournier 已提交
148
/* ----------------
M
 
Marc G. Fournier 已提交
149
 * Structure for the conninfo parameter definitions returned by PQconndefaults
M
Marc G. Fournier 已提交
150 151
 * ----------------
 */
B
Bruce Momjian 已提交
152
	typedef struct _PQconninfoOption
153
	{
154
		char	   *keyword;	/* The keyword of the option			*/
B
Bruce Momjian 已提交
155
		char	   *envvar;		/* Fallback environment variable name	*/
156 157 158 159
		char	   *compiled;	/* Fallback compiled in default value	*/
		char	   *val;		/* Options value						*/
		char	   *label;		/* Label for field in connect dialog	*/
		char	   *dispchar;	/* Character to display for this field	*/
B
Bruce Momjian 已提交
160 161 162 163 164
		/* in a connect dialog. Values are:		*/
		/* ""	Display entered value as is  */
		/* "*"	Password field - hide value  */
		/* "D"	Debug options - don't 	 */
		/* create a field by default	*/
165
		int			dispsize;	/* Field size in characters for dialog	*/
B
Bruce Momjian 已提交
166
	} PQconninfoOption;
167

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
/* ----------------
 * PQArgBlock -- structure for PQfn() arguments
 * ----------------
 */
	typedef struct
	{
		int			len;
		int			isint;
		union
		{
			int		   *ptr;	/* can't use void (dec compiler barfs)	 */
			int			integer;
		}			u;
	} PQArgBlock;

M
 
Marc G. Fournier 已提交
183 184 185 186 187
/* ----------------
 * Exported functions of libpq
 * ----------------
 */

188
/* ===	in fe-connect.c === */
M
 
Marc G. Fournier 已提交
189

190
	/* make a new client connection to the backend */
191 192 193 194
	/* Asynchronous (non-blocking) */
	extern PGconn *PQconnectStart(const char *conninfo);
	extern PostgresPollingStatusType PQconnectPoll(PGconn *conn);
	/* Synchronous (blocking) */
195
	extern PGconn *PQconnectdb(const char *conninfo);
M
 
Marc G. Fournier 已提交
196 197
	extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport,
								const char *pgoptions, const char *pgtty,
198 199
								const char *dbName,
								const char *login, const char *pwd);
M
 
Marc G. Fournier 已提交
200 201 202 203
#define PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME)  \
	PQsetdbLogin(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME, NULL, NULL)

	/* get info about connection options known to PQconnectdb */
204
	extern PQconninfoOption *PQconndefaults(void);
M
 
Marc G. Fournier 已提交
205

206
	/* close the current connection and free the PGconn data structure */
207
	extern void PQfinish(PGconn *conn);
208

209 210 211 212
	/*
	 * close the current connection and restablish a new one with the same
	 * parameters
	 */
213 214 215 216
	/* Asynchronous (non-blocking) */
	extern int PQresetStart(PGconn *conn);
	extern PostgresPollingStatusType PQresetPoll(PGconn *conn);
	/* Synchronous (blocking) */
217 218
	extern void PQreset(PGconn *conn);

M
 
Marc G. Fournier 已提交
219 220 221
	/* issue a cancel request */
	extern int	PQrequestCancel(PGconn *conn);

B
Bruce Momjian 已提交
222
	/* Accessor functions for PGconn objects */
B
Bruce Momjian 已提交
223 224 225 226 227 228 229 230 231 232 233
	extern const char *PQdb(const PGconn *conn);
	extern const char *PQuser(const PGconn *conn);
	extern const char *PQpass(const PGconn *conn);
	extern const char *PQhost(const PGconn *conn);
	extern const char *PQport(const PGconn *conn);
	extern const char *PQtty(const PGconn *conn);
	extern const char *PQoptions(const PGconn *conn);
	extern ConnStatusType PQstatus(const PGconn *conn);
	extern const char *PQerrorMessage(const PGconn *conn);
	extern int	PQsocket(const PGconn *conn);
	extern int	PQbackendPID(const PGconn *conn);
B
Bruce Momjian 已提交
234 235

	/* Enable/disable tracing */
236 237
	extern void PQtrace(PGconn *conn, FILE *debug_port);
	extern void PQuntrace(PGconn *conn);
238

B
 
Bruce Momjian 已提交
239
	/* Override default notice processor */
240
	extern PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg);
B
 
Bruce Momjian 已提交
241

242 243 244 245 246 247 248 249 250
	/* Passing of environment variables */
	/* Asynchronous (non-blocking) */
	extern PGsetenvHandle PQsetenvStart(PGconn *conn);
	extern PostgresPollingStatusType PQsetenvPoll(PGsetenvHandle handle);
	extern void PQsetenvAbort(PGsetenvHandle handle);

	/* Synchronous (blocking) */
	extern int PQsetenv(PGconn *conn);

251
/* === in fe-exec.c === */
M
 
Marc G. Fournier 已提交
252

B
Bruce Momjian 已提交
253
	/* Simple synchronous query */
254
	extern PGresult *PQexec(PGconn *conn, const char *query);
B
Bruce Momjian 已提交
255
	extern PGnotify *PQnotifies(PGconn *conn);
M
 
Marc G. Fournier 已提交
256

B
Bruce Momjian 已提交
257
	/* Interface for multiple-result or asynchronous queries */
258
	extern int	PQsendQuery(PGconn *conn, const char *query);
B
Bruce Momjian 已提交
259
	extern PGresult *PQgetResult(PGconn *conn);
M
 
Marc G. Fournier 已提交
260

B
Bruce Momjian 已提交
261 262
	/* Routines for managing an asychronous query */
	extern int	PQisBusy(PGconn *conn);
263
	extern int	PQconsumeInput(PGconn *conn);
M
 
Marc G. Fournier 已提交
264

B
Bruce Momjian 已提交
265
	/* Routines for copy in/out */
266
	extern int	PQgetline(PGconn *conn, char *string, int length);
267 268 269
	extern int	PQputline(PGconn *conn, const char *string);
	extern int	PQgetlineAsync(PGconn *conn, char *buffer, int bufsize);
	extern int	PQputnbytes(PGconn *conn, const char *buffer, int nbytes);
B
Bruce Momjian 已提交
270
	extern int	PQendcopy(PGconn *conn);
M
 
Marc G. Fournier 已提交
271

272 273 274 275
	/*
	 * "Fast path" interface --- not really recommended for application
	 * use
	 */
B
Bruce Momjian 已提交
276
	extern PGresult *PQfn(PGconn *conn,
B
Bruce Momjian 已提交
277 278 279 280 281 282
			      int fnid,
			      int *result_buf,
			      int *result_len,
			      int result_is_int,
			      const PQArgBlock *args,
			      int nargs);
B
Bruce Momjian 已提交
283 284

	/* Accessor functions for PGresult objects */
B
Bruce Momjian 已提交
285
	extern ExecStatusType PQresultStatus(const PGresult *res);
286
	extern const char *PQresStatus(ExecStatusType status);
B
Bruce Momjian 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
	extern const char *PQresultErrorMessage(const PGresult *res);
	extern int	PQntuples(const PGresult *res);
	extern int	PQnfields(const PGresult *res);
	extern int	PQbinaryTuples(const PGresult *res);
	extern const char *PQfname(const PGresult *res, int field_num);
	extern int	PQfnumber(const PGresult *res, const char *field_name);
	extern Oid	PQftype(const PGresult *res, int field_num);
	extern int	PQfsize(const PGresult *res, int field_num);
	extern int	PQfmod(const PGresult *res, int field_num);
	extern const char *PQcmdStatus(const PGresult *res);
        extern const char *PQoidStatus(const PGresult *res); /* old and ugly */
        extern Oid PQoidValue(const PGresult *res); /* new and improved */
	extern const char *PQcmdTuples(const PGresult *res);
	extern const char *PQgetvalue(const PGresult *res, int tup_num, int field_num);
	extern int	PQgetlength(const PGresult *res, int tup_num, int field_num);
	extern int	PQgetisnull(const PGresult *res, int tup_num, int field_num);
M
 
Marc G. Fournier 已提交
303

B
Bruce Momjian 已提交
304
	/* Delete a PGresult */
305
	extern void PQclear(PGresult *res);
B
Bruce Momjian 已提交
306

B
Bruce Momjian 已提交
307 308 309 310
	/*
	 * Make an empty PGresult with given status (some apps find this
	 * useful). If conn is not NULL and status indicates an error, the
	 * conn's errorMessage is copied.
311
	 */
B
Bruce Momjian 已提交
312
	extern PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
313

B
Bruce Momjian 已提交
314
/* === in fe-print.c === */
M
 
Marc G. Fournier 已提交
315

B
Bruce Momjian 已提交
316
	extern void PQprint(FILE *fout,		/* output stream */
B
Bruce Momjian 已提交
317 318
			    const PGresult *res,
			    const PQprintOpt *ps);	/* option structure */
M
 
Marc G. Fournier 已提交
319

320 321 322
	/*
	 * PQdisplayTuples() is a better version of PQprintTuples(), but both
	 * are obsoleted by PQprint().
B
Bruce Momjian 已提交
323
	 */
B
Bruce Momjian 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
	extern void PQdisplayTuples(const PGresult *res,
				    FILE *fp,	/* where to send the
						 * output */
				    int fillAlign,		/* pad the fields with
								 * spaces */
				    const char *fieldSep,		/* field separator */
				    int printHeader,	/* display headers? */
				    int quiet);

	extern void PQprintTuples(const PGresult *res,
				  FILE *fout,	/* output stream */
				  int printAttName,		/* print attribute names
								 * or not */
				  int terseOutput,		/* delimiter bars or
								 * not? */
				  int width);	/* width of column, if
						 * 0, use variable width */
B
Bruce Momjian 已提交
341

342
	/* Determine length of multibyte encoded char at *s */
B
Bruce Momjian 已提交
343
	extern int	PQmblen(const unsigned char *s);
344

345
/* === in fe-lobj.c === */
M
 
Marc G. Fournier 已提交
346 347

	/* Large-object access routines */
B
Bruce Momjian 已提交
348 349
	extern int	lo_open(PGconn *conn, Oid lobjId, int mode);
	extern int	lo_close(PGconn *conn, int fd);
B
Bruce Momjian 已提交
350 351
	extern int	lo_read(PGconn *conn, int fd, char *buf, size_t len);
	extern int	lo_write(PGconn *conn, int fd, const char *buf, size_t len);
B
Bruce Momjian 已提交
352 353 354 355
	extern int	lo_lseek(PGconn *conn, int fd, int offset, int whence);
	extern Oid	lo_creat(PGconn *conn, int mode);
	extern int	lo_tell(PGconn *conn, int fd);
	extern int	lo_unlink(PGconn *conn, Oid lobjId);
B
Bruce Momjian 已提交
356 357
	extern Oid	lo_import(PGconn *conn, const char *filename);
	extern int	lo_export(PGconn *conn, Oid lobjId, const char *filename);
B
Bruce Momjian 已提交
358

359 360
#ifdef __cplusplus
};
361

362
#endif
363

364
#endif	 /* LIBPQ_FE_H */