Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2026-06-19 0cfc87bd32822e5ba22d8c2b1a26fa60f63ad7fa
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
extern "C" {
#include "postgres.h"
#include "executor/executor.h"
#include "utils/builtins.h"
#include "utils/elog.h"
#include "funcapi.h"
}
 
#include <vector>
#include <string>
#include <cstring>
 
#include "dbms/owa_text.h"
#include "dbms/htp.h"
 
namespace owa_text {
 
struct multi_line
{
    std::vector<Datum> rows;
    bool partial_row;
 
    multi_line(): rows(), partial_row(false) {}
    multi_line(HeapTupleHeader pg_mline);
 
    Datum make_pg_record();
};
 
multi_line::multi_line(HeapTupleHeader pg_mline)
{
    bool isnull_temp;
    Datum rows_datum = GetAttributeByName(pg_mline, "rows", &isnull_temp);
    Datum partial_row_datum = GetAttributeByName(pg_mline, "partial_row", &isnull_temp);
    ArrayType *rows_arr = DatumGetArrayTypeP(rows_datum);
    partial_row = DatumGetBool(partial_row_datum);
 
    Datum *rows_elems;
    bool *rows_nulls;
    int rows_len;
    deconstruct_array(rows_arr, VARCHAR2OID, -1, false, TYPALIGN_INT,
                        &rows_elems, &rows_nulls, &rows_len);
 
    rows.assign(rows_elems, rows_elems + rows_len);
 
    pfree(rows_elems);
    pfree(rows_nulls);
}
 
Datum multi_line::make_pg_record()
{
    TupleDesc desc = CreateTemplateTupleDesc(3);
    TupleDescInitEntry(desc, 0, "rows", VARCHAR2ARRAYOID, -1, 1);
    TupleDescInitEntry(desc, 1, "num_rows", NUMBEROID, -1, 0);
    TupleDescInitEntry(desc, 2, "partial_row", BOOLOID, -1, 0);
 
    desc = BlessTupleDesc(desc);
 
    Datum values[3];
    bool isnull[3];
 
    ArrayType *new_arr = construct_array(rows.data(), rows.size(), VARCHAR2OID, -1, false, TYPALIGN_INT);
 
    values[0] = PointerGetDatum(new_arr);
    isnull[0] = false;
 
    values[1] = DirectFunctionCall1(int4_numeric, Int32GetDatum(rows.size()));
    isnull[1] = false;
 
    values[2] = BoolGetDatum(partial_row);
    isnull[2] = false;
 
    HeapTuple tuple = heap_form_tuple(desc, values, isnull);
 
    return HeapTupleGetDatum(tuple);
}
} // namespace owa_text
 
static Datum owa_text_add2multi_impl(const char *stream, owa_text::multi_line &mline, bool cont)
{
    bool prev_partial_row = false;
 
    const char *row_start = stream;
    const char *row_end = strchr(row_start, '\n');
    size_t row_len = row_end != nullptr ? row_end - row_start : strlen(stream);
 
    prev_partial_row = mline.partial_row;
    mline.partial_row = (row_end == nullptr);
 
    if (prev_partial_row && cont)
    {
        text *last_row_text = DatumGetTextP(mline.rows.back());
        char *last_row_cstr = text_to_cstring(last_row_text);
        std::string last_row = last_row_cstr;
        pfree(last_row_cstr);
 
        if (last_row.size() + row_len > 32767) {
            ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                            errmsg("Cannot create row larger than 32767 bytes")));
            return static_cast<Datum>(0);
        }
 
        last_row.append(row_start, row_len);
        mline.rows.back() = PointerGetDatum(cstring_to_text(last_row.c_str()));
 
        if (row_end == nullptr)
        {
            row_start = nullptr;
            row_len = 0;
        }
        else
        {
            row_start = row_end + 1;
            row_end = strchr(row_start, '\n');
            row_len = row_end != nullptr ? row_end - row_start : strlen(row_start);
        }
 
        if (row_start != nullptr && *row_start != '\0' && row_end == nullptr) {
            mline.partial_row = true;
        }
    }
 
    while (row_start != nullptr && *row_start != '\0') {
        mline.rows.push_back(PointerGetDatum(cstring_to_text_with_len(row_start, row_len)));
 
        if (row_end == nullptr)
        {
            row_start = nullptr;
            row_len = 0;
        }
        else
        {
            row_start = row_end + 1;
            row_start = strchr(row_start, '\n');
            row_len = row_end != nullptr ? row_end - row_start : strlen(row_start);
        }
 
        if (row_start != nullptr && *row_start != '\0' && row_end == nullptr) {
            mline.partial_row = true;
        }
    }
 
    return mline.make_pg_record();
}
 
extern "C" {
 
PG_FUNCTION_INFO_V1(owa_text_add2multi_ovr1);
PG_FUNCTION_INFO_V1(owa_text_add2multi_ovr2);
PG_FUNCTION_INFO_V1(owa_text_new_multi_ovr1);
PG_FUNCTION_INFO_V1(owa_text_new_multi_ovr2);
PG_FUNCTION_INFO_V1(owa_text_new_row_list_ovr1);
PG_FUNCTION_INFO_V1(owa_text_new_row_list_ovr2);
PG_FUNCTION_INFO_V1(owa_text_print_multi);
PG_FUNCTION_INFO_V1(owa_text_print_row_list);
PG_FUNCTION_INFO_V1(owa_text_stream2multi);
PG_FUNCTION_INFO_V1(owa_text_stream2multi2);
 
/**
 * @brief This procedure adds content to an existing Multi_Line Data Type
 * @details This procedure adds content to an existing Multi_Line Data Type
 *  * Parameters:
 *  * @param STREAM (in) VARCHAR2: The text to add.
 *  * @param MLINE (in/out) PL/SQL RECORD: The OWA_TEXT.MULTI_LINE DATA TYPE. The output of this
 * parameter contains stream.
 *  *  * @param MLINE (in/out) PL/SQL RECORD: The OWA_TEXT.MULTI_LINE DATA TYPE. The output of this
 * parameter contains stream. (Возвращается через результат)
 *  *  *  * @return record: Композитный тип (pl/sql record), содержащий OUT/INOUT параметры.
 *  *
 * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_TEXT.html
 */
Datum owa_text_add2multi_ovr1(PG_FUNCTION_ARGS)
{
    TupleDesc desc;
    text *stream_txt = PG_GETARG_TEXT_P(0);
    char *stream = text_to_cstring(stream_txt);
    HeapTupleHeader t = PG_GETARG_HEAPTUPLEHEADER(1);
 
    if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE)
    {
        ereport(ERROR,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                 errmsg("Invalid call context: cannot return record!")));
        pfree(stream);
        PG_RETURN_VOID();
    }
 
    desc = BlessTupleDesc(desc);
 
    owa_text::multi_line mline{t};
 
    Datum values[1] = {owa_text_add2multi_impl(stream, mline, true)};
    bool isnull[1] = {false};
 
    pfree(stream);
 
    HeapTuple tuple = heap_form_tuple(desc, values, isnull);
 
    PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
}
 
/**
 * @brief This procedure adds content to an existing Multi_Line Data Type
 * @details This procedure adds content to an existing Multi_Line Data Type
 *  * Parameters:
 *  * @param STREAM (in) VARCHAR2: The text to add.
 *  * @param MLINE (in/out) PL/SQL RECORD: The OWA_TEXT.MULTI_LINE DATA TYPE. The output of this
 * parameter contains stream.
 *  * @param CONTINUE (in) BOOLEAN: If TRUE, the procedure appends streamwithin the previous final
 * row (assuming it is less than 32K). If FALSE, the procedure places streamin a new row.
 *  *  * @param MLINE (in/out) PL/SQL RECORD: The OWA_TEXT.MULTI_LINE DATA TYPE. The output of this
 * parameter contains stream. (Возвращается через результат)
 *  *  *  * @return record: Композитный тип (pl/sql record), содержащий OUT/INOUT параметры.
 *  *
 * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_TEXT.html
 */
Datum owa_text_add2multi_ovr2(PG_FUNCTION_ARGS)
{
    TupleDesc desc;
    text *stream_txt = PG_GETARG_TEXT_P(0);
    char *stream = text_to_cstring(stream_txt);
    HeapTupleHeader t = PG_GETARG_HEAPTUPLEHEADER(1);
    bool cont = PG_GETARG_BOOL(2);
 
    if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE)
    {
        ereport(ERROR,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                 errmsg("Invalid call context: cannot return record!")));
        pfree(stream);
        PG_RETURN_VOID();
    }
 
    desc = BlessTupleDesc(desc);
 
    owa_text::multi_line mline{t};
 
    Datum values[1] = {owa_text_add2multi_impl(stream, mline, cont)};
    bool isnull[1] = {false};
 
    pfree(stream);
 
    HeapTuple tuple = heap_form_tuple(desc, values, isnull);
 
    PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
}
 
/**
 * @brief TODO: Добавить описание
 * @details TODO: Добавить детальное описание
 *  *  *
 * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_TEXT.html
 */
Datum owa_text_new_multi_ovr1(PG_FUNCTION_ARGS)
{
    owa_text::multi_line mline{};
 
    PG_RETURN_DATUM(mline.make_pg_record());
}
 
/**
 * @brief TODO: Добавить описание
 * @details TODO: Добавить детальное описание
 *  * Parameters:
 *  *  * @param MLINE (out) PL/SQL RECORD: TODO: Описание параметра MLINE (Возвращается через
 * результат)
 *  *  *  * @return record: Композитный тип (pl/sql record), содержащий OUT/INOUT параметры.
 *  *
 * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_TEXT.html
 */
Datum owa_text_new_multi_ovr2(PG_FUNCTION_ARGS)
{
    owa_text::multi_line mline{};
    TupleDesc result_desc;
 
    if (get_call_result_type(fcinfo, nullptr, &result_desc) != TYPEFUNC_COMPOSITE)
    {
        ereport(ERROR,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                 errmsg("Invalid call context: cannot return record!")));
        PG_RETURN_VOID();
    }
 
    result_desc = BlessTupleDesc(result_desc);
 
    Datum result_values[1] = {mline.make_pg_record()};
    bool result_nulls[1] = {false};
 
    HeapTuple result_tuple = heap_form_tuple(result_desc, result_values, result_nulls);
 
    PG_RETURN_DATUM(HeapTupleGetDatum(result_tuple));
}
 
/**
 * @brief This function or procedure creates a new OWA_TEX.ROW_LIST DATA TYPE.
 * @details This function or procedure creates a new OWA_TEX.ROW_LIST DATA TYPE.
 *  *  *
 * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_TEXT.html
 */
Datum owa_text_new_row_list_ovr1(PG_FUNCTION_ARGS)
{
    TupleDesc desc = CreateTemplateTupleDesc(3);
    TupleDescInitEntry(desc, 0, "rows", NUMBERARRAYOID, -1, 1);
    TupleDescInitEntry(desc, 1, "num_rows", NUMBEROID, -1, 0);
 
    TupleDesc blessed_desc = BlessTupleDesc(desc);
 
    Datum values[2];
    bool isnull[2];
 
    ArrayType *arr = construct_array(nullptr, 0, NUMBERARRAYOID, -1, false, TYPALIGN_INT);
 
    values[0] = PointerGetDatum(arr);
    isnull[0] = false;
 
    values[1] = DirectFunctionCall1(int4_numeric, Int32GetDatum(0));
    isnull[1] = false;
 
    HeapTuple tuple = heap_form_tuple(blessed_desc, values, isnull);
 
    PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
}
 
/**
 * @brief This function or procedure creates a new OWA_TEX.ROW_LIST DATA TYPE.
 * @details This function or procedure creates a new OWA_TEX.ROW_LIST DATA TYPE.
 *  * Parameters:
 *  *  * @param RLIST (out) PL/SQL RECORD: This is an output parameter containing the new row_list
 * data type (Возвращается через результат)
 *  *  *  * @return record: Композитный тип (pl/sql record), содержащий OUT/INOUT параметры.
 *  *
 * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_TEXT.html
 */
Datum owa_text_new_row_list_ovr2(PG_FUNCTION_ARGS)
{
    TupleDesc record_desc = CreateTemplateTupleDesc(3);
    TupleDescInitEntry(record_desc, 0, "rows", NUMBERARRAYOID, -1, 1);
    TupleDescInitEntry(record_desc, 1, "num_rows", NUMBEROID, -1, 0);
 
    Datum values[2];
    bool isnull[2];
 
    ArrayType *arr = construct_array(nullptr, 0, NUMBEROID, -1, false, TYPALIGN_INT);
 
    values[0] = PointerGetDatum(arr);
    isnull[0] = false;
 
    values[1] = DirectFunctionCall1(int4_numeric, Int32GetDatum(0));
    isnull[1] = false;
 
    HeapTuple tuple = heap_form_tuple(record_desc, values, isnull);
 
    TupleDesc result_desc;
 
    if (get_call_result_type(fcinfo, nullptr, &result_desc) != TYPEFUNC_COMPOSITE)
    {
        ereport(ERROR,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                 errmsg("Invalid call context: cannot return record!")));
        PG_RETURN_VOID();
    }
 
    result_desc = BlessTupleDesc(result_desc);
 
    Datum result_values[1] = {HeapTupleGetDatum(tuple)};
    bool result_nulls[1] = {false};
 
    HeapTuple result_tuple = heap_form_tuple(result_desc, result_values, result_nulls);
 
    PG_RETURN_DATUM(HeapTupleGetDatum(result_tuple));
}
 
/**
 * @brief This procedure uses the PRINT Procedures or the PRN Procedures to print the "rows" field
 * of the OWA_TEXT.MULTI_LINE DATA TYPE.
 * @details This procedure uses the PRINT Procedures or the PRN Procedures to print the "rows" field
 * of the OWA_TEXT.MULTI_LINE DATA TYPE.
 *  * Parameters:
 *  * @param MLINE (in) PL/SQL RECORD: The multi_line datatype to print.
 *  *  *  *  *
 * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_TEXT.html
 */
Datum owa_text_print_multi(PG_FUNCTION_ARGS)
{
    Datum *rows;
    bool *nulls;
    bool isnull;
    int nelems;
    HeapTupleHeader t = PG_GETARG_HEAPTUPLEHEADER(0);
 
    Datum rows_datum = GetAttributeByName(t, "rows", &isnull);
 
    ArrayType *rows_arr = DatumGetArrayTypeP(rows_datum);
    deconstruct_array(rows_arr, VARCHAR2OID, -1, false, TYPALIGN_INT, &rows, &nulls, &nelems);
 
    for (int i = 0; i < nelems; i++)
    {
        DirectFunctionCall1(htp_print_ovr1, rows[i]);
    }
 
    PG_RETURN_VOID();
}
 
/**
 * @brief This procedure uses the PRINT Procedures or the PRN Procedures to print the "rows" field
 * of the OWA_TEXT.ROW_LIST DATA TYPE.
 * @details This procedure uses the PRINT Procedures or the PRN Procedures to print the "rows" field
 * of the OWA_TEXT.ROW_LIST DATA TYPE.
 *  * Parameters:
 *  * @param RLIST (in) PL/SQL RECORD: The row_list datatype to print.
 *  *  *  *  *
 * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_TEXT.html
 */
Datum owa_text_print_row_list(PG_FUNCTION_ARGS)
{
    Datum *rows;
    bool *nulls;
    bool isnull;
    int nelems;
    HeapTupleHeader t = PG_GETARG_HEAPTUPLEHEADER(0);
 
    Datum rows_datum = GetAttributeByName(t, "rows", &isnull);
 
    ArrayType *rows_arr = DatumGetArrayTypeP(rows_datum);
    deconstruct_array(rows_arr, NUMBEROID, -1, false, TYPALIGN_INT, &rows, &nulls, &nelems);
 
    for (int i = 0; i < nelems; i++)
    {
        DirectFunctionCall1(htp_print_ovr3, rows[i]);
    }
 
    PG_RETURN_VOID();
}
 
/**
 * @brief This procedure converts a string to a multi_line data type.
 * @details This procedure converts a string to a multi_line data type.
 *  * Parameters:
 *  * @param STREAM (in) VARCHAR2: The string to convert.
 *  *  * @param MLINE (out) PL/SQL RECORD: The stream in OWA_TEXT.MULTI_LINE DATA TYPE format
 * (Возвращается через результат)
 *  *  *  * @return record: Композитный тип (pl/sql record), содержащий OUT/INOUT параметры.
 *  *
 * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_TEXT.html
 */
Datum owa_text_stream2multi(PG_FUNCTION_ARGS)
{
    TupleDesc desc;
    text *stream_txt = PG_GETARG_TEXT_P(0);
    char *stream = text_to_cstring(stream_txt);
 
    if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE)
    {
        ereport(ERROR,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                 errmsg("Invalid call context: cannot return record!")));
        pfree(stream);
        PG_RETURN_VOID();
    }
 
    desc = BlessTupleDesc(desc);
 
    owa_text::multi_line mline{};
 
    Datum values[1] = {owa_text_add2multi_impl(stream, mline, true)};
    bool isnull[1] = {false};
 
    HeapTuple tuple = heap_form_tuple(desc, values, isnull);
 
    pfree(stream);
 
    PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
}
 
/**
 * @brief This procedure converts a string to a multi_line data type.
 * @details This procedure converts a string to a multi_line data type.
 *  * Parameters:
 *  * @param STREAM (in) VARCHAR2: The string to convert.
 *  * @param MLINE (out) PL/SQL RECORD: The stream in OWA_TEXT.MULTI_LINE DATA TYPE format
 *  * @param LEN (out) INT4: length limit
 * (Возвращается через результат)
 *  *  *  * @return record: Композитный тип (pl/sql record), содержащий OUT/INOUT параметры.
 *  *
 * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_TEXT.html
 */
Datum owa_text_stream2multi2(PG_FUNCTION_ARGS)
{
    text *stream_txt = PG_GETARG_TEXT_P(0);
    char *stream = text_to_cstring(stream_txt);
    int limit = PG_GETARG_INT32(1);
 
 
    owa_text::multi_line mline{};
 
    if (static_cast<size_t>(limit) < strlen(stream))
    {
        stream[limit] = '\0';
    }
 
    text *new_stream = cstring_to_text(stream);
    pfree(stream);
 
    PG_RETURN_DATUM(DirectFunctionCall1(owa_text_stream2multi,
                                        PointerGetDatum(new_stream)));
}
 
 
} // extern "C"