libpq-fe.h 10.0 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
 *
B
Bruce Momjian 已提交
9
 * $Id: libpq-fe.h,v 1.50 1999/05/25 16:15:13 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 32 33
	typedef enum
	{
		CONNECTION_OK,
		CONNECTION_BAD
34
	} ConnStatusType;
35 36 37 38

	typedef enum
	{
		PGRES_EMPTY_QUERY = 0,
B
Bruce Momjian 已提交
39 40 41 42 43
		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,
44
								 * PGresult contains the result tuples */
B
Bruce Momjian 已提交
45 46
		PGRES_COPY_OUT,			/* Copy Out data transfer in progress */
		PGRES_COPY_IN,			/* Copy In data transfer in progress */
47 48 49 50
		PGRES_BAD_RESPONSE,		/* an unexpected response was recv'd from
								 * the backend */
		PGRES_NONFATAL_ERROR,
		PGRES_FATAL_ERROR
51
	} ExecStatusType;
52

53 54 55
/* String descriptions of the ExecStatusTypes.
 * NB: direct use of this array is now deprecated; call PQresStatus() instead.
 */
B
Bruce Momjian 已提交
56
	extern const char *const pgresStatus[];
57

58 59
/* PGconn encapsulates a connection to the backend.
 * The contents of this struct are not supposed to be known to applications.
60
 */
61
	typedef struct pg_conn PGconn;
62

63 64 65 66
/* 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.
67
 */
68
	typedef struct pg_result PGresult;
69

70 71 72
/* 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.
73 74
 * 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 已提交
75
 */
76 77
	typedef struct pgNotify
	{
78
		char		relname[NAMEDATALEN];		/* name of relation
79
												 * containing data */
B
Bruce Momjian 已提交
80
		int			be_pid;		/* process id of backend */
81
	} PGnotify;
82

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

B
Bruce Momjian 已提交
87
/* Print options for PQprint() */
B
Bruce Momjian 已提交
88 89 90 91 92 93 94

	/*
	 * 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 已提交
95 96

	typedef struct _PQprintOpt
97
	{
98
		pqbool		header;		/* print output field headings and row
99
								 * count */
100 101 102 103 104
		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 */
105 106 107
		char	   *fieldSep;	/* field separator */
		char	   *tableOpt;	/* insert to HTML <table ...> */
		char	   *caption;	/* HTML <caption> */
108 109
		char	  **fieldName;	/* null terminated array of repalcement
								 * field names */
B
Bruce Momjian 已提交
110
	} PQprintOpt;
111

M
Marc G. Fournier 已提交
112
/* ----------------
M
 
Marc G. Fournier 已提交
113
 * Structure for the conninfo parameter definitions returned by PQconndefaults
M
Marc G. Fournier 已提交
114 115
 * ----------------
 */
B
Bruce Momjian 已提交
116
	typedef struct _PQconninfoOption
117
	{
118
		char	   *keyword;	/* The keyword of the option			*/
B
Bruce Momjian 已提交
119
		char	   *envvar;		/* Fallback environment variable name	*/
120 121 122 123
		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 已提交
124 125 126 127 128
		/* 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	*/
129
		int			dispsize;	/* Field size in characters for dialog	*/
B
Bruce Momjian 已提交
130
	} PQconninfoOption;
131

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
/* ----------------
 * 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 已提交
147 148 149 150 151
/* ----------------
 * Exported functions of libpq
 * ----------------
 */

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

154
	/* make a new client connection to the backend */
155
	extern PGconn *PQconnectdb(const char *conninfo);
M
 
Marc G. Fournier 已提交
156 157
	extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport,
								const char *pgoptions, const char *pgtty,
B
Bruce Momjian 已提交
158 159
											const char *dbName,
									 const char *login, const char *pwd);
M
 
Marc G. Fournier 已提交
160 161 162 163
#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 */
164
	extern PQconninfoOption *PQconndefaults(void);
M
 
Marc G. Fournier 已提交
165

166
	/* close the current connection and free the PGconn data structure */
167
	extern void PQfinish(PGconn *conn);
168

169 170 171 172
	/*
	 * close the current connection and restablish a new one with the same
	 * parameters
	 */
173 174
	extern void PQreset(PGconn *conn);

M
 
Marc G. Fournier 已提交
175 176 177
	/* issue a cancel request */
	extern int	PQrequestCancel(PGconn *conn);

B
Bruce Momjian 已提交
178
	/* Accessor functions for PGconn objects */
179 180
	extern char *PQdb(PGconn *conn);
	extern char *PQuser(PGconn *conn);
181
	extern char *PQpass(PGconn *conn);
182 183 184
	extern char *PQhost(PGconn *conn);
	extern char *PQport(PGconn *conn);
	extern char *PQtty(PGconn *conn);
185
	extern char *PQoptions(PGconn *conn);
186 187
	extern ConnStatusType PQstatus(PGconn *conn);
	extern char *PQerrorMessage(PGconn *conn);
188
	extern int	PQsocket(PGconn *conn);
189
	extern int	PQbackendPID(PGconn *conn);
B
Bruce Momjian 已提交
190 191

	/* Enable/disable tracing */
192 193
	extern void PQtrace(PGconn *conn, FILE *debug_port);
	extern void PQuntrace(PGconn *conn);
194

B
 
Bruce Momjian 已提交
195
	/* Override default notice processor */
196
	extern void PQsetNoticeProcessor(PGconn *conn,
B
Bruce Momjian 已提交
197 198
												 PQnoticeProcessor proc,
												 void *arg);
B
 
Bruce Momjian 已提交
199

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

B
Bruce Momjian 已提交
202
	/* Simple synchronous query */
203
	extern PGresult *PQexec(PGconn *conn, const char *query);
B
Bruce Momjian 已提交
204
	extern PGnotify *PQnotifies(PGconn *conn);
M
 
Marc G. Fournier 已提交
205

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

B
Bruce Momjian 已提交
210 211
	/* Routines for managing an asychronous query */
	extern int	PQisBusy(PGconn *conn);
212
	extern int	PQconsumeInput(PGconn *conn);
M
 
Marc G. Fournier 已提交
213

B
Bruce Momjian 已提交
214
	/* Routines for copy in/out */
215
	extern int	PQgetline(PGconn *conn, char *string, int length);
216 217 218
	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 已提交
219
	extern int	PQendcopy(PGconn *conn);
M
 
Marc G. Fournier 已提交
220

221 222 223 224
	/*
	 * "Fast path" interface --- not really recommended for application
	 * use
	 */
B
Bruce Momjian 已提交
225
	extern PGresult *PQfn(PGconn *conn,
B
Bruce Momjian 已提交
226 227 228 229 230 231
									  int fnid,
									  int *result_buf,
									  int *result_len,
									  int result_is_int,
									  PQArgBlock *args,
									  int nargs);
B
Bruce Momjian 已提交
232 233

	/* Accessor functions for PGresult objects */
234
	extern ExecStatusType PQresultStatus(PGresult *res);
235
	extern const char *PQresStatus(ExecStatusType status);
236
	extern const char *PQresultErrorMessage(PGresult *res);
237 238
	extern int	PQntuples(PGresult *res);
	extern int	PQnfields(PGresult *res);
239
	extern int	PQbinaryTuples(PGresult *res);
240 241 242
	extern char *PQfname(PGresult *res, int field_num);
	extern int	PQfnumber(PGresult *res, const char *field_name);
	extern Oid	PQftype(PGresult *res, int field_num);
243
	extern int	PQfsize(PGresult *res, int field_num);
244
	extern int	PQfmod(PGresult *res, int field_num);
245 246 247 248 249 250
	extern char *PQcmdStatus(PGresult *res);
	extern const char *PQoidStatus(PGresult *res);
	extern const char *PQcmdTuples(PGresult *res);
	extern char *PQgetvalue(PGresult *res, int tup_num, int field_num);
	extern int	PQgetlength(PGresult *res, int tup_num, int field_num);
	extern int	PQgetisnull(PGresult *res, int tup_num, int field_num);
M
 
Marc G. Fournier 已提交
251

B
Bruce Momjian 已提交
252
	/* Delete a PGresult */
253
	extern void PQclear(PGresult *res);
B
Bruce Momjian 已提交
254

B
Bruce Momjian 已提交
255 256 257 258
	/*
	 * 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.
259
	 */
B
Bruce Momjian 已提交
260
	extern PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
261

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

B
Bruce Momjian 已提交
264 265 266
	extern void PQprint(FILE *fout,		/* output stream */
									PGresult *res,
									PQprintOpt *ps);	/* option structure */
M
 
Marc G. Fournier 已提交
267

268 269 270
	/*
	 * PQdisplayTuples() is a better version of PQprintTuples(), but both
	 * are obsoleted by PQprint().
B
Bruce Momjian 已提交
271
	 */
272
	extern void PQdisplayTuples(PGresult *res,
B
Bruce Momjian 已提交
273 274 275 276 277 278 279
											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);
280

281
	extern void PQprintTuples(PGresult *res,
B
Bruce Momjian 已提交
282 283 284 285 286 287 288
										  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 已提交
289

290
	/* Determine length of multibyte encoded char at *s */
291
	extern int	PQmblen(unsigned char *s);
292

293
/* === in fe-lobj.c === */
M
 
Marc G. Fournier 已提交
294 295

	/* Large-object access routines */
B
Bruce Momjian 已提交
296 297 298 299 300 301 302 303 304 305 306
	extern int	lo_open(PGconn *conn, Oid lobjId, int mode);
	extern int	lo_close(PGconn *conn, int fd);
	extern int	lo_read(PGconn *conn, int fd, char *buf, int len);
	extern int	lo_write(PGconn *conn, int fd, char *buf, int len);
	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);
	extern Oid	lo_import(PGconn *conn, char *filename);
	extern int	lo_export(PGconn *conn, Oid lobjId, char *filename);

307 308
#ifdef __cplusplus
};
309

310
#endif
311

312
#endif	 /* LIBPQ_FE_H */