diff --git a/include/matador/orm/session.hpp b/include/matador/orm/session.hpp index 240ab43..afd61ae 100644 --- a/include/matador/orm/session.hpp +++ b/include/matador/orm/session.hpp @@ -115,11 +115,15 @@ public: template utils::result, utils::error> update(const object::object_ptr &obj); + template + utils::result remove(const object::object_ptr &obj); template utils::result, utils::error> find(const PrimaryKeyType &pk); template utils::result, utils::error> find(); + // template + // utils::result, utils::error> find(const Condition &cond); template utils::result drop_table(); @@ -188,9 +192,9 @@ utils::result, utils::error> session::insert( Args&&... return insert(new Type(std::forward(args)...)); } -class update_object_binder final { +class pk_object_binder final { public: - explicit update_object_binder(sql::statement &stmt, size_t position) + explicit pk_object_binder(sql::statement &stmt, size_t position) : stmt_(stmt) , binding_position_(position){} @@ -254,13 +258,38 @@ utils::result, utils::error> session::update( const obj } res->bind(*obj); - update_object_binder binder(res.value(), res->bind_pos()); + pk_object_binder binder(res.value(), res->bind_pos()); if (const auto update_result = binder.bind(*obj).execute(); !update_result.is_ok()) { return utils::failure(update_result.err()); } return utils::ok(object::object_ptr{obj}); } +template +utils::result session::remove( const object::object_ptr& obj ) { + auto info = schema_->info(); + if (!info) { + return utils::failure(info.err()); + } + using namespace matador::utils; + using namespace matador::query; + + const auto col = sql::column(info.value().get().definition().primary_key()->name()); + auto res = matador::query::query::remove() + .from( info->get().name() ) + .where(col == _) + .prepare(*this); + if (!res) { + return utils::failure(res.err()); + } + + pk_object_binder binder(res.value(), res->bind_pos()); + if (const auto update_result = binder.bind(*obj).execute(); !update_result.is_ok()) { + return utils::failure(update_result.err()); + } + return utils::ok(); +} + template utils::result, utils::error> session::find( const PrimaryKeyType& pk ) { auto info = schema_->info(); @@ -307,6 +336,27 @@ utils::result, utils::error> session::find() { return result->template fetch(); } +// template +// utils::result, utils::error> session::find(const Condition &cond) { +// auto info = schema_->info(); +// 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(); +// 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(); +// } +// template utils::result session::drop_table() { auto info = schema_->info(); diff --git a/include/matador/orm/session_query_builder.hpp b/include/matador/orm/session_query_builder.hpp index 77328b2..807cc01 100644 --- a/include/matador/orm/session_query_builder.hpp +++ b/include/matador/orm/session_query_builder.hpp @@ -85,16 +85,11 @@ public: if (!is_root_entity()) { return; } - if (pk_.is_null()) { - entity_query_data_.pk_column_name = id; - } else if (pk_.is_integer()) { - // auto v = *pk_.as(); + entity_query_data_.pk_column_name = id; + if (!pk_.is_null()) { auto c = sql::column{table_info_stack_.top().table, id, ""}; auto co = std::make_unique>(c, query::basic_condition::operand_type::EQUAL, utils::_); entity_query_data_.where_clause = std::move(co); - entity_query_data_.pk_column_name = id; - } else if (pk_.is_varchar()) { - // Todo: handle varchar primary key } } diff --git a/include/matador/sql/interface/statement_impl.hpp b/include/matador/sql/interface/statement_impl.hpp index 48713a5..b168fe3 100644 --- a/include/matador/sql/interface/statement_impl.hpp +++ b/include/matador/sql/interface/statement_impl.hpp @@ -26,12 +26,12 @@ public: virtual utils::result, utils::error> fetch(const parameter_binder& bindings) = 0; template < class Type > - size_t bind_object(Type &obj, parameter_binder& bindings) { + void bind_object(Type &obj, parameter_binder& bindings) { object_parameter_binder object_binder_; object_binder_.reset(start_index()); object_binder_.bind(obj, bindings); - return object_binder_.current_index(); + current_bind_pos_ = object_binder_.current_index(); } template < class Type > diff --git a/test/backends/SessionTest.cpp b/test/backends/SessionTest.cpp index d7eb53f..6d059b8 100644 --- a/test/backends/SessionTest.cpp +++ b/test/backends/SessionTest.cpp @@ -65,6 +65,29 @@ TEST_CASE_METHOD(SessionFixture, "Session update test", "[session][update]") { REQUIRE(read_airplane->model == "A380"); } +TEST_CASE_METHOD(SessionFixture, "Session delete test", "[session][delete]") { + const auto res = ses.attach("airplanes") + .and_then([this] { return ses.create_schema(); } ); + REQUIRE(res.is_ok()); + + tables_to_drop.emplace("airplanes"); + + auto result = ses.insert(1, "Boeing", "747"); + REQUIRE(result.is_ok()); + + const auto plane = result.value(); + result = ses.find(1); + REQUIRE(result.is_ok()); + auto read_airplane = *result; + + auto update_result = ses.remove(read_airplane); + REQUIRE(update_result.is_ok()); + + result = ses.find(1); + REQUIRE(result.is_error()); + REQUIRE(result.err().ec() == orm::error_code::FailedToFindObject); +} + TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]") { const auto result = ses.attach("airplanes") .and_then([this] { return ses.attach("flights"); } )