diff options
| author | Tom Lane | 1999-04-25 03:19:27 +0000 |
|---|---|---|
| committer | Tom Lane | 1999-04-25 03:19:27 +0000 |
| commit | 95cc41b81dd3917a1b9bb0b7c9cbe231d2557760 (patch) | |
| tree | 528782b9d55a9ceb7acbe4cc55c0699514ab284d /src/include | |
| parent | fc08814e00c04cddad512494bb52d9266928619e (diff) | |
Revise backend libpq interfaces so that messages to the frontend
can be generated in a buffer and then sent to the frontend in a single
libpq call. This solves problems with NOTICE and ERROR messages generated
in the middle of a data message or COPY OUT operation.
Diffstat (limited to 'src/include')
| -rw-r--r-- | src/include/lib/stringinfo.h | 86 | ||||
| -rw-r--r-- | src/include/libpq/libpq.h | 72 | ||||
| -rw-r--r-- | src/include/libpq/pqcomm.h | 22 | ||||
| -rw-r--r-- | src/include/libpq/pqformat.h | 31 |
4 files changed, 119 insertions, 92 deletions
diff --git a/src/include/lib/stringinfo.h b/src/include/lib/stringinfo.h index cf766521ecb..6b86fd6552e 100644 --- a/src/include/lib/stringinfo.h +++ b/src/include/lib/stringinfo.h @@ -1,52 +1,108 @@ /*------------------------------------------------------------------------- * * stringinfo.h - * Declarations/definitons for "string" functions. + * Declarations/definitions for "StringInfo" functions. * + * StringInfo provides an indefinitely-extensible string data type. + * It can be used to buffer either ordinary C strings (null-terminated text) + * or arbitrary binary data. All storage is allocated with palloc(). * * Copyright (c) 1994, Regents of the University of California * - * $Id: stringinfo.h,v 1.10 1999/02/13 23:21:32 momjian Exp $ + * $Id: stringinfo.h,v 1.11 1999/04/25 03:19:27 tgl Exp $ * *------------------------------------------------------------------------- */ #ifndef STRINGINFO_H #define STRINGINFO_H - /*------------------------- - * StringInfoData holds information about a string. - * 'data' is the string. - * 'len' is the current string length (as returned by 'strlen') - * 'maxlen' is the size in bytes of 'data', i.e. the maximum string - * size (including the terminating '\0' char) that we can + * StringInfoData holds information about an extensible string. + * data is the current buffer for the string (allocated with palloc). + * len is the current string length. There is guaranteed to be + * a terminating '\0' at data[len], although this is not very + * useful when the string holds binary data rather than text. + * maxlen is the allocated size in bytes of 'data', i.e. the maximum + * string size (including the terminating '\0' char) that we can * currently store in 'data' without having to reallocate - * more space. + * more space. We must always have maxlen > len. + *------------------------- */ typedef struct StringInfoData { char *data; - int maxlen; int len; + int maxlen; } StringInfoData; typedef StringInfoData *StringInfo; + +/*------------------------ + * There are two ways to create a StringInfo object initially: + * + * StringInfo stringptr = makeStringInfo(); + * Both the StringInfoData and the data buffer are palloc'd. + * + * StringInfoData string; + * initStringInfo(&string); + * The data buffer is palloc'd but the StringInfoData is just local. + * This is the easiest approach for a StringInfo object that will + * only live as long as the current routine. + * + * To destroy a StringInfo, pfree() the data buffer, and then pfree() the + * StringInfoData if it was palloc'd. There's no special support for this. + * + * NOTE: some routines build up a string using StringInfo, and then + * release the StringInfoData but return the data string itself to their + * caller. At that point the data string looks like a plain palloc'd + * string. + *------------------------- + */ + /*------------------------ * makeStringInfo - * create a 'StringInfoData' & return a pointer to it. + * Create an empty 'StringInfoData' & return a pointer to it. */ extern StringInfo makeStringInfo(void); /*------------------------ + * initStringInfo + * Initialize a StringInfoData struct (with previously undefined contents) + * to describe an empty string. + */ +extern void initStringInfo(StringInfo str); + +/*------------------------ * appendStringInfo - * similar to 'strcat' but reallocates more space if necessary... + * Format text data under the control of fmt (an sprintf-like format string) + * and append it to whatever is already in str. More space is allocated + * to str if necessary. This is sort of like a combination of sprintf and + * strcat. + * CAUTION: the current implementation has a 1K limit on the amount of text + * generated in a single call (not on the total string length). + */ +extern void appendStringInfo(StringInfo str, const char *fmt, ...); + +/*------------------------ + * appendStringInfoChar + * Append a single byte to str. + * Like appendStringInfo(str, "%c", ch) but much faster. + */ +extern void appendStringInfoChar(StringInfo str, char ch); + +/*------------------------ + * appendBinaryStringInfo + * Append arbitrary binary data to a StringInfo, allocating more space + * if necessary. */ -extern void appendStringInfo(StringInfo str, const char *fmt,...); +extern void appendBinaryStringInfo(StringInfo str, + const char *data, int datalen); /*------------------------ - * nullStringInfo - * return the string itself or "<>" if it is NULL + * stringStringInfo + * Return the string itself or "<>" if it is NULL. + * This is just a convenience macro used by many callers of appendStringInfo. */ #define stringStringInfo(s) (((s) == NULL) ? "<>" : (s)) diff --git a/src/include/libpq/libpq.h b/src/include/libpq/libpq.h index 99d408d9c15..873992739ee 100644 --- a/src/include/libpq/libpq.h +++ b/src/include/libpq/libpq.h @@ -6,7 +6,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: libpq.h,v 1.27 1999/02/13 23:21:35 momjian Exp $ + * $Id: libpq.h,v 1.28 1999/04/25 03:19:13 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -121,17 +121,6 @@ extern PortalEntry **portals; extern size_t portals_array_size; /* - * Asynchronous notification - */ -typedef struct PQNotifyList -{ - char relname[NAMEDATALEN]; /* listen/notify name */ - int be_pid; /* process id of backend */ - int valid; /* has this already been handled by user. */ -/* SLNode Node; */ -} PQNotifyList; - -/* * Exceptions. */ @@ -194,11 +183,6 @@ extern char *PQgetvalue(PortalBuffer *portal, int tuple_index, int field_number) extern char *PQgetAttr(PortalBuffer *portal, int tuple_index, int field_number); extern int PQgetlength(PortalBuffer *portal, int tuple_index, int field_number); extern void PQclear(char *pname); -extern void PQcleanNotify(void); -extern void PQnotifies_init(void); -extern PQNotifyList *PQnotifies(void); -extern void PQremoveNotify(PQNotifyList *nPtr); -extern void PQappendNotify(char *relname, int pid); /* * prototypes for functions in portalbuf.c @@ -250,51 +234,19 @@ extern int32 pqtest(struct varlena * vlena); /* * prototypes for functions in pqcomm.c */ -extern void pq_init(int fd); -extern void pq_gettty(char *tp); -extern int pq_getport(void); -extern void pq_close(void); -extern int pq_flush(void); -extern int pq_recvbuf(void); -extern int pq_getstr(char *s, int maxlen); -extern int PQgetline(char *s, int maxlen); -extern int PQputline(char *s); -extern int pq_getchar(void); -extern int pq_peekchar(void); -extern int pq_getnchar(char *s, int off, int maxlen); -extern int pq_getint(int b); -extern int pq_putchar(unsigned char c); -extern void pq_putstr(char *s); -extern void pq_putnchar(char *s, int n); -extern void pq_putint(int i, int b); -extern int pq_getinaddr(struct sockaddr_in * sin, char *host, int port); -extern int pq_getinserv(struct sockaddr_in * sin, char *host, char *serv); - -#ifdef MULTIBYTE -extern void pq_putncharlen(char *s, int n); - -#endif - -extern int pq_connect(char *dbname, char *user, char *args, char *hostName, - char *debugTty, char *execFile, short portName); -extern int StreamOpen(char *hostName, short portName, Port *port); -extern void StreamDoUnlink(void); extern int StreamServerPort(char *hostName, short portName, int *fdP); extern int StreamConnection(int server_fd, Port *port); extern void StreamClose(int sock); - -/* - * Internal send/receive buffers in libpq. - * These probably shouldn't be global at all, but unless we merge - * pqcomm.c and pqcomprim.c they have to be... - */ - -#define PQ_BUFFER_SIZE 8192 - -extern unsigned char PqSendBuffer[PQ_BUFFER_SIZE]; -extern int PqSendPointer; /* Next index to store a byte in PqSendBuffer */ -extern unsigned char PqRecvBuffer[PQ_BUFFER_SIZE]; -extern int PqRecvPointer; /* Next index to read a byte from PqRecvBuffer */ -extern int PqRecvLength; /* End of data available in PqRecvBuffer */ +extern void pq_init(void); +extern int pq_getport(void); +extern void pq_close(void); +extern int pq_getbytes(char *s, size_t len); +extern int pq_getstring(char *s, size_t len); +extern int pq_peekbyte(void); +extern int pq_putbytes(const char *s, size_t len); +extern int pq_flush(void); +extern int pq_putmessage(char msgtype, const char *s, size_t len); +extern void pq_startcopyout(void); +extern void pq_endcopyout(bool errorAbort); #endif /* LIBPQ_H */ diff --git a/src/include/libpq/pqcomm.h b/src/include/libpq/pqcomm.h index 2245b83d625..53542993d87 100644 --- a/src/include/libpq/pqcomm.h +++ b/src/include/libpq/pqcomm.h @@ -3,16 +3,20 @@ * pqcomm.h * Definitions common to frontends and backends. * + * NOTE: for historical reasons, this does not correspond to pqcomm.c. + * pqcomm.c's routines are declared in libpq.h. * * Copyright (c) 1994, Regents of the University of California * - * $Id: pqcomm.h,v 1.33 1999/02/13 23:21:36 momjian Exp $ + * $Id: pqcomm.h,v 1.34 1999/04/25 03:19:13 tgl Exp $ * *------------------------------------------------------------------------- */ #ifndef PQCOMM_H #define PQCOMM_H +#include "postgres.h" + #include <stdio.h> #include <sys/types.h> #ifdef WIN32 @@ -23,8 +27,6 @@ #include <netinet/in.h> #endif -#include "postgres.h" - /* Define a generic socket address type. */ typedef union SockAddr @@ -151,18 +153,4 @@ typedef struct CancelRequestPacket uint32 cancelAuthCode; /* secret key to authorize cancel */ } CancelRequestPacket; - -/* in pqcomprim.c */ -int pqGetShort(int *); -int pqGetLong(int *); -int pqGetNBytes(char *, size_t); -int pqGetString(char *, size_t); -int pqGetByte(void); - -int pqPutShort(int); -int pqPutLong(int); -int pqPutNBytes(const char *, size_t); -int pqPutString(const char *); -int pqPutByte(int); - #endif /* PQCOMM_H */ diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h new file mode 100644 index 00000000000..2fe48539a2d --- /dev/null +++ b/src/include/libpq/pqformat.h @@ -0,0 +1,31 @@ +/*------------------------------------------------------------------------- + * + * pqformat.h + * Definitions for formatting and parsing frontend/backend messages + * + * Copyright (c) 1994, Regents of the University of California + * + * $Id: pqformat.h,v 1.1 1999/04/25 03:19:14 tgl Exp $ + * + *------------------------------------------------------------------------- + */ +#ifndef PQFORMAT_H +#define PQFORMAT_H + +#include "postgres.h" +#include "lib/stringinfo.h" + +#define pq_beginmessage(buf) initStringInfo(buf) + +extern void pq_sendbyte(StringInfo buf, int byt); +extern void pq_sendbytes(StringInfo buf, const char *data, int datalen); +extern void pq_sendtext(StringInfo buf, const char *str, int slen); +extern void pq_sendstring(StringInfo buf, const char *str, int slen); +extern void pq_sendint(StringInfo buf, int i, int b); +extern void pq_endmessage(StringInfo buf); + +extern int pq_getint(int *result, int b); +extern int pq_getstr(char *s, int maxlen); +extern int pq_getnchar(char *s, int len); + +#endif /* PQFORMAT_H */ |
