added session tests
This commit is contained in:
parent
5a3cdc8645
commit
f977b2afc9
|
|
@ -370,7 +370,7 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
|
|||
|
||||
auto expected_flight = flights[0];
|
||||
|
||||
REQUIRE(result.has_value());
|
||||
REQUIRE(result);
|
||||
REQUIRE(result->id == expected_flight->id);
|
||||
REQUIRE(result->pilot_name == expected_flight->pilot_name);
|
||||
REQUIRE(result->airplane.get());
|
||||
|
|
|
|||
|
|
@ -51,6 +51,9 @@ TEST_CASE_METHOD(SessionFixture, "Find object with id", "[session][find]") {
|
|||
ses.create_schema();
|
||||
auto a380 = ses.insert<airplane>(1, "Boeing", "A380");
|
||||
|
||||
ses.find<airplane>(1);
|
||||
auto result = ses.find<airplane>(1);
|
||||
|
||||
REQUIRE(result);
|
||||
auto read_a380 = result.value();
|
||||
REQUIRE(a380->id == read_a380->id);
|
||||
}
|
||||
|
|
@ -34,10 +34,10 @@ protected:
|
|||
class query_intermediate : public basic_query_intermediate
|
||||
{
|
||||
public:
|
||||
query_intermediate(connection &db, const sql::schema &schema, query_data &data);
|
||||
query_intermediate(connection &db, const sql::schema &schema, const std::shared_ptr<query_data> &data);
|
||||
|
||||
protected:
|
||||
query_data &data_;
|
||||
std::shared_ptr<query_data> data_;
|
||||
};
|
||||
|
||||
class query_execute_finish : public query_intermediate
|
||||
|
|
@ -64,15 +64,15 @@ public:
|
|||
query_result<record> fetch_all();
|
||||
|
||||
template < class Type >
|
||||
std::optional<Type> fetch_one()
|
||||
std::unique_ptr<Type> fetch_one()
|
||||
{
|
||||
auto result = query_result<Type>(fetch());
|
||||
auto first = result.begin();
|
||||
if (first == result.end()) {
|
||||
return std::nullopt;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return *first.get();
|
||||
return std::unique_ptr<Type>{first.release()};
|
||||
}
|
||||
std::optional<record> fetch_one();
|
||||
|
||||
|
|
@ -202,6 +202,10 @@ public:
|
|||
{
|
||||
return where_clause(std::make_unique<Condition>(std::move(cond)));
|
||||
}
|
||||
query_where_intermediate where(std::unique_ptr<basic_condition> &&cond)
|
||||
{
|
||||
return where_clause(std::move(cond));
|
||||
}
|
||||
query_group_by_intermediate group_by(const column &col);
|
||||
query_order_by_intermediate order_by(const column &col);
|
||||
|
||||
|
|
@ -215,7 +219,7 @@ public:
|
|||
explicit query_start_intermediate(connection &db, const sql::schema &schema);
|
||||
|
||||
protected:
|
||||
query_data data_;
|
||||
std::shared_ptr<query_data> data_ { std::make_shared<query_data>() };
|
||||
};
|
||||
|
||||
class query_select_intermediate : public query_start_intermediate
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include "matador/sql/connection.hpp"
|
||||
#include "matador/sql/connection_pool.hpp"
|
||||
#include "matador/sql/entity.hpp"
|
||||
#include "matador/sql/entity_query_builder.hpp"
|
||||
#include "matador/sql/statement.hpp"
|
||||
#include "matador/sql/schema.hpp"
|
||||
|
||||
|
|
@ -13,100 +14,6 @@ namespace matador::sql {
|
|||
|
||||
class dialect;
|
||||
|
||||
struct join_data
|
||||
{
|
||||
table join_table;
|
||||
std::unique_ptr<basic_condition> condition;
|
||||
};
|
||||
|
||||
//class join_collector
|
||||
//{
|
||||
//private:
|
||||
// explicit join_collector(const sql::schema &ts, std::vector<join_data> &joins)
|
||||
// : table_schema_(ts)
|
||||
// , joins_(joins) {}
|
||||
//
|
||||
//public:
|
||||
// template < class Type >
|
||||
// static std::vector<join_data> collect(const sql::schema &ts)
|
||||
// {
|
||||
// const auto info = ts.info<Type>();
|
||||
// if (!info) {
|
||||
// return {};
|
||||
// }
|
||||
// std::vector<join_data> joins;
|
||||
// join_collector collector(ts, joins);
|
||||
// Type obj;
|
||||
// matador::utils::access::process(collector, obj);
|
||||
// return joins;
|
||||
// }
|
||||
//
|
||||
// template < class PrimaryKeyType >
|
||||
// void on_primary_key(const char *id, PrimaryKeyType &, typename std::enable_if<std::is_integral<PrimaryKeyType>::value && !std::is_same<bool, PrimaryKeyType>::value>::type* = 0) {}
|
||||
// void on_primary_key(const char *id, std::string &, size_t) {}
|
||||
// void on_revision(const char *id, unsigned long long &/*rev*/) {}
|
||||
// template<typename Type>
|
||||
// void on_attribute(const char *id, Type &, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
|
||||
//
|
||||
// template<class Pointer>
|
||||
// void on_belongs_to(const char *id, Pointer &, const utils::foreign_attributes &attr)
|
||||
// {
|
||||
// if (attr.fetch() == utils::fetch_type::LAZY) {
|
||||
// push(id);
|
||||
// } else {
|
||||
// const auto info = table_schema_.info<typename Pointer::value_type>();
|
||||
// if (!info) {
|
||||
// return;
|
||||
// }
|
||||
// table_name_stack_.push(info.value().name);
|
||||
// typename Pointer::value_type obj;
|
||||
// matador::utils::access::process(*this, obj);
|
||||
// table_name_stack_.pop();
|
||||
// }
|
||||
// }
|
||||
// template<class Pointer>
|
||||
// void on_has_one(const char *id, Pointer &, const utils::foreign_attributes &attr)
|
||||
// {
|
||||
// if (attr.fetch() == utils::fetch_type::LAZY || force_lazy_) {
|
||||
// push(id);
|
||||
// } else {
|
||||
// const auto info = table_schema_.info<typename Pointer::value_type>();
|
||||
// if (!info) {
|
||||
// return;
|
||||
// }
|
||||
// table_name_stack_.push(info.value().name);
|
||||
// typename Pointer::value_type obj;
|
||||
// matador::utils::access::process(*this, obj);
|
||||
// table_name_stack_.pop();
|
||||
// }
|
||||
// }
|
||||
// template<class ContainerType>
|
||||
// void on_has_many(const char *, ContainerType &, const char *, const char *, const utils::foreign_attributes &attr)
|
||||
// {
|
||||
// if (attr.fetch() == utils::fetch_type::LAZY || force_lazy_) {
|
||||
// return;
|
||||
// }
|
||||
// const auto info = table_schema_.info<typename ContainerType::value_type::value_type>();
|
||||
// if (!info) {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// table_name_stack_.push(info.value().name);
|
||||
// typename ContainerType::value_type::value_type obj;
|
||||
// matador::utils::access::process(*this, obj);
|
||||
// table_name_stack_.pop();
|
||||
// }
|
||||
// template<class ContainerType>
|
||||
// void on_has_many(const char *id, ContainerType &c, const utils::foreign_attributes &attr)
|
||||
// {
|
||||
// on_has_many(id, c, "", "", attr);
|
||||
// }
|
||||
//
|
||||
//private:
|
||||
// const sql::schema &table_schema_;
|
||||
// std::vector<join_data> &joins_;
|
||||
//};
|
||||
|
||||
class session
|
||||
{
|
||||
public:
|
||||
|
|
@ -126,33 +33,35 @@ public:
|
|||
}
|
||||
|
||||
template<typename Type, typename PrimaryKeyType>
|
||||
entity<Type> find(const PrimaryKeyType &pk) {
|
||||
std::optional<entity<Type>> find(const PrimaryKeyType &pk) {
|
||||
auto c = pool_.acquire();
|
||||
if (!c.valid()) {
|
||||
throw std::logic_error("no database connection available");
|
||||
}
|
||||
// collect all columns
|
||||
// - evaluate fetch::Eager flag for relations
|
||||
auto info = schema_->info<Type>();
|
||||
if (!info) {
|
||||
return {};
|
||||
}
|
||||
|
||||
auto columns = sql::column_generator::generate<Type>(*schema_);
|
||||
entity_query_builder<PrimaryKeyType> eqb(pk, *schema_);
|
||||
auto data = eqb.template build<Type>();
|
||||
|
||||
// auto joins = sql
|
||||
c->query(*schema_).select({}).from(info->name);
|
||||
// build pk where condition
|
||||
// - check if type has pk
|
||||
// - check type
|
||||
// pk_condition_builder<Type> builder;
|
||||
// auto cond = builder.build(pk);
|
||||
auto q = c->query(*schema_)
|
||||
.select(data->columns)
|
||||
.from(data->root_table_name);
|
||||
|
||||
// create query with relations as requested
|
||||
//
|
||||
// c->query(*schema_).select<Type>().from("xyz").where().fetch_all();
|
||||
for (auto &jd : data->joins) {
|
||||
q.join_left(jd.join_table)
|
||||
.on(std::move(jd.condition));
|
||||
}
|
||||
auto e = q
|
||||
.where(std::move(data->where_clause))
|
||||
.template fetch_one<Type>();
|
||||
|
||||
return {};
|
||||
if (!e) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return entity<Type>{ e.release() };
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
|
|
@ -167,8 +76,6 @@ public:
|
|||
std::vector<sql::column_definition> describe_table(const std::string &table_name) const;
|
||||
bool table_exists(const std::string &table_name) const;
|
||||
|
||||
[[nodiscard]] const schema& tables() const;
|
||||
|
||||
const class dialect& dialect() const;
|
||||
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -11,13 +11,13 @@ basic_query_intermediate::basic_query_intermediate(connection &db, const sql::sc
|
|||
query_result<record> query_select_finish::fetch_all()
|
||||
{
|
||||
query_compiler compiler(connection_.dialect());
|
||||
return connection_.fetch(compiler.compile(&data_));
|
||||
return connection_.fetch(compiler.compile(data_.get()));
|
||||
}
|
||||
|
||||
std::optional<record> query_select_finish::fetch_one()
|
||||
{
|
||||
query_compiler compiler(connection_.dialect());
|
||||
auto result = connection_.fetch(compiler.compile(&data_));
|
||||
auto result = connection_.fetch(compiler.compile(data_.get()));
|
||||
auto first = result.begin();
|
||||
if (first == result.end()) {
|
||||
return std::nullopt;
|
||||
|
|
@ -29,142 +29,142 @@ std::optional<record> query_select_finish::fetch_one()
|
|||
query_context query_select_finish::build() const
|
||||
{
|
||||
query_compiler compiler(connection_.dialect());
|
||||
return compiler.compile(&data_);
|
||||
return compiler.compile(data_.get());
|
||||
}
|
||||
|
||||
std::unique_ptr<query_result_impl> query_select_finish::fetch()
|
||||
{
|
||||
query_compiler compiler(connection_.dialect());
|
||||
return connection_.fetch(compiler.compile(&data_).sql);
|
||||
return connection_.fetch(compiler.compile(data_.get()).sql);
|
||||
}
|
||||
|
||||
statement query_select_finish::prepare()
|
||||
{
|
||||
query_compiler compiler(connection_.dialect());
|
||||
return connection_.prepare(compiler.compile(&data_));
|
||||
return connection_.prepare(compiler.compile(data_.get()));
|
||||
}
|
||||
|
||||
query_intermediate::query_intermediate(connection &db, const sql::schema &schema, query_data &data)
|
||||
query_intermediate::query_intermediate(connection &db, const sql::schema &schema, const std::shared_ptr<query_data> &data)
|
||||
: basic_query_intermediate(db, schema), data_(data) {}
|
||||
|
||||
query_offset_intermediate query_limit_intermediate::offset(size_t offset)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_offset_part>(offset));
|
||||
data_->parts.push_back(std::make_unique<query_offset_part>(offset));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_limit_intermediate query_offset_intermediate::limit(size_t limit)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_limit_part>(limit));
|
||||
data_->parts.push_back(std::make_unique<query_limit_part>(limit));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_limit_intermediate query_order_direction_intermediate::limit(size_t limit)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_limit_part>(limit));
|
||||
data_->parts.push_back(std::make_unique<query_limit_part>(limit));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_order_by_intermediate query_group_by_intermediate::order_by(const column &col)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_order_by_part>(col));
|
||||
data_->parts.push_back(std::make_unique<query_order_by_part>(col));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_order_direction_intermediate query_order_by_intermediate::asc()
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_order_by_asc_part>());
|
||||
data_->parts.push_back(std::make_unique<query_order_by_asc_part>());
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_order_direction_intermediate query_order_by_intermediate::desc()
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_order_by_desc_part>());
|
||||
data_->parts.push_back(std::make_unique<query_order_by_desc_part>());
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_where_intermediate query_from_intermediate::where_clause(std::unique_ptr<basic_condition> &&cond)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_where_part>(std::move(cond)));
|
||||
data_->parts.push_back(std::make_unique<query_where_part>(std::move(cond)));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_group_by_intermediate query_from_intermediate::group_by(const column &col)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_group_by_part>(col));
|
||||
data_->parts.push_back(std::make_unique<query_group_by_part>(col));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_order_by_intermediate query_from_intermediate::order_by(const column &col)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_order_by_part>(col));
|
||||
data_->parts.push_back(std::make_unique<query_order_by_part>(col));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_group_by_intermediate query_where_intermediate::group_by(const column &col)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_group_by_part>(col));
|
||||
data_->parts.push_back(std::make_unique<query_group_by_part>(col));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_order_by_intermediate query_where_intermediate::order_by(const column &col)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_order_by_part>(col));
|
||||
data_->parts.push_back(std::make_unique<query_order_by_part>(col));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_join_intermediate query_on_intermediate::join_left(const table &t)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_join_part>(t));
|
||||
data_->parts.push_back(std::make_unique<query_join_part>(t));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_where_intermediate query_on_intermediate::where_clause(std::unique_ptr<basic_condition> &&cond)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_where_part>(std::move(cond)));
|
||||
data_->parts.push_back(std::make_unique<query_where_part>(std::move(cond)));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_group_by_intermediate query_on_intermediate::group_by(const column &col)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_group_by_part>(col));
|
||||
data_->parts.push_back(std::make_unique<query_group_by_part>(col));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_order_by_intermediate query_on_intermediate::order_by(const column &col)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_order_by_part>(col));
|
||||
data_->parts.push_back(std::make_unique<query_order_by_part>(col));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_on_intermediate query_join_intermediate::on_clause(std::unique_ptr<basic_condition> &&cond)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_on_part>(std::move(cond)));
|
||||
data_->parts.push_back(std::make_unique<query_on_part>(std::move(cond)));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_join_intermediate query_from_intermediate::join_left(const table &t)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_join_part>(t));
|
||||
data_->parts.push_back(std::make_unique<query_join_part>(t));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_select_intermediate::query_select_intermediate(connection &db, const sql::schema &schema, const std::vector<column>& columns)
|
||||
: query_start_intermediate(db, schema)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_select_part>(columns));
|
||||
data_->parts.push_back(std::make_unique<query_select_part>(columns));
|
||||
}
|
||||
|
||||
query_from_intermediate query_select_intermediate::from(const table& t)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_from_part>(t));
|
||||
data_->parts.push_back(std::make_unique<query_from_part>(t));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_insert_intermediate::query_insert_intermediate(connection &db, const sql::schema &schema)
|
||||
: query_start_intermediate(db, schema)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_insert_part>());
|
||||
data_->parts.push_back(std::make_unique<query_insert_part>());
|
||||
}
|
||||
|
||||
query_into_intermediate query_insert_intermediate::into(const sql::table &table, std::initializer_list<column> column_names)
|
||||
|
|
@ -174,32 +174,32 @@ query_into_intermediate query_insert_intermediate::into(const sql::table &table,
|
|||
|
||||
query_into_intermediate query_insert_intermediate::into(const table &table, std::vector<column> &&column_names)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_into_part>(table, column_names));
|
||||
data_->parts.push_back(std::make_unique<query_into_part>(table, column_names));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_into_intermediate query_insert_intermediate::into(const table &table)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_into_part>(table, table.columns));
|
||||
data_->parts.push_back(std::make_unique<query_into_part>(table, table.columns));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
size_t query_execute_finish::execute()
|
||||
{
|
||||
query_compiler compiler(connection_.dialect());
|
||||
return connection_.execute(compiler.compile(&data_).sql);
|
||||
return connection_.execute(compiler.compile(data_.get()).sql);
|
||||
}
|
||||
|
||||
statement query_execute_finish::prepare()
|
||||
{
|
||||
query_compiler compiler(connection_.dialect());
|
||||
return connection_.prepare(compiler.compile(&data_));
|
||||
return connection_.prepare(compiler.compile(data_.get()));
|
||||
}
|
||||
|
||||
query_context query_execute_finish::build() const
|
||||
{
|
||||
query_compiler compiler(connection_.dialect());
|
||||
return compiler.compile(&data_);
|
||||
return compiler.compile(data_.get());
|
||||
}
|
||||
|
||||
query_execute_finish query_into_intermediate::values(std::initializer_list<any_type> values)
|
||||
|
|
@ -209,13 +209,13 @@ query_execute_finish query_into_intermediate::values(std::initializer_list<any_t
|
|||
|
||||
query_execute_finish query_into_intermediate::values(std::vector<any_type> &&values)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_values_part>(std::move(values)));
|
||||
data_->parts.push_back(std::make_unique<query_values_part>(std::move(values)));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_create_intermediate::query_create_intermediate(connection &db, const sql::schema &schema)
|
||||
: query_start_intermediate(db, schema) {
|
||||
data_.parts.push_back(std::make_unique<query_create_part>());
|
||||
data_->parts.push_back(std::make_unique<query_create_part>());
|
||||
}
|
||||
|
||||
query_execute_finish query_create_intermediate::table(const sql::table &table, std::initializer_list<column_definition> columns)
|
||||
|
|
@ -225,38 +225,38 @@ query_execute_finish query_create_intermediate::table(const sql::table &table, s
|
|||
|
||||
query_execute_finish query_create_intermediate::table(const sql::table &table, const std::vector<column_definition> &columns)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_create_table_part>(table, columns));
|
||||
data_->parts.push_back(std::make_unique<query_create_table_part>(table, columns));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_drop_intermediate::query_drop_intermediate(connection &db, const sql::schema &schema)
|
||||
: query_start_intermediate(db, schema)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_drop_part>());
|
||||
data_->parts.push_back(std::make_unique<query_drop_part>());
|
||||
}
|
||||
|
||||
query_execute_finish query_drop_intermediate::table(const sql::table &table)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_drop_table_part>(table));
|
||||
data_->parts.push_back(std::make_unique<query_drop_table_part>(table));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_order_by_intermediate query_execute_where_intermediate::order_by(const column &col)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_order_by_part>(col));
|
||||
data_->parts.push_back(std::make_unique<query_order_by_part>(col));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_execute_where_intermediate query_set_intermediate::where_clause(std::unique_ptr<basic_condition> &&cond)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_where_part>(std::move(cond)));
|
||||
data_->parts.push_back(std::make_unique<query_where_part>(std::move(cond)));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_update_intermediate::query_update_intermediate(connection &db, const sql::schema &schema, const sql::table& table)
|
||||
: query_start_intermediate(db, schema)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_update_part>(table));
|
||||
data_->parts.push_back(std::make_unique<query_update_part>(table));
|
||||
}
|
||||
|
||||
query_set_intermediate query_update_intermediate::set(std::initializer_list<key_value_pair> columns)
|
||||
|
|
@ -266,26 +266,26 @@ query_set_intermediate query_update_intermediate::set(std::initializer_list<key_
|
|||
|
||||
query_set_intermediate query_update_intermediate::set(std::vector<key_value_pair> &&columns)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_set_part>(std::move(columns)));
|
||||
data_->parts.push_back(std::make_unique<query_set_part>(std::move(columns)));
|
||||
return {connection_, schema_, data_};
|
||||
|
||||
}
|
||||
|
||||
query_execute_where_intermediate query_delete_from_intermediate::where_clause(std::unique_ptr<basic_condition> &&cond)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_where_part>(std::move(cond)));
|
||||
data_->parts.push_back(std::make_unique<query_where_part>(std::move(cond)));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
query_delete_intermediate::query_delete_intermediate(connection &db, const sql::schema &schema)
|
||||
: query_start_intermediate(db, schema)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_delete_part>());
|
||||
data_->parts.push_back(std::make_unique<query_delete_part>());
|
||||
}
|
||||
|
||||
query_delete_from_intermediate query_delete_intermediate::from(const sql::table &table)
|
||||
{
|
||||
data_.parts.push_back(std::make_unique<query_delete_from_part>(table));
|
||||
data_->parts.push_back(std::make_unique<query_delete_from_part>(table));
|
||||
return {connection_, schema_, data_};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -89,11 +89,6 @@ bool session::table_exists(const std::string &table_name) const
|
|||
return c->exists(dialect_.default_schema_name(), table_name);
|
||||
}
|
||||
|
||||
const schema& session::tables() const
|
||||
{
|
||||
return *schema_;
|
||||
}
|
||||
|
||||
const class dialect &session::dialect() const
|
||||
{
|
||||
return dialect_;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include <matador/sql/connection.hpp>
|
||||
#include <matador/sql/dialect.hpp>
|
||||
#include <matador/sql/entity_query_builder.hpp>
|
||||
|
||||
#include "models/author.hpp"
|
||||
|
|
@ -25,8 +24,21 @@ TEST_CASE("Create sql query data for entity with eager belongs to", "[query][ent
|
|||
REQUIRE(data.has_value());
|
||||
REQUIRE(data->root_table_name == "books");
|
||||
REQUIRE(data->joins.size() == 1);
|
||||
REQUIRE(data->columns.size() == 9);
|
||||
REQUIRE(data->where_clause);
|
||||
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 {
|
||||
{ "books", "books.author_id = authors.id"}
|
||||
|
|
@ -40,15 +52,19 @@ TEST_CASE("Create sql query data for entity with eager belongs to", "[query][ent
|
|||
++index;
|
||||
}
|
||||
|
||||
REQUIRE(data->where_clause);
|
||||
auto cond = data->where_clause->evaluate(db.dialect(), qc);
|
||||
REQUIRE(cond == "books.id = 17");
|
||||
|
||||
auto &jd = data->joins.front();
|
||||
|
||||
auto context = db.query(scm)
|
||||
auto q = db.query(scm)
|
||||
.select(data->columns)
|
||||
.from(data->root_table_name)
|
||||
.join_left(jd.join_table).on(std::move(jd.condition))
|
||||
.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))
|
||||
.build();
|
||||
}
|
||||
|
|
@ -68,8 +84,27 @@ TEST_CASE("Create sql query data for entity with eager has many belongs to", "[q
|
|||
REQUIRE(data.has_value());
|
||||
REQUIRE(data->root_table_name == "orders");
|
||||
REQUIRE(data->joins.size() == 1);
|
||||
REQUIRE(data->columns.size() == 15);
|
||||
REQUIRE(data->where_clause);
|
||||
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 {
|
||||
{ "orders", "orders.order_id = order_details.order_id"}
|
||||
|
|
@ -83,6 +118,7 @@ TEST_CASE("Create sql query data for entity with eager has many belongs to", "[q
|
|||
++index;
|
||||
}
|
||||
|
||||
REQUIRE(data->where_clause);
|
||||
auto cond = data->where_clause->evaluate(db.dialect(), qc);
|
||||
REQUIRE(cond == "orders.order_id = 17");
|
||||
}
|
||||
|
|
@ -102,8 +138,16 @@ TEST_CASE("Create sql query data for entity with eager many to many", "[query][e
|
|||
REQUIRE(data.has_value());
|
||||
REQUIRE(data->root_table_name == "ingredients");
|
||||
REQUIRE(data->joins.size() == 2);
|
||||
REQUIRE(data->columns.size() == 4);
|
||||
REQUIRE(data->where_clause);
|
||||
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 {
|
||||
{ "ingredients", "ingredients.id = recipe_ingredients.ingredient_id"},
|
||||
|
|
@ -118,6 +162,7 @@ TEST_CASE("Create sql query data for entity with eager many to many", "[query][e
|
|||
++index;
|
||||
}
|
||||
|
||||
REQUIRE(data->where_clause);
|
||||
auto cond = data->where_clause->evaluate(db.dialect(), qc);
|
||||
REQUIRE(cond == "ingredients.id = 17");
|
||||
}
|
||||
Loading…
Reference in New Issue