]> granicus.if.org Git - postgresql/blob - src/interfaces/libpq/libpq-int.h
Change PQconndefaults() to return a malloc'd array, instead of a static
[postgresql] / src / interfaces / libpq / libpq-int.h
1 /*-------------------------------------------------------------------------
2  *
3  * libpq-int.h
4  *        This file contains internal definitions meant to be used only by
5  *        the frontend libpq library, not by applications that call it.
6  *
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.
11  *
12  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
13  * Portions Copyright (c) 1994, Regents of the University of California
14  *
15  * $Id: libpq-int.h,v 1.20 2000/03/11 03:08:37 tgl Exp $
16  *
17  *-------------------------------------------------------------------------
18  */
19
20 #ifndef LIBPQ_INT_H
21 #define LIBPQ_INT_H
22
23 /* We assume libpq-fe.h has already been included. */
24
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"
30
31 #ifdef USE_SSL
32 #include <openssl/ssl.h>
33 #include <openssl/err.h>
34 #endif
35
36 /* libpq supports this version of the frontend/backend protocol.
37  *
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.
45  */
46
47 #define PG_PROTOCOL_LIBPQ       PG_PROTOCOL(2,0)
48
49 /*
50  * POSTGRES backend dependent Constants.
51  */
52
53 #define CMDSTATUS_LEN 40
54
55 /*
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.
61  */
62
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!
67  */
68 typedef union pgresult_data PGresult_data;
69
70 union pgresult_data
71 {
72         PGresult_data *next;            /* link to next block, or NULL */
73         char            space[1];               /* dummy for accessing block as bytes */
74 };
75
76 /* Data about a single attribute (column) of a query result */
77
78 typedef struct pgresAttDesc
79 {
80         char       *name;                       /* type name */
81         Oid                     typid;                  /* type id */
82         int                     typlen;                 /* type size */
83         int                     atttypmod;              /* type-specific modifier info */
84 } PGresAttDesc;
85
86 /* Data for a single attribute of a single tuple */
87
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.
95
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).
100  */
101
102 #define NULL_LEN                (-1)    /* pg_result len for NULL value */
103
104 typedef struct pgresAttValue
105 {
106         int                     len;                    /* length in bytes of the value */
107         char       *value;                      /* actual value, plus terminating zero
108                                                                  * byte */
109 } PGresAttValue;
110
111 struct pg_result
112 {
113         int                     ntups;
114         int                     numAttributes;
115         PGresAttDesc *attDescs;
116         PGresAttValue **tuples;         /* each PGresTuple is an array of
117                                                                  * PGresAttValue's */
118         int                     tupArrSize;             /* size of tuples array allocated */
119         ExecStatusType resultStatus;
120         char            cmdStatus[CMDSTATUS_LEN];               /* cmd status from the
121                                                                                                  * last query */
122         int                     binary;                 /* binary tuple values if binary == 1,
123                                                                  * otherwise ASCII */
124         /*
125          * The conn link in PGresult is no longer used by any libpq code.
126          * It should be removed entirely, because it could be a dangling link
127          * (the application could keep the PGresult around longer than it keeps
128          * the PGconn!)  But there may be apps out there that depend on it,
129          * so we will leave it here at least for a release or so.
130          */
131         PGconn     *xconn;                      /* connection we did the query on, if any */
132
133         /* Callback procedure for notice/error message processing
134          * (copied from originating PGconn).
135          */
136         PQnoticeProcessor noticeHook;
137         void       *noticeArg;
138
139         char       *errMsg;                     /* error message, or NULL if no error */
140
141         /* All NULL attributes in the query result point to this null string */
142         char            null_field[1];
143
144         /*
145          * Space management information.  Note that attDescs and errMsg, if
146          * not null, point into allocated blocks.  But tuples points to a
147          * separately malloc'd block, so that we can realloc it.
148          */
149         PGresult_data *curBlock;        /* most recently allocated block */
150         int                     curOffset;              /* start offset of free space in block */
151         int                     spaceLeft;              /* number of free bytes remaining in block */
152 };
153
154 /* PGAsyncStatusType defines the state of the query-execution state machine */
155 typedef enum
156 {
157         PGASYNC_IDLE,                           /* nothing's happening, dude */
158         PGASYNC_BUSY,                           /* query in progress */
159         PGASYNC_READY,                          /* result ready for PQgetResult */
160         PGASYNC_COPY_IN,                        /* Copy In data transfer in progress */
161         PGASYNC_COPY_OUT                        /* Copy Out data transfer in progress */
162 } PGAsyncStatusType;
163
164 /* large-object-access data ... allocated only if large-object code is used. */
165 typedef struct pgLobjfuncs
166 {
167         Oid                     fn_lo_open;             /* OID of backend function lo_open              */
168         Oid                     fn_lo_close;    /* OID of backend function lo_close             */
169         Oid                     fn_lo_creat;    /* OID of backend function lo_creat             */
170         Oid                     fn_lo_unlink;   /* OID of backend function lo_unlink    */
171         Oid                     fn_lo_lseek;    /* OID of backend function lo_lseek             */
172         Oid                     fn_lo_tell;             /* OID of backend function lo_tell              */
173         Oid                     fn_lo_read;             /* OID of backend function LOread               */
174         Oid                     fn_lo_write;    /* OID of backend function LOwrite              */
175 } PGlobjfuncs;
176
177 /* PGconn stores all the state data associated with a single connection
178  * to a backend.
179  */
180 struct pg_conn
181 {
182         /* Saved values of connection options */
183         char       *pghost;                     /* the machine on which the server is
184                                                                  * running */
185         char       *pghostaddr;         /* the IPv4 address of the machine on
186                                                                  * which the server is running, in
187                                                                  * IPv4 numbers-and-dots notation. Takes
188                                                                  * precedence over above. */
189         char       *pgport;                     /* the server's communication port */
190         char       *pgtty;                      /* tty on which the backend messages is
191                                                                  * displayed (NOT ACTUALLY USED???) */
192         char       *pgoptions;          /* options to start the backend with */
193         char       *dbName;                     /* database name */
194         char       *pguser;                     /* Postgres username and password, if any */
195         char       *pgpass;
196
197         /* Optional file to write trace info to */
198         FILE       *Pfdebug;
199
200         /* Callback procedure for notice/error message processing */
201         PQnoticeProcessor noticeHook;
202         void       *noticeArg;
203
204         /* Status indicators */
205         ConnStatusType status;
206         PGAsyncStatusType asyncStatus;
207         Dllist     *notifyList;         /* Notify msgs not yet handed to
208                                                                  * application */
209
210         /* Connection data */
211         int                     sock;                   /* Unix FD for socket, -1 if not connected */
212         SockAddr        laddr;                  /* Local address */
213         SockAddr        raddr;                  /* Remote address */
214         int                     raddr_len;              /* Length of remote address */
215
216         /* Miscellaneous stuff */
217         int                     be_pid;                 /* PID of backend --- needed for cancels */
218         int                     be_key;                 /* key of backend --- needed for cancels */
219         char            salt[2];                /* password salt received from backend */
220         PGlobjfuncs *lobjfuncs;         /* private state for large-object access
221                                                                  * fns */
222
223         /* Buffer for data received from backend and not yet processed */
224         char       *inBuffer;           /* currently allocated buffer */
225         int                     inBufSize;              /* allocated size of buffer */
226         int                     inStart;                /* offset to first unconsumed data in
227                                                                  * buffer */
228         int                     inCursor;               /* next byte to tentatively consume */
229         int                     inEnd;                  /* offset to first position after avail
230                                                                  * data */
231
232         int                     nonblocking;    /* whether this connection is using a blocking
233                                                                  * socket to the backend or not */
234
235         /* Buffer for data not yet sent to backend */
236         char       *outBuffer;          /* currently allocated buffer */
237         int                     outBufSize;             /* allocated size of buffer */
238         int                     outCount;               /* number of chars waiting in buffer */
239
240         /* Status for asynchronous result construction */
241         PGresult   *result;                     /* result being constructed */
242         PGresAttValue *curTuple;        /* tuple currently being read */
243
244         /* Handle for setenv request.  Used during connection only. */
245         PGsetenvHandle setenv_handle;
246
247 #ifdef USE_SSL
248         bool allow_ssl_try;                     /* Allowed to try SSL negotiation */
249         SSL *ssl;                                       /* SSL status, if have SSL connection */
250 #endif
251
252         /* Buffer for current error message */
253         PQExpBufferData errorMessage;   /* expansible string */
254
255         /* Buffer for receiving various parts of messages */
256         PQExpBufferData workBuffer;     /* expansible string */
257
258         int client_encoding;            /* encoding id */
259 };
260
261 /* String descriptions of the ExecStatusTypes.
262  * direct use of this array is deprecated; call PQresStatus() instead.
263  */
264 extern char *const pgresStatus[];
265
266 /* ----------------
267  * Internal functions of libpq
268  * Functions declared here need to be visible across files of libpq,
269  * but are not intended to be called by applications.  We use the
270  * convention "pqXXX" for internal functions, vs. the "PQxxx" names
271  * used for application-visible routines.
272  * ----------------
273  */
274
275 /* === in fe-connect.c === */
276
277 extern int      pqPacketSend(PGconn *conn, const char *buf, size_t len);
278
279 /* === in fe-exec.c === */
280
281 extern void pqSetResultError(PGresult *res, const char *msg);
282 extern void *pqResultAlloc(PGresult *res, size_t nBytes, bool isBinary);
283 extern char *pqResultStrdup(PGresult *res, const char *str);
284 extern void pqClearAsyncResult(PGconn *conn);
285
286 /* === in fe-misc.c === */
287
288  /*
289   * "Get" and "Put" routines return 0 if successful, EOF if not. Note that
290   * for Get, EOF merely means the buffer is exhausted, not that there is
291   * necessarily any error.
292   */
293 extern int      pqGetc(char *result, PGconn *conn);
294 extern int      pqGets(PQExpBuffer buf, PGconn *conn);
295 extern int      pqPuts(const char *s, PGconn *conn);
296 extern int      pqGetnchar(char *s, size_t len, PGconn *conn);
297 extern int      pqPutnchar(const char *s, size_t len, PGconn *conn);
298 extern int      pqGetInt(int *result, size_t bytes, PGconn *conn);
299 extern int      pqPutInt(int value, size_t bytes, PGconn *conn);
300 extern int      pqReadData(PGconn *conn);
301 extern int      pqFlush(PGconn *conn);
302 extern int      pqWait(int forRead, int forWrite, PGconn *conn);
303 extern int      pqReadReady(PGconn *conn);
304 extern int      pqWriteReady(PGconn *conn);
305
306 /* bits in a byte */
307 #define BYTELEN 8
308
309 /* fall back options if they are not specified by arguments or defined
310    by environment variables */
311 #define DefaultHost             "localhost"
312 #define DefaultTty              ""
313 #define DefaultOption   ""
314 #define DefaultAuthtype           ""
315 #define DefaultPassword           ""
316
317 /* supply an implementation of strerror() macro if system doesn't have it */
318 #ifndef strerror
319 #if defined(sun) && defined(__sparc__) && !defined(__SVR4)
320 extern char *sys_errlist[];
321
322 #define strerror(A) (sys_errlist[(A)])
323 #endif   /* sunos4 */
324 #endif   /* !strerror */
325
326 /* 
327  * this is so that we can check is a connection is non-blocking internally
328  * without the overhead of a function call
329  */
330 #define pqIsnonblocking(conn)   (conn->nonblocking)
331
332 #endif   /* LIBPQ_INT_H */