introduced table repository

This commit is contained in:
2023-11-16 20:15:52 +01:00
parent 7c1e940814
commit c801d53e8c
25 changed files with 589 additions and 150 deletions
+4 -1
View File
@@ -20,7 +20,10 @@ add_executable(tests builder.cpp
column_name_generator.cpp
value_generator.cpp
models/category.hpp
models/supplier.hpp)
models/supplier.hpp
models/airplane.hpp
models/flight.hpp
models/person.hpp)
target_link_libraries(tests PRIVATE
Catch2::Catch2WithMain
matador
+3 -1
View File
@@ -1,14 +1,16 @@
#include <catch2/catch_test_macros.hpp>
#include "matador/sql/column_generator.hpp"
#include "matador/sql/table_repository.hpp"
#include "models/product.hpp"
using namespace matador::sql;
TEST_CASE("Generate columns from object", "[column generator]") {
table_repository repo;
auto columns = column_generator::generate<matador::test::product>();
auto columns = column_generator::generate<matador::test::product>(repo);
const std::vector<std::string> expected_columns = {
"product_name",
+35
View File
@@ -0,0 +1,35 @@
#ifndef QUERY_AIRPLANE_HPP
#define QUERY_AIRPLANE_HPP
#include "category.hpp"
#include "supplier.hpp"
#include "matador/utils/access.hpp"
#include "matador/utils/cascade_type.hpp"
#include "matador/utils/field_attributes.hpp"
#include "matador/sql/foreign.hpp"
#include <string>
namespace matador::test {
struct airplane
{
unsigned long id;
std::string brand;
std::string model;
template<class Operator>
void process(Operator &op) {
namespace field = matador::utils::access;
using namespace matador::utils;
field::primary_key(op, "id", id);
field::attribute(op, "brand", brand, 255);
field::attribute(op, "model", model, 255);
}
};
}
#endif //QUERY_AIRPLANE_HPP
+33
View File
@@ -0,0 +1,33 @@
#ifndef QUERY_FLIGHT_HPP
#define QUERY_FLIGHT_HPP
#include "airplane.hpp"
#include "matador/utils/access.hpp"
#include "matador/utils/cascade_type.hpp"
#include "matador/utils/field_attributes.hpp"
#include "matador/sql/foreign.hpp"
#include <string>
namespace matador::test {
struct flight {
unsigned long id;
sql::foreign<test::airplane> airplane;
std::string pilot_name;
template<class Operator>
void process(Operator &op) {
namespace field = matador::utils::access;
using namespace matador::utils;
field::primary_key(op, "id", id);
field::has_one(op, "airplane_id", airplane, utils::cascade_type::ALL);
field::attribute(op, "pilot_name", pilot_name, 255);
}
};
}
#endif //QUERY_FLIGHT_HPP
+28
View File
@@ -0,0 +1,28 @@
#ifndef QUERY_PERSON_HPP
#define QUERY_PERSON_HPP
#include "matador/utils/access.hpp"
#include "matador/utils/field_attributes.hpp"
#include <string>
namespace matador::test {
struct person
{
unsigned long id{};
std::string name;
unsigned int age{};
template<class Operator>
void process(Operator &op) {
namespace field = matador::utils::access;
using namespace matador::utils;
field::primary_key(op, "id", id);
field::attribute(op, "name", name, 255);
field::attribute(op, "age", age);
}
};
}
#endif //QUERY_PERSON_HPP
+174 -97
View File
@@ -5,11 +5,14 @@
#include <matador/sql/session.hpp>
#include "models/product.hpp"
#include "models/airplane.hpp"
#include "models/flight.hpp"
#include "models/person.hpp"
using namespace matador::sql;
using namespace matador::test;
TEST_CASE("Execute create and drop table statement", "[session]") {
TEST_CASE("Create and drop table statement", "[session]") {
connection_pool<connection> pool("sqlite://sqlite.db", 4);
session s(pool);
@@ -27,13 +30,20 @@ TEST_CASE("Execute create and drop table statement", "[session]") {
REQUIRE(res.second == R"(DROP TABLE "person")");
}
TEST_CASE("Execute create table statement with foreign keys", "[session]") {
// res = s.create().table<product>("product").execute();
TEST_CASE("Create table with foreign key relation", "[session]") {
connection_pool<connection> pool("sqlite://sqlite.db", 4);
session s(pool);
// REQUIRE(res.second == R"(CREATE TABLE "product" ("product_name" VARCHAR(255) PRIMARY KEY, "supplier_id" BIGINT NOT NULL FOREIGN KEY, "category_id" BIGINT NOT NULL FOREIGN KEY, "quantity_per_unit" VARCHAR(255), "unit_price" BIGINT, "units_in_stock" BIGINT, "units_in_order" BIGINT, "reorder_level" BIGINT, "discontinued" BOOLEAN))");
auto res = s.create().table<airplane>("airplane").execute();
REQUIRE(res.first == 0);
REQUIRE(res.second == R"(CREATE TABLE "airplane" ("id" BIGINT, "brand" VARCHAR(255), "model" VARCHAR(255), CONSTRAINT PK_airplane PRIMARY KEY (id)))");
// res = s.drop().table("person").execute();
// REQUIRE(res.first == 1);
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)))");
s.drop().table("flight").execute();
s.drop().table("airplane").execute();
}
TEST_CASE("Execute insert record statement", "[session]") {
@@ -69,149 +79,216 @@ TEST_CASE("Execute insert record statement", "[session]") {
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).value<long long>() == 7);
REQUIRE(i.at(1).name() == "name");
REQUIRE(i.at(1).type() == data_type_t::type_varchar);
REQUIRE(i.at(1).value<std::string>() == "george");
REQUIRE(i.at(2).name() == "age");
REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_int);
REQUIRE(i.at(2).value<int>() == 45);
}
s.drop().table("person").execute();
// using namespace matador::test;
// product p;
// p.discontinued = false;
// p.reorder_level = 1;
// p.units_in_order = 2;
// p.units_in_stock = 100;
// p.unit_price = 49;
// p.quantity_per_unit = "pcs";
// p.category = make_foreign<matador::test::category>();
// p.category->id = 7;
// p.supplier = make_foreign<matador::test::supplier>();;
// p.supplier->id = 13;
// p.product_name = "candle";
//
// res = s.insert().into<product>("product").values(p).execute();
//
// REQUIRE(res.second == R"(INSERT INTO "product" ("product_name", "supplier_id", "category_id", "quantity_per_unit", "unit_price", "units_in_stock", "units_in_order", "reorder_level", "discontinued") VALUES ('candle', 13, 7, 'pcs', 49, 100, 2, 1, 0))");
//
// res = s.insert().into("product", p).execute();
//
// REQUIRE(res.second == R"(INSERT INTO "product" ("product_name", "supplier_id", "category_id", "quantity_per_unit", "unit_price", "units_in_stock", "units_in_order", "reorder_level", "discontinued") VALUES ('candle', 13, 7, 'pcs', 49, 100, 2, 1, 0))");
s.drop().table("person").execute();
}
TEST_CASE("Execute update record 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
.insert()
.into("person", {"id", "name", "age"})
.values({7, "george", 45})
.execute();
REQUIRE(res.first == 1);
REQUIRE(res.second == R"(INSERT INTO "person" ("id", "name", "age") VALUES (7, 'george', 45))");
res = s.update("person")
.set({{"id", 7}, {"name", "jane"}, {"age", 35}})
.where("id"_col == 7)
.execute();
REQUIRE(res.first == 1);
REQUIRE(res.second == R"(UPDATE "person" SET "id"=7, "name"='jane', "age"=35 WHERE "id" = 7)");
auto result = s
.select({"id", "name", "age"})
.from("person")
.fetch_all();
for (const auto& i : result) {
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).value<long long>() == 7);
REQUIRE(i.at(1).name() == "name");
REQUIRE(i.at(1).type() == data_type_t::type_varchar);
REQUIRE(i.at(1).value<std::string>() == "jane");
REQUIRE(i.at(2).name() == "age");
REQUIRE(i.at(2).type() == matador::sql::data_type_t::type_int);
REQUIRE(i.at(2).value<int>() == 35);
}
s.drop().table("person").execute();
}
TEST_CASE("Execute select statement with where clause", "[session]") {
connection_pool<connection> pool("sqlite://sqlite.db", 4);
session s(pool);
auto res = s.select({"id", "name", "color"})
.from("person")
.where("id"_col == 8)
.fetch_all();
auto res = s.create()
.table<person>("person")
.execute();
REQUIRE(res.first == 0);
person george{7, "george", 45};
res = s.insert()
.into("person", george)
.execute();
REQUIRE(res.first == 1);
auto result = s.select<person>()
.from("person")
.where("id"_col == 7)
.fetch_all();
for (const auto& i : result) {
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).value<long long>() == george.id);
REQUIRE(i.at(1).name() == "name");
REQUIRE(i.at(1).type() == data_type_t::type_varchar);
REQUIRE(i.at(1).value<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).value<long long>() == george.age);
}
// Todo: prepare test data
REQUIRE(true);
// REQUIRE(res.str() == R"(SELECT "id", "name", "color" FROM "person" WHERE "id" = 8)");
s.drop().table("person").execute();
}
TEST_CASE("Execute select statement with order by", "[session]") {
connection_pool<connection> pool("sqlite://sqlite.db", 4);
session s(pool);
auto res = s.select({"id", "name", "color"})
.from("person")
.where("id"_col == 8)
.order_by("name").desc()
.fetch_all();
auto res = s
.create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
make_column<std::string>("color", 63)
})
.execute();
REQUIRE(res.first == 0);
auto result = s.select({"id", "name", "color"})
.from("person")
.where("id"_col == 8)
.order_by("name").desc()
.fetch_all();
// Todo: prepare test data
REQUIRE(true);
// REQUIRE(res.str() == R"(SELECT "id", "name", "color" FROM "person" WHERE "id" = 8 ORDER BY "name" DESC)");
s.drop().table("person").execute();
}
TEST_CASE("Execute select statement with group by and order by", "[session]") {
connection_pool<connection> pool("sqlite://sqlite.db", 4);
session s(pool);
auto res = s.select({"id", "name", "color"})
.from("person")
.where("id"_col == 8)
.group_by("color")
.order_by("name").asc()
.fetch_all();
auto res = s.create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
make_column<std::string>("color", 63)
})
.execute();
auto result = s.select({"id", "name", "color"})
.from("person")
.where("id"_col == 8)
.group_by("color")
.order_by("name").asc()
.fetch_all();
// Todo: prepare test data
REQUIRE(true);
// REQUIRE(res.str() == R"(SELECT "id", "name", "color" FROM "person" WHERE "id" = 8 GROUP BY "color" ORDER BY "name" ASC)");
s.drop().table("person").execute();
}
TEST_CASE("Execute insert statement", "[session]") {
connection_pool<connection> pool("sqlite://sqlite.db", 4);
session s(pool);
auto res = s.insert()
.into("person", {"id", "name", "color"})
.values({7, "george", "green"})
auto res = s.create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
make_column<std::string>("color", 63)
})
.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'))");
using namespace matador::test;
product p;
p.discontinued = false;
p.reorder_level = 1;
p.units_in_order = 2;
p.units_in_stock = 100;
p.unit_price = 49;
p.quantity_per_unit = "pcs";
p.category = make_foreign<matador::test::category>();
p.category->id = 7;
p.supplier = make_foreign<matador::test::supplier>();
p.supplier->id = 13;
p.product_name = "candle";
res = s.insert().into<product>("product").values(p).execute();
REQUIRE(res.second == R"(INSERT INTO "product" ("product_name", "supplier_id", "category_id", "quantity_per_unit", "unit_price", "units_in_stock", "units_in_order", "reorder_level", "discontinued") VALUES ('candle', 13, 7, 'pcs', 49, 100, 2, 1, 0))");
res = s.insert().into("product", p).execute();
REQUIRE(res.second == R"(INSERT INTO "product" ("product_name", "supplier_id", "category_id", "quantity_per_unit", "unit_price", "units_in_stock", "units_in_order", "reorder_level", "discontinued") VALUES ('candle', 13, 7, 'pcs', 49, 100, 2, 1, 0))");
s.drop().table("person").execute();
}
TEST_CASE("Execute update statement", "[session]") {
connection_pool<connection> pool("sqlite://sqlite.db", 4);
session s(pool);
auto res = s.update("person")
.set({
{"name", "george"},
{"color", "green"}
})
.where("id"_col == 9)
.execute();
auto res = s.create()
.table("person", {
make_pk_column<unsigned long>("id"),
make_column<std::string>("name", 255),
make_column<std::string>("color", 63)
})
.execute();
REQUIRE(res.second == R"(UPDATE "person" SET "name"='george', "color"='green' WHERE "id" = 9)");
res = s.insert()
.into("person", {"id", "name", "color"})
.values({7, "george", "green"})
.execute();
using namespace matador::test;
product p;
p.discontinued = false;
p.reorder_level = 1;
p.units_in_order = 2;
p.units_in_stock = 100;
p.unit_price = 49;
p.quantity_per_unit = "pcs";
p.category = make_foreign<matador::test::category>();
p.category->id = 7;
p.supplier = make_foreign<matador::test::supplier>();
p.supplier->id = 13;
p.product_name = "candle";
REQUIRE(res.first == 1);
REQUIRE(res.second == R"(INSERT INTO "person" ("id", "name", "color") VALUES (7, 'george', 'green'))");
res = s.update("product")
.set(p)
res = s.update("person")
.set({
{"name", "george"},
{"color", "green"}
})
.where("id"_col == 9)
.execute();
REQUIRE(res.second == R"(UPDATE "product" SET "product_name"='candle', "supplier_id"=13, "category_id"=7, "quantity_per_unit"='pcs', "unit_price"=49, "units_in_stock"=100, "units_in_order"=2, "reorder_level"=1, "discontinued"=0 WHERE "id" = 9)");
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]") {