add object_get/set_name/information

Signed-off-by: Folling <mail@folling.io>
This commit is contained in:
Folling 2024-01-14 02:11:13 +01:00 committed by Folling
parent 779afc4333
commit dc3537344a
No known key found for this signature in database
2 changed files with 99 additions and 0 deletions

View file

@ -22,17 +22,55 @@ struct IkarusObject;
/// \brief Fetches the project of an object. /// \brief Fetches the project of an object.
/// \param object The object to fetch the project from. /// \param object The object to fetch the project from.
/// \pre \li Must not be null. /// \pre \li Must not be null.
/// \pre \li Must exist.
/// \param error_out \see errors.h /// \param error_out \see errors.h
/// \return The project of the object or null if an error occurs. /// \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); 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. /// \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 /// \details This neither compares the pointers nor does a deep copy. Instead it figures out if the objects _are_ the
/// same object. /// same object.
/// \param lhs The left hand side object. /// \param lhs The left hand side object.
/// \pre \li Must not be null. /// \pre \li Must not be null.
/// \pre \li Must exist.
/// \param rhs The right hand side object. /// \param rhs The right hand side object.
/// \pre \li Must not be null. /// \pre \li Must not be null.
/// \pre \li Must exist.
/// \param error_out \see errors.h /// \param error_out \see errors.h
/// \return True if the objects are equal, false otherwise. /// \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); IKA_API bool ikarus_object_is_equal(IkarusObject const * lhs, IkarusObject const * rhs, IkarusErrorData * error_out);

View file

@ -2,6 +2,9 @@
#include <fmt/format.h> #include <fmt/format.h>
#include <cppbase/strings.hpp>
#include <ikarus/errors.h>
#include <ikarus/objects/object.h> #include <ikarus/objects/object.h>
#include <errors.hpp> #include <errors.hpp>
@ -21,6 +24,64 @@ IkarusProject * ikarus_object_get_project(IkarusObject const * object, IkarusErr
return object->project; 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<std::string>("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<std::string>("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) { bool ikarus_object_is_equal(IkarusObject const * lhs, IkarusObject const * rhs, IkarusErrorData * error_out) {
IKARUS_FAIL_IF_NULL(lhs, false); IKARUS_FAIL_IF_NULL(lhs, false);
IKARUS_FAIL_IF_OBJECT_MISSING(lhs, false); IKARUS_FAIL_IF_OBJECT_MISSING(lhs, false);