libpq-fe.h 15.3 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.37 1998/08/09 02:59:31 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"

B
Bruce Momjian 已提交
31 32
/* Application-visible enum types */

33 34 35 36
	typedef enum
	{
		CONNECTION_OK,
		CONNECTION_BAD
37
	} ConnStatusType;
38 39 40 41 42 43 44 45

	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 */
B
Bruce Momjian 已提交
46 47 48
		/* contains the result tuples */
		PGRES_COPY_OUT,			/* Copy Out data transfer in progress */
		PGRES_COPY_IN,			/* Copy In data transfer in progress */
49 50 51 52
		PGRES_BAD_RESPONSE,		/* an unexpected response was recv'd from
								 * the backend */
		PGRES_NONFATAL_ERROR,
		PGRES_FATAL_ERROR
53
	} ExecStatusType;
54 55

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

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

/* 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
B
Bruce Momjian 已提交
67
#define CMDSTATUS_LEN 40
68

B
Bruce Momjian 已提交
69 70 71 72 73
/* PGresult and the subsidiary types PGresAttDesc, PGresAttValue
 * represent the result of a query (or more precisely, of a single SQL
 * command --- a query string given to PQexec can contain multiple commands).
 * Note we assume that a single command can return at most one tuple group,
 * hence there is no need for multiple descriptor sets.
74
 */
75 76 77

	typedef struct pgresAttDesc
	{
78
		char	   *name;		/* type name */
79
		Oid			typid;		/* type id */
80
		short		typlen;		/* type size */
81
		int			atttypmod;	/* type-specific modifier info */
82
	} PGresAttDesc;
83 84 85 86

/* 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,
87
   and the bytes afterwards are the value.	The binary value is
88
   not guaranteed to be null-terminated.  In fact, it can have embedded nulls*/
M
Fixes:  
Marc G. Fournier 已提交
89

90 91 92 93
#define NULL_LEN		(-1)	/* pg_result len for NULL value */

	typedef struct pgresAttValue
	{
94 95
		int			len;		/* length in bytes of the value */
		char	   *value;		/* actual value */
96
	} PGresAttValue;
97

B
Bruce Momjian 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
	struct pg_conn;				/* forward reference */

	typedef struct pg_result
	{
		int			ntups;
		int			numAttributes;
		PGresAttDesc *attDescs;
		PGresAttValue **tuples; /* each PGresTuple is an array of
								 * PGresAttValue's */
		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,
								 * otherwise ASCII */
		struct pg_conn *conn;	/* connection we did the query on */
	} PGresult;

/* PGnotify represents the occurrence of a NOTIFY message */
117 118
	typedef struct pgNotify
	{
119
		char		relname[NAMEDATALEN];		/* name of relation
120
												 * containing data */
121
		int			be_pid;		/* process id of backend */
122
	} PGnotify;
123

B
 
Bruce Momjian 已提交
124 125 126
/* PQnoticeProcessor is a typedef for a callback function type */
	typedef void (*PQnoticeProcessor) (void * arg, const char * message);

B
Bruce Momjian 已提交
127 128 129 130 131 132 133 134 135 136 137 138
/* PGAsyncStatusType is private to libpq, really shouldn't be seen by users */
	typedef enum
	{
		PGASYNC_IDLE,			/* nothing's happening, dude */
		PGASYNC_BUSY,			/* query in progress */
		PGASYNC_READY,			/* result ready for PQgetResult */
		PGASYNC_COPY_IN,		/* Copy In data transfer in progress */
		PGASYNC_COPY_OUT		/* Copy Out data transfer in progress */
	} PGAsyncStatusType;

/* large-object-access data ... allocated only if large-object code is used.
 * Really shouldn't be visible to users */
139 140
	typedef struct pgLobjfuncs
	{
141 142 143 144
		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
145
										 * lo_unlink	*/
146 147 148 149
		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		*/
150
	} PGlobjfuncs;
M
Marc G. Fournier 已提交
151

B
Bruce Momjian 已提交
152 153 154
/* PGconn encapsulates a connection to the backend.
 * XXX contents of this struct really shouldn't be visible to applications
 */
155 156
	typedef struct pg_conn
	{
B
Bruce Momjian 已提交
157
		/* Saved values of connection options */
158
		char	   *pghost;		/* the machine on which the server is
159
								 * running */
B
Bruce Momjian 已提交
160
		char	   *pgport;		/* the server's communication port */
161
		char	   *pgtty;		/* tty on which the backend messages is
B
Bruce Momjian 已提交
162
								 * displayed (NOT ACTUALLY USED???) */
163 164
		char	   *pgoptions;	/* options to start the backend with */
		char	   *dbName;		/* database name */
B
Bruce Momjian 已提交
165 166 167 168
		char	   *pguser;		/* Postgres username and password, if any */
		char	   *pgpass;

		/* Optional file to write trace info to */
169
		FILE	   *Pfdebug;
B
Bruce Momjian 已提交
170

B
 
Bruce Momjian 已提交
171 172 173 174
		/* Callback procedure for notice/error message processing */
		PQnoticeProcessor	noticeHook;
		void	   *noticeArg;

B
Bruce Momjian 已提交
175 176 177 178 179 180 181
		/* Status indicators */
		ConnStatusType		status;
		PGAsyncStatusType	asyncStatus;
		Dllist	   *notifyList;	/* Notify msgs not yet handed to application */

		/* Connection data */
		int			sock;		/* Unix FD for socket, -1 if not connected */
182 183
		SockAddr	laddr;		/* Local address */
		SockAddr	raddr;		/* Remote address */
M
 
Marc G. Fournier 已提交
184
		int			raddr_len;	/* Length of remote address */
185

B
Bruce Momjian 已提交
186
		/* Miscellaneous stuff */
M
 
Marc G. Fournier 已提交
187 188
		int			be_pid;		/* PID of backend --- needed for cancels */
		int			be_key;		/* key of backend --- needed for cancels */
B
Bruce Momjian 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
		char		salt[2];	/* password salt received from backend */
		PGlobjfuncs *lobjfuncs; /* private state for large-object access fns */

		/* Buffer for data received from backend and not yet processed */
		char		*inBuffer;	/* currently allocated buffer */
		int			inBufSize;	/* allocated size of buffer */
		int			inStart;	/* offset to first unconsumed data in buffer */
		int			inCursor;	/* next byte to tentatively consume */
		int			inEnd;		/* offset to first position after avail data */

		/* Buffer for data not yet sent to backend */
		char		*outBuffer;	/* currently allocated buffer */
		int			outBufSize;	/* allocated size of buffer */
		int			outCount;	/* number of chars waiting in buffer */

		/* Status for asynchronous result construction */
		PGresult		*result;	/* result being constructed */
		PGresAttValue	*curTuple;	/* tuple currently being read */

		/* Message space.  Placed last for code-size reasons.
		 * errorMessage is the message last returned to the application.
		 * When asyncStatus=READY, asyncErrorMessage is the pending message
		 * that will be put in errorMessage by PQgetResult. */
		char		errorMessage[ERROR_MSG_LENGTH];
		char		asyncErrorMessage[ERROR_MSG_LENGTH];
	} PGconn;
215

216
	typedef char pqbool;
217 218 219 220 221 222 223

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

B
Bruce Momjian 已提交
224 225 226
/* Print options for PQprint() */

	typedef struct _PQprintOpt
227
	{
228
		pqbool		header;		/* print output field headings and row
229
								 * count */
230 231 232 233 234 235 236 237 238 239
		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 */
B
Bruce Momjian 已提交
240
	} PQprintOpt;
241

B
Bruce Momjian 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255
/* ----------------
 * 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;
256

M
Marc G. Fournier 已提交
257 258 259 260
/* ----------------
 * Structure for the conninfo parameter definitions of PQconnectdb()
 * ----------------
 */
B
Bruce Momjian 已提交
261
	typedef struct _PQconninfoOption
262
	{
263 264 265 266 267 268
		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	*/
269 270 271 272 273
		/* 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	*/
274
		int			dispsize;	/* Field size in characters for dialog	*/
B
Bruce Momjian 已提交
275
	} PQconninfoOption;
276 277 278

/* ===	in fe-connect.c === */
	/* make a new client connection to the backend */
279
	extern PGconn *PQconnectdb(const char *conninfo);
280
	extern PQconninfoOption *PQconndefaults(void);
281
	extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
282
											const char *pgtty, const char *dbName, const char *login, const char *pwd);
283
#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)
284
	/* close the current connection and free the PGconn data structure */
285
	extern void PQfinish(PGconn *conn);
M
 
Marc G. Fournier 已提交
286 287
	/* issue a cancel request */
	extern int	PQrequestCancel(PGconn *conn);
288 289 290 291 292

	/*
	 * close the current connection and restablish a new one with the same
	 * parameters
	 */
293 294
	extern void PQreset(PGconn *conn);

B
Bruce Momjian 已提交
295
	/* Accessor functions for PGconn objects */
296 297 298 299 300 301 302 303
	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);
B
Bruce Momjian 已提交
304 305 306
	extern int PQsocket(PGconn *conn);

	/* Enable/disable tracing */
307 308
	extern void PQtrace(PGconn *conn, FILE *debug_port);
	extern void PQuntrace(PGconn *conn);
309

B
 
Bruce Momjian 已提交
310 311 312 313 314
	/* Override default notice processor */
	extern void PQsetNoticeProcessor (PGconn *conn,
									  PQnoticeProcessor proc,
									  void *arg);

315
/* === in fe-exec.c === */
B
Bruce Momjian 已提交
316
	/* Simple synchronous query */
317
	extern PGresult *PQexec(PGconn *conn, const char *query);
B
Bruce Momjian 已提交
318 319 320 321 322 323 324 325
	extern PGnotify *PQnotifies(PGconn *conn);
	/* Interface for multiple-result or asynchronous queries */
	extern int  PQsendQuery(PGconn *conn, const char *query);
	extern PGresult *PQgetResult(PGconn *conn);
	/* Routines for managing an asychronous query */
	extern int	PQisBusy(PGconn *conn);
	extern void PQconsumeInput(PGconn *conn);
	/* Routines for copy in/out */
326 327
	extern int	PQgetline(PGconn *conn, char *string, int length);
	extern void PQputline(PGconn *conn, const char *string);
B
Bruce Momjian 已提交
328 329 330 331 332 333 334 335 336 337 338 339
	extern int	PQendcopy(PGconn *conn);
	/* Not really meant for application use: */
	extern PGresult *PQfn(PGconn *conn,
									  int fnid,
									  int *result_buf,
									  int *result_len,
									  int result_is_int,
									  PQArgBlock *args,
									  int nargs);
	extern void PQclearAsyncResult(PGconn *conn);

	/* Accessor functions for PGresult objects */
340 341 342 343 344 345 346
	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);
347
	extern int	PQfmod(PGresult *res, int field_num);
348 349 350 351 352 353
	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);
B
Bruce Momjian 已提交
354
	/* Delete a PGresult */
355
	extern void PQclear(PGresult *res);
B
Bruce Momjian 已提交
356 357 358 359 360 361 362 363 364

/* === in fe-print.c === */
	extern void PQprint(FILE *fout,		/* output stream */
						PGresult *res,
						PQprintOpt *ps /* option structure */
		);
	/* PQdisplayTuples() is a better version of PQprintTuples(),
	 * but both are obsoleted by PQprint().
	 */
365
	extern void PQdisplayTuples(PGresult *res,
B
Bruce Momjian 已提交
366 367 368 369 370 371 372
								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);
373
	extern void PQprintTuples(PGresult *res,
B
Bruce Momjian 已提交
374 375 376 377 378 379 380 381 382
							  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 */
		);

383
#ifdef MULTIBYTE
384 385 386
	extern int PQmblen(unsigned char *s);
#endif

387
/* === in fe-auth.c === */
388 389 390
	extern MsgType fe_getauthsvc(char *PQerrormsg);
	extern void fe_setauthsvc(const char *name, char *PQerrormsg);
	extern char *fe_getauthname(char *PQerrormsg);
391 392

/* === in fe-misc.c === */
B
Bruce Momjian 已提交
393 394 395 396 397 398 399 400 401 402 403 404
	/* "Get" and "Put" routines return 0 if successful, EOF if not.
	 * Note that for Get, EOF merely means the buffer is exhausted,
	 * not that there is necessarily any error.
	 */
	extern int	pqGetc(char *result, PGconn *conn);
	extern int	pqGets(char *s, int maxlen, PGconn *conn);
	extern int	pqPuts(const char *s, PGconn *conn);
	extern int	pqGetnchar(char *s, int len, PGconn *conn);
	extern int	pqPutnchar(const char *s, int len, PGconn *conn);
	extern int	pqGetInt(int *result, int bytes, PGconn *conn);
	extern int	pqPutInt(int value, int bytes, PGconn *conn);
	extern int	pqReadData(PGconn *conn);
B
 
Bruce Momjian 已提交
405
	extern int	pqReadReady(PGconn *conn);
B
Bruce Momjian 已提交
406 407
	extern int	pqFlush(PGconn *conn);
	extern int	pqWait(int forRead, int forWrite, PGconn *conn);
408 409

/* === in fe-lobj.c === */
B
Bruce Momjian 已提交
410 411 412 413 414 415 416 417 418 419 420
	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);

421 422 423 424 425 426
/* max length of message to send  */
#define MAX_MESSAGE_LEN 8193

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

B
Bruce Momjian 已提交
427 428 429
/* bits in a byte */
#define BYTELEN 8

430 431
/* fall back options if they are not specified by arguments or defined
   by environment variables */
432 433
#define DefaultHost		"localhost"
#define DefaultTty		""
434
#define DefaultOption	""
435 436
#define DefaultAuthtype		  ""
#define DefaultPassword		  ""
437

438

439
	typedef void *TUPLE;
440 441 442
#define palloc malloc
#define pfree free

443
#if defined(sun) && defined(sparc) && !defined(__SVR4)
444
	extern char *sys_errlist[];
445
#define strerror(A) (sys_errlist[(A)])
446
#endif							/* sunos4 */
447 448 449 450

#ifdef __cplusplus
};

451
#endif
452

453
#endif							/* LIBPQ_FE_H */