implemented session::remove
This commit is contained in:
parent
d08bdb9554
commit
1d40c72519
|
|
@ -115,11 +115,15 @@ public:
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
utils::result<object::object_ptr<Type>, utils::error> update(const object::object_ptr<Type> &obj);
|
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>
|
template<typename Type, typename PrimaryKeyType>
|
||||||
utils::result<object::object_ptr<Type>, utils::error> find(const PrimaryKeyType &pk);
|
utils::result<object::object_ptr<Type>, utils::error> find(const PrimaryKeyType &pk);
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
utils::result<sql::query_result<Type>, utils::error> find();
|
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>
|
template<typename Type>
|
||||||
utils::result<void, utils::error> drop_table();
|
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)...));
|
return insert(new Type(std::forward<Args>(args)...));
|
||||||
}
|
}
|
||||||
|
|
||||||
class update_object_binder final {
|
class pk_object_binder final {
|
||||||
public:
|
public:
|
||||||
explicit update_object_binder(sql::statement &stmt, size_t position)
|
explicit pk_object_binder(sql::statement &stmt, size_t position)
|
||||||
: stmt_(stmt)
|
: stmt_(stmt)
|
||||||
, binding_position_(position){}
|
, binding_position_(position){}
|
||||||
|
|
||||||
|
|
@ -254,13 +258,38 @@ utils::result<object::object_ptr<Type>, utils::error> session::update( const obj
|
||||||
}
|
}
|
||||||
|
|
||||||
res->bind(*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()) {
|
if (const auto update_result = binder.bind(*obj).execute(); !update_result.is_ok()) {
|
||||||
return utils::failure(update_result.err());
|
return utils::failure(update_result.err());
|
||||||
}
|
}
|
||||||
return utils::ok(object::object_ptr{obj});
|
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>
|
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>();
|
auto info = schema_->info<Type>();
|
||||||
|
|
@ -307,6 +336,27 @@ utils::result<sql::query_result<Type>, utils::error> session::find() {
|
||||||
return result->template fetch<Type>();
|
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>
|
template<typename Type>
|
||||||
utils::result<void, utils::error> session::drop_table() {
|
utils::result<void, utils::error> session::drop_table() {
|
||||||
auto info = schema_->info<Type>();
|
auto info = schema_->info<Type>();
|
||||||
|
|
|
||||||
|
|
@ -85,16 +85,11 @@ public:
|
||||||
if (!is_root_entity()) {
|
if (!is_root_entity()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (pk_.is_null()) {
|
|
||||||
entity_query_data_.pk_column_name = id;
|
entity_query_data_.pk_column_name = id;
|
||||||
} else if (pk_.is_integer()) {
|
if (!pk_.is_null()) {
|
||||||
// auto v = *pk_.as<V>();
|
|
||||||
auto c = sql::column{table_info_stack_.top().table, id, ""};
|
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::_);
|
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_.where_clause = std::move(co);
|
||||||
entity_query_data_.pk_column_name = id;
|
|
||||||
} else if (pk_.is_varchar()) {
|
|
||||||
// Todo: handle varchar primary key
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,12 +26,12 @@ public:
|
||||||
virtual utils::result<std::unique_ptr<query_result_impl>, utils::error> fetch(const parameter_binder& bindings) = 0;
|
virtual utils::result<std::unique_ptr<query_result_impl>, utils::error> fetch(const parameter_binder& bindings) = 0;
|
||||||
|
|
||||||
template < class Type >
|
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_parameter_binder object_binder_;
|
||||||
object_binder_.reset(start_index());
|
object_binder_.reset(start_index());
|
||||||
object_binder_.bind(obj, bindings);
|
object_binder_.bind(obj, bindings);
|
||||||
|
|
||||||
return object_binder_.current_index();
|
current_bind_pos_ = object_binder_.current_index();
|
||||||
}
|
}
|
||||||
|
|
||||||
template < class Type >
|
template < class Type >
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,29 @@ TEST_CASE_METHOD(SessionFixture, "Session update test", "[session][update]") {
|
||||||
REQUIRE(read_airplane->model == "A380");
|
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]") {
|
TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]") {
|
||||||
const auto result = ses.attach<airplane>("airplanes")
|
const auto result = ses.attach<airplane>("airplanes")
|
||||||
.and_then([this] { return ses.attach<flight>("flights"); } )
|
.and_then([this] { return ses.attach<flight>("flights"); } )
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue