query/include/matador/sql/column.hpp

73 lines
2.1 KiB
C++

#ifndef QUERY_COLUMN_HPP
#define QUERY_COLUMN_HPP
#include "matador/sql/types.hpp"
#include "matador/utils/field_attributes.hpp"
#include <any>
namespace matador::sql {
class column {
public:
explicit column(std::string name)
: name_(std::move(name))
, attributes_(utils::null_attributes) {}
template<typename Type>
explicit column(std::string name, utils::field_attributes attr = utils::null_attributes)
: column(std::move(name), data_type_traits<Type>::data_type(), attr)
{}
column(std::string name, data_type_t type, utils::field_attributes attr = utils::null_attributes);
[[nodiscard]] const std::string& name() const;
[[nodiscard]] const utils::field_attributes& attributes() const;
[[nodiscard]] data_type_t type() const;
private:
std::string name_;
utils::field_attributes attributes_;
data_type_t type_{};
std::any value_;
};
/**
* 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<Type>::builtin_type(0), attr);
}
template <>
column make_column<std::string>(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<Type>(name, { size, utils::constraints::PRIMARY_KEY | utils::constraints::NOT_NULL});
}
template <>
column make_pk_column<std::string>(const std::string &name, size_t size);
template < typename Type >
column make_fk_column(const std::string &name, size_t size = 0)
{
return make_column<Type>(name, { size, utils::constraints::FOREIGN_KEY });
}
template <>
column make_fk_column<std::string>(const std::string &name, size_t size);
}
#endif //QUERY_COLUMN_HPP