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
 *
9
 * $Id: libpq-fe.h,v 1.49 1999/02/13 23:22:42 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,
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,
								 * PGresult contains the result tuples */
B
Bruce Momjian 已提交
44 45
		PGRES_COPY_OUT,			/* Copy Out data transfer in progress */
		PGRES_COPY_IN,			/* Copy In data transfer in progress */
46 47 48 49
		PGRES_BAD_RESPONSE,		/* an unexpected response was recv'd from
								 * the backend */
		PGRES_NONFATAL_ERROR,
		PGRES_FATAL_ERROR
50
	} ExecStatusType;
51

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

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

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

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

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

B
Bruce Momjian 已提交
86
/* Print options for PQprint() */
87 88 89 90 91 92 93
  
  	/*
  	 * 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 已提交
94 95

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	/* Accessor functions for PGresult objects */
233
	extern ExecStatusType PQresultStatus(PGresult *res);
234
	extern const char *PQresStatus(ExecStatusType status);
235
	extern const char *PQresultErrorMessage(PGresult *res);
236 237
	extern int	PQntuples(PGresult *res);
	extern int	PQnfields(PGresult *res);
238
	extern int	PQbinaryTuples(PGresult *res);
239 240 241
	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);
242
	extern int	PQfsize(PGresult *res, int field_num);
243
	extern int	PQfmod(PGresult *res, int field_num);
244 245 246 247 248 249
	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 已提交
250

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

254 255 256 257
	/* 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.
	 */
258 259
	extern PGresult * PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);

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

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

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

279
	extern void PQprintTuples(PGresult *res,
280 281 282 283 284 285 286
							  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 已提交
287

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

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

	/* Large-object access routines */
B
Bruce Momjian 已提交
294 295 296 297 298 299 300 301 302 303 304
	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);

305 306
#ifdef __cplusplus
};
307

308
#endif
309

310
#endif	 /* LIBPQ_FE_H */