Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2026-06-19 0cfc87bd32822e5ba22d8c2b1a26fa60f63ad7fa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
 
#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<Datum> simple_patterns;
    std::vector<Datum> 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);
}
 
}