101 lines
2.2 KiB
C++
101 lines
2.2 KiB
C++
#include "matador/query/column.hpp"
|
|
|
|
#include "matador/query/table.hpp"
|
|
|
|
#include <stdexcept>
|
|
#include <utility>
|
|
|
|
namespace matador::query {
|
|
|
|
column operator ""_col(const char *name, size_t len) {
|
|
const std::string str(name, len);
|
|
const auto pos = str.find('.');
|
|
if (pos == std::string::npos) {
|
|
return column{str};
|
|
}
|
|
|
|
if (str.find('.', pos + 1) != std::string::npos) {
|
|
throw std::invalid_argument("Invalid column name: multiple dots found");
|
|
}
|
|
|
|
return column{table{str.substr(0, pos)}, str.substr(pos + 1)};
|
|
}
|
|
|
|
column::column(const char *name, const std::string& as)
|
|
: column(std::string(name), as)
|
|
{}
|
|
|
|
column::column(std::string name, std::string as)
|
|
: table_(std::make_shared<query::table>())
|
|
, name(std::move(name))
|
|
, alias_(std::move(as)) {}
|
|
|
|
column::column(const sql::sql_function_t func, std::string name)
|
|
: table_(std::make_shared<query::table>())
|
|
, name(std::move(name))
|
|
, function_(func) {}
|
|
|
|
column::column(const query::table& tab, std::string name, std::string as)
|
|
: table_(std::make_shared<query::table>(tab))
|
|
, name(std::move(name))
|
|
, alias_(std::move(as)) {
|
|
table_->columns_.push_back(*this);
|
|
}
|
|
|
|
column::column(const std::shared_ptr<query::table>& t, std::string name, std::string as)
|
|
: table_(t)
|
|
, name(std::move(name))
|
|
, alias_(std::move(as)) {
|
|
}
|
|
|
|
bool column::equals(const column &x) const {
|
|
return *table_ == *x.table_ &&
|
|
name == x.name &&
|
|
alias_ == x.alias_ &&
|
|
function_ == x.function_;
|
|
}
|
|
|
|
column &column::as(std::string a) {
|
|
alias_ = std::move(a);
|
|
return *this;
|
|
}
|
|
|
|
const std::string& column::column_name() const {
|
|
return name;
|
|
}
|
|
|
|
std::string column::full_name() const {
|
|
if (table_ && !table_->name().empty()) {
|
|
return table_->name() + "." + name;
|
|
}
|
|
return name;
|
|
}
|
|
|
|
const std::string& column::alias_name() const {
|
|
return alias_;
|
|
}
|
|
|
|
bool column::is_function() const {
|
|
return function_ != sql::sql_function_t::None;
|
|
}
|
|
|
|
sql::sql_function_t column::function() const {
|
|
return function_;
|
|
}
|
|
|
|
bool column::has_alias() const {
|
|
return !alias_.empty();
|
|
}
|
|
|
|
std::string column::alias() const {
|
|
return alias_;
|
|
}
|
|
|
|
std::shared_ptr<table> column::table() const {
|
|
return table_;
|
|
}
|
|
|
|
void column::table( const std::shared_ptr<query::table>& t ) {
|
|
table_ = t;
|
|
}
|
|
} |