determine NOT NULL constraint from std::optional
This commit is contained in:
parent
5804454ae6
commit
ed6366fef6
|
|
@ -73,7 +73,10 @@ public:
|
|||
void on_revision(const char *id, unsigned long long &rev);
|
||||
|
||||
template<typename Type>
|
||||
void on_attribute(const char *id, Type &x, const utils::field_attributes &attr = utils::null_attributes);
|
||||
void on_attribute(const char *id, Type &x, const utils::field_attributes &attr = utils::not_null_attributes);
|
||||
|
||||
template<typename Type>
|
||||
void on_attribute(const char *id, std::optional<Type> &x, const utils::field_attributes &attr = utils::null_attributes);
|
||||
|
||||
template<class Pointer>
|
||||
void on_belongs_to(const char *id, Pointer &x, utils::cascade_type)
|
||||
|
|
@ -106,14 +109,24 @@ private:
|
|||
template<typename V>
|
||||
void column_generator::on_primary_key(const char *id, V &x, typename std::enable_if<std::is_integral<V>::value && !std::is_same<bool, V>::value>::type*)
|
||||
{
|
||||
on_attribute(id, x, { utils::constraints::PRIMARY_KEY });
|
||||
on_attribute(id, x, { utils::constraints::PRIMARY_KEY | utils::constraints::NOT_NULL });
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void column_generator::on_attribute(const char *id, Type &x, const utils::field_attributes &attr)
|
||||
{
|
||||
if (attr.options() == utils::constraints::NONE) {
|
||||
columns_.push_back(column{id, x, { attr.size(), utils::constraints::NOT_NULL}});
|
||||
} else {
|
||||
columns_.emplace_back(id, x, attr);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void column_generator::on_attribute(const char *id, std::optional<Type> &x, const utils::field_attributes &attr)
|
||||
{
|
||||
columns_.emplace_back(id, data_type_traits<Type>::builtin_type(attr.size()), attr);
|
||||
}
|
||||
|
||||
}
|
||||
#endif //QUERY_COLUMN_GENERATOR_HPP
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include "matador/utils/cascade_type.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <optional>
|
||||
|
||||
namespace matador::utils {
|
||||
|
||||
|
|
@ -44,6 +45,11 @@ void attribute(Operator &op, const char *id, Type &value, const field_attributes
|
|||
op.on_attribute(id, value, attr);
|
||||
}
|
||||
|
||||
template<class Operator, class Type>
|
||||
void attribute(Operator &op, const char *id, std::optional<Type> &value, const field_attributes &attr) {
|
||||
op.on_attribute(id, value, attr);
|
||||
}
|
||||
|
||||
template<class Operator, class Type>
|
||||
void attribute(Operator &op, const char *id, Type &value) {
|
||||
op.on_attribute(id, value);
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ column_generator::column_generator(std::vector<column> &columns, const table_rep
|
|||
|
||||
void column_generator::on_primary_key(const char *id, std::string &pk, size_t size)
|
||||
{
|
||||
on_attribute(id, pk, { size, utils::constraints::PRIMARY_KEY });
|
||||
on_attribute(id, pk, { size, utils::constraints::PRIMARY_KEY | utils::constraints::NOT_NULL });
|
||||
}
|
||||
|
||||
void column_generator::on_revision(const char *id, unsigned long long int &x)
|
||||
|
|
|
|||
|
|
@ -4,29 +4,53 @@
|
|||
#include "matador/sql/table_repository.hpp"
|
||||
|
||||
#include "models/product.hpp"
|
||||
#include "models/optional.hpp"
|
||||
|
||||
using namespace matador::sql;
|
||||
using namespace matador::utils;
|
||||
|
||||
TEST_CASE("Generate columns from object", "[column generator]") {
|
||||
table_repository repo;
|
||||
|
||||
auto columns = column_generator::generate<matador::test::product>(repo);
|
||||
|
||||
const std::vector<std::string> expected_columns = {
|
||||
"product_name",
|
||||
"supplier_id",
|
||||
"category_id",
|
||||
"quantity_per_unit",
|
||||
"unit_price",
|
||||
"units_in_stock",
|
||||
"units_in_order",
|
||||
"reorder_level",
|
||||
"discontinued"
|
||||
const std::vector<column> expected_columns = {
|
||||
column{ "product_name", data_type_t::type_varchar, { constraints::PRIMARY_KEY | constraints::NOT_NULL } },
|
||||
column{ "supplier_id", data_type_t::type_unsigned_long, constraints::FOREIGN_KEY },
|
||||
column{ "category_id", data_type_t::type_unsigned_long, constraints::FOREIGN_KEY },
|
||||
column{ "quantity_per_unit", data_type_t::type_varchar, constraints::NOT_NULL },
|
||||
column{ "unit_price", data_type_t::type_unsigned_int, constraints::NOT_NULL },
|
||||
column{ "units_in_stock", data_type_t::type_unsigned_int, constraints::NOT_NULL },
|
||||
column{ "units_in_order", data_type_t::type_unsigned_int, constraints::NOT_NULL },
|
||||
column{ "reorder_level", data_type_t::type_unsigned_int, constraints::NOT_NULL },
|
||||
column{ "discontinued", data_type_t::type_bool, constraints::NOT_NULL }
|
||||
};
|
||||
REQUIRE(!columns.empty());
|
||||
REQUIRE(columns.size() == expected_columns.size());
|
||||
|
||||
for (size_t i = 0; i != expected_columns.size(); ++i) {
|
||||
REQUIRE(expected_columns[i] == columns[i].name());
|
||||
REQUIRE(expected_columns[i].name() == columns[i].name());
|
||||
REQUIRE(expected_columns[i].attributes().options() == columns[i].attributes().options() );
|
||||
REQUIRE(expected_columns[i].type() == columns[i].type() );
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Generate columns from object with nullable columns", "[column generator]") {
|
||||
table_repository repo;
|
||||
|
||||
auto columns = column_generator::generate<matador::test::optional>(repo);
|
||||
|
||||
const std::vector<column> expected_columns = {
|
||||
column{ "id", data_type_t::type_unsigned_long, { constraints::PRIMARY_KEY | constraints::NOT_NULL } },
|
||||
column{ "name", data_type_t::type_varchar, null_attributes },
|
||||
column{ "age", data_type_t::type_unsigned_int, null_attributes }
|
||||
};
|
||||
REQUIRE(!columns.empty());
|
||||
REQUIRE(columns.size() == expected_columns.size());
|
||||
|
||||
for (size_t i = 0; i != expected_columns.size(); ++i) {
|
||||
REQUIRE(expected_columns[i].name() == columns[i].name());
|
||||
REQUIRE(expected_columns[i].attributes().options() == columns[i].attributes().options() );
|
||||
REQUIRE(expected_columns[i].type() == columns[i].type() );
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue