progress on session_insert_builder
This commit is contained in:
parent
c28ba7da5e
commit
e72733d37e
|
|
@ -136,12 +136,12 @@ public:
|
||||||
* Returns true if the schema contains
|
* Returns true if the schema contains
|
||||||
* no schema nodes.
|
* no schema nodes.
|
||||||
*
|
*
|
||||||
* @return True if schema is empty
|
* @return True if the schema is empty
|
||||||
*/
|
*/
|
||||||
[[nodiscard]] bool empty() const;
|
[[nodiscard]] bool empty() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current number of schema node.
|
* Returns the current number of the schema node.
|
||||||
*
|
*
|
||||||
* @return Number of schema nodes
|
* @return Number of schema nodes
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
#ifndef QUERY_BUILDER_EXCEPTION_HPP
|
||||||
|
#define QUERY_BUILDER_EXCEPTION_HPP
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <system_error>
|
||||||
|
|
||||||
|
namespace matador::orm {
|
||||||
|
|
||||||
|
enum class query_build_error : std::uint8_t {
|
||||||
|
Ok = 0,
|
||||||
|
UnknownType,
|
||||||
|
MissingPrimaryKey,
|
||||||
|
UnexpectedError
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class query_builder_exception final : public std::exception {
|
||||||
|
public:
|
||||||
|
explicit query_builder_exception(const query_build_error error) : error_(error) {}
|
||||||
|
|
||||||
|
[[nodiscard]] query_build_error error() const { return error_; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
const query_build_error error_;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //QUERY_BUILDER_EXCEPTION_HPP
|
||||||
|
|
@ -1,11 +1,15 @@
|
||||||
#ifndef SESSION_INSERT_BUILDER_HPP
|
#ifndef SESSION_INSERT_BUILDER_HPP
|
||||||
#define SESSION_INSERT_BUILDER_HPP
|
#define SESSION_INSERT_BUILDER_HPP
|
||||||
|
|
||||||
|
#include "matador/orm/query_builder_exception.hpp"
|
||||||
|
|
||||||
#include "matador/query/condition.hpp"
|
#include "matador/query/condition.hpp"
|
||||||
#include "matador/query/query_intermediates.hpp"
|
#include "matador/query/query_intermediates.hpp"
|
||||||
|
|
||||||
#include "matador/object/schema.hpp"
|
#include "matador/object/schema.hpp"
|
||||||
|
|
||||||
|
#include "matador/utils/cascade_type.hpp"
|
||||||
|
|
||||||
namespace matador::orm {
|
namespace matador::orm {
|
||||||
struct entity_insert_data {
|
struct entity_insert_data {
|
||||||
sql::table table;
|
sql::table table;
|
||||||
|
|
@ -58,8 +62,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class V>
|
template<class V>
|
||||||
void on_primary_key(const char *id, V &x,
|
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) {
|
||||||
std::enable_if_t<std::is_integral_v<V> && !std::is_same_v<bool, V>> * = nullptr) {
|
|
||||||
push(id, x);
|
push(id, x);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -73,10 +76,27 @@ public:
|
||||||
|
|
||||||
template<class Pointer>
|
template<class Pointer>
|
||||||
void on_belongs_to(const char *id, Pointer &obj, const utils::foreign_attributes &attr) {
|
void on_belongs_to(const char *id, Pointer &obj, const utils::foreign_attributes &attr) {
|
||||||
|
if (!utils::is_cascade_type_set(attr.cascade(), utils::cascade_type::INSERT)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto info = schema_.info<typename Pointer::value_type::value_type>();
|
||||||
|
if (!info) {
|
||||||
|
throw query_builder_exception{query_build_error::UnknownType};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class Pointer>
|
template<class Pointer>
|
||||||
void on_has_one(const char *id, Pointer &obj, const utils::foreign_attributes &attr) {
|
void on_has_one(const char *id, Pointer &obj, const utils::foreign_attributes &attr) {
|
||||||
|
if (!utils::is_cascade_type_set(attr.cascade(), utils::cascade_type::INSERT)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto info = schema_.info<typename Pointer::value_type::value_type>();
|
||||||
|
if (!info) {
|
||||||
|
throw query_builder_exception{query_build_error::UnknownType};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class ContainerType>
|
template<class ContainerType>
|
||||||
|
|
@ -94,7 +114,6 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
[[nodiscard]] bool is_root_entity() const;
|
|
||||||
void push(const std::string &column_name, const utils::database_type &value);
|
void push(const std::string &column_name, const utils::database_type &value);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
#ifndef QUERY_ENTITY_QUERY_BUILDER_HPP
|
#ifndef QUERY_ENTITY_QUERY_BUILDER_HPP
|
||||||
#define QUERY_ENTITY_QUERY_BUILDER_HPP
|
#define QUERY_ENTITY_QUERY_BUILDER_HPP
|
||||||
|
|
||||||
|
#include "matador/orm/query_builder_exception.hpp"
|
||||||
|
|
||||||
#include "matador/query/condition.hpp"
|
#include "matador/query/condition.hpp"
|
||||||
#include "matador/query/query_intermediates.hpp"
|
#include "matador/query/query_intermediates.hpp"
|
||||||
|
|
||||||
|
|
@ -66,24 +68,6 @@ struct entity_query_data {
|
||||||
std::unique_ptr<query::basic_condition> where_clause{};
|
std::unique_ptr<query::basic_condition> where_clause{};
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class query_build_error : std::uint8_t {
|
|
||||||
Ok = 0,
|
|
||||||
UnknownType,
|
|
||||||
MissingPrimaryKey,
|
|
||||||
UnexpectedError
|
|
||||||
};
|
|
||||||
|
|
||||||
class query_builder_exception final : public std::exception
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
explicit query_builder_exception(const query_build_error error) : error_(error) {}
|
|
||||||
|
|
||||||
[[nodiscard]] query_build_error error() const { return error_; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
const query_build_error error_;
|
|
||||||
};
|
|
||||||
|
|
||||||
class session_query_builder final
|
class session_query_builder final
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
|
||||||
|
|
@ -23,5 +23,7 @@ inline cascade_type& operator|= (cascade_type& a, cascade_type b) { return reint
|
||||||
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 reinterpret_cast<cascade_type &>(reinterpret_cast<int &>(a) &= static_cast<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 reinterpret_cast<cascade_type &>(reinterpret_cast<int &>(a) ^= static_cast<int>(b)); }
|
||||||
|
|
||||||
|
inline bool is_cascade_type_set(const cascade_type source, const cascade_type needle) { return static_cast<int>(source & needle) > 0; }
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif //OOS_CASCADE_TYPE_HPP
|
#endif //OOS_CASCADE_TYPE_HPP
|
||||||
|
|
|
||||||
|
|
@ -14,23 +14,12 @@ enum class constraints : unsigned char {
|
||||||
// UNIQUE_NOT_NULL = UNIQUE | NOT_NULL
|
// UNIQUE_NOT_NULL = UNIQUE | NOT_NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
inline constraints operator|(constraints a, constraints b)
|
inline constraints operator|(constraints a, constraints b) { return static_cast<constraints>(static_cast<unsigned int>(a) | static_cast<unsigned int>(b)); }
|
||||||
{
|
inline constraints operator&(constraints a, constraints b) { return static_cast<constraints>(static_cast<unsigned int>(a) & static_cast<unsigned int>(b)); }
|
||||||
return static_cast<constraints>(static_cast<unsigned int>(a) | static_cast<unsigned int>(b));
|
|
||||||
}
|
|
||||||
|
|
||||||
inline constraints operator&(constraints a, constraints b)
|
|
||||||
{
|
|
||||||
return static_cast<constraints>(static_cast<unsigned int>(a) & static_cast<unsigned int>(b));
|
|
||||||
}
|
|
||||||
|
|
||||||
inline constraints& operator|= (constraints& a, constraints b) { return (constraints&)((int&)a |= (int)b); }
|
inline constraints& operator|= (constraints& a, constraints b) { return (constraints&)((int&)a |= (int)b); }
|
||||||
inline constraints& operator&= (constraints& a, constraints b) { return (constraints&)((int&)a &= (int)b); }
|
inline constraints& operator&= (constraints& a, constraints b) { return (constraints&)((int&)a &= (int)b); }
|
||||||
|
|
||||||
inline bool is_constraint_set(const constraints source, const constraints needle)
|
inline bool is_constraint_set(const constraints source, const constraints needle) { return static_cast<int>(source & needle) > 0; }
|
||||||
{
|
|
||||||
return static_cast<int>(source & needle) > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ add_executable(OrmTests
|
||||||
backend/test_result_reader.hpp
|
backend/test_result_reader.hpp
|
||||||
backend/test_statement.cpp
|
backend/test_statement.cpp
|
||||||
backend/test_statement.hpp
|
backend/test_statement.hpp
|
||||||
|
orm/SessionInsertBuilderTest.cpp
|
||||||
orm/SessionQueryBuilderTest.cpp
|
orm/SessionQueryBuilderTest.cpp
|
||||||
query/ConditionTests.cpp
|
query/ConditionTests.cpp
|
||||||
query/QueryBuilderTest.cpp
|
query/QueryBuilderTest.cpp
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@
|
||||||
|
|
||||||
#include "matador/query/query.hpp"
|
#include "matador/query/query.hpp"
|
||||||
|
|
||||||
#include "matador/orm/session_query_builder.hpp"
|
#include "matador/orm/session_insert_builder.hpp"
|
||||||
|
|
||||||
#include "../backend/test_connection.hpp"
|
#include "../backend/test_connection.hpp"
|
||||||
#include "../backend/test_backend_service.hpp"
|
#include "../backend/test_backend_service.hpp"
|
||||||
|
|
@ -28,7 +28,7 @@ using namespace matador::query;
|
||||||
using namespace matador::sql;
|
using namespace matador::sql;
|
||||||
using namespace matador::test;
|
using namespace matador::test;
|
||||||
|
|
||||||
TEST_CASE("Create sql query data for entity with eager has one", "[query][entity][builder]") {
|
TEST_CASE("Create sql insert for entity with eager has one", "[query][entity][insert][builder]") {
|
||||||
using namespace matador::test;
|
using namespace matador::test;
|
||||||
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
|
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
|
||||||
connection db("noop://noop.db");
|
connection db("noop://noop.db");
|
||||||
|
|
@ -37,270 +37,12 @@ TEST_CASE("Create sql query data for entity with eager has one", "[query][entity
|
||||||
.and_then( [&scm] { return scm.attach<flight>("flights"); } );
|
.and_then( [&scm] { return scm.attach<flight>("flights"); } );
|
||||||
REQUIRE(result);
|
REQUIRE(result);
|
||||||
|
|
||||||
session_query_builder eqb(scm);
|
session_insert_builder eib(scm);
|
||||||
|
|
||||||
auto data = eqb.build<flight>(17U);
|
auto b737 = object_ptr(new airplane{0, "Boeing", "737"});
|
||||||
|
flight f1{0, b737, "F828"};
|
||||||
|
|
||||||
|
auto data = eib.build(f1);
|
||||||
|
|
||||||
REQUIRE(data.is_ok());
|
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;
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue