implemented session::remove

This commit is contained in:
Sascha Kühl 2025-10-16 15:40:43 +02:00
parent d08bdb9554
commit 1d40c72519
4 changed files with 80 additions and 12 deletions

View File

@ -115,11 +115,15 @@ public:
template<typename Type>
utils::result<object::object_ptr<Type>, utils::error> update(const object::object_ptr<Type> &obj);
template<typename Type>
utils::result<void, utils::error> remove(const object::object_ptr<Type> &obj);
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);
template<typename Type>
utils::result<void, utils::error> drop_table();
@ -188,9 +192,9 @@ utils::result<object::object_ptr<Type>, utils::error> session::insert( Args&&...
return insert(new Type(std::forward<Args>(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<object::object_ptr<Type>, 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<typename Type>
utils::result<void, utils::error> session::remove( const object::object_ptr<Type>& obj ) {
auto info = schema_->info<Type>();
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<void>();
}
template<typename Type, typename PrimaryKeyType>
utils::result<object::object_ptr<Type>, utils::error> session::find( const PrimaryKeyType& pk ) {
auto info = schema_->info<Type>();
@ -307,6 +336,27 @@ 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>();

View File

@ -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<V>();
if (!pk_.is_null()) {
auto c = sql::column{table_info_stack_.top().table, id, ""};
auto co = std::make_unique<query::condition<sql::column, utils::placeholder>>(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
}
}

View File

@ -26,12 +26,12 @@ public:
virtual utils::result<std::unique_ptr<query_result_impl>, 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 >

View File

@ -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<airplane>("airplanes")
.and_then([this] { return ses.create_schema(); } );
REQUIRE(res.is_ok());
tables_to_drop.emplace("airplanes");
auto result = ses.insert<airplane>(1, "Boeing", "747");
REQUIRE(result.is_ok());
const auto plane = result.value();
result = ses.find<airplane>(1);
REQUIRE(result.is_ok());
auto read_airplane = *result;
auto update_result = ses.remove(read_airplane);
REQUIRE(update_result.is_ok());
result = ses.find<airplane>(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<airplane>("airplanes")
.and_then([this] { return ses.attach<flight>("flights"); } )