From 8024bcdda2124659a065ee91c06c088c438ca3e2 Mon Sep 17 00:00:00 2001 From: florian <f.spreckelsen@inidscale.com> Date: Tue, 10 Aug 2021 17:09:01 +0200 Subject: [PATCH] ENH: Add more setters and getters --- include/ccaosdb.h | 4 + src/ccaosdb.cpp | 256 +++++++++++++++++++++++++++++++++++++++++- test/test_ccaosdb.cpp | 145 ++++++++++++++++++++++++ 3 files changed, 403 insertions(+), 2 deletions(-) diff --git a/include/ccaosdb.h b/include/ccaosdb.h index 4c1641e..42a02d7 100644 --- a/include/ccaosdb.h +++ b/include/ccaosdb.h @@ -410,6 +410,10 @@ int caosdb_entity_property_set_unit(caosdb_entity_property *property, int caosdb_entity_property_set_value(caosdb_entity_property *property, const char *value); +int caosdb_entity_parent_set_id(caosdb_entity_parent *parent, const char *id); +int caosdb_entity_parent_set_name(caosdb_entity_parent *parent, + const char *name); + #ifdef __cplusplus } #endif diff --git a/src/ccaosdb.cpp b/src/ccaosdb.cpp index c47562a..4716970 100644 --- a/src/ccaosdb.cpp +++ b/src/ccaosdb.cpp @@ -79,6 +79,62 @@ extern "C" { body_part return 0; \ }) +/** + * Macro for property getters + */ +#define CAOSDB_PROPERTY_GET(element, body_part) \ + ERROR_RETURN_CODE( \ + GENERIC_ERROR, \ + int caosdb_entity_property_get_##element(caosdb_entity_property *property, \ + char *out), \ + { \ + auto *wrapped_property = \ + static_cast<caosdb::entity::Property *>(property->wrapped_property); \ + body_part return 0; \ + }) + +/** + * Macro for property setters + */ +#define CAOSDB_PROPERTY_SET(element, value, body_part) \ + ERROR_RETURN_CODE( \ + GENERIC_ERROR, \ + int caosdb_entity_property_set_##element(caosdb_entity_property *property, \ + const char *value), \ + { \ + auto *wrapped_property = \ + static_cast<caosdb::entity::Property *>(property->wrapped_property); \ + body_part return 0; \ + }) + +/** + * Macro for parent getters + */ +#define CAOSDB_PARENT_GET(element, body_part) \ + ERROR_RETURN_CODE( \ + GENERIC_ERROR, \ + int caosdb_entity_parent_get_##element(caosdb_entity_parent *parent, \ + char *out), \ + { \ + auto *wrapped_parent = \ + static_cast<caosdb::entity::Parent *>(parent->wrapped_parent); \ + body_part return 0; \ + }) + +/** + * Macro for parent setters + */ +#define CAOSDB_PARENT_SET(element, value, body_part) \ + ERROR_RETURN_CODE( \ + GENERIC_ERROR, \ + int caosdb_entity_parent_set_##element(caosdb_entity_parent *parent, \ + const char *value), \ + { \ + auto *wrapped_parent = \ + static_cast<caosdb::entity::Parent *>(parent->wrapped_parent); \ + body_part return 0; \ + }) + int caosdb_constants_LIBCAOSDB_VERSION_MAJOR() { return caosdb::LIBCAOSDB_VERSION_MAJOR; } @@ -406,8 +462,7 @@ ERROR_RETURN_CODE(GENERIC_ERROR, static_cast<caosdb::transaction::MultiResultSet *>( result_set->wrapped_result_set); auto requested_entity = wrapped_result_set->At(index); - entity->wrapped_entity = (void *)(&requested_entity); - // TODO(fspreck) explicit pointers to id, name, ... + entity->wrapped_entity = (&requested_entity); return 0; }) @@ -436,6 +491,33 @@ ERROR_RETURN_CODE(GENERIC_ERROR, return 0; }) +ERROR_RETURN_CODE( + GENERIC_ERROR, int caosdb_entity_create_property(caosdb_entity_property *out), + { + out->wrapped_property = new caosdb::entity::Property(); + return 0; + }) + +ERROR_RETURN_CODE( + GENERIC_ERROR, int caosdb_entity_delete_property(caosdb_entity_property *out), + { + delete static_cast<caosdb::entity::Property *>(out->wrapped_property); + return 0; + }) + +ERROR_RETURN_CODE(GENERIC_ERROR, + int caosdb_entity_create_parent(caosdb_entity_parent *out), { + out->wrapped_parent = new caosdb::entity::Parent(); + return 0; + }) + +ERROR_RETURN_CODE(GENERIC_ERROR, + int caosdb_entity_delete_parent(caosdb_entity_parent *out), { + delete static_cast<caosdb::entity::Parent *>( + out->wrapped_parent); + return 0; + }) + CAOSDB_ENTITY_GET(id, strcpy(out, wrapped_entity->GetId().c_str());) CAOSDB_ENTITY_GET(role, strcpy(out, wrapped_entity->GetRole().c_str());) CAOSDB_ENTITY_GET(name, strcpy(out, wrapped_entity->GetName().c_str());) @@ -447,6 +529,137 @@ CAOSDB_ENTITY_GET(unit, strcpy(out, wrapped_entity->GetUnit().c_str());) CAOSDB_ENTITY_GET(version_id, strcpy(out, wrapped_entity->GetVersionId().c_str());) +ERROR_RETURN_CODE( + GENERIC_ERROR, + int caosdb_entity_entity_get_errors_size(caosdb_entity_entity *entity, + int *out), + { + auto *wrapped_entity = + static_cast<caosdb::entity::Entity *>(entity->wrapped_entity); + *out = wrapped_entity->GetErrors().Size(); + return 0; + }) + +ERROR_RETURN_CODE( + GENERIC_ERROR, + int caosdb_entity_entity_get_error(caosdb_entity_entity *entity, + caosdb_entity_message *out, int index), + { + auto *wrapped_entity = + static_cast<caosdb::entity::Entity *>(entity->wrapped_entity); + auto requested_error = wrapped_entity->GetErrors().At(index); + out->wrapped_message = (&requested_error); + return 0; + }) + +ERROR_RETURN_CODE( + GENERIC_ERROR, + int caosdb_entity_entity_get_warnings_size(caosdb_entity_entity *entity, + int *out), + { + auto *wrapped_entity = + static_cast<caosdb::entity::Entity *>(entity->wrapped_entity); + *out = wrapped_entity->GetWarnings().Size(); + return 0; + }) + +ERROR_RETURN_CODE( + GENERIC_ERROR, + int caosdb_entity_entity_get_warning(caosdb_entity_entity *entity, + caosdb_entity_message *out, int index), + { + auto *wrapped_entity = + static_cast<caosdb::entity::Entity *>(entity->wrapped_entity); + auto requested_warning = wrapped_entity->GetWarnings().At(index); + out->wrapped_message = (&requested_warning); + return 0; + }) + +ERROR_RETURN_CODE( + GENERIC_ERROR, + int caosdb_entity_entity_get_infos_size(caosdb_entity_entity *entity, + int *out), + { + auto *wrapped_entity = + static_cast<caosdb::entity::Entity *>(entity->wrapped_entity); + *out = wrapped_entity->GetInfos().Size(); + return 0; + }) + +ERROR_RETURN_CODE( + GENERIC_ERROR, + int caosdb_entity_entity_get_info(caosdb_entity_entity *entity, + caosdb_entity_message *out, int index), + { + auto *wrapped_entity = + static_cast<caosdb::entity::Entity *>(entity->wrapped_entity); + auto requested_info = wrapped_entity->GetInfos().At(index); + out->wrapped_message = (&requested_info); + return 0; + }) + +ERROR_RETURN_CODE( + GENERIC_ERROR, + int caosdb_entity_entity_get_properties_size(caosdb_entity_entity *entity, + int *out), + { + auto *wrapped_entity = + static_cast<caosdb::entity::Entity *>(entity->wrapped_entity); + *out = wrapped_entity->GetProperties().Size(); + return 0; + }) + +ERROR_RETURN_CODE( + GENERIC_ERROR, + int caosdb_entity_entity_get_property(caosdb_entity_entity *entity, + caosdb_entity_property *out, int index), + { + auto *wrapped_entity = + static_cast<caosdb::entity::Entity *>(entity->wrapped_entity); + auto requested_property = wrapped_entity->GetProperties().At(index); + out->wrapped_property = &requested_property; + return 0; + }) + +ERROR_RETURN_CODE( + GENERIC_ERROR, + int caosdb_entity_entity_get_parents_size(caosdb_entity_entity *entity, + int *out), + { + auto *wrapped_entity = + static_cast<caosdb::entity::Entity *>(entity->wrapped_entity); + *out = wrapped_entity->GetParents().Size(); + return 0; + }) + +ERROR_RETURN_CODE( + GENERIC_ERROR, + int caosdb_entity_entity_get_parent(caosdb_entity_entity *entity, + caosdb_entity_parent *out, int index), + { + auto *wrapped_entity = + static_cast<caosdb::entity::Entity *>(entity->wrapped_entity); + auto requested_parent = wrapped_entity->GetParents().At(index); + out->wrapped_parent = &requested_parent; + return 0; + }) + +CAOSDB_PARENT_GET(id, strcpy(out, wrapped_parent->GetId().c_str());) +CAOSDB_PARENT_GET(name, strcpy(out, wrapped_parent->GetName().c_str());) +CAOSDB_PARENT_GET(description, + strcpy(out, wrapped_parent->GetDescription().c_str());) + +CAOSDB_PROPERTY_GET(id, strcpy(out, wrapped_property->GetId().c_str());) +CAOSDB_PROPERTY_GET(name, strcpy(out, wrapped_property->GetName().c_str());) +CAOSDB_PROPERTY_GET(description, + strcpy(out, wrapped_property->GetDescription().c_str());) +CAOSDB_PROPERTY_GET(importance, + strcpy(out, wrapped_property->GetImportance().c_str());) +CAOSDB_PROPERTY_GET(datatype, + strcpy(out, wrapped_property->GetDatatype().c_str());) +CAOSDB_PROPERTY_GET(unit, strcpy(out, wrapped_property->GetUnit().c_str());) +CAOSDB_PROPERTY_GET(value, strcpy(out, wrapped_property->GetValue().c_str());) + CAOSDB_ENTITY_SET(role, role, wrapped_entity->SetRole(std::string(role));) CAOSDB_ENTITY_SET(name, name, wrapped_entity->SetName(std::string(name));) CAOSDB_ENTITY_SET(description, description, @@ -455,4 +668,43 @@ CAOSDB_ENTITY_SET(datatype, datatype, wrapped_entity->SetDatatype(std::string(datatype));) CAOSDB_ENTITY_SET(unit, unit, wrapped_entity->SetUnit(std::string(unit));) CAOSDB_ENTITY_SET(value, value, wrapped_entity->SetValue(std::string(value));) + +ERROR_RETURN_CODE( + GENERIC_ERROR, + int caosdb_entity_entity_append_parent(caosdb_entity_entity *entity, + caosdb_entity_parent *parent), + { + auto *wrapped_entity = + static_cast<caosdb::entity::Entity *>(entity->wrapped_entity); + auto *wrapped_parent = + static_cast<caosdb::entity::Parent *>(parent->wrapped_parent); + wrapped_entity->AppendParent(*wrapped_parent); + return 0; + }) + +ERROR_RETURN_CODE( + GENERIC_ERROR, + int caosdb_entity_entity_append_property(caosdb_entity_entity *entity, + caosdb_entity_property *property), + { + auto *wrapped_entity = + static_cast<caosdb::entity::Entity *>(entity->wrapped_entity); + auto *wrapped_property = + static_cast<caosdb::entity::Property *>(property->wrapped_property); + wrapped_entity->AppendProperty(*wrapped_property); + return 0; + }) + +CAOSDB_PARENT_SET(id, id, wrapped_parent->SetId(std::string(id));) +CAOSDB_PARENT_SET(name, name, wrapped_parent->SetName(std::string(name));) + +CAOSDB_PROPERTY_SET(name, name, wrapped_property->SetName(std::string(name));) +CAOSDB_PROPERTY_SET(id, id, wrapped_property->SetId(std::string(id));) +CAOSDB_PROPERTY_SET(datatype, datatype, + wrapped_property->SetDatatype(std::string(datatype));) +CAOSDB_PROPERTY_SET(importance, importance, + wrapped_property->SetImportance(std::string(importance));) +CAOSDB_PROPERTY_SET(unit, unit, wrapped_property->SetUnit(std::string(unit));) +CAOSDB_PROPERTY_SET(value, value, + wrapped_property->SetValue(std::string(value));) } diff --git a/test/test_ccaosdb.cpp b/test/test_ccaosdb.cpp index d9a49cb..92c3bc0 100644 --- a/test/test_ccaosdb.cpp +++ b/test/test_ccaosdb.cpp @@ -178,3 +178,148 @@ TEST_F(test_ccaosdb, test_entity) { return_code = caosdb_entity_delete_entity(&entity); EXPECT_EQ(return_code, 0); } + +TEST_F(test_ccaosdb, test_parent) { + caosdb_entity_parent parent; + + int return_code(caosdb_entity_create_parent(&parent)); + EXPECT_EQ(return_code, 0); + + caosdb_entity_parent_set_id(&parent, "some_id"); + caosdb_entity_parent_set_name(&parent, "some_name"); + + char out[255] = {"a"}; // NOLINT + caosdb_entity_parent_get_id(&parent, out); + EXPECT_EQ(strcmp(out, "some_id"), 0); + + caosdb_entity_parent_get_name(&parent, out); + EXPECT_EQ(strcmp(out, "some_name"), 0); + + return_code = caosdb_entity_delete_parent(&parent); + EXPECT_EQ(return_code, 0); +} + +TEST_F(test_ccaosdb, test_property) { + caosdb_entity_property property; + + int return_code(caosdb_entity_create_property(&property)); + EXPECT_EQ(return_code, 0); + + caosdb_entity_property_set_id(&property, "some_id"); + caosdb_entity_property_set_name(&property, "some_name"); + caosdb_entity_property_set_datatype(&property, "some_datatype"); + caosdb_entity_property_set_importance(&property, "some_importance"); + caosdb_entity_property_set_unit(&property, "some_unit"); + caosdb_entity_property_set_value(&property, "some_value"); + + char out[255] = {"a"}; // NOLINT + caosdb_entity_property_get_id(&property, out); + EXPECT_EQ(strcmp(out, "some_id"), 0); + + caosdb_entity_property_get_name(&property, out); + EXPECT_EQ(strcmp(out, "some_name"), 0); + + caosdb_entity_property_get_datatype(&property, out); + EXPECT_EQ(strcmp(out, "some_datatype"), 0); + + caosdb_entity_property_get_importance(&property, out); + EXPECT_EQ(strcmp(out, "some_importance"), 0); + + caosdb_entity_property_get_unit(&property, out); + EXPECT_EQ(strcmp(out, "some_unit"), 0); + + caosdb_entity_property_get_value(&property, out); + EXPECT_EQ(strcmp(out, "some_value"), 0); + + return_code = caosdb_entity_delete_property(&property); + EXPECT_EQ(return_code, 0); +} + +TEST_F(test_ccaosdb, test_entity_with_parent_and_property) { + std::cout << "Creating objects ... " << std::endl; + caosdb_entity_parent input_parent; + int return_code(caosdb_entity_create_parent(&input_parent)); + EXPECT_EQ(return_code, 0); + + caosdb_entity_parent_set_id(&input_parent, "parent_id"); + caosdb_entity_parent_set_name(&input_parent, "parent_name"); + + caosdb_entity_property input_property; + return_code = caosdb_entity_create_property(&input_property); + EXPECT_EQ(return_code, 0); + + caosdb_entity_property_set_id(&input_property, "property_id"); + caosdb_entity_property_set_name(&input_property, "property_name"); + caosdb_entity_property_set_datatype(&input_property, "property_datatype"); + caosdb_entity_property_set_value(&input_property, "property_value"); + + caosdb_entity_entity entity; + return_code = caosdb_entity_create_entity(&entity); + EXPECT_EQ(return_code, 0); + + std::cout << "Appending parent and property ..." << std::endl; + return_code = caosdb_entity_entity_append_parent(&entity, &input_parent); + EXPECT_EQ(return_code, 0); + + return_code = caosdb_entity_entity_append_property(&entity, &input_property); + EXPECT_EQ(return_code, 0); + + std::cout << "Counting parents and properties ..." << std::endl; + int count[] = {0}; // NOLINT + return_code = caosdb_entity_entity_get_parents_size(&entity, count); + EXPECT_EQ(return_code, 0); + EXPECT_EQ(*count, 1); + + return_code = caosdb_entity_entity_get_properties_size(&entity, count); + EXPECT_EQ(return_code, 0); + EXPECT_EQ(*count, 1); + + char in[255] = {"a"}; // NOLINT + char out[255] = {"b"}; // NOLINT + + std::cout << "Comparing ..." << std::endl; + caosdb_entity_property output_property; + return_code = caosdb_entity_entity_get_property(&entity, &output_property, 0); + EXPECT_EQ(return_code, 0); + + caosdb_entity_property_get_id(&input_property, in); + caosdb_entity_property_get_id(&output_property, out); + EXPECT_EQ(strcmp(in, out), 0); + + caosdb_entity_property_get_name(&input_property, in); + caosdb_entity_property_get_name(&output_property, out); + EXPECT_EQ(strcmp(in, out), 0); + + caosdb_entity_property_get_datatype(&input_property, in); + caosdb_entity_property_get_datatype(&output_property, out); + EXPECT_EQ(strcmp(in, out), 0); + + caosdb_entity_property_get_value(&input_property, in); + caosdb_entity_property_get_value(&output_property, out); + EXPECT_EQ(strcmp(in, out), 0); + + caosdb_entity_parent output_parent; + return_code = caosdb_entity_entity_get_parent(&entity, &output_parent, 0); + EXPECT_EQ(return_code, 0); + + caosdb_entity_parent_get_id(&input_parent, in); + caosdb_entity_parent_get_id(&output_parent, out); + EXPECT_EQ(strcmp(in, out), 0); + + caosdb_entity_parent_get_name(&input_parent, in); + caosdb_entity_parent_get_name(&output_parent, out); + EXPECT_EQ(strcmp(in, out), 0); + + // Delete everything + std::cout << "Deleting ..." << std::endl; + return_code = caosdb_entity_delete_parent(&input_parent); + EXPECT_EQ(return_code, 0); + return_code = caosdb_entity_delete_property(&input_property); + EXPECT_EQ(return_code, 0); + return_code = caosdb_entity_delete_entity(&entity); + EXPECT_EQ(return_code, 0); + return_code = caosdb_entity_delete_parent(&output_parent); + EXPECT_EQ(return_code, 0); + return_code = caosdb_entity_delete_property(&output_property); + EXPECT_EQ(return_code, 0); +} -- GitLab