Skip to content
Snippets Groups Projects
Verified Commit 6cd15d33 authored by Timm Fitschen's avatar Timm Fitschen
Browse files

WIP: insert delete

parent 11e12f7e
No related branches found
No related tags found
1 merge request!4ENH: Allow insertion and deletion of single entities
Pipeline #10820 failed
......@@ -35,6 +35,10 @@
#include <string> // for string
namespace caosdb::entity {
using ProtoParent = caosdb::entity::v1alpha1::Parent;
using ProtoEntity = caosdb::entity::v1alpha1::Entity;
class Parents;
/**
* Parent of an Entity.
......@@ -43,12 +47,20 @@ class Parent {
public:
explicit inline Parent(caosdb::entity::v1alpha1::Parent *wrapped)
: wrapped(wrapped){};
inline Parent() : wrapped(new ProtoParent){};
[[nodiscard]] auto GetId() const -> const std::string &;
[[nodiscard]] auto GetName() const -> const std::string &;
[[nodiscard]] auto GetDescription() const -> const std::string &;
auto SetId(const std::string &id) -> void;
auto SetName(const std::string &name) -> void;
friend class Parents;
private:
caosdb::entity::v1alpha1::Parent *wrapped;
auto AppendTo(
::google::protobuf::RepeatedPtrField<caosdb::entity::v1alpha1::Parent>
*parents) const -> void;
std::shared_ptr<caosdb::entity::v1alpha1::Parent> wrapped;
};
/**
......@@ -61,7 +73,11 @@ public:
::google::protobuf::RepeatedPtrField<caosdb::entity::v1alpha1::Parent>
*wrapped)
: wrapped(wrapped){};
[[nodiscard]] auto At(int index) const -> const Parent &;
auto Append(const Parent &parent) -> void;
[[nodiscard]] inline auto Size() const -> int { return wrapped->size(); }
[[nodiscard]] inline auto At(int index) const -> const Parent {
return Parent(&(wrapped->at(index)));
}
private:
::google::protobuf::RepeatedPtrField<caosdb::entity::v1alpha1::Parent>
......@@ -72,8 +88,8 @@ private:
/**
* Property of an Entity.
*
* This is a property which belongs to another entity. Don't confuse it with an
* Entity with the "Property" role.
* This is a property which belongs to another entity. Don't confuse it with
* an Entity with the "Property" role.
*
* @brief Property of an Entity.
*/
......@@ -88,6 +104,12 @@ public:
[[nodiscard]] auto GetValue() const -> const std::string &;
[[nodiscard]] auto GetUnit() const -> const std::string &;
[[nodiscard]] auto GetDatatype() const -> const std::string &;
auto SetId(const std::string &id) -> void;
auto SetName(const std::string &name) -> void;
auto SetImportance(const std::string &importance) -> void;
auto SetValue(const std::string &value) -> void;
auto SetUnit(const std::string &unit) -> void;
auto SetDatatype(const std::string &datatype) -> void;
private:
caosdb::entity::v1alpha1::Property *wrapped;
......@@ -104,6 +126,7 @@ public:
*wrapped)
: wrapped(wrapped){};
[[nodiscard]] auto At(int index) const -> const Property &;
auto Append(const Property &property) -> void;
private:
::google::protobuf::RepeatedPtrField<caosdb::entity::v1alpha1::Property>
......@@ -152,9 +175,15 @@ private:
*/
class Entity {
public:
explicit inline Entity(caosdb::entity::v1alpha1::Entity *wrapped)
: wrapped(wrapped) {
inline Entity() : wrapped(new ProtoEntity) {
properties.wrapped = this->wrapped->mutable_properties();
parents.wrapped = this->wrapped->mutable_parents();
errors.wrapped = this->wrapped->mutable_errors();
};
explicit inline Entity(ProtoEntity *wrapped) : wrapped(wrapped) {
errors.wrapped = this->wrapped->mutable_errors();
properties.wrapped = this->wrapped->mutable_properties();
parents.wrapped = this->wrapped->mutable_parents();
};
[[nodiscard]] inline auto GetId() const -> const std::string & {
......@@ -198,8 +227,17 @@ public:
return out;
}
auto SetId(const std::string &id) -> void;
auto SetName(const std::string &name) -> void;
auto SetImportance(const std::string &importance) -> void;
auto SetValue(const std::string &value) -> void;
auto SetUnit(const std::string &unit) -> void;
auto SetDatatype(const std::string &datatype) -> void;
auto AppendProperty(const Property &property) -> void;
auto AppendParent(const Parent &parent) -> void;
private:
std::unique_ptr<caosdb::entity::v1alpha1::Entity> wrapped;
std::unique_ptr<ProtoEntity> wrapped;
Properties properties;
Parents parents;
Messages errors;
......
......@@ -22,6 +22,7 @@
# add all source files to this list
set(libcaosdb_SRC
${CMAKE_CURRENT_SOURCE_DIR}/caosdb/authentication.cpp
${CMAKE_CURRENT_SOURCE_DIR}/caosdb/entity.cpp
${CMAKE_CURRENT_SOURCE_DIR}/caosdb/logging.cpp
${CMAKE_CURRENT_SOURCE_DIR}/caosdb/connection.cpp
${CMAKE_CURRENT_SOURCE_DIR}/caosdb/configuration.cpp
......
/*
* This file is a part of the CaosDB Project.
* Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
* Copyright (C) 2021 IndiScale GmbH <info@indiscale.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "caosdb/entity.h"
#include "caosdb/entity/v1alpha1/main.grpc.pb.h" // for EntityTransactionS...
#include "caosdb/entity/v1alpha1/main.pb.h" // for SingleRetrieveRequest
#include <cassert> // for assert
#include <map> // for map
#include <memory> // for allocator, unique_ptr
#include <stdexcept> // for out_of_range
#include <utility> // for move
namespace caosdb::entity {
auto Parent::SetName(const std::string &name) -> void {
this->wrapped->set_name(name);
}
auto Parent::SetId(const std::string &id) -> void { this->wrapped->set_id(id); }
auto Parent::AppendTo(
::google::protobuf::RepeatedPtrField<caosdb::entity::v1alpha1::Parent>
*parents) const -> void {
auto *destination = parents->Add();
destination = std::move(this->wrapped.get());
}
auto Parents::Append(const Parent &parent) -> void {
auto *destination = this->wrapped->Add();
destination = std::move(parent.wrapped.get());
// parent.AppendTo(this->wrapped);
}
[[nodiscard]] auto Entity::GetParents() const -> const Parents & {
return parents;
}
auto Entity::AppendParent(const Parent &parent) -> void {
this->parents.Append(parent);
}
} // namespace caosdb::entity
......@@ -22,6 +22,7 @@
set(test_cases
test_configuration
test_connection
test_entity
test_info
test_transaction
test_utility
......
/*
*
* This file is a part of the CaosDB Project.
*
* Copyright (C) 2021 Timm Fitschen <t.fitschen@indiscale.com>
* Copyright (C) 2021 IndiScale GmbH <info@indiscale.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "caosdb/entity.h" // for Entity
#include "caosdb/entity/v1alpha1/main.pb.h" // for Entity
#include "caosdb/exceptions.h" // for ConnectionError
#include "caosdb/transaction.h" // for Transaction, UniqueResult
#include "caosdb/transaction_status.h" // for ConnectionError
#include "caosdb_test_utility.h" // for EXPECT_THROW_MESSAGE
#include "gtest/gtest-message.h" // for Message
#include "gtest/gtest-test-part.h" // for SuiteApiResolver, TestPa...
#include "gtest/gtest_pred_impl.h" // for Test, TestInfo, TEST
#include <memory> // for allocator, unique_ptr
namespace caosdb::entity {
TEST(test_entity, test_append_parent) {
auto parent = Parent();
parent.SetName("RT1");
parent.SetId("some-id");
auto entity = Entity();
EXPECT_EQ(entity.GetParents().Size(), 0);
entity.AppendParent(parent);
EXPECT_EQ(entity.GetParents().Size(), 1);
}
} // namespace caosdb::entity
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment