#include "dbms_tns.hpp"
|
|
extern "C"
|
{
|
#include "utils/builtins.h"
|
#include "utils/elog.h"
|
#include "utils/guc.h"
|
#include "miscadmin.h"
|
#include "utils/varlena.h"
|
#include "varatt.h"
|
} // extern "C"
|
|
#include <cctype>
|
#include <algorithm>
|
#include <fstream>
|
#include <iterator>
|
#include <unordered_map>
|
#include <string>
|
#include <string_view>
|
#include <vector>
|
#include <unistd.h>
|
|
namespace
|
{
|
enum class SqlnetSource
|
{
|
Tnsnames,
|
Ezconnect
|
};
|
|
enum class SqlnetParseStatus
|
{
|
MissingConfig,
|
ParsedOk,
|
Malformed
|
};
|
|
struct SqlnetParseResult
|
{
|
SqlnetParseStatus status;
|
std::vector<SqlnetSource> sources;
|
};
|
|
enum class TnsnamesParseStatus
|
{
|
MissingConfig,
|
ParsedOk,
|
Malformed
|
};
|
|
struct TnsnamesParseResult
|
{
|
TnsnamesParseStatus status;
|
std::unordered_map<std::string, std::string> aliases;
|
};
|
|
constexpr std::string_view kSqlnetDirectoryPathParam = "NAMES.DIRECTORY_PATH";
|
constexpr std::string_view kSqlnetConfigFile = "sqlnet.ora";
|
constexpr std::string_view kTnsnamesConfigFile = "tnsnames.ora";
|
constexpr std::string_view kTnsnamesToken = "TNSNAMES";
|
constexpr std::string_view kEzconnectToken = "EZCONNECT";
|
constexpr std::string_view kCidProgram = "rudb";
|
|
static std::string lower_copy(std::string_view input);
|
static bool case_insensitive_equals(std::string_view lhs, std::string_view rhs);
|
static size_t skip_spaces(const std::string& input, size_t pos);
|
static bool match_param_name(const std::string& input, size_t pos, std::string_view needle);
|
static std::string tnsnames_config_path();
|
|
/*
|
* Внутренняя схема разбора Oracle Net configuration для DBMS_TNS.
|
*
|
* Здесь держим private parser/resolver для `dbms_tns_resolve_tnsname(text)`.
|
* Он читает только `sqlnet.ora` и `tnsnames.ora` из каталога
|
* `dbms_tns.tns_admin`, не делает сетевых проверок и не эмулирует полный
|
* Oracle Net stack.
|
*
|
* 1. `sqlnet.ora`
|
* Parser ищет только `NAMES.DIRECTORY_PATH`. Из списка sources
|
* используются только `TNSNAMES` и `EZCONNECT`, остальные значения
|
* игнорируются. Если синтаксис `NAMES.DIRECTORY_PATH` повреждён, resolver
|
* возвращает SQL NULL.
|
*
|
* 2. `tnsnames.ora`
|
* Parser читает top-level записи вида
|
*
|
* ALIAS[, ALIAS2] = (DESCRIPTION=...)
|
*
|
* Alias сравниваются без учёта регистра. Если один alias встречается
|
* несколько раз, используется последняя запись. Descriptor собирается как
|
* сбалансированный блок скобок и сохраняется как pass-through text:
|
* сетевые параметры и порядок `ADDRESS` / `DESCRIPTION` не
|
* интерпретируются.
|
*
|
* 3. Scanner descriptor-а
|
* Сканер считает баланс круглых скобок, учитывает quoted text и
|
* нормализует whitespace вне quoted values. Символ `#` внутри quoted value
|
* не считается началом комментария. Незакрытые quotes или
|
* несбалансированные скобки считаются malformed input и приводят к SQL
|
* NULL.
|
*
|
* 4. Easy Connect
|
* Поддерживаются только простые формы
|
*
|
* //host/service
|
* //host:port/service
|
* host/service
|
* host:port/service
|
*
|
* Parser не выполняет DNS lookup, не открывает TCP-соединение, не
|
* проверяет listener/service availability, не нормализует localhost и не
|
* добавляет CID / descriptor decoration вне текущего локального правила.
|
*/
|
|
static inline bool is_space(unsigned char ch)
|
{
|
return std::isspace(static_cast<int>(ch)) != 0;
|
}
|
|
static inline bool is_token_boundary(char ch)
|
{
|
return ch == '\0' ||
|
(!std::isalnum(static_cast<unsigned char>(ch)) && ch != '_' && ch != '.');
|
}
|
|
static inline bool is_quote_char(char ch)
|
{
|
return ch == '\'' || ch == '"';
|
}
|
|
// Удаляет # line comments, но сохраняет # внутри quoted values.
|
static std::string strip_line_comments(const std::string& input)
|
{
|
std::string output;
|
output.reserve(input.size());
|
bool in_comment = false;
|
bool in_quote = false;
|
char quote_char = '\0';
|
|
for (char ch : input)
|
{
|
if (in_comment)
|
{
|
if (ch == '\n' || ch == '\r')
|
{
|
in_comment = false;
|
output.push_back(ch);
|
}
|
continue;
|
}
|
|
if (in_quote)
|
{
|
output.push_back(ch);
|
if (ch == quote_char)
|
{
|
in_quote = false;
|
quote_char = '\0';
|
}
|
continue;
|
}
|
|
if (is_quote_char(ch))
|
{
|
in_quote = true;
|
quote_char = ch;
|
output.push_back(ch);
|
continue;
|
}
|
|
if (ch == '#')
|
{
|
in_comment = true;
|
continue;
|
}
|
|
output.push_back(ch);
|
}
|
|
return output;
|
}
|
|
static std::string lower_copy(std::string_view input)
|
{
|
std::string output;
|
output.reserve(input.size());
|
|
for (char ch : input)
|
output.push_back(static_cast<char>(std::tolower(static_cast<unsigned char>(ch))));
|
|
return output;
|
}
|
|
static bool cid_decoration_enabled()
|
{
|
// CID включаем только по явному session marker в selfcheck, чтобы HOST/USER
|
// не меняли обычный regression output.
|
const char* appname = GetConfigOptionByName("application_name", NULL, false);
|
return appname != nullptr && case_insensitive_equals(appname, "dbms_tns_cid");
|
}
|
|
static std::string cid_host_name()
|
{
|
char buffer[256] = {0};
|
if (gethostname(buffer, sizeof(buffer)) != 0 || buffer[0] == '\0')
|
return "unknown";
|
|
buffer[sizeof(buffer) - 1] = '\0';
|
return buffer;
|
}
|
|
static std::string cid_user_name()
|
{
|
const char* user = GetUserNameFromId(GetUserId(), false);
|
if (user == nullptr || user[0] == '\0')
|
return "unknown";
|
|
return user;
|
}
|
|
static std::string build_cid_block()
|
{
|
std::string cid;
|
cid.reserve(64);
|
cid.append("(CID=(PROGRAM=");
|
cid.append(kCidProgram.data(), kCidProgram.size());
|
cid.append(")(HOST=");
|
cid.append(cid_host_name());
|
cid.append(")(USER=");
|
cid.append(cid_user_name());
|
cid.append("))");
|
return cid;
|
}
|
|
static bool find_matching_paren(const std::string& input, size_t open_pos, size_t& close_pos)
|
{
|
if (open_pos >= input.size() || input[open_pos] != '(')
|
return false;
|
|
int depth = 0;
|
bool in_quote = false;
|
char quote_char = '\0';
|
|
for (size_t pos = open_pos; pos < input.size(); ++pos)
|
{
|
const char ch = input[pos];
|
|
if (in_quote)
|
{
|
if (ch == quote_char)
|
{
|
in_quote = false;
|
quote_char = '\0';
|
}
|
continue;
|
}
|
|
if (is_quote_char(ch))
|
{
|
in_quote = true;
|
quote_char = ch;
|
continue;
|
}
|
|
if (ch == '(')
|
{
|
++depth;
|
}
|
else if (ch == ')')
|
{
|
--depth;
|
if (depth < 0)
|
return false;
|
|
if (depth == 0)
|
{
|
close_pos = pos;
|
return true;
|
}
|
}
|
}
|
|
return false;
|
}
|
|
static bool descriptor_has_top_level_cid(const std::string& input, size_t open_pos, size_t close_pos)
|
{
|
(void) close_pos;
|
bool in_quote = false;
|
char quote_char = '\0';
|
|
for (size_t pos = open_pos + 1; pos < input.size(); ++pos)
|
{
|
const char ch = input[pos];
|
|
if (in_quote)
|
{
|
if (ch == quote_char)
|
{
|
in_quote = false;
|
quote_char = '\0';
|
}
|
continue;
|
}
|
|
if (is_quote_char(ch))
|
{
|
in_quote = true;
|
quote_char = ch;
|
continue;
|
}
|
|
if (ch == '(' && pos + 5 <= input.size() &&
|
case_insensitive_equals(std::string_view(input).substr(pos + 1, 4), "CID="))
|
{
|
return true;
|
}
|
}
|
|
return false;
|
}
|
|
static bool find_connect_data_group(const std::string& descriptor, size_t& open_pos, size_t& close_pos)
|
{
|
bool in_quote = false;
|
char quote_char = '\0';
|
|
for (size_t pos = 0; pos < descriptor.size(); ++pos)
|
{
|
const char ch = descriptor[pos];
|
|
if (in_quote)
|
{
|
if (ch == quote_char)
|
{
|
in_quote = false;
|
quote_char = '\0';
|
}
|
continue;
|
}
|
|
if (is_quote_char(ch))
|
{
|
in_quote = true;
|
quote_char = ch;
|
continue;
|
}
|
|
if (!match_param_name(descriptor, pos, "CONNECT_DATA"))
|
continue;
|
|
size_t after = pos + std::string_view("CONNECT_DATA").size();
|
after = skip_spaces(descriptor, after);
|
if (after >= descriptor.size() || descriptor[after] != '=')
|
continue;
|
|
++after;
|
after = skip_spaces(descriptor, after);
|
if (after >= descriptor.size() || descriptor[after] != '(')
|
continue;
|
|
open_pos = after;
|
return find_matching_paren(descriptor, open_pos, close_pos);
|
}
|
|
return false;
|
}
|
|
static std::string decorate_descriptor_with_cid(const std::string& descriptor)
|
{
|
if (!cid_decoration_enabled())
|
return descriptor;
|
|
size_t open_pos = 0;
|
size_t close_pos = 0;
|
if (!find_connect_data_group(descriptor, open_pos, close_pos))
|
return descriptor;
|
|
if (descriptor_has_top_level_cid(descriptor, open_pos, close_pos))
|
return descriptor;
|
|
const std::string cid = build_cid_block();
|
std::string decorated;
|
decorated.reserve(descriptor.size() + cid.size());
|
decorated.append(descriptor, 0, close_pos);
|
decorated.append(cid);
|
decorated.append(descriptor, close_pos, std::string::npos);
|
return decorated;
|
}
|
|
static bool case_insensitive_equals(std::string_view lhs, std::string_view rhs)
|
{
|
if (lhs.size() != rhs.size())
|
return false;
|
|
for (size_t i = 0; i < lhs.size(); ++i)
|
{
|
const unsigned char left = static_cast<unsigned char>(lhs[i]);
|
const unsigned char right = static_cast<unsigned char>(rhs[i]);
|
if (std::tolower(left) != std::tolower(right))
|
return false;
|
}
|
|
return true;
|
}
|
|
// Формирует descriptor для поддержанных простых Easy Connect строк.
|
static bool try_parse_easy_connect_descriptor(std::string_view input,
|
std::string& descriptor)
|
{
|
descriptor.clear();
|
|
if (input.empty())
|
return false;
|
|
for (char ch : input)
|
{
|
if (is_space(static_cast<unsigned char>(ch)))
|
return false;
|
}
|
|
if (input.size() >= 2 && input[0] == '/' && input[1] == '/')
|
input.remove_prefix(2);
|
|
if (input.empty())
|
return false;
|
|
const size_t slash_pos = input.find('/');
|
if (slash_pos == std::string_view::npos)
|
return false;
|
|
const std::string_view host_port = input.substr(0, slash_pos);
|
const std::string_view service = input.substr(slash_pos + 1);
|
if (host_port.empty() || service.find('/') != std::string_view::npos)
|
return false;
|
|
std::string_view host = host_port;
|
std::string_view port = "1521";
|
const size_t colon_pos = host_port.find(':');
|
if (colon_pos != std::string_view::npos)
|
{
|
if (host_port.find(':', colon_pos + 1) != std::string_view::npos)
|
return false;
|
|
host = host_port.substr(0, colon_pos);
|
port = host_port.substr(colon_pos + 1);
|
if (host.empty() || port.empty())
|
return false;
|
}
|
|
if (host.empty())
|
return false;
|
|
descriptor.reserve(host.size() + port.size() + service.size() + 64);
|
descriptor.append("(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=");
|
descriptor.append(host.data(), host.size());
|
descriptor.append(")(PORT=");
|
descriptor.append(port.data(), port.size());
|
descriptor.append("))(CONNECT_DATA=(SERVICE_NAME=");
|
descriptor.append(service.data(), service.size());
|
descriptor.append(")))");
|
return true;
|
}
|
|
static size_t skip_spaces(const std::string& input, size_t pos)
|
{
|
while (pos < input.size() && is_space(static_cast<unsigned char>(input[pos])))
|
++pos;
|
|
return pos;
|
}
|
|
static bool match_param_name(const std::string& input, size_t pos, std::string_view needle)
|
{
|
if (pos > input.size() || input.size() - pos < needle.size())
|
return false;
|
|
if (pos > 0 && !is_token_boundary(input[pos - 1]))
|
return false;
|
|
if (pos + needle.size() < input.size() && !is_token_boundary(input[pos + needle.size()]))
|
return false;
|
|
return case_insensitive_equals(std::string_view(input).substr(pos, needle.size()), needle);
|
}
|
|
// Разбирает значение NAMES.DIRECTORY_PATH=(...).
|
static bool collect_directory_path(const std::string& input, size_t name_pos,
|
SqlnetParseResult& result)
|
{
|
size_t pos = name_pos + kSqlnetDirectoryPathParam.size();
|
pos = skip_spaces(input, pos);
|
|
if (pos >= input.size() || input[pos] != '=')
|
return false;
|
|
++pos;
|
pos = skip_spaces(input, pos);
|
if (pos >= input.size() || input[pos] != '(')
|
return false;
|
|
++pos;
|
bool saw_closing_paren = false;
|
|
while (pos < input.size())
|
{
|
pos = skip_spaces(input, pos);
|
if (pos >= input.size())
|
return false;
|
|
if (input[pos] == ')')
|
{
|
saw_closing_paren = true;
|
++pos;
|
break;
|
}
|
|
if (input[pos] == ',')
|
{
|
++pos;
|
continue;
|
}
|
|
const size_t token_start = pos;
|
while (pos < input.size())
|
{
|
const char ch = input[pos];
|
if (is_space(static_cast<unsigned char>(ch)) || ch == ',' || ch == ')')
|
break;
|
++pos;
|
}
|
|
if (pos == token_start)
|
return false;
|
|
const std::string_view token(input.data() + token_start, pos - token_start);
|
if (case_insensitive_equals(token, kTnsnamesToken))
|
result.sources.push_back(SqlnetSource::Tnsnames);
|
else if (case_insensitive_equals(token, kEzconnectToken))
|
result.sources.push_back(SqlnetSource::Ezconnect);
|
|
pos = skip_spaces(input, pos);
|
if (pos < input.size() && input[pos] == ',')
|
{
|
++pos;
|
continue;
|
}
|
|
if (pos < input.size() && input[pos] == ')')
|
{
|
saw_closing_paren = true;
|
++pos;
|
break;
|
}
|
|
if (pos < input.size())
|
{
|
const char next = input[pos];
|
if (!std::isalpha(static_cast<unsigned char>(next)))
|
return false;
|
}
|
}
|
|
if (!saw_closing_paren)
|
return false;
|
|
result.status = SqlnetParseStatus::ParsedOk;
|
return true;
|
}
|
|
// Разбирает левую часть tnsnames.ora entry: ALIAS[, ALIAS2].
|
static bool parse_tnsnames_aliases(std::string_view aliases_text,
|
std::vector<std::string>& aliases)
|
{
|
aliases.clear();
|
|
size_t pos = 0;
|
while (pos < aliases_text.size())
|
{
|
size_t begin = pos;
|
while (begin < aliases_text.size() && is_space(static_cast<unsigned char>(aliases_text[begin])))
|
++begin;
|
|
size_t end = begin;
|
while (end < aliases_text.size() && aliases_text[end] != ',')
|
++end;
|
|
while (end > begin && is_space(static_cast<unsigned char>(aliases_text[end - 1])))
|
--end;
|
|
if (end == begin)
|
return false;
|
|
aliases.push_back(lower_copy(aliases_text.substr(begin, end - begin)));
|
|
if (end >= aliases_text.size())
|
break;
|
|
pos = end + 1;
|
}
|
|
return !aliases.empty();
|
}
|
|
// Собирает descriptor как balanced parentheses block.
|
static bool collect_tnsnames_descriptor(const std::string& input, size_t pos,
|
std::string& descriptor, size_t& next_pos)
|
{
|
descriptor.clear();
|
|
if (pos >= input.size() || input[pos] != '(')
|
return false;
|
|
int depth = 0;
|
bool in_quote = false;
|
char quote_char = '\0';
|
while (pos < input.size())
|
{
|
const char ch = input[pos];
|
++pos;
|
|
if (!in_quote && is_space(static_cast<unsigned char>(ch)))
|
continue;
|
|
descriptor.push_back(ch);
|
if (in_quote)
|
{
|
if (ch == quote_char)
|
{
|
in_quote = false;
|
quote_char = '\0';
|
}
|
continue;
|
}
|
|
if (is_quote_char(ch))
|
{
|
in_quote = true;
|
quote_char = ch;
|
continue;
|
}
|
|
if (ch == '(')
|
{
|
++depth;
|
}
|
else if (ch == ')')
|
{
|
--depth;
|
if (depth < 0)
|
return false;
|
|
if (depth == 0)
|
{
|
if (in_quote)
|
return false;
|
|
next_pos = pos;
|
return true;
|
}
|
}
|
}
|
|
return !in_quote && depth == 0;
|
}
|
|
// Разбирает одну top-level запись tnsnames.ora.
|
static bool parse_tnsnames_entry(const std::string& input, size_t entry_start,
|
TnsnamesParseResult& result, size_t& next_pos)
|
{
|
const size_t eq_pos = input.find('=', entry_start);
|
if (eq_pos == std::string::npos)
|
return false;
|
|
size_t aliases_end = eq_pos;
|
while (aliases_end > entry_start &&
|
is_space(static_cast<unsigned char>(input[aliases_end - 1])))
|
--aliases_end;
|
|
const std::string_view aliases_text(input.data() + entry_start, aliases_end - entry_start);
|
std::vector<std::string> aliases;
|
if (!parse_tnsnames_aliases(aliases_text, aliases))
|
return false;
|
|
size_t pos = skip_spaces(input, eq_pos + 1);
|
std::string descriptor;
|
if (!collect_tnsnames_descriptor(input, pos, descriptor, next_pos))
|
return false;
|
|
for (const std::string& alias : aliases)
|
result.aliases[alias] = descriptor;
|
|
result.status = TnsnamesParseStatus::ParsedOk;
|
return true;
|
}
|
|
// Возвращает порядок поддержанных naming sources из sqlnet.ora.
|
static SqlnetParseResult parse_sqlnet_ora(const std::string& input)
|
{
|
SqlnetParseResult result{SqlnetParseStatus::ParsedOk, {}};
|
const std::string cleaned = strip_line_comments(input);
|
const std::string_view needle = kSqlnetDirectoryPathParam;
|
|
for (size_t pos = 0; pos < cleaned.size(); ++pos)
|
{
|
if (!match_param_name(cleaned, pos, needle))
|
continue;
|
|
if (!collect_directory_path(cleaned, pos, result))
|
{
|
result.status = SqlnetParseStatus::Malformed;
|
result.sources.clear();
|
}
|
|
return result;
|
}
|
|
return result;
|
}
|
|
// Возвращает map нормализованных aliases к descriptor text.
|
static TnsnamesParseResult parse_tnsnames_ora(const std::string& input)
|
{
|
TnsnamesParseResult result{TnsnamesParseStatus::ParsedOk, {}};
|
const std::string cleaned = strip_line_comments(input);
|
|
for (size_t pos = 0; pos < cleaned.size(); )
|
{
|
pos = skip_spaces(cleaned, pos);
|
if (pos >= cleaned.size())
|
break;
|
|
size_t next_pos = pos;
|
if (!parse_tnsnames_entry(cleaned, pos, result, next_pos))
|
{
|
result.status = TnsnamesParseStatus::Malformed;
|
result.aliases.clear();
|
return result;
|
}
|
|
pos = next_pos;
|
}
|
|
return result;
|
}
|
|
static SqlnetParseResult read_sqlnet_config()
|
{
|
const char* tns_admin = GetConfigOptionByName("dbms_tns.tns_admin", NULL, false);
|
if (tns_admin == NULL || tns_admin[0] == '\0')
|
return {SqlnetParseStatus::MissingConfig, {}};
|
|
std::string config_path(tns_admin);
|
if (!config_path.empty() && config_path.back() != '/' && config_path.back() != '\\')
|
config_path.push_back('/');
|
config_path.append(kSqlnetConfigFile);
|
|
std::ifstream file(config_path);
|
if (!file.is_open())
|
return {SqlnetParseStatus::MissingConfig, {}};
|
|
std::string contents((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
return parse_sqlnet_ora(contents);
|
}
|
|
static TnsnamesParseResult read_tnsnames_config()
|
{
|
const std::string config_path = tnsnames_config_path();
|
if (config_path.empty())
|
return {TnsnamesParseStatus::MissingConfig, {}};
|
|
std::ifstream file(config_path);
|
if (!file.is_open())
|
return {TnsnamesParseStatus::MissingConfig, {}};
|
|
std::string contents((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
return parse_tnsnames_ora(contents);
|
}
|
|
static std::string tnsnames_config_path()
|
{
|
const char* tns_admin = GetConfigOptionByName("dbms_tns.tns_admin", NULL, false);
|
if (tns_admin == NULL || tns_admin[0] == '\0')
|
return {};
|
|
std::string config_path(tns_admin);
|
if (!config_path.empty() && config_path.back() != '/' && config_path.back() != '\\')
|
config_path.push_back('/');
|
config_path.append(kTnsnamesConfigFile);
|
return config_path;
|
}
|
} // namespace
|
|
extern "C"
|
{
|
|
PG_FUNCTION_INFO_V1(dbms_tns_resolve_tnsname);
|
PG_FUNCTION_INFO_V1(dbms_tns_has_tnsnames_config);
|
PG_FUNCTION_INFO_V1(dbms_tns_get_version);
|
|
/// @brief Версия RUDB пакета DBMS_TNS (Oracle DBMS_TNS пакет)
|
#define RUDB_DBMS_TNS_VERSION "1.0.0"
|
|
/*
|
* @qdb_func: dbms_tns_resolve_tnsname(text)
|
*/
|
Datum dbms_tns_resolve_tnsname(PG_FUNCTION_ARGS)
|
{
|
if (!superuser())
|
ereport(ERROR,
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
errmsg("must be superuser to call %s()",
|
"dbms_tns_resolve_tnsname")));
|
|
if (PG_ARGISNULL(0))
|
PG_RETURN_NULL();
|
|
const text *tns_name = PG_GETARG_TEXT_PP(0);
|
if (VARSIZE_ANY_EXHDR(tns_name) == 0)
|
PG_RETURN_NULL();
|
|
const SqlnetParseResult sqlnet = read_sqlnet_config();
|
if (sqlnet.status != SqlnetParseStatus::ParsedOk)
|
PG_RETURN_NULL();
|
|
const std::string_view input_text(VARDATA_ANY(tns_name), VARSIZE_ANY_EXHDR(tns_name));
|
bool tnsnames_loaded = false;
|
TnsnamesParseResult tnsnames;
|
|
for (const SqlnetSource source : sqlnet.sources)
|
{
|
switch (source)
|
{
|
case SqlnetSource::Ezconnect:
|
{
|
std::string descriptor;
|
if (try_parse_easy_connect_descriptor(input_text, descriptor))
|
{
|
descriptor = decorate_descriptor_with_cid(descriptor);
|
PG_RETURN_TEXT_P(cstring_to_text(descriptor.c_str()));
|
}
|
break;
|
}
|
|
case SqlnetSource::Tnsnames:
|
{
|
if (!tnsnames_loaded)
|
{
|
const bool has_tnsnames = DatumGetBool(
|
DirectFunctionCall0(dbms_tns_has_tnsnames_config));
|
if (!has_tnsnames)
|
break;
|
|
tnsnames = read_tnsnames_config();
|
if (tnsnames.status != TnsnamesParseStatus::ParsedOk)
|
break;
|
|
tnsnames_loaded = true;
|
}
|
|
const std::string alias = lower_copy(input_text);
|
const auto found = tnsnames.aliases.find(alias);
|
if (found != tnsnames.aliases.end())
|
{
|
std::string descriptor = decorate_descriptor_with_cid(found->second);
|
PG_RETURN_TEXT_P(cstring_to_text(descriptor.c_str()));
|
}
|
break;
|
}
|
}
|
}
|
|
PG_RETURN_NULL();
|
}
|
|
/*
|
* @qdb_func: dbms_tns_has_tnsnames_config()
|
*/
|
Datum dbms_tns_has_tnsnames_config(PG_FUNCTION_ARGS)
|
{
|
const std::string config_path = tnsnames_config_path();
|
if (config_path.empty())
|
PG_RETURN_BOOL(false);
|
|
std::ifstream file(config_path);
|
PG_RETURN_BOOL(file.is_open());
|
}
|
|
Datum dbms_tns_get_version(PG_FUNCTION_ARGS)
|
{
|
PG_RETURN_TEXT_P(cstring_to_text(RUDB_DBMS_TNS_VERSION));
|
}
|
|
} // extern "C"
|