|
14 | 14 | #define PQFORMAT_H
|
15 | 15 |
|
16 | 16 | #include "lib/stringinfo.h"
|
| 17 | +#include "mb/pg_wchar.h" |
| 18 | +#include "port/pg_bswap.h" |
17 | 19 |
|
18 | 20 | extern void pq_beginmessage(StringInfo buf, char msgtype);
|
19 |
| -extern void pq_sendbyte(StringInfo buf, int byt); |
| 21 | +extern void pq_beginmessage_reuse(StringInfo buf, char msgtype); |
| 22 | +extern void pq_endmessage(StringInfo buf); |
| 23 | +extern void pq_endmessage_reuse(StringInfo buf); |
| 24 | + |
20 | 25 | extern void pq_sendbytes(StringInfo buf, const char *data, int datalen);
|
21 | 26 | extern void pq_sendcountedtext(StringInfo buf, const char *str, int slen,
|
22 | 27 | bool countincludesself);
|
23 | 28 | extern void pq_sendtext(StringInfo buf, const char *str, int slen);
|
24 | 29 | extern void pq_sendstring(StringInfo buf, const char *str);
|
25 | 30 | extern void pq_send_ascii_string(StringInfo buf, const char *str);
|
26 |
| -extern void pq_sendint(StringInfo buf, int i, int b); |
27 |
| -extern void pq_sendint64(StringInfo buf, int64 i); |
28 | 31 | extern void pq_sendfloat4(StringInfo buf, float4 f);
|
29 | 32 | extern void pq_sendfloat8(StringInfo buf, float8 f);
|
30 |
| -extern void pq_endmessage(StringInfo buf); |
| 33 | + |
| 34 | +extern void pq_sendfloat4(StringInfo buf, float4 f); |
| 35 | +extern void pq_sendfloat8(StringInfo buf, float8 f); |
| 36 | + |
| 37 | +/* |
| 38 | + * Append a int8 to a StringInfo buffer, which already has enough space |
| 39 | + * preallocated. |
| 40 | + * |
| 41 | + * The use of restrict allows the compiler to optimize the code based on the |
| 42 | + * assumption that buf, buf->len, buf->data and *buf->data don't |
| 43 | + * overlap. Without the annotation buf->len etc cannot be kept in a register |
| 44 | + * over subsequent pq_writeint* calls. |
| 45 | + */ |
| 46 | +static inline void |
| 47 | +pq_writeint8(StringInfo restrict buf, int8 i) |
| 48 | +{ |
| 49 | + int8 ni = i; |
| 50 | + |
| 51 | + Assert(buf->len + sizeof(i) <= buf->maxlen); |
| 52 | + memcpy((char *restrict) (buf->data + buf->len), &ni, sizeof(ni)); |
| 53 | + buf->len += sizeof(i); |
| 54 | +} |
| 55 | + |
| 56 | +/* |
| 57 | + * Append a int16 to a StringInfo buffer, which already has enough space |
| 58 | + * preallocated. |
| 59 | + */ |
| 60 | +static inline void |
| 61 | +pq_writeint16(StringInfo restrict buf, int16 i) |
| 62 | +{ |
| 63 | + int16 ni = pg_hton16(i); |
| 64 | + |
| 65 | + Assert(buf->len + sizeof(ni) <= buf->maxlen); |
| 66 | + memcpy((char *restrict) (buf->data + buf->len), &ni, sizeof(i)); |
| 67 | + buf->len += sizeof(i); |
| 68 | +} |
| 69 | + |
| 70 | +/* |
| 71 | + * Append a int32 to a StringInfo buffer, which already has enough space |
| 72 | + * preallocated. |
| 73 | + */ |
| 74 | +static inline void |
| 75 | +pq_writeint32(StringInfo restrict buf, int32 i) |
| 76 | +{ |
| 77 | + int32 ni = pg_hton32(i); |
| 78 | + |
| 79 | + Assert(buf->len + sizeof(i) <= buf->maxlen); |
| 80 | + memcpy((char *restrict) (buf->data + buf->len), &ni, sizeof(i)); |
| 81 | + buf->len += sizeof(i); |
| 82 | +} |
| 83 | + |
| 84 | +/* |
| 85 | + * Append a int64 to a StringInfo buffer, which already has enough space |
| 86 | + * preallocated. |
| 87 | + */ |
| 88 | +static inline void |
| 89 | +pq_writeint64(StringInfo restrict buf, int64 i) |
| 90 | +{ |
| 91 | + int64 ni = pg_hton64(i); |
| 92 | + |
| 93 | + Assert(buf->len + sizeof(i) <= buf->maxlen); |
| 94 | + memcpy((char *restrict) (buf->data + buf->len), &ni, sizeof(i)); |
| 95 | + buf->len += sizeof(i); |
| 96 | +} |
| 97 | + |
| 98 | +/* |
| 99 | + * Append a null-terminated text string (with conversion) to a buffer with |
| 100 | + * preallocated space. |
| 101 | + * |
| 102 | + * NB: The pre-allocated space needs to be sufficient for the string after |
| 103 | + * converting to client encoding. |
| 104 | + * |
| 105 | + * NB: passed text string must be null-terminated, and so is the data |
| 106 | + * sent to the frontend. |
| 107 | + */ |
| 108 | +static inline void |
| 109 | +pq_writestring(StringInfo restrict buf, const char *restrict str) |
| 110 | +{ |
| 111 | + int slen = strlen(str); |
| 112 | + char *p; |
| 113 | + |
| 114 | + p = pg_server_to_client(str, slen); |
| 115 | + if (p != str) /* actual conversion has been done? */ |
| 116 | + slen = strlen(p); |
| 117 | + |
| 118 | + Assert(buf->len + slen + 1 <= buf->maxlen); |
| 119 | + |
| 120 | + memcpy(((char *restrict) buf->data + buf->len), p, slen + 1); |
| 121 | + buf->len += slen + 1; |
| 122 | + |
| 123 | + if (p != str) |
| 124 | + pfree(p); |
| 125 | +} |
| 126 | + |
| 127 | +/* append a binary int8 to a StringInfo buffer */ |
| 128 | +static inline void |
| 129 | +pq_sendint8(StringInfo buf, int8 i) |
| 130 | +{ |
| 131 | + enlargeStringInfo(buf, sizeof(i)); |
| 132 | + pq_writeint8(buf, i); |
| 133 | +} |
| 134 | + |
| 135 | +/* append a binary int16 to a StringInfo buffer */ |
| 136 | +static inline void |
| 137 | +pq_sendint16(StringInfo buf, int16 i) |
| 138 | +{ |
| 139 | + enlargeStringInfo(buf, sizeof(i)); |
| 140 | + pq_writeint16(buf, i); |
| 141 | +} |
| 142 | + |
| 143 | +/* append a binary int32 to a StringInfo buffer */ |
| 144 | +static inline void |
| 145 | +pq_sendint32(StringInfo buf, int32 i) |
| 146 | +{ |
| 147 | + enlargeStringInfo(buf, sizeof(i)); |
| 148 | + pq_writeint32(buf, i); |
| 149 | +} |
| 150 | + |
| 151 | +/* append a binary int64 to a StringInfo buffer */ |
| 152 | +static inline void |
| 153 | +pq_sendint64(StringInfo buf, int64 i) |
| 154 | +{ |
| 155 | + enlargeStringInfo(buf, sizeof(i)); |
| 156 | + pq_writeint64(buf, i); |
| 157 | +} |
| 158 | + |
| 159 | +/* append a binary byte to a StringInfo buffer */ |
| 160 | +static inline void |
| 161 | +pq_sendbyte(StringInfo buf, int8 byt) |
| 162 | +{ |
| 163 | + pq_sendint8(buf, byt); |
| 164 | +} |
| 165 | + |
| 166 | +/* |
| 167 | + * Append a binary integer to a StringInfo buffer |
| 168 | + * |
| 169 | + * This function is deprecated. |
| 170 | + */ |
| 171 | +static inline void |
| 172 | +pq_sendint(StringInfo buf, int i, int b) |
| 173 | +{ |
| 174 | + switch (b) |
| 175 | + { |
| 176 | + case 1: |
| 177 | + pq_sendint8(buf, (int8) i); |
| 178 | + break; |
| 179 | + case 2: |
| 180 | + pq_sendint16(buf, (int16) i); |
| 181 | + break; |
| 182 | + case 4: |
| 183 | + pq_sendint32(buf, (int32) i); |
| 184 | + break; |
| 185 | + default: |
| 186 | + elog(ERROR, "unsupported integer size %d", b); |
| 187 | + break; |
| 188 | + } |
| 189 | +} |
| 190 | + |
31 | 191 |
|
32 | 192 | extern void pq_begintypsend(StringInfo buf);
|
33 | 193 | extern bytea *pq_endtypsend(StringInfo buf);
|
|
0 commit comments