147 lines
3.9 KiB
C++
147 lines
3.9 KiB
C++
#include "matador/sql/dialect.hpp"
|
|
|
|
#include "matador/sql/interface/connection_impl.hpp"
|
|
|
|
#include "matador/utils/string.hpp"
|
|
|
|
namespace matador::sql {
|
|
|
|
const std::string& dialect::token_at(const dialect_token token) const
|
|
{
|
|
return tokens_.at(token);
|
|
}
|
|
|
|
const std::string &dialect::data_type_at(const utils::basic_type 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_->name.empty()) {
|
|
result = prepare_identifier_string(col.table_->has_alias() ? col.table_->alias : col.table_->name) + ".";
|
|
}
|
|
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_condition(const column& col) const
|
|
{
|
|
std::string result;
|
|
if (!col.is_function()) {
|
|
// if (!col.alias.empty()) {
|
|
// result = col.alias;
|
|
// } else {
|
|
if (!col.table_->name.empty()) {
|
|
result = prepare_identifier_string(col.table_->has_alias() ? col.table_->alias : col.table_->name) + ".";
|
|
}
|
|
result += prepare_identifier_string(col.name);
|
|
// }
|
|
} else {
|
|
result = sql_func_map_.at(col.function_) + "(" + col.name + ")";
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
const std::string& dialect::to_string(const bool val) const
|
|
{
|
|
return bool_strings_[static_cast<int>(val)];
|
|
}
|
|
|
|
std::string dialect::to_sql_string(const utils::value &val) const {
|
|
if (val.is_null()) {
|
|
return "NULL";
|
|
}
|
|
if (val.is_string()) {
|
|
return token_at(dialect_token::BEGIN_STRING_DATA) + val.str() + token_at(dialect_token::BEGIN_STRING_DATA);
|
|
}
|
|
|
|
return val.str();
|
|
}
|
|
|
|
void dialect::bool_strings(const std::string &true_string, const std::string &false_string)
|
|
{
|
|
bool_strings_[0] = false_string;
|
|
bool_strings_[1] = true_string;
|
|
}
|
|
|
|
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(dialect_token::START_QUOTE));
|
|
str += token_at(dialect_token::END_QUOTE);
|
|
}
|
|
|
|
void dialect::escape_quotes_in_identifier(std::string &str) const
|
|
{
|
|
const std::string open_char(token_at(dialect_token::START_QUOTE));
|
|
const std::string close_char(token_at(dialect_token::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(dialect_token::STRING_QUOTE));
|
|
const std::string double_quote(token_at(dialect_token::STRING_QUOTE) + token_at(dialect_token::STRING_QUOTE));
|
|
utils::replace_all(str, single_quote, double_quote);
|
|
}
|
|
|
|
dialect::escape_identifier_t dialect::identifier_escape_type() const {
|
|
return identifier_escape_type_;
|
|
}
|
|
|
|
void dialect::identifier_escape_type(escape_identifier_t escape_identifier) {
|
|
identifier_escape_type_ = escape_identifier;
|
|
}
|
|
|
|
std::string dialect::next_placeholder(const std::vector<std::string> &bind_vars) const
|
|
{
|
|
return placeholder_func_(bind_vars.size());
|
|
}
|
|
|
|
std::string dialect::to_escaped_string(const utils::blob &value, const connection_impl *conn) const
|
|
{
|
|
if (conn != nullptr) {
|
|
return conn->to_escaped_string(value);
|
|
}
|
|
|
|
return to_escaped_string_func_(value);
|
|
}
|
|
|
|
std::string dialect::default_schema_name() const
|
|
{
|
|
return default_schema_name_;
|
|
}
|
|
|
|
}
|