forked from orafce/orafce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
putline.c
366 lines (299 loc) · 6.97 KB
/
putline.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include "postgres.h"
#include "funcapi.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "catalog/pg_type.h"
#include "lib/stringinfo.h"
#undef USE_SSL
#undef ENABLE_GSS
#include "libpq/libpq.h"
#include "libpq/pqformat.h"
#include "utils/memutils.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "orafce.h"
#include "builtins.h"
#if defined(WIN32) && !defined(_MSC_VER)
extern PGDLLIMPORT ProtocolVersion FrontendProtocol; /* for mingw */
#endif
/*
* TODO: BUFSIZE_UNLIMITED to be truely unlimited (or INT_MAX),
* and allocate buffers on-demand.
*/
#define BUFSIZE_DEFAULT 20000
#define BUFSIZE_MIN 2000
#define BUFSIZE_MAX 1000000
#define BUFSIZE_UNLIMITED BUFSIZE_MAX
static bool is_server_output = false;
static char *buffer = NULL;
static int buffer_size = 0; /* allocated bytes in buffer */
static int buffer_len = 0; /* used bytes in buffer */
static int buffer_get = 0; /* retrieved bytes in buffer */
static void add_str(const char *str, int len);
static void add_text(text *str);
static void add_newline(void);
static void send_buffer(void);
/*
* Aux. buffer functionality
*/
static void
add_str(const char *str, int len)
{
/* Discard all buffers if get_line was called. */
if (buffer_get > 0)
{
buffer_get = 0;
buffer_len = 0;
}
if (buffer_len + len > buffer_size)
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_RESOURCES),
errmsg("buffer overflow"),
errdetail("Buffer overflow, limit of %d bytes", buffer_size),
errhint("Increase buffer size in dbms_output.enable() next time")));
memcpy(buffer + buffer_len, str, len);
buffer_len += len;
buffer[buffer_len] = '\0';
}
static void
add_text(text *str)
{
add_str(VARDATA_ANY(str), VARSIZE_ANY_EXHDR(str));
}
static void
add_newline(void)
{
add_str("", 1); /* add \0 */
if (is_server_output)
send_buffer();
}
static void
send_buffer()
{
if (buffer_len > 0)
{
StringInfoData msgbuf;
char *cursor = buffer;
while (--buffer_len > 0)
{
if (*cursor == '\0')
*cursor = '\n';
cursor++;
}
if (*cursor != '\0')
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("internal error"),
errdetail("Wrong message format detected")));
pq_beginmessage(&msgbuf, 'N');
/*
* FrontendProtocol is not avalilable in MSVC because it is not
* PGDLLEXPORT'ed. So, we assume always the protocol >= 3.
*/
#ifndef _MSC_VER
if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3)
{
#endif
pq_sendbyte(&msgbuf, PG_DIAG_MESSAGE_PRIMARY);
pq_sendstring(&msgbuf, buffer);
pq_sendbyte(&msgbuf, '\0');
#ifndef _MSC_VER
}
else
{
*cursor++ = '\n';
*cursor = '\0';
pq_sendstring(&msgbuf, buffer);
}
#endif
pq_endmessage(&msgbuf);
pq_flush();
}
}
/*
* Aux db functions
*
*/
static void
dbms_output_enable_internal(int32 n_buf_size)
{
/* We allocate +2 bytes for an end-of-line and a string terminator. */
if (buffer == NULL)
{
buffer = MemoryContextAlloc(TopMemoryContext, n_buf_size + 2);
buffer_size = n_buf_size;
buffer_len = 0;
buffer_get = 0;
}
else if (n_buf_size > buffer_len)
{
/* We cannot shrink buffer less than current length. */
buffer = repalloc(buffer, n_buf_size + 2);
buffer_size = n_buf_size;
}
}
PG_FUNCTION_INFO_V1(dbms_output_enable_default);
Datum
dbms_output_enable_default(PG_FUNCTION_ARGS)
{
dbms_output_enable_internal(BUFSIZE_DEFAULT);
PG_RETURN_VOID();
}
PG_FUNCTION_INFO_V1(dbms_output_enable);
Datum
dbms_output_enable(PG_FUNCTION_ARGS)
{
int32 n_buf_size;
if (PG_ARGISNULL(0))
n_buf_size = BUFSIZE_UNLIMITED;
else
{
n_buf_size = PG_GETARG_INT32(0);
if (n_buf_size > BUFSIZE_MAX)
{
n_buf_size = BUFSIZE_MAX;
elog(WARNING, "Limit decreased to %d bytes.", BUFSIZE_MAX);
}
else if (n_buf_size < BUFSIZE_MIN)
{
n_buf_size = BUFSIZE_MIN;
elog(WARNING, "Limit increased to %d bytes.", BUFSIZE_MIN);
}
}
dbms_output_enable_internal(n_buf_size);
PG_RETURN_VOID();
}
PG_FUNCTION_INFO_V1(dbms_output_disable);
Datum
dbms_output_disable(PG_FUNCTION_ARGS)
{
if (buffer)
pfree(buffer);
buffer = NULL;
buffer_size = 0;
buffer_len = 0;
buffer_get = 0;
PG_RETURN_VOID();
}
PG_FUNCTION_INFO_V1(dbms_output_serveroutput);
Datum
dbms_output_serveroutput(PG_FUNCTION_ARGS)
{
is_server_output = PG_GETARG_BOOL(0);
if (is_server_output && !buffer)
dbms_output_enable_internal(BUFSIZE_DEFAULT);
PG_RETURN_VOID();
}
/*
* main functions
*/
PG_FUNCTION_INFO_V1(dbms_output_put);
Datum
dbms_output_put(PG_FUNCTION_ARGS)
{
if (buffer)
add_text(PG_GETARG_TEXT_PP(0));
PG_RETURN_VOID();
}
PG_FUNCTION_INFO_V1(dbms_output_put_line);
Datum
dbms_output_put_line(PG_FUNCTION_ARGS)
{
if (buffer)
{
add_text(PG_GETARG_TEXT_PP(0));
add_newline();
}
PG_RETURN_VOID();
}
PG_FUNCTION_INFO_V1(dbms_output_new_line);
Datum
dbms_output_new_line(PG_FUNCTION_ARGS)
{
if (buffer)
add_newline();
PG_RETURN_VOID();
}
static text *
dbms_output_next(void)
{
if (buffer_get < buffer_len)
{
text *line = cstring_to_text(buffer + buffer_get);
buffer_get += VARSIZE_ANY_EXHDR(line) + 1;
return line;
}
else
return NULL;
}
PG_FUNCTION_INFO_V1(dbms_output_get_line);
Datum
dbms_output_get_line(PG_FUNCTION_ARGS)
{
TupleDesc tupdesc;
Datum result;
HeapTuple tuple;
Datum values[2];
bool nulls[2] = { false, false };
text *line;
/* Build a tuple descriptor for our result type */
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
elog(ERROR, "return type must be a row type");
if ((line = dbms_output_next()) != NULL)
{
values[0] = PointerGetDatum(line);
values[1] = Int32GetDatum(0); /* 0: succeeded */
}
else
{
nulls[0] = true;
values[1] = Int32GetDatum(1); /* 1: failed */
}
tuple = heap_form_tuple(tupdesc, values, nulls);
result = HeapTupleGetDatum(tuple);
PG_RETURN_DATUM(result);
}
PG_FUNCTION_INFO_V1(dbms_output_get_lines);
Datum
dbms_output_get_lines(PG_FUNCTION_ARGS)
{
TupleDesc tupdesc;
Datum result;
HeapTuple tuple;
Datum values[2];
bool nulls[2] = { false, false };
text *line;
int32 max_lines = PG_GETARG_INT32(0);
int32 n;
ArrayBuildState *astate = NULL;
/* Build a tuple descriptor for our result type */
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
elog(ERROR, "return type must be a row type");
for (n = 0; n < max_lines && (line = dbms_output_next()) != NULL; n++)
{
astate = accumArrayResult(astate, PointerGetDatum(line), false,
TEXTOID, CurrentMemoryContext);
}
/* 0: lines as text array */
if (n > 0)
values[0] = makeArrayResult(astate, CurrentMemoryContext);
else
{
int16 typlen;
bool typbyval;
char typalign;
ArrayType *arr;
get_typlenbyvalalign(TEXTOID, &typlen, &typbyval, &typalign);
arr = construct_md_array(
NULL,
NULL,
0, NULL, NULL, TEXTOID, typlen, typbyval, typalign);
values[0] = PointerGetDatum(arr);
}
/* 1: # of lines as integer */
values[1] = Int32GetDatum(n);
tuple = heap_form_tuple(tupdesc, values, nulls);
result = HeapTupleGetDatum(tuple);
PG_RETURN_DATUM(result);
}