155 lines
4.3 KiB
C++
155 lines
4.3 KiB
C++
#include "matador/query/table_column.hpp"
|
|
|
|
#include "matador/query/table.hpp"
|
|
|
|
#include <stdexcept>
|
|
#include <utility>
|
|
|
|
namespace matador::query {
|
|
|
|
table_column operator ""_col(const char *name, const size_t len) {
|
|
const std::string str(name, len);
|
|
const auto pos = str.find('.');
|
|
if (pos == std::string::npos) {
|
|
return table_column{str};
|
|
}
|
|
|
|
if (str.find('.', pos + 1) != std::string::npos) {
|
|
throw std::invalid_argument("Invalid column name: multiple dots found");
|
|
}
|
|
|
|
return table_column{str.substr(pos + 1)};
|
|
}
|
|
|
|
table_column::table_column(const char *name)
|
|
: table_column(std::string(name))
|
|
{}
|
|
|
|
table_column::table_column(const std::string& name)
|
|
: table_column(name, "") {}
|
|
|
|
table_column::table_column(const std::string& name, const std::string& alias)
|
|
: table_column(nullptr, name, alias, utils::basic_type::Unknown, {}, sql::sql_function_t::None)
|
|
{}
|
|
|
|
table_column::table_column(const sql::sql_function_t func, const std::string& name)
|
|
: table_column(nullptr, name, "", utils::basic_type::Unknown, {}, func)
|
|
{}
|
|
|
|
table_column::table_column(const class table* tab, const std::string& name)
|
|
: table_column(tab, name, "", utils::basic_type::Unknown, {}, sql::sql_function_t::None)
|
|
{}
|
|
|
|
table_column::table_column(const class table* tab, const std::string& name, const std::string& alias)
|
|
: table_column(tab, name, alias, utils::basic_type::Unknown, {}, sql::sql_function_t::None)
|
|
{}
|
|
|
|
table_column::table_column(const class table* tab, const std::string& name, const utils::basic_type type, const utils::field_attributes& attributes)
|
|
: table_column(tab, name, "", type, attributes, sql::sql_function_t::None)
|
|
{}
|
|
|
|
table_column::table_column(const class table* tab,
|
|
const std::string& name,
|
|
std::string alias,
|
|
const utils::basic_type type,
|
|
const utils::field_attributes &attributes,
|
|
const sql::sql_function_t func)
|
|
: table_(tab)
|
|
, column_name_(name)
|
|
, canonical_name_(build_canonical_name(table_, name))
|
|
, alias_(std::move(alias))
|
|
, type_(type)
|
|
, attributes_(attributes)
|
|
, function_(func) {}
|
|
|
|
table_column & table_column::operator=(const table_column &other) {
|
|
if (this == &other) {
|
|
return *this;
|
|
}
|
|
table_ = other.table_;
|
|
column_name_ = other.column_name_;
|
|
canonical_name_ = other.canonical_name_;
|
|
alias_ = other.alias_;
|
|
type_ = other.type_;
|
|
attributes_ = other.attributes_;
|
|
function_ = other.function_;
|
|
return *this;
|
|
}
|
|
|
|
bool table_column::equals(const table_column &x) const {
|
|
if (table_ != nullptr && x.table_ != nullptr) {
|
|
return *table_ == *x.table_ &&
|
|
column_name_ == x.column_name_ &&
|
|
canonical_name_ == x.canonical_name_ &&
|
|
alias_ == x.alias_ &&
|
|
function_ == x.function_;
|
|
}
|
|
|
|
return alias_ == x.alias_ &&
|
|
function_ == x.function_;
|
|
}
|
|
|
|
table_column table_column::as(const std::string& alias) const {
|
|
return {table_, column_name_, alias, type_, attributes_, function_};
|
|
}
|
|
|
|
const std::string& table_column::name() const {
|
|
return canonical_name_;
|
|
}
|
|
|
|
const std::string& table_column::column_name() const {
|
|
return column_name_;
|
|
}
|
|
|
|
const std::string& table_column::canonical_name() const {
|
|
return canonical_name_;
|
|
}
|
|
|
|
const std::string& table_column::alias() const {
|
|
return alias_;
|
|
}
|
|
const std::string& table_column::result_name() const {
|
|
return alias_.empty() ? column_name_ : alias_;
|
|
}
|
|
utils::basic_type table_column::type() const {
|
|
return type_;
|
|
}
|
|
|
|
utils::field_attributes table_column::attributes() const {
|
|
return attributes_;
|
|
}
|
|
|
|
bool table_column::is_function() const {
|
|
return function_ != sql::sql_function_t::None;
|
|
}
|
|
|
|
bool table_column::is_nullable() const {
|
|
return !utils::is_constraint_set(attributes_.options(), utils::constraints::NotNull);
|
|
}
|
|
|
|
sql::sql_function_t table_column::function() const {
|
|
return function_;
|
|
}
|
|
|
|
bool table_column::has_alias() const {
|
|
return !alias_.empty();
|
|
}
|
|
|
|
const class table* table_column::table() const {
|
|
return table_;
|
|
}
|
|
|
|
void table_column::table(const query::table* tab) {
|
|
table_ = tab;
|
|
canonical_name_ = build_canonical_name(table_, column_name_);
|
|
}
|
|
|
|
table_column::operator const std::string&() const {
|
|
return name();
|
|
}
|
|
|
|
std::string table_column::build_canonical_name(const class query::table* tab, const std::string& name) {
|
|
return tab ? tab->name() + "." + name : name;
|
|
}
|
|
} // namespace matador::query
|