#ifndef QUERY_COLUMN_HPP #define QUERY_COLUMN_HPP #include "matador/sql/any_type.hpp" #include "matador/sql/any_type_to_visitor.hpp" #include "matador/sql/types.hpp" #include "matador/utils/field_attributes.hpp" #include #include namespace matador::sql { namespace detail { } class column { public: explicit column(std::string name) : name_(std::move(name)) , attributes_(utils::null_attributes) {} column(const column&) = default; column& operator=(const column&) = default; column(column&&) noexcept = default; column& operator=(column&&) noexcept = default; template explicit column(std::string name, utils::field_attributes attr = utils::null_attributes) : column(std::move(name), data_type_traits::builtin_type(attr.size()), attr) {} template column(std::string name, const Type &, utils::field_attributes attr = utils::null_attributes) : column(std::move(name), data_type_traits::builtin_type(attr.size()), attr) {} column(std::string name, data_type_t type, utils::field_attributes attr = utils::null_attributes); template column(std::string name, std::string ref_table, std::string ref_column, utils::field_attributes attr = utils::null_attributes) : column(std::move(name), data_type_traits::builtin_type(attr.size()), ref_table, ref_column, attr) {} column(std::string name, data_type_t type, size_t index, std::string ref_table, std::string ref_column, utils::field_attributes attr = utils::null_attributes); [[nodiscard]] const std::string& name() const; [[nodiscard]] size_t index() const; [[nodiscard]] const utils::field_attributes& attributes() const; [[nodiscard]] data_type_t type() const; [[nodiscard]] const std::string& ref_table() const; [[nodiscard]] const std::string& ref_column() const; template< typename Type > [[nodiscard]] bool is_type_of() const { return std::holds_alternative(value_); } [[nodiscard]] std::string str() const; template void set(const Type &value, const utils::field_attributes &attr = utils::null_attributes) { type_ = data_type_traits::builtin_type(attr.size()); attributes_ = attr; value_ = value; } void set(const std::string &value, const utils::field_attributes &attr) { type_ = data_type_traits::builtin_type(attr.size()); attributes_ = attr; value_ = value; } void set(const char *value, const utils::field_attributes &attr) { type_ = data_type_traits::builtin_type(attr.size()); attributes_ = attr; value_ = value; } template Type as() const { const Type* ptr= std::get_if(&value_); if (ptr) { return *ptr; } any_type_to_visitor visitor; std::visit(visitor, const_cast(value_)); return visitor.result; } private: template void process(Operator &op) { op.on_attribute(name_.c_str(), value_, type_, attributes_); } using data_type_index = std::vector; private: friend class record; static const data_type_index data_type_index_; std::string name_; size_t index_{}; utils::field_attributes attributes_; data_type_t type_{data_type_t::type_unknown}; any_type value_; std::string ref_table_; std::string ref_column_; }; /** * User defined literal to have a shortcut creating a column object * @param name Name of the column * @param len Length of the column name * @return A column object with given name */ column operator "" _col(const char *name, size_t len); column make_column(const std::string &name, data_type_t type, utils::field_attributes attr = utils::null_attributes); template < typename Type > column make_column(const std::string &name, utils::field_attributes attr = utils::null_attributes) { return make_column(name, data_type_traits::builtin_type(0), attr); } template <> column make_column(const std::string &name, utils::field_attributes attr); template < typename Type > column make_pk_column(const std::string &name, size_t size = 0) { return make_column(name, { size, utils::constraints::PRIMARY_KEY | utils::constraints::NOT_NULL}); } template <> column make_pk_column(const std::string &name, size_t size); template < typename Type > column make_fk_column(const std::string &name, size_t size, const std::string &ref_table, const std::string &ref_column) { return {name, data_type_traits::builtin_type(size), ref_table, ref_column, { size, utils::constraints::FOREIGN_KEY }}; } template < typename Type > [[maybe_unused]] column make_fk_column(const std::string &name, const std::string &ref_table, const std::string &ref_column) { return {name, data_type_traits::builtin_type(0), 0, ref_table, ref_column, { 0, utils::constraints::FOREIGN_KEY }}; } template <> column make_fk_column(const std::string &name, size_t size, const std::string &ref_table, const std::string &ref_column); } #endif //QUERY_COLUMN_HPP