libpq-fe.h 14.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
 *
B
Hello!  
Bruce Momjian 已提交
9
 * $Id: libpq-fe.h,v 1.39 1998/08/29 04:05: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>
M
 
Marc G. Fournier 已提交
23
/* these wouldn't need to be included if PGSockAddr weren't exported: */
B
Hello!  
Bruce Momjian 已提交
24 25 26
#ifdef WIN32
#include <winsock.h>
#else
M
 
Marc G. Fournier 已提交
27 28 29
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
B
Hello!  
Bruce Momjian 已提交
30
#endif
31
/* ----------------
32
 *		include stuff common to fe and be
33 34
 * ----------------
 */
35
#include "postgres_ext.h"
36 37
#include "lib/dllist.h"

B
Bruce Momjian 已提交
38 39
/* Application-visible enum types */

40 41 42 43
	typedef enum
	{
		CONNECTION_OK,
		CONNECTION_BAD
44
	} ConnStatusType;
45 46 47 48 49 50 51 52

	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 已提交
53 54 55
		/* contains the result tuples */
		PGRES_COPY_OUT,			/* Copy Out data transfer in progress */
		PGRES_COPY_IN,			/* Copy In data transfer in progress */
56 57 58 59
		PGRES_BAD_RESPONSE,		/* an unexpected response was recv'd from
								 * the backend */
		PGRES_NONFATAL_ERROR,
		PGRES_FATAL_ERROR
60
	} ExecStatusType;
61 62

/* string descriptions of the ExecStatusTypes */
M
 
Marc G. Fournier 已提交
63
	extern const char * const pgresStatus[];
64

65 66
/*
 * POSTGRES backend dependent Constants.
67 68 69 70
 */

/* ERROR_MSG_LENGTH should really be the same as ELOG_MAXLEN in utils/elog.h*/
#define ERROR_MSG_LENGTH 4096
B
Bruce Momjian 已提交
71
#define CMDSTATUS_LEN 40
72

B
Bruce Momjian 已提交
73 74 75 76 77
/* 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.
78
 */
79 80 81

	typedef struct pgresAttDesc
	{
82
		char	   *name;		/* type name */
83
		Oid			typid;		/* type id */
84
		short		typlen;		/* type size */
85
		int			atttypmod;	/* type-specific modifier info */
86
	} PGresAttDesc;
87 88 89 90

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

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

	typedef struct pgresAttValue
	{
99 100
		int			len;		/* length in bytes of the value */
		char	   *value;		/* actual value */
101
	} PGresAttValue;
102

B
Bruce Momjian 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	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 */
122 123
	typedef struct pgNotify
	{
124
		char		relname[NAMEDATALEN];		/* name of relation
125
												 * containing data */
126
		int			be_pid;		/* process id of backend */
127
	} PGnotify;
128

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

B
Bruce Momjian 已提交
132 133 134 135 136 137 138 139 140 141
/* 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;

M
 
Marc G. Fournier 已提交
142 143 144 145 146 147
/* generic socket address type for PGconn connection information.
 * Really shouldn't be visible to users */
	typedef union PGSockAddr
	{
		struct sockaddr sa;
		struct sockaddr_in in;
B
Hello!  
Bruce Momjian 已提交
148
#ifndef WIN32
M
 
Marc G. Fournier 已提交
149
		struct sockaddr_un un;
B
Hello!  
Bruce Momjian 已提交
150
#endif
M
 
Marc G. Fournier 已提交
151 152
	} PGSockAddr;

B
Bruce Momjian 已提交
153 154
/* large-object-access data ... allocated only if large-object code is used.
 * Really shouldn't be visible to users */
155 156
	typedef struct pgLobjfuncs
	{
157 158 159 160
		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
161
										 * lo_unlink	*/
162 163 164 165
		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		*/
166
	} PGlobjfuncs;
M
Marc G. Fournier 已提交
167

B
Bruce Momjian 已提交
168
/* PGconn encapsulates a connection to the backend.
M
 
Marc G. Fournier 已提交
169 170 171
 * XXX contents of this struct really shouldn't be visible to applications,
 * but we might break some existing applications if we tried to make it
 * completely opaque.
B
Bruce Momjian 已提交
172
 */
173 174
	typedef struct pg_conn
	{
B
Bruce Momjian 已提交
175
		/* Saved values of connection options */
176
		char	   *pghost;		/* the machine on which the server is
177
								 * running */
B
Bruce Momjian 已提交
178
		char	   *pgport;		/* the server's communication port */
179
		char	   *pgtty;		/* tty on which the backend messages is
B
Bruce Momjian 已提交
180
								 * displayed (NOT ACTUALLY USED???) */
181 182
		char	   *pgoptions;	/* options to start the backend with */
		char	   *dbName;		/* database name */
B
Bruce Momjian 已提交
183 184 185 186
		char	   *pguser;		/* Postgres username and password, if any */
		char	   *pgpass;

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

B
 
Bruce Momjian 已提交
189 190 191 192
		/* Callback procedure for notice/error message processing */
		PQnoticeProcessor	noticeHook;
		void	   *noticeArg;

B
Bruce Momjian 已提交
193 194 195 196 197 198 199
		/* 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 */
M
 
Marc G. Fournier 已提交
200 201
		PGSockAddr	laddr;		/* Local address */
		PGSockAddr	raddr;		/* Remote address */
M
 
Marc G. Fournier 已提交
202
		int			raddr_len;	/* Length of remote address */
203

B
Bruce Momjian 已提交
204
		/* Miscellaneous stuff */
M
 
Marc G. Fournier 已提交
205 206
		int			be_pid;		/* PID of backend --- needed for cancels */
		int			be_key;		/* key of backend --- needed for cancels */
B
Bruce Momjian 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
		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;
233 234 235 236 237 238 239

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

M
 
Marc G. Fournier 已提交
240 241
	typedef char pqbool;

B
Bruce Momjian 已提交
242 243 244
/* Print options for PQprint() */

	typedef struct _PQprintOpt
245
	{
246
		pqbool		header;		/* print output field headings and row
247
								 * count */
248 249 250 251 252 253 254 255 256 257
		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 已提交
258
	} PQprintOpt;
259

B
Bruce Momjian 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273
/* ----------------
 * 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;
274

M
Marc G. Fournier 已提交
275
/* ----------------
M
 
Marc G. Fournier 已提交
276
 * Structure for the conninfo parameter definitions returned by PQconndefaults
M
Marc G. Fournier 已提交
277 278
 * ----------------
 */
B
Bruce Momjian 已提交
279
	typedef struct _PQconninfoOption
280
	{
281 282 283 284 285 286
		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	*/
287 288 289 290 291
		/* 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	*/
292
		int			dispsize;	/* Field size in characters for dialog	*/
B
Bruce Momjian 已提交
293
	} PQconninfoOption;
294

M
 
Marc G. Fournier 已提交
295 296 297 298 299
/* ----------------
 * Exported functions of libpq
 * ----------------
 */

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

302
	/* make a new client connection to the backend */
303
	extern PGconn *PQconnectdb(const char *conninfo);
M
 
Marc G. Fournier 已提交
304 305 306 307 308 309 310 311
	extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport,
								const char *pgoptions, const char *pgtty,
								const char *dbName,
								const char *login, const char *pwd);
#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 */
312
	extern PQconninfoOption *PQconndefaults(void);
M
 
Marc G. Fournier 已提交
313

314
	/* close the current connection and free the PGconn data structure */
315
	extern void PQfinish(PGconn *conn);
316 317 318 319
	/*
	 * close the current connection and restablish a new one with the same
	 * parameters
	 */
320 321
	extern void PQreset(PGconn *conn);

M
 
Marc G. Fournier 已提交
322 323 324
	/* issue a cancel request */
	extern int	PQrequestCancel(PGconn *conn);

B
Bruce Momjian 已提交
325
	/* Accessor functions for PGconn objects */
326 327 328 329 330 331 332 333
	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 已提交
334 335 336
	extern int PQsocket(PGconn *conn);

	/* Enable/disable tracing */
337 338
	extern void PQtrace(PGconn *conn, FILE *debug_port);
	extern void PQuntrace(PGconn *conn);
339

B
 
Bruce Momjian 已提交
340 341 342 343 344
	/* Override default notice processor */
	extern void PQsetNoticeProcessor (PGconn *conn,
									  PQnoticeProcessor proc,
									  void *arg);

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

B
Bruce Momjian 已提交
347
	/* Simple synchronous query */
348
	extern PGresult *PQexec(PGconn *conn, const char *query);
B
Bruce Momjian 已提交
349
	extern PGnotify *PQnotifies(PGconn *conn);
M
 
Marc G. Fournier 已提交
350

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

B
Bruce Momjian 已提交
355 356 357
	/* Routines for managing an asychronous query */
	extern int	PQisBusy(PGconn *conn);
	extern void PQconsumeInput(PGconn *conn);
M
 
Marc G. Fournier 已提交
358

B
Bruce Momjian 已提交
359
	/* Routines for copy in/out */
360 361
	extern int	PQgetline(PGconn *conn, char *string, int length);
	extern void PQputline(PGconn *conn, const char *string);
M
 
Marc G. Fournier 已提交
362
	extern void PQputnbytes(PGconn *conn, const char *buffer, int nbytes);
B
Bruce Momjian 已提交
363
	extern int	PQendcopy(PGconn *conn);
M
 
Marc G. Fournier 已提交
364 365

	/* "Fast path" interface --- not really recommended for application use */
B
Bruce Momjian 已提交
366
	extern PGresult *PQfn(PGconn *conn,
M
 
Marc G. Fournier 已提交
367 368 369 370 371 372
						  int fnid,
						  int *result_buf,
						  int *result_len,
						  int result_is_int,
						  PQArgBlock *args,
						  int nargs);
B
Bruce Momjian 已提交
373 374

	/* Accessor functions for PGresult objects */
375 376 377 378 379 380 381
	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);
382
	extern int	PQfmod(PGresult *res, int field_num);
383 384 385 386 387 388
	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 已提交
389

B
Bruce Momjian 已提交
390
	/* Delete a PGresult */
391
	extern void PQclear(PGresult *res);
B
Bruce Momjian 已提交
392 393

/* === in fe-print.c === */
M
 
Marc G. Fournier 已提交
394

B
Bruce Momjian 已提交
395 396
	extern void PQprint(FILE *fout,		/* output stream */
						PGresult *res,
M
 
Marc G. Fournier 已提交
397 398
						PQprintOpt *ps); /* option structure */

B
Bruce Momjian 已提交
399 400 401
	/* PQdisplayTuples() is a better version of PQprintTuples(),
	 * but both are obsoleted by PQprint().
	 */
402
	extern void PQdisplayTuples(PGresult *res,
B
Bruce Momjian 已提交
403 404 405 406 407 408 409
								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);
410
	extern void PQprintTuples(PGresult *res,
B
Bruce Momjian 已提交
411 412 413 414 415
							  FILE *fout,	/* output stream */
							  int printAttName,		/* print attribute names
													 * or not */
							  int terseOutput,		/* delimiter bars or
													 * not? */
M
 
Marc G. Fournier 已提交
416
							  int width);	/* width of column, if
B
Bruce Momjian 已提交
417 418
											 * 0, use variable width */

419
#ifdef MULTIBYTE
420 421 422
	extern int PQmblen(unsigned char *s);
#endif

423
/* === in fe-lobj.c === */
M
 
Marc G. Fournier 已提交
424 425

	/* Large-object access routines */
B
Bruce Momjian 已提交
426 427 428 429 430 431 432 433 434 435 436
	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);

437 438
#ifdef __cplusplus
};
439
#endif
440

441
#endif							/* LIBPQ_FE_H */