fixed table_column naming consistency and added result_name method

This commit is contained in:
Sascha Kühl
2026-01-23 13:03:41 +01:00
parent 15f959b24e
commit 4b015c8ced
7 changed files with 75 additions and 43 deletions
+2 -9
View File
@@ -38,19 +38,12 @@ sql::query_context query_builder::compile(const query_data &data,
std::string handle_column(sql::query_context &ctx, const sql::dialect *d, const query_data &data, const table_column &col) {
if (col.is_function()) {
ctx.prototype.emplace_back(col.name());
ctx.prototype.emplace_back(col.has_alias() ? col.alias() : col.canonical_name());
ctx.prototype.back().change_type(utils::basic_type::Int32);
} else {
ctx.prototype.emplace_back(col.name());
ctx.prototype.emplace_back(col.has_alias() ? col.alias() : col.canonical_name());
}
// if (col.table() != nullptr) {
// if (const auto it = data.tables.find(col.table()->name()); it != data.tables.end()) {
// return prepare_identifier(*d,col);
// }
// }
return prepare_identifier(*d, col);
}
+1 -1
View File
@@ -82,7 +82,7 @@ table::operator const std::vector<query::table_column>&() const {
const table_column* table::operator[](const std::string &column_name) const {
for (const auto &col : columns_) {
if (col.name() == column_name) {
if (col.column_name() == column_name) {
return &col;
}
}
+18 -17
View File
@@ -26,18 +26,18 @@ table_column::table_column(const char *name)
{}
table_column::table_column(const std::string& name)
: table_column(name, 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, name, utils::basic_type::Unknown, {}, func)
: 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, name, utils::basic_type::Unknown, {}, sql::sql_function_t::None)
: 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)
@@ -45,19 +45,19 @@ table_column::table_column(const class table* tab, const std::string& name, cons
{}
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, name, type, attributes, sql::sql_function_t::None)
: table_column(tab, name, "", type, attributes, sql::sql_function_t::None)
{}
table_column::table_column(const class table* tab,
const std::string& name,
const std::string& alias,
std::string alias,
const utils::basic_type type,
const utils::field_attributes &attributes,
const sql::sql_function_t func)
: table_(tab)
, name_(build_column_name(table_, name))
, column_name_(name)
, alias_(alias)
, canonical_name_(build_canonical_name(table_, name))
, alias_(std::move(alias))
, type_(type)
, attributes_(attributes)
, function_(func) {}
@@ -67,8 +67,8 @@ table_column & table_column::operator=(const table_column &other) {
return *this;
}
table_ = other.table_;
name_ = other.name_;
column_name_ = other.column_name_;
canonical_name_ = other.canonical_name_;
alias_ = other.alias_;
type_ = other.type_;
attributes_ = other.attributes_;
@@ -79,14 +79,13 @@ table_column & table_column::operator=(const table_column &other) {
bool table_column::equals(const table_column &x) const {
if (table_ != nullptr && x.table_ != nullptr) {
return *table_ == *x.table_ &&
name_ == x.name_ &&
column_name_ == x.column_name_ &&
canonical_name_ == x.canonical_name_ &&
alias_ == x.alias_ &&
function_ == x.function_;
}
return name_ == x.name_ &&
alias_ == x.alias_ &&
return alias_ == x.alias_ &&
function_ == x.function_;
}
@@ -95,7 +94,7 @@ table_column table_column::as(const std::string& alias) const {
}
const std::string& table_column::name() const {
return has_alias() ? alias_ : name_;
return canonical_name_;
}
const std::string& table_column::column_name() const {
@@ -103,13 +102,15 @@ const std::string& table_column::column_name() const {
}
const std::string& table_column::canonical_name() const {
return name_;
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_;
}
@@ -131,7 +132,7 @@ sql::sql_function_t table_column::function() const {
}
bool table_column::has_alias() const {
return alias_ != column_name_;
return !alias_.empty();
}
const class table* table_column::table() const {
@@ -140,14 +141,14 @@ const class table* table_column::table() const {
void table_column::table(const query::table* tab) {
table_ = tab;
name_ = build_column_name(table_, column_name_);
canonical_name_ = build_canonical_name(table_, column_name_);
}
table_column::operator const std::string&() const {
return name();
}
std::string table_column::build_column_name(const class query::table* tab, const std::string& 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