Compare commits

...

4 Commits

Author SHA1 Message Date
Sascha Kühl c28ba7da5e added test 2025-05-08 22:04:45 +02:00
Sascha Kühl 62e3d18144 formatted file 2025-05-08 22:04:25 +02:00
Sascha Kühl 7d47ef0b82 use static and reinterpret cast instead of c cast 2025-05-08 22:04:14 +02:00
Sascha Kühl c9cf4b8997 added missing include 2025-05-08 22:03:42 +02:00
4 changed files with 361 additions and 52 deletions

View File

@ -7,67 +7,68 @@
#include "matador/object/schema.hpp" #include "matador/object/schema.hpp"
namespace matador::orm { namespace matador::orm {
struct entity_insert_data { struct entity_insert_data {
sql::table table; sql::table table;
std::vector<std::string> columns; std::vector<std::string> columns;
std::vector<utils::database_type> values; std::vector<utils::database_type> values;
}; };
enum class insert_build_error : std::uint8_t { enum class insert_build_error : std::uint8_t {
Ok = 0, Ok = 0,
UnknownType, UnknownType,
MissingPrimaryKey, MissingPrimaryKey,
UnexpectedError UnexpectedError
}; };
class insert_builder_exception final : public std::exception class insert_builder_exception final : public std::exception {
{
public: public:
explicit insert_builder_exception(const insert_build_error error) : error_(error) {} explicit insert_builder_exception(const insert_build_error error) : error_(error) {
}
[[nodiscard]] insert_build_error error() const { return error_; } [[nodiscard]] insert_build_error error() const { return error_; }
private: private:
const insert_build_error error_; const insert_build_error error_;
}; };
class session_insert_builder final { class session_insert_builder final {
public: public:
explicit session_insert_builder(const object::schema &scm) explicit session_insert_builder(const object::schema &scm)
: schema_(scm) {} : schema_(scm) {
}
template<class EntityType> template<class EntityType>
utils::result<std::vector<entity_insert_data>, insert_build_error> build(const EntityType &obj) { utils::result<std::vector<entity_insert_data>, insert_build_error> build(const EntityType &obj) {
auto info = schema_.info<EntityType>(); auto info = schema_.info<EntityType>();
if (!info) { if (!info) {
return utils::failure(insert_build_error::UnknownType); return utils::failure(insert_build_error::UnknownType);
}
table_info_stack_.push({info.value()});
entity_insert_data_ = {{sql::table{info.value().get().name()}}};
// processed_tables_.insert({info->get().name(), entity_insert_data_.root_table});
try {
access::process(*this, obj);
return {utils::ok(std::move(entity_insert_data_))};
} catch (const insert_builder_exception &ex) {
return {utils::failure(ex.error())};
} catch (...) {
return {utils::failure(insert_build_error::UnexpectedError)};
}
} }
table_info_stack_.push({info.value()});
entity_insert_data_ = {{sql::table{info.value().get().name()}}};
// processed_tables_.insert({info->get().name(), entity_insert_data_.root_table});
try {
access::process(*this, obj);
template < class V > return {utils::ok(std::move(entity_insert_data_))};
void on_primary_key(const char *id, V &x, std::enable_if_t<std::is_integral_v<V> && !std::is_same_v<bool, V>>* = nullptr) } catch (const insert_builder_exception &ex) {
{ return {utils::failure(ex.error())};
push(id, x); } catch (...) {
return {utils::failure(insert_build_error::UnexpectedError)};
} }
}
void on_primary_key(const char *id, std::string &, size_t); template<class V>
void on_revision(const char *id, unsigned long long &/*rev*/); void on_primary_key(const char *id, V &x,
std::enable_if_t<std::is_integral_v<V> && !std::is_same_v<bool, V>> * = nullptr) {
push(id, x);
}
template<typename Type> void on_primary_key(const char *id, std::string &, size_t);
void on_attribute(const char *id, Type &, const utils::field_attributes &/*attr*/ = utils::null_attributes) { 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) {
push(id, x);
} }
template<class Pointer> template<class Pointer>
@ -79,11 +80,13 @@ public:
} }
template<class ContainerType> template<class ContainerType>
void on_has_many(const char * /*id*/, ContainerType &, const char *join_column, const utils::foreign_attributes &attr) { void on_has_many(const char * /*id*/, ContainerType &, const char *join_column,
const utils::foreign_attributes &attr) {
} }
template<class ContainerType> template<class ContainerType>
void on_has_many_to_many(const char *id, ContainerType &/*cont*/, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &attr) { void on_has_many_to_many(const char *id, ContainerType &/*cont*/, const char *join_column,
const char *inverse_join_column, const utils::foreign_attributes &attr) {
} }
template<class ContainerType> template<class ContainerType>
@ -100,11 +103,10 @@ private:
}; };
std::stack<table_info> table_info_stack_; std::stack<table_info> table_info_stack_;
std::unordered_map<std::string, std::shared_ptr<sql::table>> processed_tables_; std::unordered_map<std::string, std::shared_ptr<sql::table> > processed_tables_;
const object::schema &schema_; const object::schema &schema_;
std::vector<entity_insert_data> entity_insert_data_; std::vector<entity_insert_data> entity_insert_data_;
}; };
} }
#endif //SESSION_INSERT_BUILDER_HPP #endif //SESSION_INSERT_BUILDER_HPP

View File

@ -15,13 +15,13 @@ enum class cascade_type
ALL = REMOVE | UPDATE | INSERT /**< Cascade type all */ ALL = REMOVE | UPDATE | INSERT /**< Cascade type all */
}; };
inline cascade_type operator~ (cascade_type a) { return (cascade_type)~(int)a; } inline cascade_type operator~ (cascade_type a) { return static_cast<cascade_type>(~static_cast<int>(a)); }
inline cascade_type operator| (cascade_type a, cascade_type b) { return (cascade_type)((int)a | (int)b); } inline cascade_type operator| (cascade_type a, cascade_type b) { return static_cast<cascade_type>(static_cast<int>(a) | static_cast<int>(b)); }
inline cascade_type operator& (cascade_type a, cascade_type b) { return (cascade_type)((int)a & (int)b); } inline cascade_type operator& (cascade_type a, cascade_type b) { return static_cast<cascade_type>(static_cast<int>(a) & static_cast<int>(b)); }
inline cascade_type operator^ (cascade_type a, cascade_type b) { return (cascade_type)((int)a ^ (int)b); } inline cascade_type operator^ (cascade_type a, cascade_type b) { return static_cast<cascade_type>(static_cast<int>(a) ^ static_cast<int>(b)); }
inline cascade_type& operator|= (cascade_type& a, cascade_type b) { return (cascade_type&)((int&)a |= (int)b); } inline cascade_type& operator|= (cascade_type& a, cascade_type b) { return reinterpret_cast<cascade_type &>(reinterpret_cast<int &>(a) |= static_cast<int>(b)); }
inline cascade_type& operator&= (cascade_type& a, cascade_type b) { return (cascade_type&)((int&)a &= (int)b); } inline cascade_type& operator&= (cascade_type& a, cascade_type b) { return reinterpret_cast<cascade_type &>(reinterpret_cast<int &>(a) &= static_cast<int>(b)); }
inline cascade_type& operator^= (cascade_type& a, cascade_type b) { return (cascade_type&)((int&)a ^= (int)b); } inline cascade_type& operator^= (cascade_type& a, cascade_type b) { return reinterpret_cast<cascade_type &>(reinterpret_cast<int &>(a) ^= static_cast<int>(b)); }
} }
#endif //OOS_CASCADE_TYPE_HPP #endif //OOS_CASCADE_TYPE_HPP

View File

@ -3,6 +3,7 @@
#include <array> #include <array>
#include <string> #include <string>
#include <cstdint>
namespace matador::utils { namespace matador::utils {

View File

@ -0,0 +1,306 @@
#include <iostream>
#include <catch2/catch_test_macros.hpp>
#include "matador/sql/backend_provider.hpp"
#include "matador/sql/connection.hpp"
#include "matador/sql/column.hpp"
#include "matador/sql/table.hpp"
#include "matador/query/query.hpp"
#include "matador/orm/session_query_builder.hpp"
#include "../backend/test_connection.hpp"
#include "../backend/test_backend_service.hpp"
#include "../../models/airplane.hpp"
#include "../../models/author.hpp"
#include "../../models/department.hpp"
#include "../../models/book.hpp"
#include "../../models/flight.hpp"
#include "../../models/recipe.hpp"
#include "../../models/order.hpp"
#include "../../models/student.hpp"
using namespace matador::object;
using namespace matador::orm;
using namespace matador::query;
using namespace matador::sql;
using namespace matador::test;
TEST_CASE("Create sql query data for entity with eager has one", "[query][entity][builder]") {
using namespace matador::test;
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
connection db("noop://noop.db");
schema scm("noop");
auto result = scm.attach<airplane>("airplanes")
.and_then( [&scm] { return scm.attach<flight>("flights"); } );
REQUIRE(result);
session_query_builder eqb(scm);
auto data = eqb.build<flight>(17U);
REQUIRE(data.is_ok());
REQUIRE(data->root_table->name == "flights");
REQUIRE(data->joins.size() == 1);
const std::vector<column> expected_columns {
{ "flights", "id", "c01" },
{ "airplanes", "id", "c02" },
{ "airplanes", "brand", "c03" },
{ "airplanes", "model", "c04" },
{ "flights", "pilot_name", "c05" },
};
REQUIRE(data->columns.size() == expected_columns.size());
for (size_t i = 0; i != expected_columns.size(); ++i) {
REQUIRE(expected_columns[i].equals(data->columns[i]));
}
std::vector<std::pair<std::string, std::string>> expected_join_data {
{ "airplanes", R"("t01"."airplane_id" = "t02"."id")"}
};
query_context qc;
size_t index{0};
for (const auto &jd : data->joins) {
REQUIRE(jd.join_table->name == expected_join_data[index].first);
REQUIRE(jd.condition->evaluate(db.dialect(), qc) == expected_join_data[index].second);
++index;
}
REQUIRE(data->where_clause);
auto cond = data->where_clause->evaluate(db.dialect(), qc);
REQUIRE(cond == R"("t01"."id" = 17)");
}
TEST_CASE("Create sql query data for entity with eager belongs to", "[query][entity][builder]") {
using namespace matador::test;
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
connection db("noop://noop.db");
schema scm("noop");
auto result = scm.attach<author>("authors")
.and_then( [&scm] { return scm.attach<book>("books"); } );
REQUIRE(result);
session_query_builder eqb(scm);
auto data = eqb.build<book>(17);
REQUIRE(data.is_ok());
REQUIRE(data->root_table->name == "books");
REQUIRE(data->joins.size() == 1);
const std::vector<column> expected_columns {
{ "books", "id", "c01" },
{ "books", "title", "c02" },
{ "authors", "id", "c03" },
{ "authors", "first_name", "c04" },
{ "authors", "last_name", "c05" },
{ "authors", "date_of_birth", "c06" },
{ "authors", "year_of_birth", "c07" },
{ "authors", "distinguished", "c08" },
{ "books", "published_in", "c09" }
};
REQUIRE(data->columns.size() == expected_columns.size());
for (size_t i = 0; i != expected_columns.size(); ++i) {
REQUIRE(expected_columns[i].equals(data->columns[i]));
}
std::vector<std::pair<std::string, std::string>> expected_join_data {
{ "authors", R"("t01"."author_id" = "t02"."id")"}
};
query_context qc;
size_t index{0};
for (const auto &jd : data->joins) {
REQUIRE(jd.join_table->name == expected_join_data[index].first);
REQUIRE(jd.condition->evaluate(db.dialect(), qc) == expected_join_data[index].second);
++index;
}
REQUIRE(data->where_clause);
auto cond = data->where_clause->evaluate(db.dialect(), qc);
REQUIRE(cond == R"("t01"."id" = 17)");
auto q = matador::query::query::select(data->columns)
.from(data->root_table->name);
for (auto &jd : data->joins) {
q.join_left(*jd.join_table)
.on(std::move(jd.condition));
}
auto context = q
.where(std::move(data->where_clause))
.str(db);
}
TEST_CASE("Create sql query data for entity with eager has many belongs to", "[query][entity][builder]") {
using namespace matador::test;
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
connection db("noop://noop.db");
schema scm("noop");
auto result = scm.attach<product>("products")
.and_then( [&scm] { return scm.attach<order_details>("order_details"); } )
.and_then( [&scm] { return scm.attach<supplier>("suppliers"); } )
.and_then( [&scm] { return scm.attach<category>("categories"); } )
.and_then( [&scm] { return scm.attach<order>("orders"); } );
REQUIRE(result);
session_query_builder eqb(scm);
auto data = eqb.build<order>(17);
REQUIRE(data.is_ok());
REQUIRE(data->root_table->name == "orders");
REQUIRE(data->joins.size() == 1);
const std::vector<column> expected_columns = {
{ "orders", "order_id", "c01" },
{ "orders", "order_date", "c02" },
{ "orders", "required_date", "c03" },
{ "orders", "shipped_date", "c04" },
{ "orders", "ship_via", "c05" },
{ "orders", "freight", "c06" },
{ "orders", "ship_name", "c07" },
{ "orders", "ship_address", "c08" },
{ "orders", "ship_city", "c09" },
{ "orders", "ship_region", "c10" },
{ "orders", "ship_postal_code", "c11" },
{ "orders", "ship_country", "c12" },
{ "order_details", "order_details_id", "c13" },
{ "order_details", "order_id", "c14" },
{ "order_details", "product_id", "c15" }
};
REQUIRE(data->columns.size() == expected_columns.size());
for (size_t i = 0; i != expected_columns.size(); ++i) {
REQUIRE(expected_columns[i].equals(data->columns[i]));
}
std::vector<std::pair<std::string, std::string>> expected_join_data {
{ "order_details", R"("t01"."order_id" = "t02"."order_id")"}
};
query_context qc;
size_t index{0};
for (const auto &jd : data->joins) {
REQUIRE(jd.join_table->name == expected_join_data[index].first);
REQUIRE(jd.condition->evaluate(db.dialect(), qc) == expected_join_data[index].second);
++index;
}
REQUIRE(data->where_clause);
auto cond = data->where_clause->evaluate(db.dialect(), qc);
REQUIRE(cond == R"("t01"."order_id" = 17)");
}
TEST_CASE("Create sql query data for entity with eager many to many", "[query][entity][builder]") {
using namespace matador::test;
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
connection db("noop://noop.db");
schema scm("noop");
auto result = scm.attach<recipe>("recipes")
.and_then( [&scm] { return scm.attach<ingredient>("ingredients"); } )
.and_then( [&scm] { return scm.attach<recipe_ingredient>("recipe_ingredients"); } );
session_query_builder eqb(scm);
auto data = eqb.build<ingredient>(17);
REQUIRE(data.is_ok());
REQUIRE(data->root_table->name == "ingredients");
REQUIRE(data->joins.size() == 2);
const std::vector<column> expected_columns {
{ "ingredients", "id", "c01" },
{ "ingredients", "name", "c02" },
{ "recipes", "id", "c03" },
{ "recipes", "name", "c04" }
};
REQUIRE(data->columns.size() == expected_columns.size());
for (size_t i = 0; i != expected_columns.size(); ++i) {
REQUIRE(expected_columns[i].equals(data->columns[i]));
}
std::vector<std::pair<std::string, std::string>> expected_join_data {
{ "recipe_ingredients", R"("t01"."id" = "t02"."ingredient_id")"},
{ "recipes", R"("t02"."recipe_id" = "t03"."id")"}
};
query_context qc;
size_t index{0};
for (const auto &jd : data->joins) {
REQUIRE(jd.join_table->name == expected_join_data[index].first);
REQUIRE(jd.condition->evaluate(db.dialect(), qc) == expected_join_data[index].second);
++index;
}
REQUIRE(data->where_clause);
auto cond = data->where_clause->evaluate(db.dialect(), qc);
REQUIRE(cond == R"("t01"."id" = 17)");
}
TEST_CASE("Create sql query data for entity with eager many to many (inverse part)", "[query][entity][builder]") {
using namespace matador::test;
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
connection db("noop://noop.db");
schema scm("noop");
auto result = scm.attach<student>("students")
.and_then( [&scm] { return scm.attach<course>("courses"); } )
.and_then( [&scm] { return scm.attach<student_course>("student_courses"); } );
session_query_builder eqb(scm);
auto data = eqb.build<course>(17);
REQUIRE(data.is_ok());
REQUIRE(data->root_table->name == "courses");
REQUIRE(data->joins.size() == 2);
const std::vector<column> expected_columns {
{ "courses", "id", "c01" },
{ "courses", "title", "c02" },
{ "students", "id", "c03" },
{ "students", "name", "c04" }
};
REQUIRE(data->columns.size() == expected_columns.size());
for (size_t i = 0; i != expected_columns.size(); ++i) {
REQUIRE(expected_columns[i].equals(data->columns[i]));
}
std::vector<std::pair<std::string, std::string>> expected_join_data {
{ "student_courses", R"("t01"."id" = "t02"."course_id")"},
{ "students", R"("t02"."student_id" = "t03"."id")"}
};
query_context qc;
size_t index{0};
for (const auto &jd : data->joins) {
REQUIRE(jd.join_table->name == expected_join_data[index].first);
REQUIRE(jd.condition->evaluate(db.dialect(), qc) == expected_join_data[index].second);
++index;
}
REQUIRE(data->where_clause);
auto cond = data->where_clause->evaluate(db.dialect(), qc);
REQUIRE(cond == R"("t01"."id" = 17)");
}
TEST_CASE("Test eager relationship", "[session][eager]") {
using namespace matador::test;
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
connection db("noop://noop.db");
schema scm("noop");
auto result = scm.attach<department>("departments")
.and_then( [&scm] { return scm.attach<employee>("employees"); } );
session_query_builder eqb(scm);
auto data = eqb.build<department>();
REQUIRE(data.is_ok());
auto ctx = query::select(data->columns)
.from(*data->root_table)
.join_left(data->joins)
.where(std::move(data->where_clause))
.order_by(column{data->root_table, data->pk_column_name})
.asc()
.str(db);
std::cout << ctx << std::endl;
}