collection_resolver progress
This commit is contained in:
@@ -26,7 +26,7 @@ protected:
|
||||
public:
|
||||
template < class Type >
|
||||
utils::result<sql::query_result<Type>, utils::error> fetch_all(sql::executor &exec) {
|
||||
auto result = fetch(exec);
|
||||
auto result = fetch(exec, typeid(Type));
|
||||
if (!result.is_ok()) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
@@ -40,9 +40,8 @@ public:
|
||||
[[nodiscard]] utils::result<sql::query_result<sql::record>, utils::error> fetch_all(const sql::executor &exec) const;
|
||||
|
||||
template < class Type >
|
||||
utils::result<object::object_ptr<Type>, utils::error> fetch_one(sql::executor &exec)
|
||||
{
|
||||
auto result = fetch(exec);
|
||||
utils::result<object::object_ptr<Type>, utils::error> fetch_one(sql::executor &exec) {
|
||||
auto result = fetch(exec, typeid(Type));
|
||||
if (!result.is_ok()) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
@@ -83,7 +82,7 @@ public:
|
||||
[[nodiscard]] sql::query_context compile(const sql::dialect &d) const;
|
||||
|
||||
private:
|
||||
[[nodiscard]] utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> fetch(const sql::executor &exec) const;
|
||||
[[nodiscard]] utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> fetch(const sql::executor &exec, const std::type_index& index) const;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -6,9 +6,6 @@
|
||||
#include "matador/sql/internal/identifier_statement_binder.hpp"
|
||||
#include "matador/sql/statement.hpp"
|
||||
|
||||
#include "matador/query/table.hpp"
|
||||
#include "matador/query/select_query_builder.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
class executor;
|
||||
}
|
||||
@@ -17,47 +14,33 @@ namespace matador::query {
|
||||
template<typename Type>
|
||||
class query_collection_resolver : public object::collection_resolver<Type> {
|
||||
public:
|
||||
explicit query_collection_resolver(const basic_schema &repo, sql::executor& exec, const table &tab, std::string pk_name, const std::type_index& root_type, std::string join_column)
|
||||
explicit query_collection_resolver(sql::statement &&stmt, const std::type_index& root_type, std::string join_column)
|
||||
: object::collection_resolver<Type>(root_type, join_column)
|
||||
, executor_(exec)
|
||||
, schema_(repo)
|
||||
, table_(tab)
|
||||
, pk_name_(std::move(pk_name))
|
||||
, join_column_(std::move(join_column))
|
||||
, stmt_(std::move(stmt))
|
||||
{}
|
||||
|
||||
std::vector<Type> resolve(const utils::identifier &id) override;
|
||||
protected:
|
||||
sql::executor& executor_;
|
||||
const basic_schema &schema_;
|
||||
const table &table_;
|
||||
std::string pk_name_;
|
||||
std::string join_column_;
|
||||
sql::statement stmt_;
|
||||
std::type_index index{typeid(Type)};
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
std::vector<Type> query_collection_resolver<Type>::resolve(const utils::identifier &id) {
|
||||
const auto *pk_column = table_[pk_name_];
|
||||
const auto *join_column = table_[join_column_];
|
||||
|
||||
auto stmt = query::select({*pk_column})
|
||||
.from(table_)
|
||||
.where(*join_column == id)
|
||||
.prepare(executor_);
|
||||
|
||||
if (!stmt) {
|
||||
return {};
|
||||
}
|
||||
|
||||
sql::identifier_statement_binder binder(*stmt);
|
||||
sql::identifier_statement_binder binder(stmt_);
|
||||
binder.bind(id);
|
||||
|
||||
auto result = stmt->template fetch_one_raw<Type>();
|
||||
auto result = stmt_.fetch();
|
||||
if (!result) {
|
||||
return {};
|
||||
}
|
||||
return {};
|
||||
std::vector<Type> out;
|
||||
for (const auto &i: *result) {
|
||||
// Todo: convert the first value of record into an utils::identifier
|
||||
// then create a object_proxy<Type>(resolver, identifier)
|
||||
// out.emplace_back(resolver, identifier);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
#endif //MATADOR_QUERY_CONTAINER_RESOLVER_HPP
|
||||
@@ -51,7 +51,19 @@ public:
|
||||
{}
|
||||
|
||||
std::shared_ptr<object::abstract_collection_resolver> produce(sql::executor &exec) override {
|
||||
return std::make_shared<query_collection_resolver<Type>>(repo_, exec, table_, std::move(pk_name_), root_type(), collection_name());
|
||||
const auto *pk_column = table_[pk_name_];
|
||||
const auto *join_column = table_[collection_name()];
|
||||
|
||||
auto stmt = query::select({*pk_column})
|
||||
.from(table_)
|
||||
.where(*join_column == utils::_)
|
||||
.prepare(exec);
|
||||
|
||||
if (!stmt) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return std::make_shared<query_collection_resolver<Type>>(stmt.release(), root_type(), collection_name());
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -201,13 +213,13 @@ public:
|
||||
throw query_builder_exception{query_build_error::MissingPrimaryKey};
|
||||
}
|
||||
|
||||
auto producer = std::make_unique<query_collection_resolver_producer<typename CollectionType::value_type::value_type>>(
|
||||
auto producer = std::make_unique<query_collection_resolver_producer<typename CollectionType::value_type>>(
|
||||
schema_,
|
||||
it->second.table(),
|
||||
it->second.node().info().primary_key_attribute()->name(),
|
||||
root_type_,
|
||||
join_column);
|
||||
const sql::composite_key key{root_type_, typeid(typename CollectionType::value_type::value_type), join_column};
|
||||
const sql::composite_key key{root_type_, typeid(typename CollectionType::value_type), join_column};
|
||||
schema_.collection_resolver_producers_[key] = std::move(producer);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user