Compare commits
2 Commits
2060883d59
...
ed6366fef6
| Author | SHA1 | Date |
|---|---|---|
|
|
ed6366fef6 | |
|
|
5804454ae6 |
|
|
@ -73,7 +73,10 @@ public:
|
||||||
void on_revision(const char *id, unsigned long long &rev);
|
void on_revision(const char *id, unsigned long long &rev);
|
||||||
|
|
||||||
template<typename Type>
|
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>
|
template<class Pointer>
|
||||||
void on_belongs_to(const char *id, Pointer &x, utils::cascade_type)
|
void on_belongs_to(const char *id, Pointer &x, utils::cascade_type)
|
||||||
|
|
@ -106,13 +109,23 @@ private:
|
||||||
template<typename V>
|
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*)
|
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>
|
template<typename Type>
|
||||||
void column_generator::on_attribute(const char *id, Type &x, const utils::field_attributes &attr)
|
void column_generator::on_attribute(const char *id, Type &x, const utils::field_attributes &attr)
|
||||||
{
|
{
|
||||||
columns_.emplace_back(id, x, 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
#include "matador/utils/cascade_type.hpp"
|
#include "matador/utils/cascade_type.hpp"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
namespace matador::utils {
|
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);
|
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>
|
template<class Operator, class Type>
|
||||||
void attribute(Operator &op, const char *id, Type &value) {
|
void attribute(Operator &op, const char *id, Type &value) {
|
||||||
op.on_attribute(id, 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)
|
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)
|
void column_generator::on_revision(const char *id, unsigned long long int &x)
|
||||||
|
|
|
||||||
|
|
@ -4,29 +4,53 @@
|
||||||
#include "matador/sql/table_repository.hpp"
|
#include "matador/sql/table_repository.hpp"
|
||||||
|
|
||||||
#include "models/product.hpp"
|
#include "models/product.hpp"
|
||||||
|
#include "models/optional.hpp"
|
||||||
|
|
||||||
using namespace matador::sql;
|
using namespace matador::sql;
|
||||||
|
using namespace matador::utils;
|
||||||
|
|
||||||
TEST_CASE("Generate columns from object", "[column generator]") {
|
TEST_CASE("Generate columns from object", "[column generator]") {
|
||||||
table_repository repo;
|
table_repository repo;
|
||||||
|
|
||||||
auto columns = column_generator::generate<matador::test::product>(repo);
|
auto columns = column_generator::generate<matador::test::product>(repo);
|
||||||
|
|
||||||
const std::vector<std::string> expected_columns = {
|
const std::vector<column> expected_columns = {
|
||||||
"product_name",
|
column{ "product_name", data_type_t::type_varchar, { constraints::PRIMARY_KEY | constraints::NOT_NULL } },
|
||||||
"supplier_id",
|
column{ "supplier_id", data_type_t::type_unsigned_long, constraints::FOREIGN_KEY },
|
||||||
"category_id",
|
column{ "category_id", data_type_t::type_unsigned_long, constraints::FOREIGN_KEY },
|
||||||
"quantity_per_unit",
|
column{ "quantity_per_unit", data_type_t::type_varchar, constraints::NOT_NULL },
|
||||||
"unit_price",
|
column{ "unit_price", data_type_t::type_unsigned_int, constraints::NOT_NULL },
|
||||||
"units_in_stock",
|
column{ "units_in_stock", data_type_t::type_unsigned_int, constraints::NOT_NULL },
|
||||||
"units_in_order",
|
column{ "units_in_order", data_type_t::type_unsigned_int, constraints::NOT_NULL },
|
||||||
"reorder_level",
|
column{ "reorder_level", data_type_t::type_unsigned_int, constraints::NOT_NULL },
|
||||||
"discontinued"
|
column{ "discontinued", data_type_t::type_bool, constraints::NOT_NULL }
|
||||||
};
|
};
|
||||||
REQUIRE(!columns.empty());
|
REQUIRE(!columns.empty());
|
||||||
REQUIRE(columns.size() == expected_columns.size());
|
REQUIRE(columns.size() == expected_columns.size());
|
||||||
|
|
||||||
for (size_t i = 0; i != expected_columns.size(); ++i) {
|
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() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,10 +1,12 @@
|
||||||
#include <catch2/catch_test_macros.hpp>
|
#include <catch2/catch_test_macros.hpp>
|
||||||
#include <catch2/generators/catch_generators.hpp>
|
#include <catch2/catch_template_test_macros.hpp>
|
||||||
|
|
||||||
#include <matador/sql/column.hpp>
|
#include <matador/sql/column.hpp>
|
||||||
#include <matador/sql/condition.hpp>
|
#include <matador/sql/condition.hpp>
|
||||||
#include <matador/sql/session.hpp>
|
#include <matador/sql/session.hpp>
|
||||||
|
|
||||||
|
#include "Databases.hpp"
|
||||||
|
|
||||||
#include "models/product.hpp"
|
#include "models/product.hpp"
|
||||||
#include "models/airplane.hpp"
|
#include "models/airplane.hpp"
|
||||||
#include "models/flight.hpp"
|
#include "models/flight.hpp"
|
||||||
|
|
@ -13,23 +15,34 @@
|
||||||
using namespace matador::sql;
|
using namespace matador::sql;
|
||||||
using namespace matador::test;
|
using namespace matador::test;
|
||||||
|
|
||||||
TEST_CASE("Create table with foreign key relation", "[session]") {
|
template<class Type>
|
||||||
auto dns = GENERATE(as<std::string>{},
|
class SessionTestFixture
|
||||||
"sqlite://sqlite.db",
|
{
|
||||||
"postgres://test:test123@127.0.0.1:5432/matador_test" );
|
public:
|
||||||
|
SessionTestFixture()
|
||||||
|
: pool_(Type::dns, 4), session_(pool_)
|
||||||
|
{}
|
||||||
|
~SessionTestFixture() = default;
|
||||||
|
|
||||||
|
matador::sql::session &session()
|
||||||
|
{ return session_; }
|
||||||
|
|
||||||
connection_pool<connection> pool(dns, 4);
|
private:
|
||||||
session s(pool);
|
matador::sql::connection_pool<matador::sql::connection> pool_;
|
||||||
|
matador::sql::session session_;
|
||||||
|
};
|
||||||
|
|
||||||
|
TEMPLATE_TEST_CASE_METHOD(SessionTestFixture, "Create table with foreign key relation", "[session]", Sqlite, Postgres) {
|
||||||
|
auto &s = SessionTestFixture<TestType>::session();
|
||||||
|
|
||||||
s.create()
|
s.create()
|
||||||
.table<airplane>("airplane")
|
.template table<airplane>("airplane")
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
REQUIRE(s.table_exists("airplane"));
|
REQUIRE(s.table_exists("airplane"));
|
||||||
|
|
||||||
s.create()
|
s.create()
|
||||||
.table<flight>("flight")
|
.template table<flight>("flight")
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
REQUIRE(s.table_exists("flight"));
|
REQUIRE(s.table_exists("flight"));
|
||||||
|
|
@ -41,12 +54,11 @@ TEST_CASE("Create table with foreign key relation", "[session]") {
|
||||||
REQUIRE(!s.table_exists("airplane"));
|
REQUIRE(!s.table_exists("airplane"));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Execute select statement with where clause", "[session]") {
|
TEMPLATE_TEST_CASE_METHOD(SessionTestFixture, "Execute select statement with where clause", "[session]", Postgres, Sqlite) {
|
||||||
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
auto &s = SessionTestFixture<TestType>::session();
|
||||||
session s(pool);
|
|
||||||
|
|
||||||
s.create()
|
s.create()
|
||||||
.table<person>("person")
|
.template table<person>("person")
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
person george{7, "george", 45};
|
person george{7, "george", 45};
|
||||||
|
|
@ -57,7 +69,7 @@ TEST_CASE("Execute select statement with where clause", "[session]") {
|
||||||
REQUIRE(res == 1);
|
REQUIRE(res == 1);
|
||||||
|
|
||||||
// fetch person as record
|
// fetch person as record
|
||||||
auto result_record = s.select<person>()
|
auto result_record = s.template select<person>()
|
||||||
.from("person")
|
.from("person")
|
||||||
.where("id"_col == 7)
|
.where("id"_col == 7)
|
||||||
.fetch_all();
|
.fetch_all();
|
||||||
|
|
@ -66,20 +78,20 @@ TEST_CASE("Execute select statement with where clause", "[session]") {
|
||||||
REQUIRE(i.size() == 3);
|
REQUIRE(i.size() == 3);
|
||||||
REQUIRE(i.at(0).name() == "id");
|
REQUIRE(i.at(0).name() == "id");
|
||||||
REQUIRE(i.at(0).type() == data_type_t::type_unsigned_long);
|
REQUIRE(i.at(0).type() == data_type_t::type_unsigned_long);
|
||||||
REQUIRE(i.at(0).as<long long>() == george.id);
|
REQUIRE(i.at(0).template as<long long>() == george.id);
|
||||||
REQUIRE(i.at(1).name() == "name");
|
REQUIRE(i.at(1).name() == "name");
|
||||||
REQUIRE(i.at(1).type() == data_type_t::type_varchar);
|
REQUIRE(i.at(1).type() == data_type_t::type_varchar);
|
||||||
REQUIRE(i.at(1).as<std::string>() == george.name);
|
REQUIRE(i.at(1).template as<std::string>() == george.name);
|
||||||
REQUIRE(i.at(2).name() == "age");
|
REQUIRE(i.at(2).name() == "age");
|
||||||
REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_unsigned_int);
|
REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_unsigned_int);
|
||||||
REQUIRE(i.at(2).as<long long>() == george.age);
|
REQUIRE(i.at(2).template as<long long>() == george.age);
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetch person as person
|
// fetch person as person
|
||||||
auto result_person = s.select<person>()
|
auto result_person = s.template select<person>()
|
||||||
.from("person")
|
.from("person")
|
||||||
.where("id"_col == 7)
|
.where("id"_col == 7)
|
||||||
.fetch_all<person>();
|
.template fetch_all<person>();
|
||||||
|
|
||||||
for (const auto& i : result_person) {
|
for (const auto& i : result_person) {
|
||||||
REQUIRE(i.id == 7);
|
REQUIRE(i.id == 7);
|
||||||
|
|
@ -90,9 +102,8 @@ TEST_CASE("Execute select statement with where clause", "[session]") {
|
||||||
s.drop().table("person").execute();
|
s.drop().table("person").execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Execute insert statement", "[session]") {
|
TEMPLATE_TEST_CASE_METHOD(SessionTestFixture, "Execute insert statement", "[session]", Sqlite, Postgres) {
|
||||||
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
auto &s = SessionTestFixture<TestType>::session();
|
||||||
session s(pool);
|
|
||||||
|
|
||||||
s.create()
|
s.create()
|
||||||
.table("person", {
|
.table("person", {
|
||||||
|
|
@ -112,16 +123,15 @@ TEST_CASE("Execute insert statement", "[session]") {
|
||||||
s.drop().table("person").execute();
|
s.drop().table("person").execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Select statement with foreign key", "[session]") {
|
TEMPLATE_TEST_CASE_METHOD(SessionTestFixture, "Select statement with foreign key", "[session]", Sqlite, Postgres) {
|
||||||
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
auto &s = SessionTestFixture<TestType>::session();
|
||||||
session s(pool);
|
|
||||||
|
|
||||||
s.create()
|
s.create()
|
||||||
.table<airplane>("airplane")
|
.template table<airplane>("airplane")
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
s.create()
|
s.create()
|
||||||
.table<flight>("flight")
|
.template table<flight>("flight")
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
std::vector<entity<airplane>> planes {
|
std::vector<entity<airplane>> planes {
|
||||||
|
|
@ -131,19 +141,19 @@ TEST_CASE("Select statement with foreign key", "[session]") {
|
||||||
};
|
};
|
||||||
|
|
||||||
for (const auto &plane : planes) {
|
for (const auto &plane : planes) {
|
||||||
auto res = s.insert().into<airplane>("airplane").values(*plane).execute();
|
auto res = s.insert().template into<airplane>("airplane").values(*plane).execute();
|
||||||
REQUIRE(res == 1);
|
REQUIRE(res == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto count = s.select({count_all()}).from("airplane").fetch_value<int>();
|
auto count = s.select({count_all()}).from("airplane").template fetch_value<int>();
|
||||||
REQUIRE(count == 3);
|
REQUIRE(count == 3);
|
||||||
|
|
||||||
flight f4711{4, planes.at(1), "hans"};
|
flight f4711{4, planes.at(1), "hans"};
|
||||||
|
|
||||||
auto res = s.insert().into<flight>("flight").values(f4711).execute();
|
auto res = s.insert().template into<flight>("flight").values(f4711).execute();
|
||||||
REQUIRE(res == 1);
|
REQUIRE(res == 1);
|
||||||
|
|
||||||
auto f = *s.select<flight>().from("flight").fetch_all<flight>().begin();
|
auto f = *s.template select<flight>().from("flight").template fetch_all<flight>().begin();
|
||||||
REQUIRE(f.id == 4);
|
REQUIRE(f.id == 4);
|
||||||
REQUIRE(f.pilot_name == "hans");
|
REQUIRE(f.pilot_name == "hans");
|
||||||
REQUIRE(f.airplane.get() != nullptr);
|
REQUIRE(f.airplane.get() != nullptr);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue