libpq-fe.h 11.4 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.23 1997/09/08 21:55:45 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
 *		include stuff common to fe and be
25 26
 * ----------------
 */
27
#include "postgres_ext.h"
28 29 30
#include "libpq/pqcomm.h"
#include "lib/dllist.h"

31 32 33 34
	typedef enum
	{
		CONNECTION_OK,
		CONNECTION_BAD
35
	} ConnStatusType;
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

	typedef enum
	{
		PGRES_EMPTY_QUERY = 0,
		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 resulttuples */
		PGRES_COPY_OUT,
		PGRES_COPY_IN,
		PGRES_BAD_RESPONSE,		/* an unexpected response was recv'd from
								 * the backend */
		PGRES_NONFATAL_ERROR,
		PGRES_FATAL_ERROR

52
	} ExecStatusType;
53 54

/* string descriptions of the ExecStatusTypes */
55
	extern const char *pgresStatus[];
56

57 58
/*
 * POSTGRES backend dependent Constants.
59 60 61 62 63 64 65 66 67 68
 */

/* ERROR_MSG_LENGTH should really be the same as ELOG_MAXLEN in utils/elog.h*/
#define ERROR_MSG_LENGTH 4096
#define COMMAND_LENGTH 20
#define REMARK_LENGTH 80
#define PORTAL_NAME_LENGTH 16

/* ----------------
 * PQArgBlock --
69 70
 *		Information (pointer to array of this structure) required
 *		for the PQfn() call.
71 72
 * ----------------
 */
73 74
	typedef struct
	{
75 76
		int			len;
		int			isint;
77 78
		union
		{
79 80 81
			int		   *ptr;	/* can't use void (dec compiler barfs)	 */
			int			integer;
		}			u;
82
	} PQArgBlock;
83 84 85

	typedef struct pgresAttDesc
	{
86 87 88
		char	   *name;		/* type name */
		Oid			adtid;		/* type id */
		short		adtsize;	/* type size */
89
	} PGresAttDesc;
90 91 92 93

/* use char* for Attribute values,
   ASCII tuples are guaranteed to be null-terminated
   For binary tuples, the first four bytes of the value is the size,
94
   and the bytes afterwards are the value.	The binary value is
95
   not guaranteed to be null-terminated.  In fact, it can have embedded nulls*/
M
Fixes:  
Marc G. Fournier 已提交
96

97 98 99 100
#define NULL_LEN		(-1)	/* pg_result len for NULL value */

	typedef struct pgresAttValue
	{
101 102
		int			len;		/* length in bytes of the value */
		char	   *value;		/* actual value */
103
	} PGresAttValue;
104 105 106

	typedef struct pgNotify
	{
107
		char		relname[NAMEDATALEN];		/* name of relation
108
												 * containing data */
109
		int			be_pid;		/* process id of backend */
110
	} PGnotify;
111 112 113

	typedef struct pgLobjfuncs
	{
114 115 116 117
		Oid			fn_lo_open; /* OID of backend function lo_open		*/
		Oid			fn_lo_close;/* OID of backend function lo_close		*/
		Oid			fn_lo_creat;/* OID of backend function lo_creat		*/
		Oid			fn_lo_unlink;		/* OID of backend function
118
										 * lo_unlink	*/
119 120 121 122
		Oid			fn_lo_lseek;/* OID of backend function lo_lseek		*/
		Oid			fn_lo_tell; /* OID of backend function lo_tell		*/
		Oid			fn_lo_read; /* OID of backend function LOread		*/
		Oid			fn_lo_write;/* OID of backend function LOwrite		*/
123
	} PGlobjfuncs;
M
Marc G. Fournier 已提交
124

125
/* PGconn encapsulates a connection to the backend */
126 127
	typedef struct pg_conn
	{
128
		char	   *pghost;		/* the machine on which the server is
129
								 * running */
130
		char	   *pgtty;		/* tty on which the backend messages is
131
								 * displayed */
132 133 134 135 136
		char	   *pgport;		/* the communication port with the backend */
		char	   *pgoptions;	/* options to start the backend with */
		char	   *dbName;		/* database name */
		ConnStatusType status;
		char		errorMessage[ERROR_MSG_LENGTH];
137
		/* pipes for be/fe communication */
138 139 140 141 142 143 144
		FILE	   *Pfin;
		FILE	   *Pfout;
		FILE	   *Pfdebug;
		void	   *port;		/* really a Port* */
		int			asyncNotifyWaiting;
		Dllist	   *notifyList;
		char	   *pguser;		/* Postgres username of user who is
145
								 * connected */
146 147 148 149
		char	   *pgpass;
		char	   *pgauth;
		PGlobjfuncs *lobjfuncs; /* Backend function OID's for large object
								 * access */
150
	} PGconn;
151 152 153 154 155

#define CMDSTATUS_LEN 40

/* PGresult encapsulates the result of a query */
/* unlike the old libpq, we assume that queries only return in one group */
156 157
	typedef struct pg_result
	{
158 159 160 161
		int			ntups;
		int			numAttributes;
		PGresAttDesc *attDescs;
		PGresAttValue **tuples; /* each PGresTuple is an array of
162
								 * PGresAttValue's */
163 164 165 166 167
		int			tupArrSize; /* size of tuples array allocated */
		ExecStatusType resultStatus;
		char		cmdStatus[CMDSTATUS_LEN];	/* cmd status from the
												 * last insert query */
		int			binary;		/* binary tuple values if binary == 1,
168
								 * otherwise ASCII */
169
		PGconn	   *conn;
170
	} PGresult;
171

172
	typedef char pqbool;
173 174 175 176 177 178 179 180 181

	/*
	 * 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.
	 */

	struct _PQprintOpt
	{
182
		pqbool		header;		/* print output field headings and row
183
								 * count */
184 185 186 187 188 189 190 191 192 193
		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 */
		char	   *fieldSep;	/* field separator */
		char	   *tableOpt;	/* insert to HTML <table ...> */
		char	   *caption;	/* HTML <caption> */
		char	  **fieldName;	/* null terminated array of repalcement
								 * field names */
194 195 196
	};

	typedef struct _PQprintOpt PQprintOpt;
197

M
Marc G. Fournier 已提交
198 199 200 201
/* ----------------
 * Structure for the conninfo parameter definitions of PQconnectdb()
 * ----------------
 */
202 203
	struct _PQconninfoOption
	{
204 205 206 207 208 209
		char	   *keyword;	/* The keyword of the option			*/
		char	   *environ;	/* 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	*/
210 211 212 213 214
		/* 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	*/
215
		int			dispsize;	/* Field size in characters for dialog	*/
216 217 218 219 220 221
	};

	typedef struct _PQconninfoOption PQconninfoOption;

/* ===	in fe-connect.c === */
	/* make a new client connection to the backend */
222
	extern PGconn *PQconnectdb(const char *conninfo);
223
	extern PQconninfoOption *PQconndefaults(void);
224
	extern PGconn *PQsetdb(const char *pghost, const char *pgport, const char *pgoptions,
225 226
								  const char *pgtty, const char *dbName);
	/* close the current connection and free the PGconn data structure */
227
	extern void PQfinish(PGconn *conn);
228 229 230 231 232

	/*
	 * close the current connection and restablish a new one with the same
	 * parameters
	 */
233 234 235 236 237 238 239 240 241 242 243 244
	extern void PQreset(PGconn *conn);

	extern char *PQdb(PGconn *conn);
	extern char *PQuser(PGconn *conn);
	extern char *PQhost(PGconn *conn);
	extern char *PQoptions(PGconn *conn);
	extern char *PQport(PGconn *conn);
	extern char *PQtty(PGconn *conn);
	extern ConnStatusType PQstatus(PGconn *conn);
	extern char *PQerrorMessage(PGconn *conn);
	extern void PQtrace(PGconn *conn, FILE *debug_port);
	extern void PQuntrace(PGconn *conn);
245 246

/* === in fe-exec.c === */
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
	extern PGresult *PQexec(PGconn *conn, const char *query);
	extern int	PQgetline(PGconn *conn, char *string, int length);
	extern int	PQendcopy(PGconn *conn);
	extern void PQputline(PGconn *conn, const char *string);
	extern ExecStatusType PQresultStatus(PGresult *res);
	extern int	PQntuples(PGresult *res);
	extern int	PQnfields(PGresult *res);
	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);
	extern short PQfsize(PGresult *res, int field_num);
	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);
	extern void PQclear(PGresult *res);
265
/* PQdisplayTuples() is a better version of PQprintTuples() */
266 267
	extern void PQdisplayTuples(PGresult *res,
											FILE *fp,	/* where to send the
268 269 270 271 272 273
														 * output */
											int fillAlign,		/* pad the fields with
																 * spaces */
											const char *fieldSep,		/* field separator */
											int printHeader,	/* display headers? */
											int quiet);
274 275
	extern void PQprintTuples(PGresult *res,
										  FILE *fout,	/* output stream */
276 277 278 279 280 281
										  int printAttName,		/* print attribute names
																 * or not */
										  int terseOutput,		/* delimiter bars or
																 * not? */
										  int width		/* width of column, if
														 * 0, use variable width */
282
	);
283 284 285
	extern void PQprint(FILE *fout,		/* output stream */
									PGresult *res,
									PQprintOpt *ps		/* option structure */
286
	);
287 288
	extern PGnotify *PQnotifies(PGconn *conn);
	extern PGresult *PQfn(PGconn *conn,
289 290 291 292
									  int fnid,
									  int *result_buf,
									  int *result_len,
									  int result_is_int,
293
									  PQArgBlock *args,
294
									  int nargs);
295
/* === in fe-auth.c === */
296 297 298
	extern MsgType fe_getauthsvc(char *PQerrormsg);
	extern void fe_setauthsvc(const char *name, char *PQerrormsg);
	extern char *fe_getauthname(char *PQerrormsg);
299 300 301

/* === in fe-misc.c === */
/* pqGets and pqPuts gets and sends strings to the file stream
302 303
   returns 0 if successful
   if debug is non-null, debugging output is sent to that stream
304
*/
305 306 307 308 309
	extern int	pqGets(char *s, int maxlen, FILE *stream, FILE *debug);
	extern int	pqGetnchar(char *s, int maxlen, FILE *stream, FILE *debug);
	extern int	pqPutnchar(const char *s, int maxlen, FILE *stream, FILE *debug);
	extern int	pqPuts(const char *s, FILE *stream, FILE *debug);
	extern int	pqGetc(FILE *stream, FILE *debug);
310 311
/* get a n-byte integer from the stream into result */
/* returns 0 if successful */
312
	extern int	pqGetInt(int *result, int bytes, FILE *stream, FILE *debug);
313 314
/* put a n-byte integer into the stream */
/* returns 0 if successful */
315 316
	extern int	pqPutInt(const int n, int bytes, FILE *stream, FILE *debug);
	extern void pqFlush(FILE *stream, FILE *debug);
317 318

/* === in fe-lobj.c === */
319 320 321 322 323 324 325 326 327 328
	int			lo_open(PGconn *conn, Oid lobjId, int mode);
	int			lo_close(PGconn *conn, int fd);
	int			lo_read(PGconn *conn, int fd, char *buf, int len);
	int			lo_write(PGconn *conn, int fd, char *buf, int len);
	int			lo_lseek(PGconn *conn, int fd, int offset, int whence);
	Oid			lo_creat(PGconn *conn, int mode);
	int			lo_tell(PGconn *conn, int fd);
	int			lo_unlink(PGconn *conn, Oid lobjId);
	Oid			lo_import(PGconn *conn, char *filename);
	int			lo_export(PGconn *conn, Oid lobjId, char *filename);
329 330 331 332 333 334 335 336 337
/* max length of message to send  */
#define MAX_MESSAGE_LEN 8193

/* maximum number of fields in a tuple */
#define BYTELEN 8
#define MAX_FIELDS 512

/* fall back options if they are not specified by arguments or defined
   by environment variables */
338 339
#define DefaultHost		"localhost"
#define DefaultTty		""
340
#define DefaultOption	""
341 342
#define DefaultAuthtype		  ""
#define DefaultPassword		  ""
343

344

345
	typedef void *TUPLE;
346 347 348
#define palloc malloc
#define pfree free

349
#if defined(sunos4)
350
	extern char *sys_errlist[];
351
#define strerror(A) (sys_errlist[(A)])
352
#endif							/* sunos4 */
353 354 355 356

#ifdef __cplusplus
};

357
#endif
358

359
#endif							/* LIBPQ_FE_H */