Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
#include "dbms_logrep_exp.hpp"
 
#include <string>
 
extern "C"
{
#include "access/htup_details.h"
#include "catalog/pg_type.h"
#include "utils/builtins.h"
#include "utils/numeric.h"
}
 
namespace
{
std::string
text_to_string(text *value)
{
    if (value == nullptr)
        return std::string();
    return std::string(VARDATA_ANY(value), VARSIZE_ANY_EXHDR(value));
}
 
std::string
oracle_logrep_version_number_text(const std::string &version)
{
    std::string out;
    bool decimal_point_seen = false;
 
    for (const char ch : version)
    {
        if (ch == '.')
        {
            if (!decimal_point_seen)
            {
                out.push_back(ch);
                decimal_point_seen = true;
            }
            continue;
        }
 
        out.push_back(ch);
    }
 
    if (decimal_point_seen)
    {
        while (!out.empty() && out.back() == '0')
            out.pop_back();
        if (!out.empty() && out.back() == '.')
            out.pop_back();
    }
 
    return out;
}
} // namespace
 
extern "C"
{
PG_FUNCTION_INFO_V1(dbms_logrep_exp_version_var2_2_number);
 
Datum
dbms_logrep_exp_version_var2_2_number(PG_FUNCTION_ARGS)
{
    if (PG_ARGISNULL(0))
        PG_RETURN_NULL();
 
    std::string normalized = oracle_logrep_version_number_text(text_to_string(PG_GETARG_TEXT_PP(0)));
    if (normalized.empty())
        PG_RETURN_NULL();
 
    PG_RETURN_DATUM(DirectFunctionCall3(numeric_in,
                                        CStringGetDatum(normalized.c_str()),
                                        ObjectIdGetDatum(InvalidOid),
                                        Int32GetDatum(-1)));
}
} // extern "C"