query/include/matador/query/builder.hpp

116 lines
3.2 KiB
C++

#ifndef MATADOR_BUILDER_HPP
#define MATADOR_BUILDER_HPP
#include "matador/query/table_column.hpp"
#include "matador/query/table_constraint.hpp"
#include "matador/query/table.hpp"
#include "matador/utils/basic_types.hpp"
#include "matador/utils/constraints.hpp"
#include "matador/utils/data_type_traits.hpp"
#include <string>
namespace matador::object {
class data_type {
public:
explicit data_type(const utils::basic_type type, const size_t size = 0)
: type_(type), size_(size) {}
[[nodiscard]] const utils::basic_type& type() const { return type_; }
[[nodiscard]] size_t size() const { return size_; }
private:
utils::basic_type type_{};
size_t size_{0};
};
template<typename Type>
class typed_data_type final : public data_type {
public:
typed_data_type()
: data_type(utils::data_type_traits<Type>::type()) {}
};
template<typename Type>
class sized_typed_data_type final : public data_type {
public:
explicit sized_typed_data_type(size_t size)
: data_type(utils::data_type_traits<Type>::type(size), size) {}
};
using TinyInt = typed_data_type<int8_t>;
using SmallInt = typed_data_type<int16_t>;
using Integer = typed_data_type<int32_t>;
using BigInt = typed_data_type<int64_t>;
using TinyIntUnsigned = typed_data_type<uint8_t>;
using SmallIntUnsigned = typed_data_type<uint16_t>;
using IntegerUnsigned = typed_data_type<uint32_t>;
using BigIntUnsigned = typed_data_type<uint64_t>;
using Real = typed_data_type<float>;
using Double = typed_data_type<double>;
using Text = typed_data_type<std::string>;
using Boolean = typed_data_type<bool>;
using Varchar = sized_typed_data_type<std::string>;
using Blob = sized_typed_data_type<std::vector<std::byte>>;
}
namespace matador::query {
class column_builder {
public:
explicit column_builder(std::string column_name, utils::basic_type type, size_t size = 0);
// ReSharper disable once CppNonExplicitConversionOperator
operator table_column() const; // NOLINT(*-explicit-constructor)
column_builder& not_null();
column_builder& primary_key();
column_builder& unique();
column_builder& identity();
private:
std::string column_name_;
utils::basic_type type_{};
utils::constraints constraints_{utils::constraints::NotNull};
size_t size_{0};
};
class table_builder {
public:
explicit table_builder(std::string name);
// ReSharper disable once CppNonExplicitConversionOperator
operator table() const; // NOLINT(*-explicit-constructor)
private:
std::string table_name;
};
class constraint_builder {
public:
constraint_builder& constraint(std::string name);
constraint_builder& primary_key(std::string name);
constraint_builder& foreign_key(std::string name);
constraint_builder& references(std::string table, std::string column);
// ReSharper disable once CppNonExplicitConversionOperator
operator table_constraint() const; // NOLINT(*-explicit-constructor)
private:
std::string constraint_name_;
std::string column_name_;
std::string ref_table_name_;
std::string ref_column_name_;
utils::constraints type_{};
};
constraint_builder constraint(std::string name);
// table_builder table(std::string name);
column_builder column(std::string name, utils::basic_type type, size_t size = 0);
}
#endif //MATADOR_BUILDER_HPP