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.7 1996/08/13 01:34:29 scrappy Exp $
11 *-------------------------------------------------------------------------
22 * include stuff common to fe and be
25 /* #include "libpq/libpq.h" */
26 #include "libpq/pqcomm.h"
27 #include "lib/dllist.h"
29 typedef enum {CONNECTION_OK,
30 CONNECTION_BAD} ConnStatusType;
33 PGRES_EMPTY_QUERY = 0,
34 PGRES_COMMAND_OK, /* a query command that doesn't return */
35 /* anything was executed properly by the backend */
36 PGRES_TUPLES_OK, /* a query command that returns tuples */
37 /* was executed properly by the backend, PGresult */
38 /* contains the resulttuples */
41 PGRES_BAD_RESPONSE, /* an unexpected response was recv'd from the backend */
47 /* string descriptions of the ExecStatusTypes */
48 extern const char* pgresStatus[];
51 * POSTGRES backend dependent Constants.
54 /* ERROR_MSG_LENGTH should really be the same as ELOG_MAXLEN in utils/elog.h*/
55 #define ERROR_MSG_LENGTH 4096
56 #define COMMAND_LENGTH 20
57 #define REMARK_LENGTH 80
58 #define PORTAL_NAME_LENGTH 16
62 * Information (pointer to array of this structure) required
63 * for the PQfn() call.
70 int *ptr; /* can't use void (dec compiler barfs) */
75 typedef struct pgresAttDesc {
76 char* name; /* type name */
77 Oid adtid; /* type id */
78 int2 adtsize; /* type size */
81 /* use char* for Attribute values,
82 ASCII tuples are guaranteed to be null-terminated
83 For binary tuples, the first four bytes of the value is the size,
84 and the bytes afterwards are the value. The binary value is
85 not guaranteed to be null-terminated. In fact, it can have embedded nulls*/
87 #define NULL_LEN (-1) /* pg_result len for NULL value */
89 typedef struct pgresAttValue {
90 int len; /* length in bytes of the value */
91 char *value; /* actual value */
94 typedef struct pgNotify {
95 char relname[NAMEDATALEN]; /* name of relation containing data */
96 int be_pid; /* process id of backend */
99 /* PGconn encapsulates a connection to the backend */
100 typedef struct pg_conn{
101 char *pghost; /* the machine on which the server is running */
102 char *pgtty; /* tty on which the backend messages is displayed */
103 char *pgport; /* the communication port with the backend */
104 char *pgoptions; /* options to start the backend with */
105 char *dbName; /* database name */
106 ConnStatusType status;
107 char errorMessage[ERROR_MSG_LENGTH];
108 /* pipes for be/fe communication */
112 void *port; /* really a Port* */
113 int asyncNotifyWaiting;
117 #define CMDSTATUS_LEN 40
119 /* PGresult encapsulates the result of a query */
120 /* unlike the old libpq, we assume that queries only return in one group */
121 typedef struct pg_result{
124 PGresAttDesc *attDescs;
125 PGresAttValue* *tuples; /* each PGresTuple is an array of PGresAttValue's */
126 int tupArrSize; /* size of tuples array allocated */
127 ExecStatusType resultStatus;
128 char cmdStatus[CMDSTATUS_LEN]; /* cmd status from the last insert query*/
129 int binary; /* binary tuple values if binary == 1, otherwise ASCII */
134 bool header; /* print output field headings and row count */
135 bool align; /* fill align the fields */
136 bool standard; /* old brain dead format */
137 bool html3; /* output html tables */
138 bool expanded; /* expand tables */
139 bool pager; /* use pager for output if needed */
140 char *fieldSep; /* field separator */
141 char *tableOpt; /* insert to HTML <table ...> */
142 char *caption; /* HTML <caption> */
143 char **fieldName; /* null terminated array of repalcement field names */
146 typedef struct _PQprintOpt PQprintOpt;
148 /* === in fe-connect.c === */
149 /* make a new client connection to the backend */
150 extern PGconn* PQsetdb(const char* pghost, const char* pgport, const char* pgoptions,
151 const char* pgtty, const char* dbName);
152 /* close the current connection and free the PGconn data structure */
153 extern void PQfinish(PGconn* conn);
154 /* close the current connection and restablish a new one with the same
156 extern void PQreset(PGconn* conn);
158 extern char* PQdb(PGconn* conn);
159 extern char* PQhost(PGconn* conn);
160 extern char* PQoptions(PGconn* conn);
161 extern char* PQport(PGconn* conn);
162 extern char* PQtty(PGconn* conn);
163 extern ConnStatusType PQstatus(PGconn* conn);
164 extern char* PQerrorMessage(PGconn* conn);
165 extern void PQtrace(PGconn *conn, FILE* debug_port);
166 extern void PQuntrace(PGconn *conn);
168 /* === in fe-exec.c === */
169 extern PGresult* PQexec(PGconn* conn, const char* query);
170 extern int PQgetline(PGconn *conn, char* string, int length);
171 extern int PQendcopy(PGconn *conn);
172 extern void PQputline(PGconn *conn, const char* string);
173 extern ExecStatusType PQresultStatus(PGresult* res);
174 extern int PQntuples(PGresult *res);
175 extern int PQnfields(PGresult *res);
176 extern char* PQfname(PGresult *res, int field_num);
177 extern int PQfnumber(PGresult *res, const char* field_name);
178 extern Oid PQftype(PGresult *res, int field_num);
179 extern int2 PQfsize(PGresult *res, int field_num);
180 extern char* PQcmdStatus(PGresult *res);
181 extern const char* PQoidStatus(PGresult *res);
182 extern char* PQgetvalue(PGresult *res, int tup_num, int field_num);
183 extern int PQgetlength(PGresult *res, int tup_num, int field_num);
184 extern int PQgetisnull(PGresult *res, int tup_num, int field_num);
185 extern void PQclear(PGresult* res);
186 /* PQdisplayTuples() is a better version of PQprintTuples() */
187 extern void PQdisplayTuples(PGresult *res,
188 FILE *fp, /* where to send the output */
189 int fillAlign, /* pad the fields with spaces */
190 const char *fieldSep, /* field separator */
191 int printHeader, /* display headers? */
193 extern void PQprintTuples(PGresult* res,
194 FILE* fout, /* output stream */
195 int printAttName,/* print attribute names or not*/
196 int terseOutput, /* delimiter bars or not?*/
197 int width /* width of column,
198 if 0, use variable width */
200 extern void PQprint(FILE* fout, /* output stream */
202 PQprintOpt *ps /* option structure */
204 extern PGnotify* PQnotifies(PGconn *conn);
205 extern PGresult* PQfn(PGconn* conn,
212 /* === in fe-auth.c === */
213 extern MsgType fe_getauthsvc(char* PQerrormsg);
214 extern void fe_setauthsvc(const char *name, char* PQerrormsg);
215 extern char *fe_getauthname(char* PQerrormsg);
217 /* === in fe-misc.c === */
218 /* pqGets and pqPuts gets and sends strings to the file stream
219 returns 0 if successful
220 if debug is non-null, debugging output is sent to that stream
222 extern int pqGets(char* s, int maxlen, FILE* stream, FILE* debug);
223 extern int pqGetnchar(char* s, int maxlen, FILE* stream, FILE* debug);
224 extern int pqPutnchar(const char* s, int maxlen, FILE* stream, FILE* debug);
225 extern int pqPuts(const char* s, FILE* stream, FILE* debug );
226 extern int pqGetc(FILE* stream, FILE *debug);
227 /* get a n-byte integer from the stream into result */
228 /* returns 0 if successful */
229 extern int pqGetInt(int* result, int bytes, FILE* stream, FILE *debug );
230 /* put a n-byte integer into the stream */
231 /* returns 0 if successful */
232 extern int pqPutInt(int n, int bytes, FILE* stream, FILE *debug );
233 extern void pqFlush(FILE* stream, FILE* debug);
235 /* === in fe-lobj.c === */
236 int lo_open(PGconn* conn, Oid lobjId, int mode);
237 int lo_close(PGconn *conn, int fd);
238 int lo_read(PGconn *conn, int fd, char *buf, int len);
239 int lo_write(PGconn *conn, int fd, char *buf, int len);
240 int lo_lseek(PGconn *conn, int fd, int offset, int whence);
241 Oid lo_creat(PGconn *conn, int mode);
242 int lo_tell(PGconn *conn, int fd);
243 int lo_unlink(PGconn *conn, Oid lobjId);
244 Oid lo_import(PGconn *conn, char *filename);
245 int lo_export(PGconn *conn, Oid lobjId, char *filename);
246 /* max length of message to send */
247 #define MAX_MESSAGE_LEN 8193
249 /* maximum number of fields in a tuple */
251 #define MAX_FIELDS 512
253 /* fall back options if they are not specified by arguments or defined
254 by environment variables */
255 #define DefaultHost "localhost"
256 #define DefaultTty ""
257 #define DefaultOption ""
260 #define palloc malloc
263 #if defined(PORTNAME_sparc)
264 extern char *sys_errlist[];
265 #define strerror(A) (sys_errlist[(A)])
266 #endif /* PORTNAME_sparc */
272 #endif /* LIBPQ_FE_H */