query/include/matador/query/table_column.hpp

129 lines
4.1 KiB
C++

#ifndef QUERY_COLUMN_HPP
#define QUERY_COLUMN_HPP
#include "expression/abstract_column_expression.hpp"
#include "matador/sql/sql_functions.hpp"
#include "matador/utils/basic_types.hpp"
#include "matador/utils/field_attributes.hpp"
#include <string>
namespace matador::query {
class table;
class column_expression;
// ReSharper disable CppNonExplicitConvertingConstructor
class table_column {
public:
table_column() = default;
table_column(const char *name); // NOLINT(*-explicit-constructor)
table_column(const std::string& name); // NOLINT(*-explicit-constructor)
table_column(const std::string& name, const std::string& alias);
table_column(sql::sql_function_t func, const std::string& name);
table_column(const class table* tab, const std::string& name);
table_column(const class table* tab, const std::string& name, const std::string& alias);
table_column(const class table* tab, const std::string& name, utils::basic_type type, const utils::field_attributes& attributes);
table_column(const class table*,
const std::string& name,
std::string alias,
utils::basic_type type,
const utils::field_attributes& attributes,
sql::sql_function_t func = sql::sql_function_t::None,
const std::shared_ptr<abstract_column_expression>& expression = {});
table_column(const std::shared_ptr<abstract_column_expression>& expression) noexcept;
table_column(column_expression&& expression) noexcept;
table_column& operator=(const table_column& other);
table_column(const table_column& other) = default;
table_column(table_column&& other) noexcept = default;
~table_column() = default;
[[nodiscard]] bool equals(const table_column &x) const;
[[nodiscard]] table_column as(const std::string& alias) const;
/**
* Returns the canonical column name.
*
* @return The canonical column name
*/
[[nodiscard]] const std::string& name() const;
/**
* Returns the column name without prepending
* a table name or alias.
*
* @return Returns the column name
*/
[[nodiscard]] const std::string& column_name() const;
/**
* Returns the canonical column name which means
* if the column is owned by a table, the table name
* id prepended.
*
* @return Returns the canonical column name
*/
[[nodiscard]] const std::string& canonical_name() const;
/**
* Returns the alias name for the column. If no alias
* is set, an empty string is returned.
*
* @return Returns the alias name for the column
*/
[[nodiscard]] const std::string& alias() const;
/**
* Returns the result label name for this column in a SELECT list.
* Semantics: alias if set, otherwise the raw column name (never table-qualified).
*
* @return If set the alias otherwise the raw column name
*/
[[nodiscard]] const std::string& result_name() const;
[[nodiscard]] utils::basic_type type() const;
[[nodiscard]] utils::field_attributes attributes() const;
[[nodiscard]] bool is_function() const;
[[nodiscard]] bool is_nullable() const;
[[nodiscard]] sql::sql_function_t function() const;
[[nodiscard]] bool has_alias() const;
[[nodiscard]] const class table* table() const;
void table(const query::table* tab);
// New: expression-backed column support
[[nodiscard]] bool is_expression() const;
[[nodiscard]] std::shared_ptr<abstract_column_expression> expression() const;
// ReSharper disable once CppNonExplicitConversionOperator
operator const std::string&() const; // NOLINT(*-explicit-constructor)
private:
static std::string build_canonical_name(const class table *tab, const std::string& name);
private:
friend class table;
const class table* table_{nullptr};
std::string column_name_;
std::string canonical_name_;
std::string alias_;
utils::basic_type type_{utils::basic_type::Unknown};
utils::field_attributes attributes_{};
sql::sql_function_t function_{sql::sql_function_t::None};
std::shared_ptr<abstract_column_expression> expression_;
};
table_column operator ""_col(const char *name, size_t len);
}
#endif //QUERY_COLUMN_HPP