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"
|