uses criteria for session::find methods
This commit is contained in:
@@ -10,6 +10,7 @@ enum class error_code : uint8_t {
|
||||
Ok = 0,
|
||||
NoConnectionAvailable,
|
||||
UnknownType,
|
||||
NoPrimaryKey,
|
||||
FailedToBuildQuery,
|
||||
FailedToFindObject,
|
||||
Failed
|
||||
|
||||
@@ -121,9 +121,7 @@ public:
|
||||
template<typename Type, typename PrimaryKeyType>
|
||||
utils::result<object::object_ptr<Type>, utils::error> find(const PrimaryKeyType &pk);
|
||||
template<typename Type>
|
||||
utils::result<sql::query_result<Type>, utils::error> find();
|
||||
// template<typename Type, typename Condition>
|
||||
// utils::result<sql::query_result<Type>, utils::error> find(const Condition &cond);
|
||||
utils::result<sql::query_result<Type>, utils::error> find(query::criteria_ptr clause = {});
|
||||
|
||||
template<typename Type>
|
||||
utils::result<void, utils::error> drop_table();
|
||||
@@ -291,16 +289,23 @@ utils::result<void, utils::error> session::remove( const object::object_ptr<Type
|
||||
}
|
||||
|
||||
template<typename Type, typename PrimaryKeyType>
|
||||
utils::result<object::object_ptr<Type>, utils::error> session::find( const PrimaryKeyType& pk ) {
|
||||
utils::result<object::object_ptr<Type>, utils::error> session::find(const PrimaryKeyType& pk) {
|
||||
auto info = schema_->info<Type>();
|
||||
if (!info) {
|
||||
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
||||
}
|
||||
if (!info.value().get().definition().has_primary_key()) {
|
||||
return utils::failure(make_error(error_code::NoPrimaryKey, "Type hasn't primary key."));
|
||||
}
|
||||
|
||||
const auto& type_info = info.value().get();
|
||||
|
||||
session_query_builder eqb(*schema_, *this);
|
||||
auto data = eqb.build<Type>(pk);
|
||||
const sql::column col(sql::table{type_info.reference_column()->table_name()}, type_info.reference_column()->name());
|
||||
using namespace matador::query;
|
||||
auto data = eqb.build<Type>(col == utils::_);
|
||||
if (!data.is_ok()) {
|
||||
return utils::failure(make_error(error_code::FailedToBuildQuery, "Failed to build query for type " + info->get().name() + "."));
|
||||
return utils::failure(make_error(error_code::FailedToBuildQuery, "Failed to build query for type " + type_info.name() + "."));
|
||||
}
|
||||
|
||||
auto res = build_select_query(data.release()).prepare(*this);
|
||||
@@ -310,20 +315,20 @@ utils::result<object::object_ptr<Type>, utils::error> session::find( const Prima
|
||||
}
|
||||
auto stmt_result = res->bind(0, const_cast<PrimaryKeyType&>(pk)).template fetch_one<Type>();
|
||||
if (stmt_result && !stmt_result.value()) {
|
||||
return utils::failure(make_error(error_code::FailedToFindObject, "Failed to find object of type " + info->get().name() + " with primary key " + std::to_string(pk) + "."));
|
||||
return utils::failure(make_error(error_code::FailedToFindObject, "Failed to find object of type " + type_info.name() + " with primary key " + std::to_string(pk) + "."));
|
||||
}
|
||||
return utils::ok(object::object_ptr<Type>{ stmt_result->release() });
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
utils::result<sql::query_result<Type>, utils::error> session::find() {
|
||||
utils::result<sql::query_result<Type>, utils::error> session::find(query::criteria_ptr clause) {
|
||||
auto info = schema_->info<Type>();
|
||||
if (!info) {
|
||||
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
||||
}
|
||||
|
||||
session_query_builder eqb(*schema_, *this);
|
||||
auto data = eqb.build<Type>();
|
||||
auto data = eqb.build<Type>(std::move(clause));
|
||||
if (!data.is_ok()) {
|
||||
return utils::failure(make_error(error_code::FailedToBuildQuery, "Failed to build query for type " + info->get().name() + "."));
|
||||
}
|
||||
@@ -336,27 +341,6 @@ utils::result<sql::query_result<Type>, utils::error> session::find() {
|
||||
return result->template fetch<Type>();
|
||||
}
|
||||
|
||||
// template<typename Type, typename Condition>
|
||||
// utils::result<sql::query_result<Type>, utils::error> session::find(const Condition &cond) {
|
||||
// auto info = schema_->info<Type>();
|
||||
// if (!info) {
|
||||
// return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
||||
// }
|
||||
//
|
||||
// session_query_builder eqb(*schema_, *this);
|
||||
// auto data = eqb.build<Type>();
|
||||
// if (!data.is_ok()) {
|
||||
// return utils::failure(make_error(error_code::FailedToBuildQuery, "Failed to build query for type " + info->get().name() + "."));
|
||||
// }
|
||||
//
|
||||
// auto result = build_select_query(data.release()).prepare(*this);
|
||||
// if (!result.is_ok()) {
|
||||
// return utils::failure(result.err());
|
||||
// }
|
||||
//
|
||||
// return result->template fetch<Type>();
|
||||
// }
|
||||
//
|
||||
template<typename Type>
|
||||
utils::result<void, utils::error> session::drop_table() {
|
||||
auto info = schema_->info<Type>();
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
#include "matador/object/join_columns_collector.hpp"
|
||||
#include "matador/object/repository.hpp"
|
||||
#include "matador/query/criteria/criteria_visitor.hpp"
|
||||
|
||||
#include "matador/utils/primary_key_attribute.hpp"
|
||||
#include "matador/utils/result.hpp"
|
||||
@@ -30,46 +31,49 @@ struct entity_query_data {
|
||||
query::criteria_ptr where_clause;
|
||||
};
|
||||
|
||||
class criteria_transformer final : public query::criteria_visitor {
|
||||
public:
|
||||
criteria_transformer(const object::repository &repo, const std::unordered_map<std::string, std::shared_ptr<sql::table>>& tables_by_name);
|
||||
void visit( const query::between_criteria& node ) override;
|
||||
void visit( const query::binary_criteria& node ) override;
|
||||
void visit( const query::binary_column_criteria& node ) override;
|
||||
void visit( const query::collection_criteria& node ) override;
|
||||
void visit( const query::collection_query_criteria& node ) override;
|
||||
void visit( const query::like_criteria& node ) override;
|
||||
void visit( const query::logical_criteria& node ) override;
|
||||
void visit( const query::not_criteria& node ) override;
|
||||
|
||||
private:
|
||||
void update_criteria_column(const query::abstract_column_criteria& node) const;
|
||||
|
||||
private:
|
||||
const object::repository &repo_;
|
||||
const std::unordered_map<std::string, std::shared_ptr<sql::table>>& tables_by_name_;
|
||||
};
|
||||
|
||||
class session_query_builder final {
|
||||
public:
|
||||
session_query_builder(const object::repository &scm, sql::executor &exec)
|
||||
: schema_(scm)
|
||||
, executor_(exec){}
|
||||
|
||||
template<class EntityType, typename PrimaryKeyType>
|
||||
utils::result<entity_query_data, query_build_error> build(const PrimaryKeyType &pk) {
|
||||
auto info = schema_.info<EntityType>();
|
||||
if (!info) {
|
||||
return utils::failure(query_build_error::UnknownType);
|
||||
}
|
||||
pk_ = pk;
|
||||
table_info_stack_.push({info.value(), std::make_shared<sql::table>(info.value().get().name(), build_alias('t', ++table_index))});
|
||||
entity_query_data_ = { table_info_stack_.top().table };
|
||||
processed_tables_.insert({info->get().name(), entity_query_data_.root_table});
|
||||
try {
|
||||
access::process(*this, info->get().prototype());
|
||||
|
||||
return {utils::ok(std::move(entity_query_data_))};
|
||||
} catch (const query_builder_exception &ex) {
|
||||
return {utils::failure(ex.error_type())};
|
||||
} catch (...) {
|
||||
return {utils::failure(query_build_error::UnexpectedError)};
|
||||
}
|
||||
}
|
||||
|
||||
template<class EntityType>
|
||||
utils::result<entity_query_data, query_build_error> build() {
|
||||
utils::result<entity_query_data, query_build_error> build(query::criteria_ptr clause = {}) {
|
||||
const auto info = schema_.info<EntityType>();
|
||||
if (!info) {
|
||||
return utils::failure(query_build_error::UnknownType);
|
||||
}
|
||||
pk_ = nullptr;
|
||||
table_info_stack_.push({info.value(), std::make_shared<sql::table>(info.value().get().name(), build_alias('t', ++table_index))});
|
||||
entity_query_data_ = { table_info_stack_.top().table };
|
||||
processed_tables_.insert({info->get().name(), entity_query_data_.root_table});
|
||||
try {
|
||||
access::process(*this, info->get().prototype());
|
||||
|
||||
if (clause) {
|
||||
criteria_transformer transformer{schema_, processed_tables_};
|
||||
clause->accept(transformer);
|
||||
entity_query_data_.where_clause = std::move(clause);
|
||||
}
|
||||
return {utils::ok(std::move(entity_query_data_))};
|
||||
} catch (const query_builder_exception &ex) {
|
||||
return {utils::failure(ex.error_type())};
|
||||
@@ -79,19 +83,12 @@ public:
|
||||
}
|
||||
|
||||
template < class V >
|
||||
void on_primary_key(const char *id, V &, const utils::primary_key_attribute& /*attr*/ = utils::default_pk_attributes)
|
||||
{
|
||||
void on_primary_key(const char *id, V &, const utils::primary_key_attribute& /*attr*/ = utils::default_pk_attributes) {
|
||||
push(id);
|
||||
if (!is_root_entity()) {
|
||||
return;
|
||||
}
|
||||
entity_query_data_.pk_column_name = id;
|
||||
if (!pk_.is_null()) {
|
||||
const auto c = sql::column{table_info_stack_.top().table, id, ""};
|
||||
using namespace matador::query;
|
||||
auto co = c == utils::_;
|
||||
entity_query_data_.where_clause = std::move(co);
|
||||
}
|
||||
}
|
||||
|
||||
void on_revision(const char *id, uint64_t &/*rev*/);
|
||||
@@ -235,7 +232,6 @@ private:
|
||||
void append_join(const sql::column &left, const sql::column &right);
|
||||
|
||||
private:
|
||||
utils::value pk_;
|
||||
struct table_info {
|
||||
std::reference_wrapper<const object::basic_object_info> info;
|
||||
std::shared_ptr<sql::table> table;
|
||||
|
||||
Reference in New Issue
Block a user