91 lines
2.3 KiB
C++
91 lines
2.3 KiB
C++
#include "matador/sql/dialect.hpp"
|
|
|
|
#include "matador/utils/string.hpp"
|
|
|
|
namespace matador::sql {
|
|
|
|
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;
|
|
if (!col.is_function()) {
|
|
if (!col.table.empty()) {
|
|
result = prepare_identifier_string(col.table) + ".";
|
|
}
|
|
result += prepare_identifier_string(col.name);
|
|
} else {
|
|
result = sql_func_map_.at(col.function_) + "(" + col.name + ")";
|
|
}
|
|
if (!col.alias.empty()) {
|
|
result += " AS " + col.alias;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
std::string dialect::prepare_identifier_string(const std::string &col) const
|
|
{
|
|
auto parts = utils::split(col, '.');
|
|
|
|
for (auto &part : parts) {
|
|
escape_quotes_in_identifier(part);
|
|
quote_identifier(part);
|
|
}
|
|
return utils::join(parts, ".");
|
|
}
|
|
|
|
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 std::vector<std::string> &bind_vars) const
|
|
{
|
|
return placeholder_func_(bind_vars.size());
|
|
}
|
|
|
|
std::string dialect::default_schema_name() const
|
|
{
|
|
return default_schema_name_;
|
|
}
|
|
|
|
} |