1 /*-------------------------------------------------------------------------
4 * This file contains definitions for structures and
5 * externs for functions used by frontend postgres applications.
7 * Copyright (c) 1994, Regents of the University of California
9 * $Id: libpq-fe.h,v 1.25 1998/01/26 01:42:37 scrappy Exp $
11 *-------------------------------------------------------------------------
24 * include stuff common to fe and be
27 #include "postgres_ext.h"
28 #include "libpq/pqcomm.h"
29 #include "lib/dllist.h"
39 PGRES_EMPTY_QUERY = 0,
40 PGRES_COMMAND_OK, /* a query command that doesn't return */
41 /* anything was executed properly by the backend */
42 PGRES_TUPLES_OK, /* a query command that returns tuples */
43 /* was executed properly by the backend, PGresult */
44 /* contains the resulttuples */
47 PGRES_BAD_RESPONSE, /* an unexpected response was recv'd from
54 /* string descriptions of the ExecStatusTypes */
55 extern const char *pgresStatus[];
58 * POSTGRES backend dependent Constants.
61 /* ERROR_MSG_LENGTH should really be the same as ELOG_MAXLEN in utils/elog.h*/
62 #define ERROR_MSG_LENGTH 4096
63 #define COMMAND_LENGTH 20
64 #define REMARK_LENGTH 80
65 #define PORTAL_NAME_LENGTH 16
69 * Information (pointer to array of this structure) required
70 * for the PQfn() call.
79 int *ptr; /* can't use void (dec compiler barfs) */
84 typedef struct pgresAttDesc
86 char *name; /* type name */
87 Oid adtid; /* type id */
88 short adtsize; /* type size */
91 /* use char* for Attribute values,
92 ASCII tuples are guaranteed to be null-terminated
93 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*/
97 #define NULL_LEN (-1) /* pg_result len for NULL value */
99 typedef struct pgresAttValue
101 int len; /* length in bytes of the value */
102 char *value; /* actual value */
105 typedef struct pgNotify
107 char relname[NAMEDATALEN]; /* name of relation
109 int be_pid; /* process id of backend */
112 typedef struct pgLobjfuncs
114 Oid fn_lo_open; /* OID of backend function lo_open */
115 Oid fn_lo_close;/* OID of backend function lo_close */
116 Oid fn_lo_creat;/* OID of backend function lo_creat */
117 Oid fn_lo_unlink; /* OID of backend function
119 Oid fn_lo_lseek;/* OID of backend function lo_lseek */
120 Oid fn_lo_tell; /* OID of backend function lo_tell */
121 Oid fn_lo_read; /* OID of backend function LOread */
122 Oid fn_lo_write;/* OID of backend function LOwrite */
125 /* PGconn encapsulates a connection to the backend */
126 typedef struct pg_conn
128 char *pghost; /* the machine on which the server is
130 char *pgtty; /* tty on which the backend messages is
132 char *pgport; /* the communication port with the backend */
133 char *pgoptions; /* options to start the backend with */
134 char *dbName; /* database name */
135 ConnStatusType status;
136 char errorMessage[ERROR_MSG_LENGTH];
137 /* pipes for be/fe communication */
141 int sock; /* The socket */
142 SockAddr laddr; /* Local address */
143 SockAddr raddr; /* Remote address */
145 int asyncNotifyWaiting;
147 char *pguser; /* Postgres username of user who is
150 PGlobjfuncs *lobjfuncs; /* Backend function OID's for large object
154 #define CMDSTATUS_LEN 40
156 /* PGresult encapsulates the result of a query */
157 /* unlike the old libpq, we assume that queries only return in one group */
158 typedef struct pg_result
162 PGresAttDesc *attDescs;
163 PGresAttValue **tuples; /* each PGresTuple is an array of
165 int tupArrSize; /* size of tuples array allocated */
166 ExecStatusType resultStatus;
167 char cmdStatus[CMDSTATUS_LEN]; /* cmd status from the
168 * last insert query */
169 int binary; /* binary tuple values if binary == 1,
177 * We can't use the conventional "bool", because we are designed to be
178 * included in a user's program, and user may already have that type
179 * defined. Pqbool, on the other hand, is unlikely to be used.
184 pqbool header; /* print output field headings and row
186 pqbool align; /* fill align the fields */
187 pqbool standard; /* old brain dead format */
188 pqbool html3; /* output html tables */
189 pqbool expanded; /* expand tables */
190 pqbool pager; /* use pager for output if needed */
191 char *fieldSep; /* field separator */
192 char *tableOpt; /* insert to HTML <table ...> */
193 char *caption; /* HTML <caption> */
194 char **fieldName; /* null terminated array of repalcement
198 typedef struct _PQprintOpt PQprintOpt;
201 * Structure for the conninfo parameter definitions of PQconnectdb()
204 struct _PQconninfoOption
206 char *keyword; /* The keyword of the option */
207 char *environ; /* Fallback environment variable name */
208 char *compiled; /* Fallback compiled in default value */
209 char *val; /* Options value */
210 char *label; /* Label for field in connect dialog */
211 char *dispchar; /* Character to display for this field */
212 /* in a connect dialog. Values are: */
213 /* "" Display entered value as is */
214 /* "*" Password field - hide value */
215 /* "D" Debug options - don't */
216 /* create a field by default */
217 int dispsize; /* Field size in characters for dialog */
220 typedef struct _PQconninfoOption PQconninfoOption;
222 /* === in fe-connect.c === */
223 /* make a new client connection to the backend */
224 extern PGconn *PQconnectdb(const char *conninfo);
225 extern PQconninfoOption *PQconndefaults(void);
226 extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions,
227 const char *pgtty, const char *dbName, const char *login, const char *pwd);
228 #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)
229 /* close the current connection and free the PGconn data structure */
230 extern void PQfinish(PGconn *conn);
233 * close the current connection and restablish a new one with the same
236 extern void PQreset(PGconn *conn);
238 extern char *PQdb(PGconn *conn);
239 extern char *PQuser(PGconn *conn);
240 extern char *PQhost(PGconn *conn);
241 extern char *PQoptions(PGconn *conn);
242 extern char *PQport(PGconn *conn);
243 extern char *PQtty(PGconn *conn);
244 extern ConnStatusType PQstatus(PGconn *conn);
245 extern char *PQerrorMessage(PGconn *conn);
246 extern void PQtrace(PGconn *conn, FILE *debug_port);
247 extern void PQuntrace(PGconn *conn);
249 /* === in fe-exec.c === */
250 extern PGresult *PQexec(PGconn *conn, const char *query);
251 extern int PQgetline(PGconn *conn, char *string, int length);
252 extern int PQendcopy(PGconn *conn);
253 extern void PQputline(PGconn *conn, const char *string);
254 extern ExecStatusType PQresultStatus(PGresult *res);
255 extern int PQntuples(PGresult *res);
256 extern int PQnfields(PGresult *res);
257 extern char *PQfname(PGresult *res, int field_num);
258 extern int PQfnumber(PGresult *res, const char *field_name);
259 extern Oid PQftype(PGresult *res, int field_num);
260 extern short PQfsize(PGresult *res, int field_num);
261 extern char *PQcmdStatus(PGresult *res);
262 extern const char *PQoidStatus(PGresult *res);
263 extern const char *PQcmdTuples(PGresult *res);
264 extern char *PQgetvalue(PGresult *res, int tup_num, int field_num);
265 extern int PQgetlength(PGresult *res, int tup_num, int field_num);
266 extern int PQgetisnull(PGresult *res, int tup_num, int field_num);
267 extern void PQclear(PGresult *res);
268 /* PQdisplayTuples() is a better version of PQprintTuples() */
269 extern void PQdisplayTuples(PGresult *res,
270 FILE *fp, /* where to send the
272 int fillAlign, /* pad the fields with
274 const char *fieldSep, /* field separator */
275 int printHeader, /* display headers? */
277 extern void PQprintTuples(PGresult *res,
278 FILE *fout, /* output stream */
279 int printAttName, /* print attribute names
281 int terseOutput, /* delimiter bars or
283 int width /* width of column, if
284 * 0, use variable width */
286 extern void PQprint(FILE *fout, /* output stream */
288 PQprintOpt *ps /* option structure */
290 extern PGnotify *PQnotifies(PGconn *conn);
291 extern PGresult *PQfn(PGconn *conn,
298 /* === in fe-auth.c === */
299 extern MsgType fe_getauthsvc(char *PQerrormsg);
300 extern void fe_setauthsvc(const char *name, char *PQerrormsg);
301 extern char *fe_getauthname(char *PQerrormsg);
303 /* === in fe-misc.c === */
304 /* pqGets and pqPuts gets and sends strings to the file stream
305 returns 0 if successful
306 if debug is non-null, debugging output is sent to that stream
308 extern int pqGets(char *s, int maxlen, FILE *stream, FILE *debug);
309 extern int pqGetnchar(char *s, int maxlen, FILE *stream, FILE *debug);
310 extern int pqPutnchar(const char *s, int maxlen, FILE *stream, FILE *debug);
311 extern int pqPuts(const char *s, FILE *stream, FILE *debug);
312 extern int pqGetc(FILE *stream, FILE *debug);
313 /* get a n-byte integer from the stream into result */
314 /* returns 0 if successful */
315 extern int pqGetInt(int *result, int bytes, FILE *stream, FILE *debug);
316 /* put a n-byte integer into the stream */
317 /* returns 0 if successful */
318 extern int pqPutInt(const int n, int bytes, FILE *stream, FILE *debug);
319 extern void pqFlush(FILE *stream, FILE *debug);
321 /* === in fe-lobj.c === */
322 int lo_open(PGconn *conn, Oid lobjId, int mode);
323 int lo_close(PGconn *conn, int fd);
324 int lo_read(PGconn *conn, int fd, char *buf, int len);
325 int lo_write(PGconn *conn, int fd, char *buf, int len);
326 int lo_lseek(PGconn *conn, int fd, int offset, int whence);
327 Oid lo_creat(PGconn *conn, int mode);
328 int lo_tell(PGconn *conn, int fd);
329 int lo_unlink(PGconn *conn, Oid lobjId);
330 Oid lo_import(PGconn *conn, char *filename);
331 int lo_export(PGconn *conn, Oid lobjId, char *filename);
332 /* max length of message to send */
333 #define MAX_MESSAGE_LEN 8193
335 /* maximum number of fields in a tuple */
337 #define MAX_FIELDS 512
339 /* fall back options if they are not specified by arguments or defined
340 by environment variables */
341 #define DefaultHost "localhost"
342 #define DefaultTty ""
343 #define DefaultOption ""
344 #define DefaultAuthtype ""
345 #define DefaultPassword ""
349 #define palloc malloc
353 extern char *sys_errlist[];
354 #define strerror(A) (sys_errlist[(A)])
362 #endif /* LIBPQ_FE_H */