#include #include #include #include #include "dbms/owa_text.h" #include "dbms/owa_pattern.h" extern "C" { #include "catalog/pg_collation.h" #include "mb/pg_wchar.h" #include "utils/builtins.h" } static const char *SPECIAL_CHARS = " @*()+-/=\\<>;:\"|&?{}[]'\t\n\r\f"; extern "C" { Datum owa_match_match(PG_FUNCTION_ARGS); Datum owa_match_match2(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(owa_match_match); PG_FUNCTION_INFO_V1(owa_match_match2); Datum owa_match_match(PG_FUNCTION_ARGS) { if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); } text *p_string = PG_GETARG_TEXT_P(0); std::string str; str.resize(VARSIZE_ANY_EXHDR(p_string)); memcpy(str.data(), VARDATA_ANY(p_string), str.size()); bool p_use_special_chars = PG_ARGISNULL(3) ? true : PG_GETARG_BOOL(3); if (p_use_special_chars) { auto it = std::find_if( str.begin(), str.end(), [](char c) { return std::strchr(SPECIAL_CHARS, c) != nullptr; }); if (it != str.end()) { PG_RETURN_BOOL(true); } } std::vector simple_patterns; std::vector complex_patterns; if (!PG_ARGISNULL(1)) { ArrayType *p_simple_patterns = PG_GETARG_ARRAYTYPE_P(1); Datum *simple_patterns_elems; bool *simple_pattern_nulls; int simple_patterns_len; deconstruct_array(p_simple_patterns, VARCHAR2OID, -1, false, TYPALIGN_INT, &simple_patterns_elems, &simple_pattern_nulls, &simple_patterns_len); simple_patterns.assign(simple_patterns_elems, simple_patterns_elems + simple_patterns_len); pfree(simple_patterns_elems); pfree(simple_pattern_nulls); } if (!PG_ARGISNULL(2)) { ArrayType *p_complex_patterns = PG_GETARG_ARRAYTYPE_P(2); Datum *complex_patterns_elems; bool *complex_patterns_nulls; int complex_patterns_len; deconstruct_array(p_complex_patterns, VARCHAR2OID, -1, false, TYPALIGN_INT, &complex_patterns_elems, &complex_patterns_nulls, &complex_patterns_len); complex_patterns.assign(complex_patterns_elems, complex_patterns_elems + complex_patterns_len); pfree(complex_patterns_elems); pfree(complex_patterns_nulls); } for (Datum &pattern: simple_patterns) { Datum res_datum = DirectFunctionCall2Coll(texticlike, PG_GET_COLLATION(), PointerGetDatum(p_string), pattern); if (DatumGetBool(res_datum)) { return true; } } text *match_param = cstring_to_text("i"); for (Datum &pattern: complex_patterns) { Datum res_datum = DirectFunctionCall3Coll(owa_pattern_match_ovr2, PG_GET_COLLATION(), PointerGetDatum(p_string), pattern, PointerGetDatum(match_param)); if (DatumGetBool(res_datum)) { pfree(match_param); return true; } } pfree(match_param); return false; } Datum owa_match_match2(PG_FUNCTION_ARGS) { Datum string = PG_GETARG_DATUM(0); Datum simple_patterns = PG_GETARG_DATUM(1); Datum complex_patterns = PG_GETARG_DATUM(2); Datum use_special_chars = PG_GETARG_DATUM(3); int32_t magic = PG_GETARG_INT32(4); if (magic != 0x13371337) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Invalid magic value!"))); } return DirectFunctionCall4Coll(owa_match_match, PG_GET_COLLATION(), string, simple_patterns, complex_patterns, use_special_chars); } }