1 /*-------------------------------------------------------------------------
4 * This file contains definitions for structures and
5 * externs for functions used by frontend postgres applications.
7 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $Id: libpq-fe.h,v 1.71 2001/03/22 04:01:27 momjian Exp $
12 *-------------------------------------------------------------------------
24 /* postgres_ext.h defines the backend's externally visible types,
27 #include "postgres_ext.h"
29 #include <openssl/ssl.h>
32 /* Application-visible enum types */
38 * Although you may decide to change this list in some way, values
39 * which become unused should never be removed, nor should
40 * constants be redefined - that would break compatibility with
45 /* Non-blocking mode only below here */
48 * The existence of these should never be relied upon - they
49 * should only be used for user feedback or similar purposes.
51 CONNECTION_STARTED, /* Waiting for connection to be made. */
52 CONNECTION_MADE, /* Connection OK; waiting to send. */
53 CONNECTION_AWAITING_RESPONSE, /* Waiting for a response from the
55 CONNECTION_AUTH_OK, /* Received authentication; waiting for
57 CONNECTION_SETENV /* Negotiating environment. */
62 PGRES_POLLING_FAILED = 0,
63 PGRES_POLLING_READING, /* These two indicate that one may */
64 PGRES_POLLING_WRITING, /* use select before polling again. */
66 PGRES_POLLING_ACTIVE /* Can call poll function immediately. */
67 } PostgresPollingStatusType;
71 PGRES_EMPTY_QUERY = 0,
72 PGRES_COMMAND_OK, /* a query command that doesn't return
73 * anything was executed properly by the
75 PGRES_TUPLES_OK, /* a query command that returns tuples was
76 * executed properly by the backend,
77 * PGresult contains the result tuples */
78 PGRES_COPY_OUT, /* Copy Out data transfer in progress */
79 PGRES_COPY_IN, /* Copy In data transfer in progress */
80 PGRES_BAD_RESPONSE, /* an unexpected response was recv'd from
86 /* PGconn encapsulates a connection to the backend.
87 * The contents of this struct are not supposed to be known to applications.
89 typedef struct pg_conn PGconn;
91 /* PGresult encapsulates the result of a query (or more precisely, of a single
92 * SQL command --- a query string given to PQsendQuery can contain multiple
93 * commands and thus return multiple PGresult objects).
94 * The contents of this struct are not supposed to be known to applications.
96 typedef struct pg_result PGresult;
98 /* PGnotify represents the occurrence of a NOTIFY message.
99 * Ideally this would be an opaque typedef, but it's so simple that it's
100 * unlikely to change.
101 * NOTE: in Postgres 6.4 and later, the be_pid is the notifying backend's,
102 * whereas in earlier versions it was always your own backend's PID.
104 typedef struct pgNotify
106 char relname[NAMEDATALEN]; /* name of relation
108 int be_pid; /* process id of backend */
111 /* PQnoticeProcessor is the function type for the notice-message callback.
113 typedef void (*PQnoticeProcessor) (void *arg, const char *message);
115 /* Print options for PQprint() */
118 typedef struct _PQprintOpt
120 pqbool header; /* print output field headings and row
122 pqbool align; /* fill align the fields */
123 pqbool standard; /* old brain dead format */
124 pqbool html3; /* output html tables */
125 pqbool expanded; /* expand tables */
126 pqbool pager; /* use pager for output if needed */
127 char *fieldSep; /* field separator */
128 char *tableOpt; /* insert to HTML <table ...> */
129 char *caption; /* HTML <caption> */
130 char **fieldName; /* null terminated array of repalcement
135 * Structure for the conninfo parameter definitions returned by PQconndefaults
137 * All fields except "val" point at static strings which must not be altered.
138 * "val" is either NULL or a malloc'd current-value string. PQconninfoFree()
139 * will release both the val strings and the PQconninfoOption array itself.
142 typedef struct _PQconninfoOption
144 char *keyword; /* The keyword of the option */
145 char *envvar; /* Fallback environment variable name */
146 char *compiled; /* Fallback compiled in default value */
147 char *val; /* Option's current value, or NULL */
148 char *label; /* Label for field in connect dialog */
149 char *dispchar; /* Character to display for this field in
150 * a connect dialog. Values are: ""
151 * Display entered value as is "*"
152 * Password field - hide value "D" Debug
153 * option - don't show by default */
154 int dispsize; /* Field size in characters for dialog */
158 * PQArgBlock -- structure for PQfn() arguments
167 int *ptr; /* can't use void (dec compiler barfs) */
173 * Exported functions of libpq
177 /* === in fe-connect.c === */
179 /* make a new client connection to the backend */
180 /* Asynchronous (non-blocking) */
181 extern PGconn *PQconnectStart(const char *conninfo);
182 extern PostgresPollingStatusType PQconnectPoll(PGconn *conn);
183 /* Synchronous (blocking) */
184 extern PGconn *PQconnectdb(const char *conninfo);
185 extern PGconn *PQsetdbLogin(const char *pghost, const char *pgport,
186 const char *pgoptions, const char *pgtty,
188 const char *login, const char *pwd);
189 #define PQsetdb(M_PGHOST,M_PGPORT,M_PGOPT,M_PGTTY,M_DBNAME) \
190 PQsetdbLogin(M_PGHOST, M_PGPORT, M_PGOPT, M_PGTTY, M_DBNAME, NULL, NULL)
192 /* close the current connection and free the PGconn data structure */
193 extern void PQfinish(PGconn *conn);
195 /* get info about connection options known to PQconnectdb */
196 extern PQconninfoOption *PQconndefaults(void);
198 /* free the data structure returned by PQconndefaults() */
199 extern void PQconninfoFree(PQconninfoOption *connOptions);
202 * close the current connection and restablish a new one with the same
205 /* Asynchronous (non-blocking) */
206 extern int PQresetStart(PGconn *conn);
207 extern PostgresPollingStatusType PQresetPoll(PGconn *conn);
208 /* Synchronous (blocking) */
209 extern void PQreset(PGconn *conn);
211 /* issue a cancel request */
212 extern int PQrequestCancel(PGconn *conn);
214 /* Accessor functions for PGconn objects */
215 extern char *PQdb(const PGconn *conn);
216 extern char *PQuser(const PGconn *conn);
217 extern char *PQpass(const PGconn *conn);
218 extern char *PQhost(const PGconn *conn);
219 extern char *PQport(const PGconn *conn);
220 extern char *PQtty(const PGconn *conn);
221 extern char *PQoptions(const PGconn *conn);
222 extern ConnStatusType PQstatus(const PGconn *conn);
223 extern char *PQerrorMessage(const PGconn *conn);
224 extern int PQsocket(const PGconn *conn);
225 extern int PQbackendPID(const PGconn *conn);
226 extern int PQclientEncoding(const PGconn *conn);
227 extern int PQsetClientEncoding(PGconn *conn, const char *encoding);
229 /* Get the SSL structure associated with a connection */
230 extern SSL *PQgetssl(PGconn *conn);
234 /* Enable/disable tracing */
235 extern void PQtrace(PGconn *conn, FILE *debug_port);
236 extern void PQuntrace(PGconn *conn);
238 /* Override default notice processor */
239 extern PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg);
241 /* === in fe-exec.c === */
243 /* Simple synchronous query */
244 extern PGresult *PQexec(PGconn *conn, const char *query);
245 extern PGnotify *PQnotifies(PGconn *conn);
247 /* Interface for multiple-result or asynchronous queries */
248 extern int PQsendQuery(PGconn *conn, const char *query);
249 extern PGresult *PQgetResult(PGconn *conn);
251 /* Routines for managing an asychronous query */
252 extern int PQisBusy(PGconn *conn);
253 extern int PQconsumeInput(PGconn *conn);
255 /* Routines for copy in/out */
256 extern int PQgetline(PGconn *conn, char *string, int length);
257 extern int PQputline(PGconn *conn, const char *string);
258 extern int PQgetlineAsync(PGconn *conn, char *buffer, int bufsize);
259 extern int PQputnbytes(PGconn *conn, const char *buffer, int nbytes);
260 extern int PQendcopy(PGconn *conn);
262 /* Set blocking/nonblocking connection to the backend */
263 extern int PQsetnonblocking(PGconn *conn, int arg);
264 extern int PQisnonblocking(const PGconn *conn);
266 /* Force the write buffer to be written (or at least try) */
267 extern int PQflush(PGconn *conn);
270 * "Fast path" interface --- not really recommended for application
273 extern PGresult *PQfn(PGconn *conn,
278 const PQArgBlock *args,
281 /* Accessor functions for PGresult objects */
282 extern ExecStatusType PQresultStatus(const PGresult *res);
283 extern char *PQresStatus(ExecStatusType status);
284 extern char *PQresultErrorMessage(const PGresult *res);
285 extern int PQntuples(const PGresult *res);
286 extern int PQnfields(const PGresult *res);
287 extern int PQbinaryTuples(const PGresult *res);
288 extern char *PQfname(const PGresult *res, int field_num);
289 extern int PQfnumber(const PGresult *res, const char *field_name);
290 extern Oid PQftype(const PGresult *res, int field_num);
291 extern int PQfsize(const PGresult *res, int field_num);
292 extern int PQfmod(const PGresult *res, int field_num);
293 extern char *PQcmdStatus(PGresult *res);
294 extern char *PQoidStatus(const PGresult *res); /* old and ugly */
295 extern Oid PQoidValue(const PGresult *res); /* new and improved */
296 extern char *PQcmdTuples(PGresult *res);
297 extern char *PQgetvalue(const PGresult *res, int tup_num, int field_num);
298 extern int PQgetlength(const PGresult *res, int tup_num, int field_num);
299 extern int PQgetisnull(const PGresult *res, int tup_num, int field_num);
301 /* Delete a PGresult */
302 extern void PQclear(PGresult *res);
305 * Make an empty PGresult with given status (some apps find this
306 * useful). If conn is not NULL and status indicates an error, the
307 * conn's errorMessage is copied.
309 extern PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status);
311 /* === in fe-print.c === */
313 extern void PQprint(FILE *fout, /* output stream */
315 const PQprintOpt *ps); /* option structure */
318 * really old printing routines
320 extern void PQdisplayTuples(const PGresult *res,
321 FILE *fp, /* where to send the
323 int fillAlign, /* pad the fields with
325 const char *fieldSep, /* field separator */
326 int printHeader, /* display headers? */
329 extern void PQprintTuples(const PGresult *res,
330 FILE *fout, /* output stream */
331 int printAttName, /* print attribute names */
332 int terseOutput, /* delimiter bars */
333 int width); /* width of column, if
334 * 0, use variable width */
337 /* === in fe-lobj.c === */
339 /* Large-object access routines */
340 extern int lo_open(PGconn *conn, Oid lobjId, int mode);
341 extern int lo_close(PGconn *conn, int fd);
342 extern int lo_read(PGconn *conn, int fd, char *buf, size_t len);
343 extern int lo_write(PGconn *conn, int fd, char *buf, size_t len);
344 extern int lo_lseek(PGconn *conn, int fd, int offset, int whence);
345 extern Oid lo_creat(PGconn *conn, int mode);
346 extern int lo_tell(PGconn *conn, int fd);
347 extern int lo_unlink(PGconn *conn, Oid lobjId);
348 extern Oid lo_import(PGconn *conn, const char *filename);
349 extern int lo_export(PGconn *conn, Oid lobjId, const char *filename);
351 /* === in fe-misc.c === */
353 /* Determine length of multibyte encoded char at *s */
354 extern int PQmblen(const unsigned char *s, int encoding);
356 /* Get encoding id from environment variable PGCLIENTENCODING */
357 extern int PQenv2encoding(void);
364 #endif /* LIBPQ_FE_H */