proxy lazy resolve progress
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
#ifndef QUERY_BUILDER_EXCEPTION_HPP
|
||||
#define QUERY_BUILDER_EXCEPTION_HPP
|
||||
|
||||
#include "matador/utils/error.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <system_error>
|
||||
|
||||
namespace matador::orm {
|
||||
|
||||
@@ -10,18 +11,20 @@ enum class query_build_error : std::uint8_t {
|
||||
Ok = 0,
|
||||
UnknownType,
|
||||
MissingPrimaryKey,
|
||||
UnexpectedError
|
||||
UnexpectedError,
|
||||
QueryError
|
||||
};
|
||||
|
||||
|
||||
class query_builder_exception final : public std::exception {
|
||||
public:
|
||||
explicit query_builder_exception(const query_build_error error) : error_(error) {}
|
||||
explicit query_builder_exception(const query_build_error error, utils::error &&err = {});
|
||||
|
||||
[[nodiscard]] query_build_error error() const { return error_; }
|
||||
[[nodiscard]] query_build_error error_type() const;
|
||||
[[nodiscard]] const utils::error &error() const;
|
||||
|
||||
private:
|
||||
const query_build_error error_;
|
||||
const query_build_error error_type_;
|
||||
utils::error error_;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -50,10 +50,6 @@ public:
|
||||
|
||||
template<typename Type, typename PrimaryKeyType>
|
||||
utils::result<object::object_ptr<Type>, utils::error> find(const PrimaryKeyType &pk) {
|
||||
// auto c = pool_.acquire();
|
||||
// if (!c.valid()) {
|
||||
// return utils::failure(make_error(error_code::NoConnectionAvailable, "Failed to acquire connection."));
|
||||
// }
|
||||
auto info = schema_->info<Type>();
|
||||
if (!info) {
|
||||
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
||||
@@ -78,10 +74,6 @@ public:
|
||||
|
||||
template<typename Type>
|
||||
utils::result<sql::query_result<Type>, utils::error> find() {
|
||||
// const auto c = pool_.acquire();
|
||||
// if (!c.valid()) {
|
||||
// return utils::failure(make_error(error_code::NoConnectionAvailable, "Failed to acquire connection."));
|
||||
// }
|
||||
auto info = schema_->info<Type>();
|
||||
if (!info) {
|
||||
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
||||
@@ -93,7 +85,13 @@ public:
|
||||
return utils::failure(make_error(error_code::FailedToBuildQuery, "Failed to build query for type " + info->get().name() + "."));
|
||||
}
|
||||
|
||||
return build_select_query(data.release()).template fetch_all<Type>(*this);
|
||||
const auto ctx = build_select_query(data.release()).compile(dialect_, query::query_mode::Prepared);
|
||||
|
||||
auto result = fetch(ctx);
|
||||
if (!result.is_ok()) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
return utils::ok(sql::query_result<Type>(result.release(), []{ return new Type();}));
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
@@ -139,7 +137,6 @@ utils::result<void, utils::error> session::attach( const std::string& table_name
|
||||
|
||||
template<typename Type>
|
||||
utils::result<object::object_ptr<Type>, utils::error> session::insert(Type *obj) {
|
||||
// auto c = pool_.acquire();
|
||||
auto info = schema_->info<Type>();
|
||||
if (!info) {
|
||||
return utils::failure(info.err());
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
#include "matador/query/condition.hpp"
|
||||
#include "matador/query/query.hpp"
|
||||
|
||||
#include "matador/sql/connection.hpp"
|
||||
#include "matador/sql/executor.hpp"
|
||||
#include "matador/sql/statement.hpp"
|
||||
|
||||
#include "matador/object/join_columns_collector.hpp"
|
||||
#include "matador/object/schema.hpp"
|
||||
@@ -17,7 +17,7 @@
|
||||
#include "matador/utils/value.hpp"
|
||||
|
||||
#include <stack>
|
||||
#include <unordered_set>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace matador::orm {
|
||||
|
||||
@@ -25,6 +25,7 @@ struct entity_query_data {
|
||||
std::shared_ptr<sql::table> root_table;
|
||||
std::string pk_column_name{};
|
||||
std::vector<sql::column> columns{};
|
||||
std::unordered_map<std::string, sql::statement> lazy_loading_statements{};
|
||||
std::vector<query::join_data> joins{};
|
||||
std::unique_ptr<query::basic_condition> where_clause{};
|
||||
};
|
||||
@@ -50,7 +51,7 @@ public:
|
||||
|
||||
return {utils::ok(std::move(entity_query_data_))};
|
||||
} catch (const query_builder_exception &ex) {
|
||||
return {utils::failure(ex.error())};
|
||||
return {utils::failure(ex.error_type())};
|
||||
} catch (...) {
|
||||
return {utils::failure(query_build_error::UnexpectedError)};
|
||||
}
|
||||
@@ -71,7 +72,7 @@ public:
|
||||
|
||||
return {utils::ok(std::move(entity_query_data_))};
|
||||
} catch (const query_builder_exception &ex) {
|
||||
return {utils::failure(ex.error())};
|
||||
return {utils::failure(ex.error_type())};
|
||||
} catch (...) {
|
||||
return {utils::failure(query_build_error::UnexpectedError)};
|
||||
}
|
||||
@@ -286,10 +287,15 @@ void session_query_builder::on_foreign_object(const char *id, Pointer &, const u
|
||||
using namespace matador::utils;
|
||||
using namespace matador::query;
|
||||
// create select query
|
||||
const auto result = matador::query::query::select<typename Pointer::value_type>(schema_)
|
||||
auto result = matador::query::query::select<typename Pointer::value_type>(schema_)
|
||||
.from(*foreign_table)
|
||||
.where(sql::column(foreign_table, id, "") == _)
|
||||
.prepare(executor_);
|
||||
if (!result) {
|
||||
throw query_builder_exception(query_build_error::QueryError, result.release_error());
|
||||
}
|
||||
|
||||
entity_query_data_.lazy_loading_statements.emplace(id, std::move(result.release()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -129,10 +129,15 @@ public:
|
||||
|
||||
public:
|
||||
explicit query_result(std::unique_ptr<query_result_impl> &&impl)
|
||||
: impl_(std::move(impl)) {}
|
||||
: impl_(std::move(impl))
|
||||
, creator_([this]{ return detail::create_prototype<Type>(impl_->prototype()); } ) {}
|
||||
|
||||
query_result(std::unique_ptr<query_result_impl> &&impl, creator_func&& creator)
|
||||
: impl_(std::move(impl))
|
||||
, creator_(std::move(creator)) {}
|
||||
|
||||
iterator begin() { return std::move(++iterator(this)); }
|
||||
iterator end() { return {}; }
|
||||
static iterator end() { return {}; }
|
||||
|
||||
private:
|
||||
friend class query_result_iterator<Type>;
|
||||
@@ -143,12 +148,14 @@ private:
|
||||
|
||||
protected:
|
||||
std::unique_ptr<query_result_impl> impl_;
|
||||
creator_func creator_;
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
Type *query_result<Type>::create() {
|
||||
return detail::create_prototype<Type>(impl_->prototype());
|
||||
return creator_();
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
void query_result<Type>::bind(const Type &obj) {
|
||||
impl_->bind(obj);
|
||||
|
||||
Reference in New Issue
Block a user