added read typed entity with foreign key
This commit is contained in:
parent
72bc6db6b3
commit
700cbc0652
|
|
@ -16,6 +16,11 @@ public:
|
|||
foreign() = default;
|
||||
explicit foreign(Type *obj)
|
||||
: obj_(obj) {}
|
||||
foreign(const foreign&) = default;
|
||||
foreign& operator=(const foreign&) = default;
|
||||
foreign(foreign&&) noexcept = default;
|
||||
foreign& operator=(foreign&&) noexcept = default;
|
||||
~foreign() = default;
|
||||
|
||||
pointer operator->() { return obj_.get(); }
|
||||
const value_type* operator->() const { return obj_.get(); }
|
||||
|
|
@ -24,7 +29,7 @@ public:
|
|||
const value_type& operator*() const { return *obj_; }
|
||||
|
||||
private:
|
||||
std::unique_ptr<value_type> obj_;
|
||||
std::shared_ptr<value_type> obj_;
|
||||
};
|
||||
|
||||
template<class Type, typename... Args>
|
||||
|
|
|
|||
|
|
@ -327,3 +327,38 @@ TEST_CASE("Execute select statement with group by and order by", "[session recor
|
|||
|
||||
s.drop().table("person").execute();
|
||||
}
|
||||
|
||||
TEST_CASE("Execute delete statement", "[session record]") {
|
||||
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
||||
session s(pool);
|
||||
|
||||
auto res = s.create()
|
||||
.table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
}).execute();
|
||||
|
||||
REQUIRE(res.first == 0);
|
||||
|
||||
res = s.insert().into("person", {"id", "name", "age"}).values({1, "george", 45}).execute();
|
||||
REQUIRE(res.first == 1);
|
||||
res = s.insert().into("person", {"id", "name", "age"}).values({2, "jane", 45}).execute();
|
||||
REQUIRE(res.first == 1);
|
||||
|
||||
auto count = s.select({count_all()}).from("person").fetch_value<int>();
|
||||
REQUIRE(count == 2);
|
||||
|
||||
res = s.remove()
|
||||
.from("person")
|
||||
.where("id"_col == 1)
|
||||
.execute();
|
||||
|
||||
REQUIRE(res.second == R"(DELETE FROM "person" WHERE "id" = 1)");
|
||||
REQUIRE(res.first == 1);
|
||||
|
||||
count = s.select({count_all()}).from("person").fetch_value<int>();
|
||||
REQUIRE(count == 1);
|
||||
|
||||
s.drop().table("person").execute();
|
||||
}
|
||||
|
|
@ -49,26 +49,8 @@ TEST_CASE("Execute select statement with where clause", "[session]") {
|
|||
.execute();
|
||||
REQUIRE(res.first == 1);
|
||||
|
||||
// fetch specific columns as record
|
||||
auto result_record = s.select({"id", "name", "age"})
|
||||
.from("person")
|
||||
.fetch_all();
|
||||
|
||||
for (const auto& i : result_record) {
|
||||
REQUIRE(i.size() == 3);
|
||||
REQUIRE(i.at(0).name() == "id");
|
||||
REQUIRE(i.at(0).type() == data_type_t::type_long_long);
|
||||
REQUIRE(i.at(0).as<long long>() == george.id);
|
||||
REQUIRE(i.at(1).name() == "name");
|
||||
REQUIRE(i.at(1).type() == data_type_t::type_varchar);
|
||||
REQUIRE(i.at(1).as<std::string>() == george.name);
|
||||
REQUIRE(i.at(2).name() == "age");
|
||||
REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_long_long);
|
||||
REQUIRE(i.at(2).as<long long>() == george.age);
|
||||
}
|
||||
|
||||
// fetch person as record
|
||||
result_record = s.select<person>()
|
||||
auto result_record = s.select<person>()
|
||||
.from("person")
|
||||
.where("id"_col == 7)
|
||||
.fetch_all();
|
||||
|
|
@ -124,58 +106,44 @@ TEST_CASE("Execute insert statement", "[session]") {
|
|||
s.drop().table("person").execute();
|
||||
}
|
||||
|
||||
TEST_CASE("Execute update statement", "[session]") {
|
||||
TEST_CASE("Select statement with foreign key", "[session]") {
|
||||
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
||||
session s(pool);
|
||||
|
||||
auto res = s.create()
|
||||
.table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<std::string>("color", 63)
|
||||
})
|
||||
.table<airplane>("airplane")
|
||||
.execute();
|
||||
|
||||
res = s.insert()
|
||||
.into("person", {"id", "name", "color"})
|
||||
.values({7, "george", "green"})
|
||||
.execute();
|
||||
|
||||
REQUIRE(res.first == 1);
|
||||
REQUIRE(res.second == R"(INSERT INTO "person" ("id", "name", "color") VALUES (7, 'george', 'green'))");
|
||||
|
||||
res = s.update("person")
|
||||
.set({
|
||||
{"name", "george"},
|
||||
{"color", "green"}
|
||||
})
|
||||
.where("id"_col == 9)
|
||||
.execute();
|
||||
|
||||
REQUIRE(res.second == R"(UPDATE "person" SET "name"='george', "color"='green' WHERE "id" = 9)");
|
||||
|
||||
s.drop().table("person").execute();
|
||||
}
|
||||
|
||||
TEST_CASE("Execute delete statement", "[session]") {
|
||||
connection_pool<connection> pool("sqlite://sqlite.db", 4);
|
||||
session s(pool);
|
||||
|
||||
auto res = s.create()
|
||||
.table("person", {
|
||||
make_pk_column<unsigned long>("id"),
|
||||
make_column<std::string>("name", 255),
|
||||
make_column<unsigned short>("age")
|
||||
}).execute();
|
||||
|
||||
REQUIRE(res.first == 0);
|
||||
res = s.remove()
|
||||
.from("person")
|
||||
.where("id"_col == 9)
|
||||
REQUIRE(res.second == R"(CREATE TABLE "airplane" ("id" BIGINT, "brand" VARCHAR(255), "model" VARCHAR(255), CONSTRAINT PK_airplane PRIMARY KEY (id)))");
|
||||
|
||||
res = s.create()
|
||||
.table<flight>("flight")
|
||||
.execute();
|
||||
REQUIRE(res.first == 0);
|
||||
REQUIRE(res.second == R"(CREATE TABLE "flight" ("id" BIGINT, "airplane_id" BIGINT, "pilot_name" VARCHAR(255), CONSTRAINT PK_flight PRIMARY KEY (id), CONSTRAINT FK_flight_airplane_id FOREIGN KEY (airplane_id) REFERENCES airplane(id)))");
|
||||
|
||||
REQUIRE(res.second == R"(DELETE FROM "person" WHERE "id" = 9)");
|
||||
std::vector<foreign<airplane>> planes {
|
||||
make_foreign<airplane>(1, "Airbus", "A380"),
|
||||
make_foreign<airplane>(2, "Boeing", "707"),
|
||||
make_foreign<airplane>(3, "Boeing", "747")
|
||||
};
|
||||
|
||||
REQUIRE(s.drop().table("person").execute().first == 0);
|
||||
for (const auto &plane : planes) {
|
||||
res = s.insert().into<airplane>("airplane").values(*plane).execute();
|
||||
REQUIRE(res.first == 1);
|
||||
}
|
||||
|
||||
auto count = s.select({count_all()}).from("airplane").fetch_value<int>();
|
||||
REQUIRE(count == 3);
|
||||
|
||||
flight f4711{4, *planes.begin(), "hans"};
|
||||
|
||||
res = s.insert().into<flight>("flight").values(f4711).execute();
|
||||
REQUIRE(res.first == 1);
|
||||
|
||||
auto f = *s.select<flight>().from("flight").fetch_all<flight>().begin();
|
||||
|
||||
|
||||
s.drop().table("flight").execute();
|
||||
s.drop().table("airplane").execute();
|
||||
}
|
||||
|
|
@ -16,6 +16,11 @@ namespace matador::test {
|
|||
|
||||
struct airplane
|
||||
{
|
||||
airplane() = default;
|
||||
airplane(unsigned long id, std::string b, std::string m)
|
||||
: id(id)
|
||||
, brand(std::move(b))
|
||||
, model(std::move(m)) {}
|
||||
unsigned long id;
|
||||
std::string brand;
|
||||
std::string model;
|
||||
|
|
|
|||
Loading…
Reference in New Issue