added session tests
This commit is contained in:
@@ -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
|
||||
|
||||
+18
-111
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user