1 /*-------------------------------------------------------------------------
4 * This file contains internal definitions meant to be used only by
5 * the frontend libpq library, not by applications that call it.
7 * An application can include this file if it wants to bypass the
8 * official API defined by libpq-fe.h, but code that does so is much
9 * more likely to break across PostgreSQL releases than code that uses
10 * only the official API.
12 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
13 * Portions Copyright (c) 1994, Regents of the University of California
15 * $Id: libpq-int.h,v 1.25 2000/05/27 03:58:20 momjian Exp $
17 *-------------------------------------------------------------------------
23 /* We assume libpq-fe.h has already been included. */
25 /* include stuff common to fe and be */
26 #include "libpq/pqcomm.h"
27 #include "lib/dllist.h"
28 /* include stuff found in fe only */
29 #include "pqexpbuffer.h"
32 #include <openssl/ssl.h>
33 #include <openssl/err.h>
36 /* libpq supports this version of the frontend/backend protocol.
38 * NB: we used to use PG_PROTOCOL_LATEST from the backend pqcomm.h file,
39 * but that's not really the right thing: just recompiling libpq
40 * against a more recent backend isn't going to magically update it
41 * for most sorts of protocol changes. So, when you change libpq
42 * to support a different protocol revision, you have to change this
43 * constant too. PG_PROTOCOL_EARLIEST and PG_PROTOCOL_LATEST in
44 * pqcomm.h describe what the backend knows, not what libpq knows.
47 #define PG_PROTOCOL_LIBPQ PG_PROTOCOL(2,0)
50 * POSTGRES backend dependent Constants.
53 #define CMDSTATUS_LEN 40
56 * PGresult and the subsidiary types PGresAttDesc, PGresAttValue
57 * represent the result of a query (or more precisely, of a single SQL
58 * command --- a query string given to PQexec can contain multiple commands).
59 * Note we assume that a single command can return at most one tuple group,
60 * hence there is no need for multiple descriptor sets.
63 /* Subsidiary-storage management structure for PGresult.
64 * See space management routines in fe-exec.c for details.
65 * Note that space[k] refers to the k'th byte starting from the physical
66 * head of the block --- it's a union, not a struct!
68 typedef union pgresult_data PGresult_data;
72 PGresult_data *next; /* link to next block, or NULL */
73 char space[1]; /* dummy for accessing block as bytes */
76 /* Data about a single attribute (column) of a query result */
78 typedef struct pgresAttDesc
80 char *name; /* type name */
81 Oid typid; /* type id */
82 int typlen; /* type size */
83 int atttypmod; /* type-specific modifier info */
86 /* Data for a single attribute of a single tuple */
88 /* We use char* for Attribute values.
89 The value pointer always points to a null-terminated area; we add a
90 null (zero) byte after whatever the backend sends us. This is only
91 particularly useful for ASCII tuples ... with a binary value, the
92 value might have embedded nulls, so the application can't use C string
93 operators on it. But we add a null anyway for consistency.
94 Note that the value itself does not contain a length word.
96 A NULL attribute is a special case in two ways: its len field is NULL_LEN
97 and its value field points to null_field in the owning PGresult. All the
98 NULL attributes in a query result point to the same place (there's no need
99 to store a null string separately for each one).
102 #define NULL_LEN (-1) /* pg_result len for NULL value */
104 typedef struct pgresAttValue
106 int len; /* length in bytes of the value */
107 char *value; /* actual value, plus terminating zero
115 PGresAttDesc *attDescs;
116 PGresAttValue **tuples; /* each PGresTuple is an array of
118 int tupArrSize; /* size of tuples array allocated */
119 ExecStatusType resultStatus;
120 char cmdStatus[CMDSTATUS_LEN]; /* cmd status from the
122 int binary; /* binary tuple values if binary == 1,
126 * The conn link in PGresult is no longer used by any libpq code. It
127 * should be removed entirely, because it could be a dangling link
128 * (the application could keep the PGresult around longer than it
129 * keeps the PGconn!) But there may be apps out there that depend on
130 * it, so we will leave it here at least for a release or so.
132 PGconn *xconn; /* connection we did the query on, if any */
135 * These fields are copied from the originating PGconn, so that
136 * operations on the PGresult don't have to reference the PGconn.
138 PQnoticeProcessor noticeHook; /* notice/error message processor */
140 int client_encoding;/* encoding id */
143 char *errMsg; /* error message, or NULL if no error */
145 /* All NULL attributes in the query result point to this null string */
149 * Space management information. Note that attDescs and errMsg, if
150 * not null, point into allocated blocks. But tuples points to a
151 * separately malloc'd block, so that we can realloc it.
153 PGresult_data *curBlock; /* most recently allocated block */
154 int curOffset; /* start offset of free space in block */
155 int spaceLeft; /* number of free bytes remaining in block */
158 /* PGAsyncStatusType defines the state of the query-execution state machine */
161 PGASYNC_IDLE, /* nothing's happening, dude */
162 PGASYNC_BUSY, /* query in progress */
163 PGASYNC_READY, /* result ready for PQgetResult */
164 PGASYNC_COPY_IN, /* Copy In data transfer in progress */
165 PGASYNC_COPY_OUT /* Copy Out data transfer in progress */
168 /* PGSetenvStatusType defines the state of the PQSetenv state machine */
171 SETENV_STATE_OPTION_SEND, /* About to send an Environment Option */
172 SETENV_STATE_OPTION_WAIT, /* Waiting for above send to complete */
173 /* these next two are only used in MULTIBYTE mode */
174 SETENV_STATE_ENCODINGS_SEND,/* About to send an "encodings" query */
175 SETENV_STATE_ENCODINGS_WAIT,/* Waiting for query to complete */
177 } PGSetenvStatusType;
179 /* large-object-access data ... allocated only if large-object code is used. */
180 typedef struct pgLobjfuncs
182 Oid fn_lo_open; /* OID of backend function lo_open */
183 Oid fn_lo_close; /* OID of backend function lo_close */
184 Oid fn_lo_creat; /* OID of backend function lo_creat */
185 Oid fn_lo_unlink; /* OID of backend function lo_unlink */
186 Oid fn_lo_lseek; /* OID of backend function lo_lseek */
187 Oid fn_lo_tell; /* OID of backend function lo_tell */
188 Oid fn_lo_read; /* OID of backend function LOread */
189 Oid fn_lo_write; /* OID of backend function LOwrite */
192 /* PGconn stores all the state data associated with a single connection
197 /* Saved values of connection options */
198 char *pghost; /* the machine on which the server is
200 char *pghostaddr; /* the IPv4 address of the machine on
201 * which the server is running, in IPv4
202 * numbers-and-dots notation. Takes
203 * precedence over above. */
204 char *pgport; /* the server's communication port */
205 char *pgtty; /* tty on which the backend messages is
206 * displayed (NOT ACTUALLY USED???) */
207 char *pgoptions; /* options to start the backend with */
208 char *dbName; /* database name */
209 char *pguser; /* Postgres username and password, if any */
212 /* Optional file to write trace info to */
215 /* Callback procedure for notice/error message processing */
216 PQnoticeProcessor noticeHook;
219 /* Status indicators */
220 ConnStatusType status;
221 PGAsyncStatusType asyncStatus;
222 Dllist *notifyList; /* Notify msgs not yet handed to
225 /* Connection data */
226 int sock; /* Unix FD for socket, -1 if not connected */
227 SockAddr laddr; /* Local address */
228 SockAddr raddr; /* Remote address */
229 int raddr_len; /* Length of remote address */
231 /* Miscellaneous stuff */
232 int be_pid; /* PID of backend --- needed for cancels */
233 int be_key; /* key of backend --- needed for cancels */
234 char salt[2]; /* password salt received from backend */
235 PGlobjfuncs *lobjfuncs; /* private state for large-object access
238 /* Buffer for data received from backend and not yet processed */
239 char *inBuffer; /* currently allocated buffer */
240 int inBufSize; /* allocated size of buffer */
241 int inStart; /* offset to first unconsumed data in
243 int inCursor; /* next byte to tentatively consume */
244 int inEnd; /* offset to first position after avail
247 int nonblocking; /* whether this connection is using a
248 * blocking socket to the backend or not */
250 /* Buffer for data not yet sent to backend */
251 char *outBuffer; /* currently allocated buffer */
252 int outBufSize; /* allocated size of buffer */
253 int outCount; /* number of chars waiting in buffer */
255 /* Status for asynchronous result construction */
256 PGresult *result; /* result being constructed */
257 PGresAttValue *curTuple; /* tuple currently being read */
259 /* Status for sending environment info. Used during PQSetenv only. */
260 PGSetenvStatusType setenv_state;
261 const struct EnvironmentOptions *next_eo;
264 bool allow_ssl_try; /* Allowed to try SSL negotiation */
265 SSL *ssl; /* SSL status, if have SSL connection */
268 /* Buffer for current error message */
269 PQExpBufferData errorMessage; /* expansible string */
271 /* Buffer for receiving various parts of messages */
272 PQExpBufferData workBuffer; /* expansible string */
274 int client_encoding;/* encoding id */
277 /* String descriptions of the ExecStatusTypes.
278 * direct use of this array is deprecated; call PQresStatus() instead.
280 extern char *const pgresStatus[];
283 * Internal functions of libpq
284 * Functions declared here need to be visible across files of libpq,
285 * but are not intended to be called by applications. We use the
286 * convention "pqXXX" for internal functions, vs. the "PQxxx" names
287 * used for application-visible routines.
291 /* === in fe-connect.c === */
293 extern int pqPacketSend(PGconn *conn, const char *buf, size_t len);
295 /* === in fe-exec.c === */
297 extern void pqSetResultError(PGresult *res, const char *msg);
298 extern void *pqResultAlloc(PGresult *res, size_t nBytes, bool isBinary);
299 extern char *pqResultStrdup(PGresult *res, const char *str);
300 extern void pqClearAsyncResult(PGconn *conn);
302 /* === in fe-misc.c === */
305 * "Get" and "Put" routines return 0 if successful, EOF if not. Note that
306 * for Get, EOF merely means the buffer is exhausted, not that there is
307 * necessarily any error.
309 extern int pqGetc(char *result, PGconn *conn);
310 extern int pqGets(PQExpBuffer buf, PGconn *conn);
311 extern int pqPuts(const char *s, PGconn *conn);
312 extern int pqGetnchar(char *s, size_t len, PGconn *conn);
313 extern int pqPutnchar(const char *s, size_t len, PGconn *conn);
314 extern int pqGetInt(int *result, size_t bytes, PGconn *conn);
315 extern int pqPutInt(int value, size_t bytes, PGconn *conn);
316 extern int pqReadData(PGconn *conn);
317 extern int pqFlush(PGconn *conn);
318 extern int pqWait(int forRead, int forWrite, PGconn *conn);
319 extern int pqReadReady(PGconn *conn);
320 extern int pqWriteReady(PGconn *conn);
325 /* fall back options if they are not specified by arguments or defined
326 by environment variables */
327 #define DefaultHost "localhost"
328 #define DefaultTty ""
329 #define DefaultOption ""
330 #define DefaultAuthtype ""
331 #define DefaultPassword ""
333 /* supply an implementation of strerror() macro if system doesn't have it */
335 #if defined(sun) && defined(__sparc__) && !defined(__SVR4)
336 extern char *sys_errlist[];
338 #define strerror(A) (sys_errlist[(A)])
340 #endif /* !strerror */
343 * this is so that we can check is a connection is non-blocking internally
344 * without the overhead of a function call
346 #define pqIsnonblocking(conn) (conn->nonblocking)
348 #endif /* LIBPQ_INT_H */