Compare commits

..

No commits in common. "80426c5293cd1c5daac7634e27e3d2ed363a88f1" and "f38be4c6d987d1aceeca87fea193c349f713e96b" have entirely different histories.

6 changed files with 98 additions and 112 deletions

View File

@ -11,13 +11,6 @@
#include <mutex> #include <mutex>
namespace matador::object { namespace matador::object {
enum class object_state : uint8_t {
Transient,
Persistent,
Detached,
Removed
};
template<class Type> template<class Type>
class object_proxy final { class object_proxy final {
public: public:
@ -56,13 +49,6 @@ public:
[[nodiscard]] const utils::identifier &primary_key() const { return pk_; } [[nodiscard]] const utils::identifier &primary_key() const { return pk_; }
void primary_key(const utils::identifier &pk) { pk_ = pk; } void primary_key(const utils::identifier &pk) { pk_ = pk; }
bool is_persistent() const { return state_ == object_state::Persistent; }
bool is_transient() const { return state_ == object_state::Transient; }
bool is_detached() const { return state_ == object_state::Detached; }
bool is_removed() const { return state_ == object_state::Removed; }
void change_state(const object_state state) {
state_ = state;
}
private: private:
Type* resolve() const { Type* resolve() const {
if (obj_) { if (obj_) {
@ -86,7 +72,6 @@ private:
std::shared_ptr<Type> obj_{}; std::shared_ptr<Type> obj_{};
std::weak_ptr<object_resolver<Type>> resolver_{}; std::weak_ptr<object_resolver<Type>> resolver_{};
utils::identifier pk_{}; utils::identifier pk_{};
object_state state_{object_state::Transient};
mutable std::mutex mutex_{}; mutable std::mutex mutex_{};
}; };
} }

View File

@ -59,11 +59,6 @@ struct is_object_ptr : std::false_type {
template<typename Type> template<typename Type>
struct is_object_ptr<object_ptr<Type> > : std::true_type { struct is_object_ptr<object_ptr<Type> > : std::true_type {
}; };
template<class Type, typename... Args>
object_ptr<Type> make_object(Args &&... args) {
return object_ptr<Type>(std::make_shared<Type>(std::forward<Args>(args)...));
}
} }
#endif //OBJECT_PTR_HPP #endif //OBJECT_PTR_HPP

View File

@ -107,12 +107,12 @@ public:
* @param obj Object to insert * @param obj Object to insert
* @return Inserted object * @return Inserted object
*/ */
// template<typename Type> template<typename Type>
// utils::result<object::object_ptr<Type>, utils::error> insert(Type *obj); utils::result<object::object_ptr<Type>, utils::error> insert(Type *obj);
template<typename Type> template<typename Type>
utils::result<object::object_ptr<Type>, utils::error> insert(object::object_ptr<Type> obj); utils::result<object::object_ptr<Type>, utils::error> insert(object::object_ptr<Type> obj);
// template<class Type, typename... Args> template<class Type, typename... Args>
// utils::result<object::object_ptr<Type>, utils::error> insert(Args &&... args); utils::result<object::object_ptr<Type>, utils::error> insert(Args &&... args);
template<typename Type> template<typename Type>
utils::result<object::object_ptr<Type>, utils::error> update(const object::object_ptr<Type> &obj); utils::result<object::object_ptr<Type>, utils::error> update(const object::object_ptr<Type> &obj);
@ -157,6 +157,28 @@ private:
std::shared_ptr<sql::resolver_service> resolver_service_; std::shared_ptr<sql::resolver_service> resolver_service_;
}; };
template<typename Type>
utils::result<object::object_ptr<Type>, utils::error> session::insert(Type *obj) {
std::shared_ptr<Type> ptr(obj);
const auto it = schema_.find(typeid(Type));
if (it == schema_.end()) {
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
}
auto res = query::query::insert()
.into(it->second.name(), query::generator::columns<Type>(schema_))
.values(query::generator::placeholders<Type>())
.prepare(*this);
if (!res) {
return utils::failure(res.err());
}
if (const auto insert_result = res->bind(*obj).execute(); !insert_result.is_ok()) {
return utils::failure(insert_result.err());
}
return utils::ok(object::object_ptr{ptr});
}
template<typename Type> template<typename Type>
utils::result<object::object_ptr<Type>, utils::error> session::insert(object::object_ptr<Type> obj) { utils::result<object::object_ptr<Type>, utils::error> session::insert(object::object_ptr<Type> obj) {
const auto it = schema_.find(typeid(Type)); const auto it = schema_.find(typeid(Type));
@ -178,6 +200,11 @@ utils::result<object::object_ptr<Type>, utils::error> session::insert(object::ob
return utils::ok(obj); return utils::ok(obj);
} }
template<class Type, typename... Args>
utils::result<object::object_ptr<Type>, utils::error> session::insert(Args &&... args) {
return insert(new Type(std::forward<Args>(args)...));
}
class pk_object_binder final { class pk_object_binder final {
public: public:
explicit pk_object_binder(sql::statement &stmt, const size_t position) explicit pk_object_binder(sql::statement &stmt, const size_t position)

View File

@ -153,31 +153,6 @@ TEST_CASE_METHOD(QueryFixture, "Create and drop table statement", "[query][recor
check_table_not_exists("person"); check_table_not_exists("person");
} }
TEST_CASE_METHOD(QueryFixture, "Create table with identity primary key", "[query][record][create][identity]") {
check_table_not_exists("person");
auto res = query::create()
.table("person")
.columns({
column("id", basic_type::UInt32).primary_key().identity(),
column("name", basic_type::Varchar, 255),
column("age", basic_type::UInt16)
})
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(res->affected_rows == 0);
check_table_exists("person");
tables_to_drop.emplace("person");
res = query::drop()
.table("person")
.execute(db);
REQUIRE(res.is_ok());
REQUIRE(res->affected_rows == 0);
check_table_not_exists("person");
}
TEST_CASE_METHOD(QueryFixture, "Create and drop table statement with foreign key", "[query][record]") TEST_CASE_METHOD(QueryFixture, "Create and drop table statement with foreign key", "[query][record]")
{ {
auto res = query::create() auto res = query::create()

View File

@ -22,7 +22,7 @@ TEST_CASE_METHOD(SessionFixture, "Session insert test", "[session][insert]") {
} ); } );
REQUIRE(result.is_ok()); REQUIRE(result.is_ok());
auto plane = ses.insert(make_object<airplane>(1, "Boeing", "A380")); auto plane = ses.insert<airplane>(1, "Boeing", "A380");
REQUIRE(plane.is_ok()); REQUIRE(plane.is_ok());
const auto res = ses.find<airplane>(1); const auto res = ses.find<airplane>(1);
@ -40,7 +40,7 @@ TEST_CASE_METHOD(SessionFixture, "Session update test", "[session][update]") {
} ); } );
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
auto result = ses.insert(make_object<airplane>(1, "Boeing", "A380")); auto result = ses.insert<airplane>(1, "Boeing", "747");
REQUIRE(result.is_ok()); REQUIRE(result.is_ok());
const auto plane = result.value(); const auto plane = result.value();
@ -72,7 +72,7 @@ TEST_CASE_METHOD(SessionFixture, "Session delete test", "[session][delete]") {
schema.initialize_executor(ses); schema.initialize_executor(ses);
auto result = ses.insert(make_object<airplane>(1, "Boeing", "A380")); auto result = ses.insert<airplane>(1, "Boeing", "747");
REQUIRE(result.is_ok()); REQUIRE(result.is_ok());
const auto plane = result.value(); const auto plane = result.value();
@ -94,9 +94,9 @@ TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]")
.and_then([this] { return schema.create(db); } ); .and_then([this] { return schema.create(db); } );
REQUIRE(result.is_ok()); REQUIRE(result.is_ok());
auto plane = ses.insert(make_object<airplane>(1, "Boeing", "A380")); auto plane = ses.insert<airplane>(1, "Boeing", "A380");
REQUIRE(plane.is_ok()); REQUIRE(plane.is_ok());
auto f = ses.insert(make_object<flight>(2, *plane, "sully")); auto f = ses.insert<flight>(2, *plane, "sully");
REQUIRE(f.is_ok()); REQUIRE(f.is_ok());
const auto res = ses.find<flight>(2); const auto res = ses.find<flight>(2);
@ -115,7 +115,7 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find object with id", "[session
.and_then([this] { return schema.create(db); } ); .and_then([this] { return schema.create(db); } );
REQUIRE(result.is_ok()); REQUIRE(result.is_ok());
auto a380 = ses.insert(make_object<airplane>(1, "Boeing", "A380")); auto a380 = ses.insert<airplane>(1, "Boeing", "A380");
REQUIRE(a380.is_ok()); REQUIRE(a380.is_ok());
auto find_result = ses.find<airplane>(2); auto find_result = ses.find<airplane>(2);
@ -136,14 +136,13 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects", "[session][f
} ); } );
REQUIRE(result.is_ok()); REQUIRE(result.is_ok());
std::vector planes { std::vector<std::unique_ptr<airplane>> planes;
make_object<airplane>(1, "Airbus", "A380"), planes.emplace_back(new airplane(1, "Airbus", "A380"));
make_object<airplane>(2, "Boeing", "707"), planes.emplace_back(new airplane(2, "Boeing", "707"));
make_object<airplane>(3, "Boeing", "747"), planes.emplace_back(new airplane(3, "Boeing", "747"));
};
for (auto &&plane: planes) { for (auto &&plane: planes) {
auto res = ses.insert(plane); auto res = ses.insert(plane.release());
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
} }
@ -194,21 +193,20 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-ma
} }
REQUIRE(author_repo.size() == 2); REQUIRE(author_repo.size() == 2);
std::vector books { std::vector<std::unique_ptr<book>> books;
make_object<book>(1, "Jurassic Park", author_repo[0], 1990), books.emplace_back( new book{3, "Jurassic Park", author_repo[0], 1990} );
make_object<book>(2, "Timeline", author_repo[0], 1999), books.emplace_back( new book{4, "Timeline", author_repo[0], 1999} );
make_object<book>(3, "The Andromeda Strain", author_repo[0], 1969), books.emplace_back( new book{5, "The Andromeda Strain", author_repo[0], 1969} );
make_object<book>(4, "Congo", author_repo[0], 1980), books.emplace_back( new book{6, "Congo", author_repo[0], 1980} );
make_object<book>(5, "Prey", author_repo[0], 2002), books.emplace_back( new book{7, "Prey", author_repo[0], 2002} );
make_object<book>(6, "Carrie", author_repo[1], 1974), books.emplace_back( new book{8, "Carrie", author_repo[1], 1974} );
make_object<book>(7, "The Shining", author_repo[1], 1977), books.emplace_back( new book{9, "The Shining", author_repo[1], 1977} );
make_object<book>(8, "It", author_repo[1], 1986), books.emplace_back( new book{10, "It", author_repo[1], 1986} );
make_object<book>(9, "Misery", author_repo[1], 1987), books.emplace_back( new book{11, "Misery", author_repo[1], 1987} );
make_object<book>(10, "The Dark Tower: The Gunslinger", author_repo[1], 1982) books.emplace_back( new book{12, "The Dark Tower: The Gunslinger", author_repo[1], 1982} );
};
for (auto &&b: books) { for (auto &&b: books) {
auto res = ses.insert(b); auto res = ses.insert(b.release());
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
} }
@ -222,18 +220,21 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-ma
} }
TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-many eager relation", "[session][find][one-to-many][eager]") { TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-many eager relation", "[session][find][one-to-many][eager]") {
// auto result = repo.attach<department>("departments")
// .and_then([this] { return repo.attach<employee>("employees"); })
auto result = schema.attach<employee>("employees") auto result = schema.attach<employee>("employees")
.and_then([this] { return schema.attach<department>("departments"); }) .and_then([this] { return schema.attach<department>("departments"); })
.and_then([this] { return schema.create(db); } ); .and_then([this] {
return schema.create(db);
} );
REQUIRE(result.is_ok()); REQUIRE(result.is_ok());
std::vector departments { std::vector<std::unique_ptr<department>> departments;
make_object<department>(1, "Insurance"), departments.emplace_back(new department{1, "Insurance"});
make_object<department>(2, "Invoice") departments.emplace_back(new department{2, "Invoice"});
};
for (auto &&a: departments) { for (auto &&a: departments) {
auto res = ses.insert(a); auto res = ses.insert(a.release());
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
} }
@ -247,21 +248,20 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with one-to-ma
} }
REQUIRE(departments_repo.size() == 2); REQUIRE(departments_repo.size() == 2);
std::vector employees { std::vector<std::unique_ptr<employee>> employees;
make_object<employee>(1, "George", "Orwell", departments_repo[0]), employees.emplace_back( new employee{3, "George", "Orwell", departments_repo[0]} );
make_object<employee>(2, "Chris", "Tucker", departments_repo[0]), employees.emplace_back( new employee{4, "Chris", "Tucker", departments_repo[0]} );
make_object<employee>(3, "Steven", "King", departments_repo[0]), employees.emplace_back( new employee{5, "Steven", "King", departments_repo[0]} );
make_object<employee>(4, "John", "Wayne", departments_repo[0]), employees.emplace_back( new employee{6, "John", "Wayne", departments_repo[0]} );
make_object<employee>(5, "Clint", "Eastwood", departments_repo[0]), employees.emplace_back( new employee{7, "Clint", "Eastwood", departments_repo[0]} );
make_object<employee>(6, "Emma", "Thompson", departments_repo[1]), employees.emplace_back( new employee{8, "Emma", "Thompson", departments_repo[1]} );
make_object<employee>(7, "Ed", "Wood", departments_repo[1]), employees.emplace_back( new employee{9, "Ed", "Wood", departments_repo[1]} );
make_object<employee>(8, "Steven", "Spielberg", departments_repo[1]), employees.emplace_back( new employee{10, "Steven", "Spielberg", departments_repo[1]} );
make_object<employee>(9, "Jane", "Fonda", departments_repo[1]), employees.emplace_back( new employee{11, "Jane", "Fonda", departments_repo[1]} );
make_object<employee>(10, "Julia", "Roberts", departments_repo[1]) employees.emplace_back( new employee{12, "Julia", "Roberts", departments_repo[1]} );
};
for (auto &&b: employees) { for (auto &&b: employees) {
auto res = ses.insert(b); auto res = ses.insert(b.release());
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
} }
@ -285,13 +285,13 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with many-to-m
} ); } );
std::vector ingredients { std::vector ingredients {
make_object<ingredient>(1, "Apple"), object_ptr(std::make_shared<ingredient>(1, "Apple")),
make_object<ingredient>(2, "Strawberry"), object_ptr(std::make_shared<ingredient>(2, "Strawberry")),
make_object<ingredient>(3, "Pineapple"), object_ptr(std::make_shared<ingredient>(3, "Pineapple")),
make_object<ingredient>(4, "Sugar"), object_ptr(std::make_shared<ingredient>(4, "Sugar")),
make_object<ingredient>(5, "Flour"), object_ptr(std::make_shared<ingredient>(5, "Flour")),
make_object<ingredient>(6, "Butter"), object_ptr(std::make_shared<ingredient>(6, "Butter")),
make_object<ingredient>(7, "Beans") object_ptr(std::make_shared<ingredient>(7, "Beans"))
}; };
for (auto &i: ingredients) { for (auto &i: ingredients) {
@ -300,13 +300,15 @@ TEST_CASE_METHOD(SessionFixture, "Use session to find all objects with many-to-m
} }
std::vector recipes { std::vector recipes {
make_object<recipe>(1, "Apple Pie", std::vector{ingredients[0], ingredients[3], ingredients[4]}), object_ptr(std::make_shared<recipe>(1, "Apple Pie", std::vector{ingredients[0], ingredients[3], ingredients[4]})),
make_object<recipe>(2, "Strawberry Cake", std::vector{ingredients[5], ingredients[6]}), object_ptr(std::make_shared<recipe>(2, "Strawberry Cake", std::vector{ingredients[5], ingredients[6]})),
make_object<recipe>(3, "Pineapple Pie", std::vector{ingredients[0], ingredients[1], ingredients[2]}) object_ptr(std::make_shared<recipe>(3, "Pineapple Pie", std::vector{ingredients[0], ingredients[1], ingredients[2]}))
}; };
for (auto &r: recipes) { for (auto &r: recipes) {
auto res = ses.insert(r); auto res = ses.insert(r);
REQUIRE(res.is_ok()); REQUIRE(res.is_ok());
} }
} }

20
todo.md
View File

@ -9,15 +9,17 @@
- implement a flag class for enumerations - implement a flag class for enumerations
- PK generator - PK generator
1. Introduce execute_result 1. Introduce execute_context
2. Add `abstract_pk_generator` class 2. Introduce execute_result
3. Add `identity_pk_generator` class 3. Add `abstract_pk_generator` class
4. Add `sequence_pk_generator` class 4. Add `identity_pk_generator` class
5. Add `table_pk_generator` class 5. Add `sequence_pk_generator` class
6. Add `manual_pk_generator` class 6. Add `table_pk_generator` class
7. Extend `session::insert` logic to use pk generator 7. Add `manual_pk_generator` class
8. Add generator to `schema_node` or `table` class when attaching type 8. Extend `session::insert` logic to use pk generator
9. add `make_object`function 9. Extend `postgres_connection::execute` to handle `returning` clause
10. Add generator to `schema_node` or `table` class when attaching type
11. Extend `query_builder` and `intermediates` with `returning` intermediate
__Proposal for polymorphic classes:__ __Proposal for polymorphic classes:__