#include #include #include #include "pg_config.h" #ifdef USE_PCRE2 #define PCRE2_CODE_UNIT_WIDTH 8 #include #endif /* USE_PCRE2 */ extern "C" { #include "postgres.h" #include "utils/builtins.h" #include "utils/elog.h" #include "funcapi.h" } #include "dbms/owa_pattern.h" #ifdef USE_PCRE2 /* RAII wrappers for PCRE2 structures */ struct pattern { pcre2_code *re; pattern(pcre2_code *re): re(re) {} ~pattern() { pcre2_code_free(re); } }; static int32_t current_handle = 0; static std::unordered_map patterns; static int owa_pattern_amatch_impl(char *line, int from_loc, pcre2_code *re, std::vector &backrefs) { pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(re, nullptr); int res = pcre2_match(re, reinterpret_cast(line), strlen(line), from_loc, 0, match_data, nullptr); if (res < 0) { pcre2_match_data_free(match_data); if (res == PCRE2_ERROR_NOMATCH) { return 0; } else { return res; } } PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data); for (int i = 0; i < res; i++) { char *substring_start = line + ovector[2 * i]; PCRE2_SIZE substring_length = ovector[2 * i + 1] - ovector[2 * i]; text *match_txt = cstring_to_text_with_len(substring_start, (int)substring_length); backrefs.push_back(PointerGetDatum(match_txt)); } pcre2_match_data_free(match_data); return res; } static std::string owa_pattern_change_impl(char *from_str, pcre2_code *re, char *to_str, bool global, int &res) { std::string result; PCRE2_SIZE output_len; pcre2_substitute(re, reinterpret_cast(from_str), PCRE2_ZERO_TERMINATED, 0, (global ? PCRE2_SUBSTITUTE_GLOBAL : 0) | PCRE2_SUBSTITUTE_OVERFLOW_LENGTH, nullptr, nullptr, reinterpret_cast(to_str), PCRE2_ZERO_TERMINATED, reinterpret_cast(result.data()), &output_len); result.resize(output_len); res = pcre2_substitute(re, reinterpret_cast(from_str), PCRE2_ZERO_TERMINATED, 0, (global ? PCRE2_SUBSTITUTE_GLOBAL : 0), nullptr, nullptr, reinterpret_cast(to_str), PCRE2_ZERO_TERMINATED, reinterpret_cast(result.data()), &output_len); return result; } static bool owa_pattern_match_impl(char *line, pcre2_code *re, std::vector &backrefs) { pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(re, nullptr); int res = pcre2_match(re, reinterpret_cast(line), strlen(line), 0, 0, match_data, nullptr); if (res < 0) { pcre2_match_data_free(match_data); return false; } PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(match_data); for (int i = 0; i < res; i++) { char *substring_start = line + ovector[2 * i]; PCRE2_SIZE substring_length = ovector[2 * i + 1] - ovector[2 * i]; text *match_txt = cstring_to_text_with_len(substring_start, (int)substring_length); backrefs.push_back(PointerGetDatum(match_txt)); } pcre2_match_data_free(match_data); return true; } #endif /* USE_PCRE2 */ static int32_t numeric_get_int32(Numeric num) { Datum res_datum = DirectFunctionCall1(numeric_int4, NumericGetDatum(num)); int32_t res = DatumGetInt32(res_datum); return res; } extern "C" { PG_FUNCTION_INFO_V1(owa_pattern_amatch_ovr1); PG_FUNCTION_INFO_V1(owa_pattern_amatch_ovr2); PG_FUNCTION_INFO_V1(owa_pattern_amatch_ovr3); PG_FUNCTION_INFO_V1(owa_pattern_amatch_ovr4); PG_FUNCTION_INFO_V1(owa_pattern_amatch_ovr5); PG_FUNCTION_INFO_V1(owa_pattern_amatch_ovr6); PG_FUNCTION_INFO_V1(owa_pattern_amatch_ovr7); PG_FUNCTION_INFO_V1(owa_pattern_amatch_ovr8); PG_FUNCTION_INFO_V1(owa_pattern_change_ovr1); PG_FUNCTION_INFO_V1(owa_pattern_change_ovr2); PG_FUNCTION_INFO_V1(owa_pattern_change_ovr3); PG_FUNCTION_INFO_V1(owa_pattern_change_ovr4); PG_FUNCTION_INFO_V1(owa_pattern_change_ovr5); PG_FUNCTION_INFO_V1(owa_pattern_change_ovr6); PG_FUNCTION_INFO_V1(owa_pattern_change_ovr7); PG_FUNCTION_INFO_V1(owa_pattern_change_ovr8); PG_FUNCTION_INFO_V1(owa_pattern_getpat); PG_FUNCTION_INFO_V1(owa_pattern_match_ovr1); PG_FUNCTION_INFO_V1(owa_pattern_match_ovr2); PG_FUNCTION_INFO_V1(owa_pattern_match_ovr3); PG_FUNCTION_INFO_V1(owa_pattern_match_ovr4); PG_FUNCTION_INFO_V1(owa_pattern_match_ovr5); PG_FUNCTION_INFO_V1(owa_pattern_match_ovr6); PG_FUNCTION_INFO_V1(owa_pattern_match_ovr7); PG_FUNCTION_INFO_V1(owa_pattern_match_ovr8); PG_FUNCTION_INFO_V1(owa_pattern_match_ovr9); PG_FUNCTION_INFO_V1(owa_pattern_match_ovr10); PG_FUNCTION_INFO_V1(owa_pattern_match_ovr11); PG_FUNCTION_INFO_V1(owa_pattern_match_ovr12); PG_FUNCTION_INFO_V1(owa_pattern_match2); /** * @brief This function specifies if a pattern occurs in a particular location in a string. * @details This function specifies if a pattern occurs in a particular location in a string. * * Parameters: * * @param LINE (in) VARCHAR2: The text to search in. * * @param FROM_LOC (in) NUMBER: The location (in number of characters) inlinewhere the search is to begin. * * @param PAT (in) VARCHAR2: The string to match. It can contain regular expressions. This can be either aVARCHAR2or a pattern. If it is a pattern, the output value of this parameter is the pattern matched. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive. If the value of this parameter is "i", the search is case-insensitive. Otherwise the search is case-sensitive. * * * * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_amatch_ovr1(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.amatch еще не реализована."))); PG_RETURN_NULL(); #else std::vector backrefs; int result; pcre2_code *re; int error_number; PCRE2_SIZE error_offset; text *line_txt = PG_GETARG_TEXT_P(0); Numeric from_loc_num = PG_GETARG_NUMERIC(1); text *pattern_txt = PG_GETARG_TEXT_P(2); char *line = text_to_cstring(line_txt); int32_t from_loc = numeric_get_int32(from_loc_num); char *pattern = text_to_cstring(pattern_txt); re = pcre2_compile(reinterpret_cast(pattern), PCRE2_ZERO_TERMINATED, 0, &error_number, &error_offset, nullptr); if (re == nullptr) { PCRE2_UCHAR buffer[256]; pcre2_get_error_message(error_number, buffer, sizeof(buffer)); ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("PCRE2 compilation failed at offset %zu: %s", error_offset, buffer))); PG_RETURN_NULL(); } result = owa_pattern_amatch_impl(line, from_loc, re, backrefs); pcre2_code_free(re); pfree(line); pfree(pattern); PG_RETURN_DATUM(DirectFunctionCall1(int4_numeric, Int32GetDatum(result))); #endif /* USE_PCRE2 */ } /** * @brief This function specifies if a pattern occurs in a particular location in a string. * @details This function specifies if a pattern occurs in a particular location in a string. * * Parameters: * * @param LINE (in) VARCHAR2: The text to search in. * * @param FROM_LOC (in) NUMBER: The location (in number of characters) inlinewhere the search is to begin. * * @param PAT (in) VARCHAR2: The string to match. It can contain regular expressions. This can be either aVARCHAR2or a pattern. If it is a pattern, the output value of this parameter is the pattern matched. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive. If the value of this parameter is "i", the search is case-insensitive. Otherwise the search is case-sensitive. * * * * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_amatch_ovr2(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.amatch еще не реализована."))); PG_RETURN_NULL(); #else std::vector backrefs; int result; pcre2_code *re; int error_number; PCRE2_SIZE error_offset; text *line_txt = PG_GETARG_TEXT_P(0); Numeric from_loc_num = PG_GETARG_NUMERIC(1); text *pattern_txt = PG_GETARG_TEXT_P(2); text *flags_txt; int caseless_match = 0; if (!PG_ARGISNULL(3)) { flags_txt = PG_GETARG_TEXT_P(3); char *flags = text_to_cstring(flags_txt); caseless_match = *flags == 'i' ? PCRE2_CASELESS : 0; pfree(flags); } char *line = text_to_cstring(line_txt); int32_t from_loc = numeric_get_int32(from_loc_num); char *pattern = text_to_cstring(pattern_txt); re = pcre2_compile(reinterpret_cast(pattern), PCRE2_ZERO_TERMINATED, caseless_match, &error_number, &error_offset, nullptr); if (re == nullptr) { PCRE2_UCHAR buffer[256]; pcre2_get_error_message(error_number, buffer, sizeof(buffer)); ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("PCRE2 compilation failed at offset %zu: %s", error_offset, buffer))); PG_RETURN_NULL(); } result = owa_pattern_amatch_impl(line, from_loc, re, backrefs); pcre2_code_free(re); pfree(line); pfree(pattern); PG_RETURN_DATUM(DirectFunctionCall1(int4_numeric, Int32GetDatum(result))); #endif /* USE_PCRE2 */ } /** * @brief This function specifies if a pattern occurs in a particular location in a string. * @details This function specifies if a pattern occurs in a particular location in a string. * * Parameters: * * @param LINE (in) VARCHAR2: The text to search in. * * @param FROM_LOC (in) NUMBER: The location (in number of characters) inlinewhere the search is to begin. * * @param PAT (in/out) PL/SQL TABLE: The string to match. It can contain regular expressions. This can be either aVARCHAR2or a pattern. If it is a pattern, the output value of this parameter is the pattern matched. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive. If the value of this parameter is "i", the search is case-insensitive. Otherwise the search is case-sensitive. * * * @param PAT (in/out) PL/SQL TABLE: The string to match. It can contain regular expressions. This can be either aVARCHAR2or a pattern. If it is a pattern, the output value of this parameter is the pattern matched. (Возвращается через результат) * * * * @return record: Композитный тип (), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_amatch_ovr3(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.amatch еще не реализована."))); PG_RETURN_VOID(); #else std::vector backrefs; int result; pcre2_code *re; text *line_txt = PG_GETARG_TEXT_P(0); Numeric from_loc_num = PG_GETARG_NUMERIC(1); int pattern_handle = PG_GETARG_INT32(2); char *line = text_to_cstring(line_txt); int32_t from_loc = numeric_get_int32(from_loc_num); auto pattern_it = patterns.find(pattern_handle); if (pattern_it == patterns.end()) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Invalid pattern handle: %d", pattern_handle))); pfree(line); PG_RETURN_NULL(); } re = pattern_it->second.re; result = owa_pattern_amatch_impl(line, from_loc, re, backrefs); pfree(line); TupleDesc desc; if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Invalid call context: cannot return record!"))); PG_RETURN_VOID(); } desc = BlessTupleDesc(desc); Datum values[2]; values[0] = DirectFunctionCall1(int4_numeric, Int32GetDatum(result)); values[1] = Int32GetDatum(pattern_handle); bool isnull[2] = {false, false}; HeapTuple tuple = heap_form_tuple(desc, values, isnull); PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); #endif /* USE_PCRE2 */ } /** * @brief This function specifies if a pattern occurs in a particular location in a string. * @details This function specifies if a pattern occurs in a particular location in a string. * * Parameters: * * @param LINE (in) VARCHAR2: The text to search in. * * @param FROM_LOC (in) NUMBER: The location (in number of characters) inlinewhere the search is to begin. * * @param PAT (in/out) PL/SQL TABLE: The string to match. It can contain regular expressions. This can be either aVARCHAR2or a pattern. If it is a pattern, the output value of this parameter is the pattern matched. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive. If the value of this parameter is "i", the search is case-insensitive. Otherwise the search is case-sensitive. * * * @param PAT (in/out) PL/SQL TABLE: The string to match. It can contain regular expressions. This can be either aVARCHAR2or a pattern. If it is a pattern, the output value of this parameter is the pattern matched. (Возвращается через результат) * * * * @return record: Композитный тип (), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_amatch_ovr4(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.amatch еще не реализована."))); PG_RETURN_VOID(); #else std::vector backrefs; int result; pcre2_code *re; text *line_txt = PG_GETARG_TEXT_P(0); Numeric from_loc_num = PG_GETARG_NUMERIC(1); int pattern_handle = PG_GETARG_INT32(2); /* TODO: Since caselessness is fixed in pcre2_code, we have to isgnore flags parameter */ char *line = text_to_cstring(line_txt); int32_t from_loc = numeric_get_int32(from_loc_num); auto pattern_it = patterns.find(pattern_handle); if (pattern_it == patterns.end()) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Invalid pattern handle: %d", pattern_handle))); pfree(line); PG_RETURN_NULL(); } re = pattern_it->second.re; result = owa_pattern_amatch_impl(line, from_loc, re, backrefs); pfree(line); TupleDesc desc; if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Invalid call context: cannot return record!"))); PG_RETURN_VOID(); } desc = BlessTupleDesc(desc); Datum values[2]; values[0] = DirectFunctionCall1(int4_numeric, Int32GetDatum(result)); values[1] = Int32GetDatum(pattern_handle); bool isnull[2] = {false, false}; HeapTuple tuple = heap_form_tuple(desc, values, isnull); PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); #endif /* USE_PCRE2 */ } /** * @brief This function specifies if a pattern occurs in a particular location in a string. * @details This function specifies if a pattern occurs in a particular location in a string. * * Parameters: * * @param LINE (in) VARCHAR2: The text to search in. * * @param FROM_LOC (in) NUMBER: The location (in number of characters) inlinewhere the search is to begin. * * @param PAT (in) VARCHAR2: The string to match. It can contain regular expressions. This can be either aVARCHAR2or a pattern. If it is a pattern, the output value of this parameter is the pattern matched. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive. If the value of this parameter is "i", the search is case-insensitive. Otherwise the search is case-sensitive. * * * @param BACKREFS (out) PL/SQL TABLE: The text that is matched. Each token that is matched is placed in a cell in theOWA_TEXT.VC_ARR DATA TYPEPL/SQL table. (Возвращается через результат) * * * * @return record: Композитный тип (), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_amatch_ovr5(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.amatch еще не реализована."))); PG_RETURN_VOID(); #else std::vector backrefs; int result; pcre2_code *re; int error_number; PCRE2_SIZE error_offset; text *line_txt = PG_GETARG_TEXT_P(0); Numeric from_loc_num = PG_GETARG_NUMERIC(1); text *pattern_txt = PG_GETARG_TEXT_P(2); char *line = text_to_cstring(line_txt); int32_t from_loc = numeric_get_int32(from_loc_num); char *pattern = text_to_cstring(pattern_txt); re = pcre2_compile(reinterpret_cast(pattern), PCRE2_ZERO_TERMINATED, 0, &error_number, &error_offset, nullptr); if (re == nullptr) { PCRE2_UCHAR buffer[256]; pcre2_get_error_message(error_number, buffer, sizeof(buffer)); ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("PCRE2 compilation failed at offset %zu: %s", error_offset, buffer))); PG_RETURN_NULL(); } result = owa_pattern_amatch_impl(line, from_loc, re, backrefs); pcre2_code_free(re); pfree(line); pfree(pattern); TupleDesc desc; if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Invalid call context: cannot return record!"))); PG_RETURN_VOID(); } desc = BlessTupleDesc(desc); ArrayType *arr = construct_array(backrefs.data(), backrefs.size(), VARCHAR2OID, -1, false, TYPALIGN_INT); Datum values[2]; values[0] = DirectFunctionCall1(int4_numeric, Int32GetDatum(result)); values[1] = PointerGetDatum(arr); bool isnull[2] = {false, false}; HeapTuple tuple = heap_form_tuple(desc, values, isnull); PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); #endif /* USE_PCRE2 */ } /** * @brief This function specifies if a pattern occurs in a particular location in a string. * @details This function specifies if a pattern occurs in a particular location in a string. * * Parameters: * * @param LINE (in) VARCHAR2: The text to search in. * * @param FROM_LOC (in) NUMBER: The location (in number of characters) inlinewhere the search is to begin. * * @param PAT (in) VARCHAR2: The string to match. It can contain regular expressions. This can be either aVARCHAR2or a pattern. If it is a pattern, the output value of this parameter is the pattern matched. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive. If the value of this parameter is "i", the search is case-insensitive. Otherwise the search is case-sensitive. * * * @param BACKREFS (out) PL/SQL TABLE: The text that is matched. Each token that is matched is placed in a cell in theOWA_TEXT.VC_ARR DATA TYPEPL/SQL table. (Возвращается через результат) * * * * @return record: Композитный тип (), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_amatch_ovr6(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.amatch еще не реализована."))); PG_RETURN_VOID(); #else std::vector backrefs; int result; pcre2_code *re; int error_number; PCRE2_SIZE error_offset; text *line_txt = PG_GETARG_TEXT_P(0); Numeric from_loc_num = PG_GETARG_NUMERIC(1); text *pattern_txt = PG_GETARG_TEXT_P(2); text *flags_txt; int caseless_match = 0; if (PG_ARGISNULL(3)) { caseless_match = 0; } else { flags_txt = PG_GETARG_TEXT_P(3); char *flags = text_to_cstring(flags_txt); caseless_match = *flags == 'i' ? PCRE2_CASELESS : 0; pfree(flags); } char *line = text_to_cstring(line_txt); int32_t from_loc = numeric_get_int32(from_loc_num); char *pattern = text_to_cstring(pattern_txt); re = pcre2_compile(reinterpret_cast(pattern), PCRE2_ZERO_TERMINATED, caseless_match, &error_number, &error_offset, nullptr); if (re == nullptr) { PCRE2_UCHAR buffer[256]; pcre2_get_error_message(error_number, buffer, sizeof(buffer)); ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("PCRE2 compilation failed at offset %zu: %s", error_offset, buffer))); PG_RETURN_NULL(); } result = owa_pattern_amatch_impl(line, from_loc, re, backrefs); pcre2_code_free(re); pfree(line); pfree(pattern); TupleDesc desc; if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Invalid call context: cannot return record!"))); PG_RETURN_VOID(); } desc = BlessTupleDesc(desc); ArrayType *arr = construct_array(backrefs.data(), backrefs.size(), VARCHAR2OID, -1, false, TYPALIGN_INT); Datum values[2]; values[0] = DirectFunctionCall1(int4_numeric, Int32GetDatum(result)); values[1] = PointerGetDatum(arr); bool isnull[2] = {false, false}; HeapTuple tuple = heap_form_tuple(desc, values, isnull); PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); #endif /* USE_PCRE2 */ } /** * @brief This function specifies if a pattern occurs in a particular location in a string. * @details This function specifies if a pattern occurs in a particular location in a string. * * Parameters: * * @param LINE (in) VARCHAR2: The text to search in. * * @param FROM_LOC (in) NUMBER: The location (in number of characters) inlinewhere the search is to begin. * * @param PAT (in/out) PL/SQL TABLE: The string to match. It can contain regular expressions. This can be either aVARCHAR2or a pattern. If it is a pattern, the output value of this parameter is the pattern matched. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive. If the value of this parameter is "i", the search is case-insensitive. Otherwise the search is case-sensitive. * * * @param PAT (in/out) PL/SQL TABLE: The string to match. It can contain regular expressions. This can be either aVARCHAR2or a pattern. If it is a pattern, the output value of this parameter is the pattern matched. (Возвращается через результат) * * @param BACKREFS (out) PL/SQL TABLE: The text that is matched. Each token that is matched is placed in a cell in theOWA_TEXT.VC_ARR DATA TYPEPL/SQL table. (Возвращается через результат) * * * * @return record: Композитный тип (), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_amatch_ovr7(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.amatch еще не реализована."))); PG_RETURN_VOID(); #else std::vector backrefs; int result; pcre2_code *re; text *line_txt = PG_GETARG_TEXT_P(0); Numeric from_loc_num = PG_GETARG_NUMERIC(1); int pattern_handle = PG_GETARG_INT32(2); char *line = text_to_cstring(line_txt); int32_t from_loc = numeric_get_int32(from_loc_num); auto pattern_it = patterns.find(pattern_handle); if (pattern_it == patterns.end()) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Invalid pattern handle: %d", pattern_handle))); pfree(line); PG_RETURN_NULL(); } re = pattern_it->second.re; result = owa_pattern_amatch_impl(line, from_loc, re, backrefs); pfree(line); TupleDesc desc; if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Invalid call context: cannot return record!"))); PG_RETURN_VOID(); } desc = BlessTupleDesc(desc); ArrayType *arr = construct_array(backrefs.data(), backrefs.size(), VARCHAR2OID, -1, false, TYPALIGN_INT); Datum values[3]; values[0] = DirectFunctionCall1(int4_numeric, Int32GetDatum(result)); values[1] = Int32GetDatum(pattern_handle); values[3] = PointerGetDatum(arr); bool isnull[3] = {false, false, false}; HeapTuple tuple = heap_form_tuple(desc, values, isnull); PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); #endif /* USE_PCRE2 */ } /** * @brief This function specifies if a pattern occurs in a particular location in a string. * @details This function specifies if a pattern occurs in a particular location in a string. * * Parameters: * * @param LINE (in) VARCHAR2: The text to search in. * * @param FROM_LOC (in) NUMBER: The location (in number of characters) inlinewhere the search is to begin. * * @param PAT (in/out) PL/SQL TABLE: The string to match. It can contain regular expressions. This can be either aVARCHAR2or a pattern. If it is a pattern, the output value of this parameter is the pattern matched. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive. If the value of this parameter is "i", the search is case-insensitive. Otherwise the search is case-sensitive. * * * @param PAT (in/out) PL/SQL TABLE: The string to match. It can contain regular expressions. This can be either aVARCHAR2or a pattern. If it is a pattern, the output value of this parameter is the pattern matched. (Возвращается через результат) * * @param BACKREFS (out) PL/SQL TABLE: The text that is matched. Each token that is matched is placed in a cell in theOWA_TEXT.VC_ARR DATA TYPEPL/SQL table. (Возвращается через результат) * * * * @return record: Композитный тип (), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_amatch_ovr8(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.amatch еще не реализована."))); PG_RETURN_VOID(); #else std::vector backrefs; int result; pcre2_code *re; text *line_txt = PG_GETARG_TEXT_P(0); Numeric from_loc_num = PG_GETARG_NUMERIC(1); int pattern_handle = PG_GETARG_INT32(2); char *line = text_to_cstring(line_txt); int32_t from_loc = numeric_get_int32(from_loc_num); auto pattern_it = patterns.find(pattern_handle); if (pattern_it == patterns.end()) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Invalid pattern handle: %d", pattern_handle))); pfree(line); PG_RETURN_NULL(); } re = pattern_it->second.re; result = owa_pattern_amatch_impl(line, from_loc, re, backrefs); pfree(line); TupleDesc desc; if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Invalid call context: cannot return record!"))); PG_RETURN_VOID(); } desc = BlessTupleDesc(desc); ArrayType *arr = construct_array(backrefs.data(), backrefs.size(), VARCHAR2OID, -1, false, TYPALIGN_INT); Datum values[3]; values[0] = DirectFunctionCall1(int4_numeric, Int32GetDatum(result)); values[1] = Int32GetDatum(pattern_handle); values[2] = PointerGetDatum(arr); bool isnull[3] = {false, false, false}; HeapTuple tuple = heap_form_tuple(desc, values, isnull); PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); #endif /* USE_PCRE2 */ } /** * @brief This function or procedure searches and replaces a string ormulti_linedatatype. If multiple overlapping strings match the regular expression, this subprogram takes the longest match. * @details This function or procedure searches and replaces a string ormulti_linedatatype. If multiple overlapping strings match the regular expression, this subprogram takes the longest match. * * Parameters: * * @param LINE (in/out) VARCHAR2: The text to search in. The output value of this parameter is the altered string. * * @param FROM_STR (in) VARCHAR2: The regular expression to replace. * * @param TO_STR (in) VARCHAR2: The substitution pattern. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive, and whether or not changes are to be made globally. If "i" is specified, the search is case-insensitive. If "g" is specified, changes are made to all matches. Otherwise, the function stops after the first substitution is made. * * * @param LINE (in/out) VARCHAR2: The text to search in. The output value of this parameter is the altered string. (Возвращается через результат) * * * * @return record: Композитный тип (), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_change_ovr1(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.change еще не реализована."))); PG_RETURN_VOID(); #else text *line_txt = PG_GETARG_TEXT_P(0); text *from_str_txt = PG_GETARG_TEXT_P(1); text *to_str_txt = PG_GETARG_TEXT_P(2); char *line = text_to_cstring(line_txt); char *from_str = text_to_cstring(from_str_txt); char *to_str = text_to_cstring(to_str_txt); int error_number; PCRE2_SIZE error_offset; pcre2_code *re = pcre2_compile(reinterpret_cast(from_str), PCRE2_ZERO_TERMINATED, 0, &error_number, &error_offset, nullptr); if (re == nullptr) { PCRE2_UCHAR buffer[256]; pcre2_get_error_message(error_number, buffer, sizeof(buffer)); ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("PCRE2 compilation failed at offset %zu: %s", error_offset, buffer))); PG_RETURN_NULL(); } int res; std::string substituted_line = owa_pattern_change_impl(line, re, to_str, false, res); pcre2_code_free(re); TupleDesc desc; if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Invalid call context: cannot return record!"))); PG_RETURN_VOID(); } desc = BlessTupleDesc(desc); Datum values[2]; values[0] = DirectFunctionCall1(int4_numeric, Int32GetDatum(res)); values[1] = PointerGetDatum(cstring_to_text(substituted_line.c_str())); bool isnull[2] = {false, false}; HeapTuple tuple = heap_form_tuple(desc, values, isnull); PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); #endif /* USE_PCRE2 */ } /** * @brief This function or procedure searches and replaces a string ormulti_linedatatype. If multiple overlapping strings match the regular expression, this subprogram takes the longest match. * @details This function or procedure searches and replaces a string ormulti_linedatatype. If multiple overlapping strings match the regular expression, this subprogram takes the longest match. * * Parameters: * * @param LINE (in/out) VARCHAR2: The text to search in. The output value of this parameter is the altered string. * * @param FROM_STR (in) VARCHAR2: The regular expression to replace. * * @param TO_STR (in) VARCHAR2: The substitution pattern. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive, and whether or not changes are to be made globally. If "i" is specified, the search is case-insensitive. If "g" is specified, changes are made to all matches. Otherwise, the function stops after the first substitution is made. * * * @param LINE (in/out) VARCHAR2: The text to search in. The output value of this parameter is the altered string. (Возвращается через результат) * * * * @return record: Композитный тип (), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_change_ovr2(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.change еще не реализована."))); PG_RETURN_VOID(); #else text *line_txt = PG_GETARG_TEXT_P(0); text *from_str_txt = PG_GETARG_TEXT_P(1); text *to_str_txt = PG_GETARG_TEXT_P(2); char *line = text_to_cstring(line_txt); char *from_str = text_to_cstring(from_str_txt); char *to_str = text_to_cstring(to_str_txt); bool global = false; bool caseless = false; if (!PG_ARGISNULL(3)) { text *flags_txt = PG_GETARG_TEXT_P(3); char *flags = text_to_cstring(flags_txt); for (char *flag = flags; *flag != '\0'; flag++) { switch(*flag) { case 'i': caseless = true; break; case 'g': global = true; break; default: continue; } } pfree(flags); } int error_number; PCRE2_SIZE error_offset; pcre2_code *re = pcre2_compile(reinterpret_cast(from_str), PCRE2_ZERO_TERMINATED, caseless ? PCRE2_CASELESS : 0, &error_number, &error_offset, nullptr); if (re == nullptr) { PCRE2_UCHAR buffer[256]; pcre2_get_error_message(error_number, buffer, sizeof(buffer)); ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("PCRE2 compilation failed at offset %zu: %s", error_offset, buffer))); PG_RETURN_NULL(); } int res; std::string substituted_line = owa_pattern_change_impl(line, nullptr, to_str, global, res); pcre2_code_free(re); TupleDesc desc; if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Invalid call context: cannot return record!"))); PG_RETURN_VOID(); } desc = BlessTupleDesc(desc); Datum values[2]; values[0] = DirectFunctionCall1(int4_numeric, Int32GetDatum(res)); values[1] = PointerGetDatum(cstring_to_text(substituted_line.c_str())); bool isnull[2] = {false, false}; HeapTuple tuple = heap_form_tuple(desc, values, isnull); PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); #endif /* USE_PCRE2 */ } /** * @brief This function or procedure searches and replaces a string ormulti_linedatatype. If multiple overlapping strings match the regular expression, this subprogram takes the longest match. * @details This function or procedure searches and replaces a string ormulti_linedatatype. If multiple overlapping strings match the regular expression, this subprogram takes the longest match. * * Parameters: * * @param LINE (in/out) VARCHAR2: The text to search in. The output value of this parameter is the altered string. * * @param FROM_STR (in) VARCHAR2: The regular expression to replace. * * @param TO_STR (in) VARCHAR2: The substitution pattern. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive, and whether or not changes are to be made globally. If "i" is specified, the search is case-insensitive. If "g" is specified, changes are made to all matches. Otherwise, the function stops after the first substitution is made. * * * @param LINE (in/out) VARCHAR2: The text to search in. The output value of this parameter is the altered string. (Возвращается через результат) * * * * @return record: Композитный тип (varchar2), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_change_ovr3(PG_FUNCTION_ARGS) { // ovr1 but do not return number of substitutions #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.change еще не реализована."))); PG_RETURN_VOID(); #else text *line_txt = PG_GETARG_TEXT_P(0); text *from_str_txt = PG_GETARG_TEXT_P(1); text *to_str_txt = PG_GETARG_TEXT_P(2); char *line = text_to_cstring(line_txt); char *from_str = text_to_cstring(from_str_txt); char *to_str = text_to_cstring(to_str_txt); int error_number; PCRE2_SIZE error_offset; pcre2_code *re = pcre2_compile(reinterpret_cast(from_str), PCRE2_ZERO_TERMINATED, 0, &error_number, &error_offset, nullptr); if (re == nullptr) { PCRE2_UCHAR buffer[256]; pcre2_get_error_message(error_number, buffer, sizeof(buffer)); ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("PCRE2 compilation failed at offset %zu: %s", error_offset, buffer))); PG_RETURN_NULL(); } int res; std::string substituted_line = owa_pattern_change_impl(line, re, to_str, false, res); pcre2_code_free(re); text *substituted_line_txt = cstring_to_text(substituted_line.c_str()); PG_RETURN_TEXT_P(substituted_line_txt); #endif /* USE_PCRE2 */ } /** * @brief This function or procedure searches and replaces a string ormulti_linedatatype. If multiple overlapping strings match the regular expression, this subprogram takes the longest match. * @details This function or procedure searches and replaces a string ormulti_linedatatype. If multiple overlapping strings match the regular expression, this subprogram takes the longest match. * * Parameters: * * @param LINE (in/out) VARCHAR2: The text to search in. The output value of this parameter is the altered string. * * @param FROM_STR (in) VARCHAR2: The regular expression to replace. * * @param TO_STR (in) VARCHAR2: The substitution pattern. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive, and whether or not changes are to be made globally. If "i" is specified, the search is case-insensitive. If "g" is specified, changes are made to all matches. Otherwise, the function stops after the first substitution is made. * * * @param LINE (in/out) VARCHAR2: The text to search in. The output value of this parameter is the altered string. (Возвращается через результат) * * * * @return record: Композитный тип (varchar2), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_change_ovr4(PG_FUNCTION_ARGS) { // ovr2 but do not return number of substitutions #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.change еще не реализована."))); PG_RETURN_VOID(); #else text *line_txt = PG_GETARG_TEXT_P(0); text *from_str_txt = PG_GETARG_TEXT_P(1); text *to_str_txt = PG_GETARG_TEXT_P(2); char *line = text_to_cstring(line_txt); char *from_str = text_to_cstring(from_str_txt); char *to_str = text_to_cstring(to_str_txt); bool global = false; bool caseless = false; if (!PG_ARGISNULL(3)) { text *flags_txt = PG_GETARG_TEXT_P(3); char *flags = text_to_cstring(flags_txt); for (char *flag = flags; *flag != '\0'; flag++) { switch(*flag) { case 'i': caseless = true; break; case 'g': global = true; break; default: continue; } } pfree(flags); } int error_number; PCRE2_SIZE error_offset; pcre2_code *re = pcre2_compile(reinterpret_cast(from_str), PCRE2_ZERO_TERMINATED, caseless ? PCRE2_CASELESS : 0, &error_number, &error_offset, nullptr); if (re == nullptr) { PCRE2_UCHAR buffer[256]; pcre2_get_error_message(error_number, buffer, sizeof(buffer)); ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("PCRE2 compilation failed at offset %zu: %s", error_offset, buffer))); PG_RETURN_NULL(); } int res; std::string substituted_line = owa_pattern_change_impl(line, re, to_str, global, res); pcre2_code_free(re); text *substituted_line_txt = cstring_to_text(substituted_line.c_str()); PG_RETURN_TEXT_P(substituted_line_txt); #endif /* USE_PCRE2 */ } /** * @brief This function or procedure searches and replaces a string ormulti_linedatatype. If multiple overlapping strings match the regular expression, this subprogram takes the longest match. * @details This function or procedure searches and replaces a string ormulti_linedatatype. If multiple overlapping strings match the regular expression, this subprogram takes the longest match. * * Parameters: * * @param MLINE (in/out) PL/SQL RECORD: The text to search in. This is aowa_text.multi_line datatype. The output value of this parameter is the altered string. * * @param FROM_STR (in) VARCHAR2: The regular expression to replace. * * @param TO_STR (in) VARCHAR2: The substitution pattern. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive, and whether or not changes are to be made globally. If "i" is specified, the search is case-insensitive. If "g" is specified, changes are made to all matches. Otherwise, the function stops after the first substitution is made. * * * @param MLINE (in/out) PL/SQL RECORD: The text to search in. This is aowa_text.multi_line datatype. The output value of this parameter is the altered string. (Возвращается через результат) * * * * @return record: Композитный тип (), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_change_ovr5(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.change еще не реализована."))); PG_RETURN_VOID(); #else HeapTupleHeader mline = PG_GETARG_HEAPTUPLEHEADER(0); bool isattnull; Datum rows_datum = GetAttributeByName(mline, "rows", &isattnull); Datum partial_row_datum = GetAttributeByName(mline, "partial_row", &isattnull); ArrayType *rows_arr = DatumGetArrayTypeP(rows_datum); Datum *rows; bool *row_nulls; int rows_len; deconstruct_array(rows_arr, VARCHAR2OID, -1, false, TYPALIGN_INT, &rows, &row_nulls, &rows_len); text *from_str_txt = PG_GETARG_TEXT_P(1); text *to_str_txt = PG_GETARG_TEXT_P(2); char *from_str = text_to_cstring(from_str_txt); char *to_str = text_to_cstring(to_str_txt); int error_number; PCRE2_SIZE error_offset; pcre2_code *re = pcre2_compile(reinterpret_cast(from_str), PCRE2_ZERO_TERMINATED, 0, &error_number, &error_offset, nullptr); if (re == nullptr) { PCRE2_UCHAR buffer[256]; pcre2_get_error_message(error_number, buffer, sizeof(buffer)); ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("PCRE2 compilation failed at offset %zu: %s", error_offset, buffer))); pfree(rows); pfree(row_nulls); PG_RETURN_NULL(); } std::vector lines; int total_res = 0; for (int i = 0; i < rows_len; i++) { int res; text *line_txt = DatumGetTextP(rows[i]); char *line = text_to_cstring(line_txt); std::string substituted_line = owa_pattern_change_impl(line, re, to_str, false, res); text *substituted_line_txt = cstring_to_text(substituted_line.c_str()); lines.push_back(PointerGetDatum(substituted_line_txt)); pfree(line); total_res += res; } pcre2_code_free(re); pfree(rows); pfree(row_nulls); TupleDesc record_desc = CreateTemplateTupleDesc(3); TupleDescInitEntry(record_desc, 0, "rows", VARCHAR2ARRAYOID, -1, 1); TupleDescInitEntry(record_desc, 1, "num_rows", NUMBEROID, -1, 0); TupleDescInitEntry(record_desc, 2, "partial_row", BOOLOID, -1, 0); record_desc = BlessTupleDesc(record_desc); Datum record_values[3]; bool record_isnull[3]; ArrayType *new_arr = construct_array(lines.data(), lines.size(), VARCHAR2OID, -1, false, TYPALIGN_INT); record_values[0] = PointerGetDatum(new_arr); record_isnull[0] = false; record_values[1] = DirectFunctionCall1(int4_numeric, Int32GetDatum(lines.size())); record_isnull[1] = false; record_values[2] = partial_row_datum; record_isnull[2] = false; HeapTuple record_tuple = heap_form_tuple(record_desc, record_values, record_isnull); TupleDesc desc; if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Invalid call context: cannot return record!"))); PG_RETURN_VOID(); } desc = BlessTupleDesc(desc); Datum values[2]; values[0] = DirectFunctionCall1(int4_numeric, Int32GetDatum(total_res)); values[1] = HeapTupleGetDatum(record_tuple); bool isnull[2] = {false, false}; HeapTuple tuple = heap_form_tuple(desc, values, isnull); PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); #endif /* USE_PCRE2 */ } /** * @brief This function or procedure searches and replaces a string ormulti_linedatatype. If multiple overlapping strings match the regular expression, this subprogram takes the longest match. * @details This function or procedure searches and replaces a string ormulti_linedatatype. If multiple overlapping strings match the regular expression, this subprogram takes the longest match. * * Parameters: * * @param MLINE (in/out) PL/SQL RECORD: The text to search in. This is aowa_text.multi_line datatype. The output value of this parameter is the altered string. * * @param FROM_STR (in) VARCHAR2: The regular expression to replace. * * @param TO_STR (in) VARCHAR2: The substitution pattern. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive, and whether or not changes are to be made globally. If "i" is specified, the search is case-insensitive. If "g" is specified, changes are made to all matches. Otherwise, the function stops after the first substitution is made. * * * @param MLINE (in/out) PL/SQL RECORD: The text to search in. This is aowa_text.multi_line datatype. The output value of this parameter is the altered string. (Возвращается через результат) * * * * @return record: Композитный тип (), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_change_ovr6(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.change еще не реализована."))); PG_RETURN_VOID(); #else HeapTupleHeader mline = PG_GETARG_HEAPTUPLEHEADER(0); bool isattnull; Datum rows_datum = GetAttributeByName(mline, "rows", &isattnull); Datum partial_row_datum = GetAttributeByName(mline, "partial_row", &isattnull); ArrayType *rows_arr = DatumGetArrayTypeP(rows_datum); Datum *rows; bool *row_nulls; int rows_len; deconstruct_array(rows_arr, VARCHAR2OID, -1, false, TYPALIGN_INT, &rows, &row_nulls, &rows_len); text *from_str_txt = PG_GETARG_TEXT_P(1); text *to_str_txt = PG_GETARG_TEXT_P(2); char *from_str = text_to_cstring(from_str_txt); char *to_str = text_to_cstring(to_str_txt); bool global = false; bool caseless = false; if (!PG_ARGISNULL(3)) { text *flags_txt = PG_GETARG_TEXT_P(3); char *flags = text_to_cstring(flags_txt); for (char *flag = flags; *flag != '\0'; flag++) { switch(*flag) { case 'i': caseless = true; break; case 'g': global = true; break; default: continue; } } pfree(flags); } int error_number; PCRE2_SIZE error_offset; pcre2_code *re = pcre2_compile(reinterpret_cast(from_str), PCRE2_ZERO_TERMINATED, caseless ? PCRE2_CASELESS : 0, &error_number, &error_offset, nullptr); if (re == nullptr) { PCRE2_UCHAR buffer[256]; pcre2_get_error_message(error_number, buffer, sizeof(buffer)); ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("PCRE2 compilation failed at offset %zu: %s", error_offset, buffer))); pfree(rows); pfree(row_nulls); PG_RETURN_NULL(); } std::vector lines; int total_res = 0; for (int i = 0; i < rows_len; i++) { int res; text *line_txt = DatumGetTextP(rows[i]); char *line = text_to_cstring(line_txt); std::string substituted_line = owa_pattern_change_impl(line, re, to_str, global, res); text *substituted_line_txt = cstring_to_text(substituted_line.c_str()); lines.push_back(PointerGetDatum(substituted_line_txt)); pfree(line); total_res += res; } pcre2_code_free(re); TupleDesc record_desc = CreateTemplateTupleDesc(3); TupleDescInitEntry(record_desc, 0, "rows", VARCHAR2ARRAYOID, -1, 1); TupleDescInitEntry(record_desc, 1, "num_rows", NUMBEROID, -1, 0); TupleDescInitEntry(record_desc, 2, "partial_row", BOOLOID, -1, 0); record_desc = BlessTupleDesc(record_desc); Datum record_values[3]; bool record_isnull[3]; ArrayType *new_arr = construct_array(lines.data(), lines.size(), VARCHAR2OID, -1, false, TYPALIGN_INT); record_values[0] = PointerGetDatum(new_arr); record_isnull[0] = false; record_values[1] = DirectFunctionCall1(int4_numeric, Int32GetDatum(lines.size())); record_isnull[1] = false; record_values[2] = partial_row_datum; record_isnull[2] = false; HeapTuple record_tuple = heap_form_tuple(record_desc, record_values, record_isnull); TupleDesc desc; if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Invalid call context: cannot return record!"))); PG_RETURN_VOID(); } desc = BlessTupleDesc(desc); Datum values[2]; values[0] = DirectFunctionCall1(int4_numeric, Int32GetDatum(total_res)); values[1] = HeapTupleGetDatum(record_tuple); bool isnull[2] = {false, false}; HeapTuple tuple = heap_form_tuple(desc, values, isnull); PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); #endif /* USE_PCRE2 */ } /** * @brief This function or procedure searches and replaces a string ormulti_linedatatype. If multiple overlapping strings match the regular expression, this subprogram takes the longest match. * @details This function or procedure searches and replaces a string ormulti_linedatatype. If multiple overlapping strings match the regular expression, this subprogram takes the longest match. * * Parameters: * * @param MLINE (in/out) PL/SQL RECORD: The text to search in. This is aowa_text.multi_line datatype. The output value of this parameter is the altered string. * * @param FROM_STR (in) VARCHAR2: The regular expression to replace. * * @param TO_STR (in) VARCHAR2: The substitution pattern. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive, and whether or not changes are to be made globally. If "i" is specified, the search is case-insensitive. If "g" is specified, changes are made to all matches. Otherwise, the function stops after the first substitution is made. * * * @param MLINE (in/out) PL/SQL RECORD: The text to search in. This is aowa_text.multi_line datatype. The output value of this parameter is the altered string. (Возвращается через результат) * * * * @return record: Композитный тип (pl/sql record), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_change_ovr7(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.change еще не реализована."))); PG_RETURN_VOID(); #else HeapTupleHeader mline = PG_GETARG_HEAPTUPLEHEADER(0); bool isattnull; Datum rows_datum = GetAttributeByName(mline, "rows", &isattnull); Datum partial_row_datum = GetAttributeByName(mline, "partial_row", &isattnull); ArrayType *rows_arr = DatumGetArrayTypeP(rows_datum); Datum *rows; bool *row_nulls; int rows_len; deconstruct_array(rows_arr, VARCHAR2OID, -1, false, TYPALIGN_INT, &rows, &row_nulls, &rows_len); text *from_str_txt = PG_GETARG_TEXT_P(1); text *to_str_txt = PG_GETARG_TEXT_P(2); char *from_str = text_to_cstring(from_str_txt); char *to_str = text_to_cstring(to_str_txt); int error_number; PCRE2_SIZE error_offset; pcre2_code *re = pcre2_compile(reinterpret_cast(from_str), PCRE2_ZERO_TERMINATED, 0, &error_number, &error_offset, nullptr); if (re == nullptr) { PCRE2_UCHAR buffer[256]; pcre2_get_error_message(error_number, buffer, sizeof(buffer)); ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("PCRE2 compilation failed at offset %zu: %s", error_offset, buffer))); pfree(rows); pfree(row_nulls); PG_RETURN_NULL(); } std::vector lines; for (int i = 0; i < rows_len; i++) { int res; text *line_txt = DatumGetTextP(rows[i]); char *line = text_to_cstring(line_txt); std::string substituted_line = owa_pattern_change_impl(line, re, to_str, false, res); text *substituted_line_txt = cstring_to_text(substituted_line.c_str()); lines.push_back(PointerGetDatum(substituted_line_txt)); pfree(line); } pcre2_code_free(re); pfree(rows); pfree(row_nulls); TupleDesc record_desc = CreateTemplateTupleDesc(3); TupleDescInitEntry(record_desc, 0, "rows", VARCHAR2ARRAYOID, -1, 1); TupleDescInitEntry(record_desc, 1, "num_rows", NUMBEROID, -1, 0); TupleDescInitEntry(record_desc, 2, "partial_row", BOOLOID, -1, 0); record_desc = BlessTupleDesc(record_desc); Datum record_values[3]; bool record_isnull[3]; ArrayType *new_arr = construct_array(lines.data(), lines.size(), VARCHAR2OID, -1, false, TYPALIGN_INT); record_values[0] = PointerGetDatum(new_arr); record_isnull[0] = false; record_values[1] = DirectFunctionCall1(int4_numeric, Int32GetDatum(lines.size())); record_isnull[1] = false; record_values[2] = partial_row_datum; record_isnull[2] = false; HeapTuple record_tuple = heap_form_tuple(record_desc, record_values, record_isnull); TupleDesc desc; if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Invalid call context: cannot return record!"))); PG_RETURN_VOID(); } desc = BlessTupleDesc(desc); Datum values[1] = {HeapTupleGetDatum(record_tuple)}; bool isnull[1] = {false}; HeapTuple tuple = heap_form_tuple(desc, values, isnull); PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); #endif /* USE_PCRE2 */ } /** * @brief This function or procedure searches and replaces a string ormulti_linedatatype. If multiple overlapping strings match the regular expression, this subprogram takes the longest match. * @details This function or procedure searches and replaces a string ormulti_linedatatype. If multiple overlapping strings match the regular expression, this subprogram takes the longest match. * * Parameters: * * @param MLINE (in/out) PL/SQL RECORD: The text to search in. This is aowa_text.multi_line datatype. The output value of this parameter is the altered string. * * @param FROM_STR (in) VARCHAR2: The regular expression to replace. * * @param TO_STR (in) VARCHAR2: The substitution pattern. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive, and whether or not changes are to be made globally. If "i" is specified, the search is case-insensitive. If "g" is specified, changes are made to all matches. Otherwise, the function stops after the first substitution is made. * * * @param MLINE (in/out) PL/SQL RECORD: The text to search in. This is aowa_text.multi_line datatype. The output value of this parameter is the altered string. (Возвращается через результат) * * * * @return record: Композитный тип (pl/sql record), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_change_ovr8(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.change еще не реализована."))); PG_RETURN_VOID(); #else HeapTupleHeader mline = PG_GETARG_HEAPTUPLEHEADER(0); bool isattnull; Datum rows_datum = GetAttributeByName(mline, "rows", &isattnull); Datum partial_row_datum = GetAttributeByName(mline, "partial_row", &isattnull); ArrayType *rows_arr = DatumGetArrayTypeP(rows_datum); Datum *rows; bool *row_nulls; int rows_len; deconstruct_array(rows_arr, VARCHAR2OID, -1, false, TYPALIGN_INT, &rows, &row_nulls, &rows_len); text *from_str_txt = PG_GETARG_TEXT_P(1); text *to_str_txt = PG_GETARG_TEXT_P(2); char *from_str = text_to_cstring(from_str_txt); char *to_str = text_to_cstring(to_str_txt); bool global = false; bool caseless = false; if (!PG_ARGISNULL(3)) { text *flags_txt = PG_GETARG_TEXT_P(3); char *flags = text_to_cstring(flags_txt); for (char *flag = flags; *flag != '\0'; flag++) { switch(*flag) { case 'i': caseless = true; break; case 'g': global = true; break; default: continue; } } pfree(flags); } int error_number; PCRE2_SIZE error_offset; pcre2_code *re = pcre2_compile(reinterpret_cast(from_str), PCRE2_ZERO_TERMINATED, caseless ? PCRE2_CASELESS : 0, &error_number, &error_offset, nullptr); if (re == nullptr) { PCRE2_UCHAR buffer[256]; pcre2_get_error_message(error_number, buffer, sizeof(buffer)); ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("PCRE2 compilation failed at offset %zu: %s", error_offset, buffer))); pfree(rows); pfree(row_nulls); PG_RETURN_NULL(); } std::vector lines; for (int i = 0; i < rows_len; i++) { int res; text *line_txt = DatumGetTextP(rows[i]); char *line = text_to_cstring(line_txt); std::string substituted_line = owa_pattern_change_impl(line, re, to_str, global, res); text *substituted_line_txt = cstring_to_text(substituted_line.c_str()); lines.push_back(PointerGetDatum(substituted_line_txt)); pfree(line); } pcre2_code_free(re); pfree(rows); pfree(row_nulls); TupleDesc record_desc = CreateTemplateTupleDesc(3); TupleDescInitEntry(record_desc, 0, "rows", VARCHAR2ARRAYOID, -1, 1); TupleDescInitEntry(record_desc, 1, "num_rows", NUMBEROID, -1, 0); TupleDescInitEntry(record_desc, 2, "partial_row", BOOLOID, -1, 0); record_desc = BlessTupleDesc(record_desc); Datum record_values[3]; bool record_isnull[3]; ArrayType *new_arr = construct_array(lines.data(), lines.size(), VARCHAR2OID, -1, false, TYPALIGN_INT); record_values[0] = PointerGetDatum(new_arr); record_isnull[0] = false; record_values[1] = DirectFunctionCall1(int4_numeric, Int32GetDatum(lines.size())); record_isnull[1] = false; record_values[2] = partial_row_datum; record_isnull[2] = false; HeapTuple record_tuple = heap_form_tuple(record_desc, record_values, record_isnull); TupleDesc desc; if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Invalid call context: cannot return record!"))); PG_RETURN_VOID(); } desc = BlessTupleDesc(desc); Datum values[1] = {HeapTupleGetDatum(record_tuple)}; bool isnull[1] = {false}; HeapTuple tuple = heap_form_tuple(desc, values, isnull); PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); #endif /* USE_PCRE2 */ } /** * @brief This procedure converts a VARCHAR2 string into anOWA_PATTERN.PATTERN DATA TYPE. * @details This procedure converts a VARCHAR2 string into anOWA_PATTERN.PATTERN DATA TYPE. * * Parameters: * * @param ARG (in) VARCHAR2: The string to convert. * * @param PAT (in/out) PL/SQL TABLE: theOWA_PATTERN.PATTERN DATA TYPEinitialized witharg. * * * @param PAT (in/out) PL/SQL TABLE: theOWA_PATTERN.PATTERN DATA TYPEinitialized witharg. (Возвращается через результат) * * * * @return record: Композитный тип (pl/sql table), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_getpat(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.getpat еще не реализована."))); PG_RETURN_NULL(); #else int error_number; PCRE2_SIZE error_offset; char *arg; pcre2_code *re = nullptr; int32_t pat_handle; text *arg_txt = PG_GETARG_TEXT_P(0); if (PG_ARGISNULL(1)) { pat_handle = current_handle++; } else { Numeric pat_numeric = PG_GETARG_NUMERIC(1); pat_handle = numeric_get_int32(pat_numeric); patterns.erase(pat_handle); } arg = text_to_cstring(arg_txt); re = pcre2_compile(reinterpret_cast(arg), PCRE2_ZERO_TERMINATED, 0, &error_number, &error_offset, nullptr); if (re == nullptr) { PCRE2_UCHAR buffer[256]; pcre2_get_error_message(error_number, buffer, sizeof(buffer)); ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("PCRE2 compilation failed at offset %zu: %s", error_offset, buffer))); PG_RETURN_NULL(); } patterns.emplace(pat_handle, re); PG_RETURN_DATUM(DirectFunctionCall1(int4_numeric, Int32GetDatum(pat_handle))); #endif /* USE_PCRE2 */ } /** * @brief This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * @details This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * * Parameters: * * @param LINE (in) VARCHAR2: The line to search in. * * @param PAT (in) VARCHAR2: The pattern to match. This is either aVARCHAR2or aOWA_PATTERN.PATTERN DATA TYPE. It is a pattern, the output value of this parameter is the pattern matched. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive. If the value of this parameter is "i", the search is case-insensitive. Otherwise the search is case-sensitive. * * * * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_match_ovr1(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.match еще не реализована."))); PG_RETURN_VOID(); #else std::vector backrefs; bool result; pcre2_code *re; int error_number; PCRE2_SIZE error_offset; text *line_txt = PG_GETARG_TEXT_P(0); text *pattern_txt = PG_GETARG_TEXT_P(1); char *line = text_to_cstring(line_txt); char *pattern = text_to_cstring(pattern_txt); re = pcre2_compile(reinterpret_cast(pattern), PCRE2_ZERO_TERMINATED, 0, &error_number, &error_offset, nullptr); if (re == nullptr) { PCRE2_UCHAR buffer[256]; pcre2_get_error_message(error_number, buffer, sizeof(buffer)); ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("PCRE2 compilation failed at offset %zu: %s", error_offset, buffer))); PG_RETURN_NULL(); } result = owa_pattern_match_impl(line, re, backrefs); pcre2_code_free(re); pfree(line); pfree(pattern); PG_RETURN_BOOL(result); #endif /* USE_PCRE2 */ } /** * @brief This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * @details This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * * Parameters: * * @param LINE (in) VARCHAR2: The line to search in. * * @param PAT (in) VARCHAR2: The pattern to match. This is either aVARCHAR2or aOWA_PATTERN.PATTERN DATA TYPE. It is a pattern, the output value of this parameter is the pattern matched. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive. If the value of this parameter is "i", the search is case-insensitive. Otherwise the search is case-sensitive. * * * * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_match_ovr2(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.match еще не реализована."))); PG_RETURN_VOID(); #else std::vector backrefs; bool result; pcre2_code *re; int error_number; PCRE2_SIZE error_offset; text *line_txt = PG_GETARG_TEXT_P(0); text *pattern_txt = PG_GETARG_TEXT_P(1); char *line = text_to_cstring(line_txt); char *pattern = text_to_cstring(pattern_txt); int caseless_match = 0; if (!PG_ARGISNULL(3)) { text *flags_txt = PG_GETARG_TEXT_P(3); char *flags = text_to_cstring(flags_txt); caseless_match = *flags == 'i' ? PCRE2_CASELESS : 0; pfree(flags); } re = pcre2_compile(reinterpret_cast(pattern), PCRE2_ZERO_TERMINATED, caseless_match, &error_number, &error_offset, nullptr); if (re == nullptr) { PCRE2_UCHAR buffer[256]; pcre2_get_error_message(error_number, buffer, sizeof(buffer)); ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("PCRE2 compilation failed at offset %zu: %s", error_offset, buffer))); PG_RETURN_NULL(); } result = owa_pattern_match_impl(line, re, backrefs); pcre2_code_free(re); pfree(line); pfree(pattern); PG_RETURN_BOOL(result); #endif /* USE_PCRE2 */ } /** * @brief This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * @details This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * * Parameters: * * @param LINE (in) VARCHAR2: The line to search in. * * @param PAT (in/out) PL/SQL TABLE: The pattern to match. This is either aVARCHAR2or aOWA_PATTERN.PATTERN DATA TYPE. It is a pattern, the output value of this parameter is the pattern matched. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive. If the value of this parameter is "i", the search is case-insensitive. Otherwise the search is case-sensitive. * * * @param PAT (in/out) PL/SQL TABLE: The pattern to match. This is either aVARCHAR2or aOWA_PATTERN.PATTERN DATA TYPE. It is a pattern, the output value of this parameter is the pattern matched. (Возвращается через результат) * * * * @return record: Композитный тип (), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_match_ovr3(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.match еще не реализована."))); PG_RETURN_VOID(); #else std::vector backrefs; bool result; pcre2_code *re; text *line_txt = PG_GETARG_TEXT_P(0); int pattern_handle = PG_GETARG_INT32(1); char *line = text_to_cstring(line_txt); auto pattern_it = patterns.find(pattern_handle); if (pattern_it == patterns.end()) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Invalid pattern handle: %d", pattern_handle))); pfree(line); PG_RETURN_NULL(); } re = pattern_it->second.re; result = owa_pattern_match_impl(line, re, backrefs); pfree(line); TupleDesc desc; if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Invalid call context: cannot return record!"))); PG_RETURN_VOID(); } desc = BlessTupleDesc(desc); Datum values[2]; values[0] = BoolGetDatum(result); values[1] = Int32GetDatum(pattern_handle); bool isnull[2] = {false, false}; HeapTuple tuple = heap_form_tuple(desc, values, isnull); PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); #endif /* USE_PCRE2 */ } /** * @brief This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * @details This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * * Parameters: * * @param LINE (in) VARCHAR2: The line to search in. * * @param PAT (in/out) PL/SQL TABLE: The pattern to match. This is either aVARCHAR2or aOWA_PATTERN.PATTERN DATA TYPE. It is a pattern, the output value of this parameter is the pattern matched. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive. If the value of this parameter is "i", the search is case-insensitive. Otherwise the search is case-sensitive. * * * @param PAT (in/out) PL/SQL TABLE: The pattern to match. This is either aVARCHAR2or aOWA_PATTERN.PATTERN DATA TYPE. It is a pattern, the output value of this parameter is the pattern matched. (Возвращается через результат) * * * * @return record: Композитный тип (), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_match_ovr4(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.match еще не реализована."))); PG_RETURN_VOID(); #else std::vector backrefs; bool result; pcre2_code *re; text *line_txt = PG_GETARG_TEXT_P(0); int pattern_handle = PG_GETARG_INT32(1); char *line = text_to_cstring(line_txt); auto pattern_it = patterns.find(pattern_handle); if (pattern_it == patterns.end()) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Invalid pattern handle: %d", pattern_handle))); pfree(line); PG_RETURN_NULL(); } re = pattern_it->second.re; /* ignore flags parameter since caselessness is specified in pcre2_code already */ result = owa_pattern_match_impl(line, re, backrefs); pfree(line); TupleDesc desc; if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Invalid call context: cannot return record!"))); PG_RETURN_VOID(); } desc = BlessTupleDesc(desc); Datum values[2]; values[0] = BoolGetDatum(result); values[1] = Int32GetDatum(pattern_handle); bool isnull[2] = {false, false}; HeapTuple tuple = heap_form_tuple(desc, values, isnull); PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); #endif /* USE_PCRE2 */ } /** * @brief This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * @details This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * * Parameters: * * @param LINE (in) VARCHAR2: The line to search in. * * @param PAT (in) VARCHAR2: The pattern to match. This is either aVARCHAR2or aOWA_PATTERN.PATTERN DATA TYPE. It is a pattern, the output value of this parameter is the pattern matched. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive. If the value of this parameter is "i", the search is case-insensitive. Otherwise the search is case-sensitive. * * * @param BACKREFS (out) PL/SQL TABLE: The text that is matched. Each token that is matched is placed in a cell in theOWA_TEXT.VC_ARR DATA TYPEPL/SQL table.This parameter is a row_listthat holds each string in the target that was matched by a sequence of tokens in the regular expression. (Возвращается через результат) * * * * @return record: Композитный тип (), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_match_ovr5(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.match еще не реализована."))); PG_RETURN_VOID(); #else std::vector backrefs; bool result; pcre2_code *re; int error_number; PCRE2_SIZE error_offset; text *line_txt = PG_GETARG_TEXT_P(0); text *pattern_txt = PG_GETARG_TEXT_P(1); char *line = text_to_cstring(line_txt); char *pattern = text_to_cstring(pattern_txt); re = pcre2_compile(reinterpret_cast(pattern), PCRE2_ZERO_TERMINATED, 0, &error_number, &error_offset, nullptr); if (re == nullptr) { PCRE2_UCHAR buffer[256]; pcre2_get_error_message(error_number, buffer, sizeof(buffer)); ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("PCRE2 compilation failed at offset %zu: %s", error_offset, buffer))); PG_RETURN_NULL(); } result = owa_pattern_match_impl(line, re, backrefs); pcre2_code_free(re); pfree(line); pfree(pattern); TupleDesc desc; if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Invalid call context: cannot return record!"))); PG_RETURN_VOID(); } desc = BlessTupleDesc(desc); ArrayType *arr = construct_array(backrefs.data(), backrefs.size(), VARCHAR2OID, -1, false, TYPALIGN_INT); Datum values[2]; values[0] = BoolGetDatum(result); values[1] = PointerGetDatum(arr); bool isnull[2] = {false, false}; HeapTuple tuple = heap_form_tuple(desc, values, isnull); PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); #endif /* USE_PCRE2 */ } /** * @brief This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * @details This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * * Parameters: * * @param LINE (in) VARCHAR2: The line to search in. * * @param PAT (in) VARCHAR2: The pattern to match. This is either aVARCHAR2or aOWA_PATTERN.PATTERN DATA TYPE. It is a pattern, the output value of this parameter is the pattern matched. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive. If the value of this parameter is "i", the search is case-insensitive. Otherwise the search is case-sensitive. * * * @param BACKREFS (out) PL/SQL TABLE: The text that is matched. Each token that is matched is placed in a cell in theOWA_TEXT.VC_ARR DATA TYPEPL/SQL table.This parameter is a row_listthat holds each string in the target that was matched by a sequence of tokens in the regular expression. (Возвращается через результат) * * * * @return record: Композитный тип (), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_match_ovr6(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.match еще не реализована."))); PG_RETURN_VOID(); #else std::vector backrefs; bool result; pcre2_code *re; int error_number; PCRE2_SIZE error_offset; text *line_txt = PG_GETARG_TEXT_P(0); text *pattern_txt = PG_GETARG_TEXT_P(1); text *flags_txt; int caseless_match = 0; if (PG_ARGISNULL(3)) { caseless_match = 0; } else { flags_txt = PG_GETARG_TEXT_P(3); char *flags = text_to_cstring(flags_txt); caseless_match = *flags == 'i' ? PCRE2_CASELESS : 0; pfree(flags); } char *line = text_to_cstring(line_txt); char *pattern = text_to_cstring(pattern_txt); re = pcre2_compile(reinterpret_cast(pattern), PCRE2_ZERO_TERMINATED, caseless_match, &error_number, &error_offset, nullptr); if (re == nullptr) { PCRE2_UCHAR buffer[256]; pcre2_get_error_message(error_number, buffer, sizeof(buffer)); ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("PCRE2 compilation failed at offset %zu: %s", error_offset, buffer))); PG_RETURN_NULL(); } result = owa_pattern_match_impl(line, re, backrefs); pcre2_code_free(re); pfree(line); pfree(pattern); TupleDesc desc; if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Invalid call context: cannot return record!"))); PG_RETURN_VOID(); } desc = BlessTupleDesc(desc); ArrayType *arr = construct_array(backrefs.data(), backrefs.size(), VARCHAR2OID, -1, false, TYPALIGN_INT); Datum values[2]; values[0] = BoolGetDatum(result); values[1] = PointerGetDatum(arr); bool isnull[2] = {false, false}; HeapTuple tuple = heap_form_tuple(desc, values, isnull); PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); #endif /* USE_PCRE2 */ } /** * @brief This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * @details This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * * Parameters: * * @param LINE (in) VARCHAR2: The line to search in. * * @param PAT (in/out) PL/SQL TABLE: The pattern to match. This is either aVARCHAR2or aOWA_PATTERN.PATTERN DATA TYPE. It is a pattern, the output value of this parameter is the pattern matched. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive. If the value of this parameter is "i", the search is case-insensitive. Otherwise the search is case-sensitive. * * * @param PAT (in/out) PL/SQL TABLE: The pattern to match. This is either aVARCHAR2or aOWA_PATTERN.PATTERN DATA TYPE. It is a pattern, the output value of this parameter is the pattern matched. (Возвращается через результат) * * @param BACKREFS (out) PL/SQL TABLE: The text that is matched. Each token that is matched is placed in a cell in theOWA_TEXT.VC_ARR DATA TYPEPL/SQL table.This parameter is a row_listthat holds each string in the target that was matched by a sequence of tokens in the regular expression. (Возвращается через результат) * * * * @return record: Композитный тип (), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_match_ovr7(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.match еще не реализована."))); PG_RETURN_VOID(); #else std::vector backrefs; bool result; pcre2_code *re; text *line_txt = PG_GETARG_TEXT_P(0); int pattern_handle = PG_GETARG_INT32(1); char *line = text_to_cstring(line_txt); auto pattern_it = patterns.find(pattern_handle); if (pattern_it == patterns.end()) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Invalid pattern handle: %d", pattern_handle))); pfree(line); PG_RETURN_NULL(); } re = pattern_it->second.re; result = owa_pattern_match_impl(line, re, backrefs); pfree(line); TupleDesc desc; if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Invalid call context: cannot return record!"))); PG_RETURN_VOID(); } desc = BlessTupleDesc(desc); ArrayType *arr = construct_array(backrefs.data(), backrefs.size(), VARCHAR2OID, -1, false, TYPALIGN_INT); Datum values[3]; values[0] = BoolGetDatum(result); values[1] = Int32GetDatum(pattern_handle); values[2] = PointerGetDatum(arr); bool isnull[3] = {false, false, false}; HeapTuple tuple = heap_form_tuple(desc, values, isnull); PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); #endif /* USE_PCRE2 */ } /** * @brief This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * @details This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * * Parameters: * * @param LINE (in) VARCHAR2: The line to search in. * * @param PAT (in/out) PL/SQL TABLE: The pattern to match. This is either aVARCHAR2or aOWA_PATTERN.PATTERN DATA TYPE. It is a pattern, the output value of this parameter is the pattern matched. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive. If the value of this parameter is "i", the search is case-insensitive. Otherwise the search is case-sensitive. * * * @param PAT (in/out) PL/SQL TABLE: The pattern to match. This is either aVARCHAR2or aOWA_PATTERN.PATTERN DATA TYPE. It is a pattern, the output value of this parameter is the pattern matched. (Возвращается через результат) * * @param BACKREFS (out) PL/SQL TABLE: The text that is matched. Each token that is matched is placed in a cell in theOWA_TEXT.VC_ARR DATA TYPEPL/SQL table.This parameter is a row_listthat holds each string in the target that was matched by a sequence of tokens in the regular expression. (Возвращается через результат) * * * * @return record: Композитный тип (), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_match_ovr8(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.match еще не реализована."))); PG_RETURN_VOID(); #else std::vector backrefs; bool result; pcre2_code *re; text *line_txt = PG_GETARG_TEXT_P(0); int pattern_handle = PG_GETARG_INT32(1); char *line = text_to_cstring(line_txt); auto pattern_it = patterns.find(pattern_handle); if (pattern_it == patterns.end()) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Invalid pattern handle: %d", pattern_handle))); pfree(line); PG_RETURN_NULL(); } /* ignore flags parameter since caselessness is specified in pcre2_code already */ re = pattern_it->second.re; result = owa_pattern_match_impl(line, re, backrefs); pfree(line); TupleDesc desc; if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Invalid call context: cannot return record!"))); PG_RETURN_VOID(); } desc = BlessTupleDesc(desc); ArrayType *arr = construct_array(backrefs.data(), backrefs.size(), VARCHAR2OID, -1, false, TYPALIGN_INT); Datum values[3]; values[0] = BoolGetDatum(result); values[1] = Int32GetDatum(pattern_handle); values[2] = PointerGetDatum(arr); bool isnull[3] = {false, false, false}; HeapTuple tuple = heap_form_tuple(desc, values, isnull); PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); #endif /* USE_PCRE2 */ } /** * @brief This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * @details This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * * Parameters: * * @param MLINE (in) PL/SQL RECORD: The text to search in. This is aowa_text.multi_line datatype.. * * @param PAT (in) VARCHAR2: The pattern to match. This is either aVARCHAR2or aOWA_PATTERN.PATTERN DATA TYPE. It is a pattern, the output value of this parameter is the pattern matched. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive. If the value of this parameter is "i", the search is case-insensitive. Otherwise the search is case-sensitive. * * * @param RLIST (out) PL/SQL RECORD: An output parameter containing a list of matches. (Возвращается через результат) * * * * @return record: Композитный тип (), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_match_ovr9(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.match еще не реализована."))); PG_RETURN_VOID(); #else std::vector backrefs; bool isattnull; pcre2_code *re; HeapTupleHeader mline = PG_GETARG_HEAPTUPLEHEADER(0); Datum rows_datum = GetAttributeByName(mline, "rows", &isattnull); ArrayType *rows_arr = DatumGetArrayTypeP(rows_datum); Datum *rows; bool *row_nulls; int rows_len; deconstruct_array(rows_arr, VARCHAR2OID, -1, false, TYPALIGN_INT, &rows, &row_nulls, &rows_len); text *pattern_txt = PG_GETARG_TEXT_P(1); char *pattern = text_to_cstring(pattern_txt); int error_number; PCRE2_SIZE error_offset; re = pcre2_compile(reinterpret_cast(pattern), PCRE2_ZERO_TERMINATED, 0, &error_number, &error_offset, nullptr); if (re == nullptr) { PCRE2_UCHAR buffer[256]; pcre2_get_error_message(error_number, buffer, sizeof(buffer)); ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("PCRE2 compilation failed at offset %zu: %s", error_offset, buffer))); pfree(rows); pfree(row_nulls); PG_RETURN_NULL(); } std::vector matches; for (int i = 0; i < rows_len; i++) { text *line_txt = DatumGetTextP(rows[i]); char *line = text_to_cstring(line_txt); int res = owa_pattern_amatch_impl(line, 0, re, backrefs); matches.push_back(DirectFunctionCall1(int4_numeric, Int32GetDatum(res))); pfree(line); } pcre2_code_free(re); pfree(pattern); pfree(rows); pfree(row_nulls); TupleDesc record_desc = CreateTemplateTupleDesc(3); TupleDescInitEntry(record_desc, 0, "rows", NUMBERARRAYOID, -1, 1); TupleDescInitEntry(record_desc, 1, "num_rows", NUMBEROID, -1, 0); record_desc = BlessTupleDesc(record_desc); ArrayType *arr = construct_array(matches.data(), matches.size(), NUMBERARRAYOID, -1, false, TYPALIGN_INT); Datum record_values[2]; bool record_isnull[2]; record_values[0] = PointerGetDatum(arr); record_isnull[0] = false; record_values[1] = DirectFunctionCall1(int4_numeric, Int32GetDatum(matches.size())); record_isnull[1] = false; HeapTuple record_tuple = heap_form_tuple(record_desc, record_values, record_isnull); TupleDesc desc; if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Invalid call context: cannot return record!"))); PG_RETURN_VOID(); } desc = BlessTupleDesc(desc); Datum values[2]; values[0] = BoolGetDatum(!matches.empty()); values[1] = HeapTupleGetDatum(record_tuple); bool isnull[2] = {false, false}; HeapTuple tuple = heap_form_tuple(desc, values, isnull); PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); #endif } /** * @brief This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * @details This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * * Parameters: * * @param MLINE (in) PL/SQL RECORD: The text to search in. This is aowa_text.multi_line datatype.. * * @param PAT (in) VARCHAR2: The pattern to match. This is either aVARCHAR2or aOWA_PATTERN.PATTERN DATA TYPE. It is a pattern, the output value of this parameter is the pattern matched. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive. If the value of this parameter is "i", the search is case-insensitive. Otherwise the search is case-sensitive. * * * @param RLIST (out) PL/SQL RECORD: An output parameter containing a list of matches. (Возвращается через результат) * * * * @return record: Композитный тип (), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_match_ovr10(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.match еще не реализована."))); PG_RETURN_VOID(); #else std::vector backrefs; bool isattnull; pcre2_code *re; HeapTupleHeader mline = PG_GETARG_HEAPTUPLEHEADER(0); Datum rows_datum = GetAttributeByName(mline, "rows", &isattnull); ArrayType *rows_arr = DatumGetArrayTypeP(rows_datum); Datum *rows; bool *row_nulls; int rows_len; deconstruct_array(rows_arr, VARCHAR2OID, -1, false, TYPALIGN_INT, &rows, &row_nulls, &rows_len); text *pattern_txt = PG_GETARG_TEXT_P(1); char *pattern = text_to_cstring(pattern_txt); int caseless_match = 0; if (!PG_ARGISNULL(3)) { text *flags_txt = PG_GETARG_TEXT_P(3); char *flags = text_to_cstring(flags_txt); caseless_match = *flags == 'i' ? PCRE2_CASELESS : 0; pfree(flags); } int error_number; PCRE2_SIZE error_offset; re = pcre2_compile(reinterpret_cast(pattern), PCRE2_ZERO_TERMINATED, caseless_match, &error_number, &error_offset, nullptr); if (re == nullptr) { PCRE2_UCHAR buffer[256]; pcre2_get_error_message(error_number, buffer, sizeof(buffer)); ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("PCRE2 compilation failed at offset %zu: %s", error_offset, buffer))); pfree(rows); pfree(row_nulls); PG_RETURN_NULL(); } std::vector matches; for (int i = 0; i < rows_len; i++) { text *line_txt = DatumGetTextP(rows[i]); char *line = text_to_cstring(line_txt); int res = owa_pattern_amatch_impl(line, 0, re, backrefs); matches.push_back(DirectFunctionCall1(int4_numeric, Int32GetDatum(res))); pfree(line); } pcre2_code_free(re); pfree(pattern); pfree(rows); pfree(row_nulls); TupleDesc record_desc = CreateTemplateTupleDesc(3); TupleDescInitEntry(record_desc, 0, "rows", NUMBERARRAYOID, -1, 1); TupleDescInitEntry(record_desc, 1, "num_rows", NUMBEROID, -1, 0); record_desc = BlessTupleDesc(record_desc); ArrayType *arr = construct_array(matches.data(), matches.size(), NUMBERARRAYOID, -1, false, TYPALIGN_INT); Datum record_values[2]; bool record_isnull[2]; record_values[0] = PointerGetDatum(arr); record_isnull[0] = false; record_values[1] = DirectFunctionCall1(int4_numeric, Int32GetDatum(matches.size())); record_isnull[1] = false; HeapTuple record_tuple = heap_form_tuple(record_desc, record_values, record_isnull); TupleDesc desc; if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Invalid call context: cannot return record!"))); PG_RETURN_VOID(); } desc = BlessTupleDesc(desc); Datum values[2]; values[0] = BoolGetDatum(!matches.empty()); values[1] = HeapTupleGetDatum(record_tuple); bool isnull[2] = {false, false}; HeapTuple tuple = heap_form_tuple(desc, values, isnull); PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); #endif } /** * @brief This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * @details This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * * Parameters: * * @param MLINE (in) PL/SQL RECORD: The text to search in. This is aowa_text.multi_line datatype.. * * @param PAT (in/out) PL/SQL TABLE: The pattern to match. This is either aVARCHAR2or aOWA_PATTERN.PATTERN DATA TYPE. It is a pattern, the output value of this parameter is the pattern matched. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive. If the value of this parameter is "i", the search is case-insensitive. Otherwise the search is case-sensitive. * * * @param PAT (in/out) PL/SQL TABLE: The pattern to match. This is either aVARCHAR2or aOWA_PATTERN.PATTERN DATA TYPE. It is a pattern, the output value of this parameter is the pattern matched. (Возвращается через результат) * * @param RLIST (out) PL/SQL RECORD: An output parameter containing a list of matches. (Возвращается через результат) * * * * @return record: Композитный тип (), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_match_ovr11(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.match еще не реализована."))); PG_RETURN_VOID(); #else std::vector backrefs; bool isattnull; pcre2_code *re; HeapTupleHeader mline = PG_GETARG_HEAPTUPLEHEADER(0); Datum rows_datum = GetAttributeByName(mline, "rows", &isattnull); ArrayType *rows_arr = DatumGetArrayTypeP(rows_datum); Datum *rows; bool *row_nulls; int rows_len; deconstruct_array(rows_arr, VARCHAR2OID, -1, false, TYPALIGN_INT, &rows, &row_nulls, &rows_len); int32_t pattern_handle = PG_GETARG_INT32(1); auto pattern_it = patterns.find(pattern_handle); if (pattern_it == patterns.end()) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Invalid pattern handle: %d", pattern_handle))); pfree(rows); pfree(row_nulls); PG_RETURN_NULL(); } re = pattern_it->second.re; std::vector matches; for (int i = 0; i < rows_len; i++) { text *line_txt = DatumGetTextP(rows[i]); char *line = text_to_cstring(line_txt); int res = owa_pattern_amatch_impl(line, 0, re, backrefs); matches.push_back(DirectFunctionCall1(int4_numeric, Int32GetDatum(res))); pfree(line); } pfree(rows); pfree(row_nulls); TupleDesc record_desc = CreateTemplateTupleDesc(3); TupleDescInitEntry(record_desc, 0, "rows", NUMBERARRAYOID, -1, 1); TupleDescInitEntry(record_desc, 1, "num_rows", NUMBEROID, -1, 0); record_desc = BlessTupleDesc(record_desc); ArrayType *arr = construct_array(matches.data(), matches.size(), NUMBERARRAYOID, -1, false, TYPALIGN_INT); Datum record_values[2]; bool record_isnull[2]; record_values[0] = PointerGetDatum(arr); record_isnull[0] = false; record_values[1] = DirectFunctionCall1(int4_numeric, Int32GetDatum(matches.size())); record_isnull[1] = false; HeapTuple record_tuple = heap_form_tuple(record_desc, record_values, record_isnull); TupleDesc desc; if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Invalid call context: cannot return record!"))); PG_RETURN_VOID(); } desc = BlessTupleDesc(desc); Datum values[2]; values[0] = BoolGetDatum(!matches.empty()); values[1] = HeapTupleGetDatum(record_tuple); bool isnull[2] = {false, false}; HeapTuple tuple = heap_form_tuple(desc, values, isnull); PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); #endif } /** * @brief This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * @details This function determines if a string contains the specified pattern. The pattern can contain regular expressions. If multiple overlapping strings can match the regular expression, this function takes the longest match. * * Parameters: * * @param MLINE (in) PL/SQL RECORD: The text to search in. This is aowa_text.multi_line datatype.. * * @param PAT (in/out) PL/SQL TABLE: The pattern to match. This is either aVARCHAR2or aOWA_PATTERN.PATTERN DATA TYPE. It is a pattern, the output value of this parameter is the pattern matched. * * @param FLAGS (in) VARCHAR2: Whether or not the search is case-sensitive. If the value of this parameter is "i", the search is case-insensitive. Otherwise the search is case-sensitive. * * * @param PAT (in/out) PL/SQL TABLE: The pattern to match. This is either aVARCHAR2or aOWA_PATTERN.PATTERN DATA TYPE. It is a pattern, the output value of this parameter is the pattern matched. (Возвращается через результат) * * @param RLIST (out) PL/SQL RECORD: An output parameter containing a list of matches. (Возвращается через результат) * * * * @return record: Композитный тип (), содержащий OUT/INOUT параметры. * * * @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/OWA_PATTERN.html */ Datum owa_pattern_match_ovr12(PG_FUNCTION_ARGS) { #ifndef USE_PCRE2 ereport(NOTICE, (errmsg("Функция owa_pattern.match еще не реализована."))); PG_RETURN_VOID(); #else std::vector backrefs; bool isattnull; pcre2_code *re; HeapTupleHeader mline = PG_GETARG_HEAPTUPLEHEADER(0); Datum rows_datum = GetAttributeByName(mline, "rows", &isattnull); ArrayType *rows_arr = DatumGetArrayTypeP(rows_datum); Datum *rows; bool *row_nulls; int rows_len; deconstruct_array(rows_arr, VARCHAR2OID, -1, false, TYPALIGN_INT, &rows, &row_nulls, &rows_len); int32_t pattern_handle = PG_GETARG_INT32(1); auto pattern_it = patterns.find(pattern_handle); if (pattern_it == patterns.end()) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Invalid pattern handle: %d", pattern_handle))); pfree(rows); pfree(row_nulls); PG_RETURN_NULL(); } re = pattern_it->second.re; /* ignore flags parameter since caselessness is specified in pcre2_code already */ std::vector matches; for (int i = 0; i < rows_len; i++) { text *line_txt = DatumGetTextP(rows[i]); char *line = text_to_cstring(line_txt); int res = owa_pattern_amatch_impl(line, 0, re, backrefs); matches.push_back(DirectFunctionCall1(int4_numeric, Int32GetDatum(res))); pfree(line); } pfree(rows); pfree(row_nulls); TupleDesc record_desc = CreateTemplateTupleDesc(3); TupleDescInitEntry(record_desc, 0, "rows", NUMBERARRAYOID, -1, 1); TupleDescInitEntry(record_desc, 1, "num_rows", NUMBEROID, -1, 0); record_desc = BlessTupleDesc(record_desc); ArrayType *arr = construct_array(matches.data(), matches.size(), NUMBERARRAYOID, -1, false, TYPALIGN_INT); Datum record_values[2]; bool record_isnull[2]; record_values[0] = PointerGetDatum(arr); record_isnull[0] = false; record_values[1] = DirectFunctionCall1(int4_numeric, Int32GetDatum(matches.size())); record_isnull[1] = false; HeapTuple record_tuple = heap_form_tuple(record_desc, record_values, record_isnull); TupleDesc desc; if (get_call_result_type(fcinfo, nullptr, &desc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Invalid call context: cannot return record!"))); PG_RETURN_VOID(); } desc = BlessTupleDesc(desc); Datum values[2]; values[0] = BoolGetDatum(!matches.empty()); values[1] = HeapTupleGetDatum(record_tuple); bool isnull[2] = {false, false}; HeapTuple tuple = heap_form_tuple(desc, values, isnull); PG_RETURN_DATUM(HeapTupleGetDatum(tuple)); #endif } Datum owa_pattern_match2(PG_FUNCTION_ARGS) { Datum line = PG_GETARG_DATUM(0); Datum pat = PG_GETARG_DATUM(1); int32_t magic = PG_GETARG_INT32(2); if (magic != 0x13371337) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Invalid magic value!"))); PG_RETURN_NULL(); } return DirectFunctionCall2(owa_pattern_match_ovr1, line, pat); } } // extern "C"