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