From dc3537344abb681c8114c538ce9bcb4144264935 Mon Sep 17 00:00:00 2001 From: Folling Date: Sun, 14 Jan 2024 02:11:13 +0100 Subject: [PATCH] add object_get/set_name/information Signed-off-by: Folling --- include/ikarus/objects/object.h | 38 ++++++++++++++++++++ src/objects/object.cpp | 61 +++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/include/ikarus/objects/object.h b/include/ikarus/objects/object.h index 9dd3b39..7dcc97f 100644 --- a/include/ikarus/objects/object.h +++ b/include/ikarus/objects/object.h @@ -22,17 +22,55 @@ struct IkarusObject; /// \brief Fetches the project of an object. /// \param object The object to fetch the project from. /// \pre \li Must not be null. +/// \pre \li Must exist. /// \param error_out \see errors.h /// \return The project of the object or null if an error occurs. IKA_API struct IkarusProject * ikarus_object_get_project(IkarusObject const * object, IkarusErrorData * error_out); +/// \brief Fetches the name of an object. +/// \param object The object to fetch the name from. +/// \pre \li Must not be null. +/// \pre \li Must exist. +/// \param error_out \see errors.h +/// \return The name of the object or null if an error occurs. +IKA_API char const * ikarus_object_get_name(IkarusObject const * object, IkarusErrorData * error_out); + +/// \brief Sets the name of an object. +/// \param object The object to set the name of. +/// \pre \li Must not be null. +/// \pre \li Must exist. +/// \param new_name The new name of the object. +/// \pre \li Must not be null. +/// \pre \li Must not be empty. +/// \param error_out \see errors.h +IKA_API void ikarus_object_set_name(IkarusObject const * object, char const * new_name, IkarusErrorData * error_out); + +/// \brief Fetches the information of an object. +/// \param object The object to fetch the information from. +/// \pre \li Must not be null. +/// \pre \li Must exist. +/// \param error_out \see errors.h +/// \return The information of the object or null if an error occurs. +IKA_API char const * ikarus_object_get_info(IkarusObject const * object, IkarusErrorData * error_out); + +/// \brief Sets the information of an object. +/// \param object The object to set the information of. +/// \pre \li Must not be null. +/// \pre \li Must exist. +/// \param new_info The new information of the object. +/// \pre \li Must not be null. +/// \param error_out \see errors.h +IKA_API void ikarus_object_set_info(IkarusObject const * object, char const * new_info, IkarusErrorData * error_out); + /// \brief Compares two objects for equality. /// \details This neither compares the pointers nor does a deep copy. Instead it figures out if the objects _are_ the /// same object. /// \param lhs The left hand side object. /// \pre \li Must not be null. +/// \pre \li Must exist. /// \param rhs The right hand side object. /// \pre \li Must not be null. +/// \pre \li Must exist. /// \param error_out \see errors.h /// \return True if the objects are equal, false otherwise. IKA_API bool ikarus_object_is_equal(IkarusObject const * lhs, IkarusObject const * rhs, IkarusErrorData * error_out); diff --git a/src/objects/object.cpp b/src/objects/object.cpp index 2f52b48..e8a3353 100644 --- a/src/objects/object.cpp +++ b/src/objects/object.cpp @@ -2,6 +2,9 @@ #include +#include + +#include #include #include @@ -21,6 +24,64 @@ IkarusProject * ikarus_object_get_project(IkarusObject const * object, IkarusErr return object->project; } +char const * ikarus_object_get_name(IkarusObject const * object, IkarusErrorData * error_out) { + IKARUS_FAIL_IF_NULL(object, nullptr); + IKARUS_FAIL_IF_OBJECT_MISSING(object, nullptr); + + IKARUS_VTRYRV_OR_FAIL( + std::string ret, + nullptr, + "unable to get object name: {}", + IkarusErrorInfo_Database_QueryFailed, + object->project->db->template query_one("SELECT `name` FROM `objects` WHERE `id` = ?", object->id) + ); + + return strdup(ret.data()); +} + +void ikarus_object_set_name(IkarusObject * object, char const * name, IkarusErrorData * error_out) { + IKARUS_FAIL_IF_NULL(object, ); + IKARUS_FAIL_IF_OBJECT_MISSING(object, ); + IKARUS_FAIL_IF_NULL(name, ); + IKARUS_FAIL_IF(cppbase::is_empty_or_blank(name), , "name must not be empty", IkarusErrorInfo_Client_InvalidInput); + + IKARUS_TRYRV_OR_FAIL( + , + "unable to set object name: {}", + IkarusErrorInfo_Database_QueryFailed, + object->project->db->template execute("UPDATE `objects` SET `name` = ? WHERE `id` = ?", name, object->id) + ); +} + +char const * ikarus_object_get_information(IkarusObject const * object, IkarusErrorData * error_out) { + IKARUS_FAIL_IF_NULL(object, nullptr); + IKARUS_FAIL_IF_OBJECT_MISSING(object, nullptr); + + IKARUS_VTRYRV_OR_FAIL( + std::string ret, + nullptr, + "unable to get object information: {}", + IkarusErrorInfo_Database_QueryFailed, + object->project->db->template query_one("SELECT `information` FROM `objects` WHERE `id` = ?", object->id) + ); + + return strdup(ret.data()); +} + +void ikarus_object_set_information(IkarusObject * object, char const * information, IkarusErrorData * error_out) { + IKARUS_FAIL_IF_NULL(object, ); + IKARUS_FAIL_IF_OBJECT_MISSING(object, ); + IKARUS_FAIL_IF_NULL(information, ); + IKARUS_FAIL_IF(cppbase::is_empty_or_blank(information), , "information must not be empty", IkarusErrorInfo_Client_InvalidInput); + + IKARUS_TRYRV_OR_FAIL( + , + "unable to set object information: {}", + IkarusErrorInfo_Database_QueryFailed, + object->project->db->template execute("UPDATE `objects` SET `information` = ? WHERE `id` = ?", information, object->id) + ); +} + bool ikarus_object_is_equal(IkarusObject const * lhs, IkarusObject const * rhs, IkarusErrorData * error_out) { IKARUS_FAIL_IF_NULL(lhs, false); IKARUS_FAIL_IF_OBJECT_MISSING(lhs, false);