added update_object_binder progress

This commit is contained in:
Sascha Kühl 2025-10-15 16:14:22 +02:00
parent d3483ea5c2
commit d08bdb9554
4 changed files with 50 additions and 5 deletions

View File

@ -190,15 +190,48 @@ utils::result<object::object_ptr<Type>, utils::error> session::insert( Args&&...
class update_object_binder final { class update_object_binder final {
public: public:
explicit update_object_binder(sql::statement &stmt) explicit update_object_binder(sql::statement &stmt, size_t position)
: stmt_(stmt) {} : stmt_(stmt)
, binding_position_(position){}
template < class Type > template < class Type >
sql::statement& bind(Type &obj) { sql::statement& bind(Type &obj) {
access::process(*this, obj);
return stmt_; return stmt_;
} }
template<class Type>
void on_primary_key(const char * /*id*/, Type &x, const utils::primary_key_attribute& /*attr*/ = utils::default_pk_attributes) {
stmt_.bind(binding_position_, x);
}
static void on_revision(const char * /*id*/, uint64_t &/*rev*/) {}
template < class Type >
static void on_attribute(const char * /*id*/, Type &/*x*/, const utils::field_attributes &/*attr*/ = utils::null_attributes) {}
template < class Pointer >
static void on_belongs_to(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/ = utils::default_foreign_attributes) {}
template < class Pointer >
static void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/ = utils::default_foreign_attributes) {}
template<class ContainerType>
static void on_has_many(const char * /*id*/,
ContainerType &/*c*/,
const char * /*join_column*/,
const utils::foreign_attributes &/*attr*/ = utils::default_foreign_attributes) {}
template<class ContainerType>
static void on_has_many_to_many(const char * /*id*/,
ContainerType &/*c*/,
const char * /*join_column*/,
const char * /*inverse_join_column*/,
const utils::foreign_attributes &/*attr*/) {}
template<class ContainerType>
static void on_has_many_to_many(const char * /*id*/,
ContainerType &/*c*/,
const utils::foreign_attributes &/*attr*/) {}
private: private:
sql::statement &stmt_; sql::statement &stmt_;
size_t binding_position_{0};
sql::object_pk_binder pk_binder_; sql::object_pk_binder pk_binder_;
}; };
@ -221,7 +254,7 @@ utils::result<object::object_ptr<Type>, utils::error> session::update( const obj
} }
res->bind(*obj); res->bind(*obj);
update_object_binder binder(res.value());; update_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());
} }

View File

@ -164,6 +164,7 @@ statement &statement::bind(const Type &obj) {
template<class Type> template<class Type>
utils::result<query_result<Type>, utils::error> statement::fetch() { utils::result<query_result<Type>, utils::error> statement::fetch() {
std::cout << statement_proxy_->sql() << std::endl;
return statement_proxy_->fetch(*bindings_).and_then([](std::unique_ptr<query_result_impl> &&value) { return statement_proxy_->fetch(*bindings_).and_then([](std::unique_ptr<query_result_impl> &&value) {
const auto prototype = value->prototype(); const auto prototype = value->prototype();
return utils::ok(query_result<Type>(std::forward<decltype(value)>(value), [prototype] { return utils::ok(query_result<Type>(std::forward<decltype(value)>(value), [prototype] {
@ -174,6 +175,7 @@ utils::result<query_result<Type>, utils::error> statement::fetch() {
template<class Type> template<class Type>
utils::result<std::unique_ptr<Type>, utils::error> statement::fetch_one() { utils::result<std::unique_ptr<Type>, utils::error> statement::fetch_one() {
std::cout << statement_proxy_->sql() << std::endl;
auto result = statement_proxy_->fetch(*bindings_); auto result = statement_proxy_->fetch(*bindings_);
if (!result.is_ok()) { if (!result.is_ok()) {
return utils::failure(result.err()); return utils::failure(result.err());

View File

@ -51,11 +51,13 @@ statement &statement::bind(const size_t pos, std::string &val, const size_t size
} }
utils::result<size_t, utils::error> statement::execute() const { utils::result<size_t, utils::error> statement::execute() const {
// logger_.info(statement_->query_.sql); logger_->on_execute(statement_proxy_->sql());
std::cout << statement_proxy_->sql() << std::endl;
return statement_proxy_->execute(*bindings_); return statement_proxy_->execute(*bindings_);
} }
utils::result<query_result<record>, utils::error> statement::fetch() const { utils::result<query_result<record>, utils::error> statement::fetch() const {
std::cout << statement_proxy_->sql() << std::endl;
auto result = statement_proxy_->fetch(*bindings_); auto result = statement_proxy_->fetch(*bindings_);
if (!result.is_ok()) { if (!result.is_ok()) {
return utils::failure(result.err()); return utils::failure(result.err());

View File

@ -46,7 +46,7 @@ TEST_CASE_METHOD(SessionFixture, "Session update test", "[session][update]") {
const auto plane = result.value(); const auto plane = result.value();
result = ses.find<airplane>(1); result = ses.find<airplane>(1);
REQUIRE(result.is_ok()); REQUIRE(result.is_ok());
const auto& read_airplane = *result; auto read_airplane = *result;
REQUIRE(read_airplane->id == plane->id); REQUIRE(read_airplane->id == plane->id);
REQUIRE(read_airplane->brand == plane->brand); REQUIRE(read_airplane->brand == plane->brand);
REQUIRE(read_airplane->model == plane->model); REQUIRE(read_airplane->model == plane->model);
@ -55,6 +55,14 @@ TEST_CASE_METHOD(SessionFixture, "Session update test", "[session][update]") {
read_airplane->model = "A380"; read_airplane->model = "A380";
auto update_result = ses.update(read_airplane); auto update_result = ses.update(read_airplane);
REQUIRE(update_result.is_ok());
result = ses.find<airplane>(1);
REQUIRE(result.is_ok());
read_airplane = *result;
REQUIRE(read_airplane->id == plane->id);
REQUIRE(read_airplane->brand == "Airbus");
REQUIRE(read_airplane->model == "A380");
} }
TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]") { TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]") {