113 lines
2.8 KiB
C++
113 lines
2.8 KiB
C++
#include "matador/sql/dialect.hpp"
|
|
#include "matador/sql/column.hpp"
|
|
|
|
#include "matador/utils/string.hpp"
|
|
|
|
namespace matador::sql {
|
|
|
|
dialect::dialect(const dialect::token_to_string_map &token_replace_map, const dialect::data_type_to_string_map &data_type_replace_map)
|
|
{
|
|
for (const auto &token : token_replace_map) {
|
|
tokens_[token.first] = token.second;
|
|
}
|
|
|
|
for (const auto &data_type : data_type_replace_map) {
|
|
data_types_[data_type.first] = data_type.second;
|
|
}
|
|
}
|
|
|
|
dialect::dialect(const dialect::data_type_to_string_map &data_type_replace_map)
|
|
: dialect({}, data_type_replace_map)
|
|
{}
|
|
|
|
dialect::dialect(const dialect::token_to_string_map &token_replace_map)
|
|
: dialect(token_replace_map, {})
|
|
{}
|
|
|
|
const std::string& dialect::token_at(dialect::token_t token) const
|
|
{
|
|
return tokens_.at(token);
|
|
}
|
|
|
|
const std::string &dialect::data_type_at(data_type_t type) const
|
|
{
|
|
return data_types_.at(type);
|
|
}
|
|
|
|
std::string dialect::prepare_identifier(const column &col) const
|
|
{
|
|
std::string result(col.name());
|
|
if (!col.is_function()) {
|
|
escape_quotes_in_identifier(result);
|
|
quote_identifier(result);
|
|
} else {
|
|
result = sql_func_map_.at(col.function()) + "(" + result + ")";
|
|
}
|
|
if (!col.alias().empty()) {
|
|
result += " AS " + col.alias();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
std::string dialect::prepare_literal(const std::string &str) const
|
|
{
|
|
std::string result(str);
|
|
escape_quotes_in_literals(result);
|
|
return result;
|
|
}
|
|
|
|
void dialect::quote_identifier(std::string &str) const
|
|
{
|
|
str.insert(0, token_at(token_t::START_QUOTE));
|
|
str += token_at(token_t::END_QUOTE);
|
|
}
|
|
|
|
void dialect::escape_quotes_in_identifier(std::string &str) const
|
|
{
|
|
const std::string open_char(token_at(token_t::START_QUOTE));
|
|
std::string close_char(token_at(token_t::END_QUOTE));
|
|
if (identifier_escape_type() == escape_identifier_t::ESCAPE_CLOSING_BRACKET) {
|
|
utils::replace_all(str, close_char, close_char + close_char);
|
|
} else {
|
|
utils::replace_all(str, open_char, open_char + open_char);
|
|
}
|
|
}
|
|
|
|
void dialect::escape_quotes_in_literals(std::string &str) const
|
|
{
|
|
const std::string single_quote(token_at(token_t::STRING_QUOTE));
|
|
const std::string double_quote(token_at(token_t::STRING_QUOTE) + token_at(token_t::STRING_QUOTE));
|
|
utils::replace_all(str, single_quote, double_quote);
|
|
}
|
|
|
|
dialect::escape_identifier_t dialect::identifier_escape_type() const
|
|
{
|
|
return dialect::escape_identifier_t::ESCAPE_BOTH_SAME;
|
|
}
|
|
|
|
std::string dialect::next_placeholder() const
|
|
{
|
|
return "?";
|
|
}
|
|
|
|
void dialect::add_host_var(const std::string &host_var)
|
|
{
|
|
host_vars_.emplace_back(host_var);
|
|
}
|
|
|
|
void dialect::add_column(const std::string &column)
|
|
{
|
|
columns_.emplace_back(column);
|
|
}
|
|
|
|
const std::vector<std::string> &dialect::host_vars() const
|
|
{
|
|
return host_vars_;
|
|
}
|
|
|
|
const std::vector<std::string> &dialect::columns() const
|
|
{
|
|
return columns_;
|
|
}
|
|
|
|
} |