orm schema class progress added SchemaTest.cpp
This commit is contained in:
parent
5632746eb6
commit
557abecf91
|
|
@ -2,8 +2,8 @@ CPMAddPackage("gh:catchorg/Catch2@3.7.1")
|
||||||
|
|
||||||
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
|
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
|
||||||
|
|
||||||
#set(POSTGRES_CONNECTION_STRING "postgres://news:news@127.0.0.1:15432/matador")
|
set(POSTGRES_CONNECTION_STRING "postgres://test:test123!@127.0.0.1:15442/matador")
|
||||||
set(POSTGRES_CONNECTION_STRING "postgres://test:test123!@127.0.0.1:5432/matador")
|
#set(POSTGRES_CONNECTION_STRING "postgres://test:test123!@127.0.0.1:5432/matador")
|
||||||
|
|
||||||
configure_file(Connection.hpp.in ${PROJECT_BINARY_DIR}/backends/postgres/test/connection.hpp @ONLY IMMEDIATE)
|
configure_file(Connection.hpp.in ${PROJECT_BINARY_DIR}/backends/postgres/test/connection.hpp @ONLY IMMEDIATE)
|
||||||
|
|
||||||
|
|
@ -22,6 +22,7 @@ set(TEST_SOURCES
|
||||||
../../../test/backends/QueryRecordTest.cpp
|
../../../test/backends/QueryRecordTest.cpp
|
||||||
../../../test/backends/QueryStatementTests.cpp
|
../../../test/backends/QueryStatementTests.cpp
|
||||||
../../../test/backends/QueryTest.cpp
|
../../../test/backends/QueryTest.cpp
|
||||||
|
../../../test/backends/SchemaTest.cpp
|
||||||
../../../test/backends/SessionFixture.cpp
|
../../../test/backends/SessionFixture.cpp
|
||||||
../../../test/backends/SessionFixture.hpp
|
../../../test/backends/SessionFixture.hpp
|
||||||
../../../test/backends/SessionTest.cpp
|
../../../test/backends/SessionTest.cpp
|
||||||
|
|
|
||||||
|
|
@ -9,9 +9,24 @@ class connection_pool;
|
||||||
|
|
||||||
namespace matador::orm {
|
namespace matador::orm {
|
||||||
|
|
||||||
class schema {
|
class schema;
|
||||||
|
|
||||||
|
using schema_ref = std::reference_wrapper<schema>;
|
||||||
|
|
||||||
|
class schema_repository final {
|
||||||
public:
|
public:
|
||||||
explicit schema(const std::string &name);
|
utils::result<void, utils::error> add(const std::string &name, const schema &schema);
|
||||||
|
utils::result<void, utils::error> remove(const std::string &name);
|
||||||
|
|
||||||
|
[[nodiscard]] utils::result<schema_ref, utils::error> get(const std::string &name);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unordered_map<std::string, schema> schema_map_;
|
||||||
|
};
|
||||||
|
|
||||||
|
class schema final {
|
||||||
|
public:
|
||||||
|
schema(sql::connection_pool &pool, const std::string &name);
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name, const std::string &parent = "") {
|
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name, const std::string &parent = "") {
|
||||||
|
|
@ -23,12 +38,30 @@ public:
|
||||||
return repo_.attach<Type, SuperType>(name);
|
return repo_.attach<Type, SuperType>(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
utils::result<void, utils::error> create(sql::connection_pool &pool) const;
|
utils::result<void, utils::error> create() const;
|
||||||
utils::result<void, utils::error> drop() const;
|
utils::result<void, utils::error> drop() const;
|
||||||
|
|
||||||
|
template<typename Type>
|
||||||
|
utils::result<void, utils::error> drop_table();
|
||||||
|
utils::result<void, utils::error> drop_table(const std::string &table_name) const;
|
||||||
|
|
||||||
|
[[nodiscard]] utils::result<std::vector<object::attribute_definition>, utils::error> describe_table(const std::string &table_name) const;
|
||||||
|
[[nodiscard]] utils::result<bool, utils::error> table_exists(const std::string &table_name) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
object::repository repo_;
|
object::repository repo_;
|
||||||
|
sql::connection_pool &pool_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename Type>
|
||||||
|
utils::result<void, utils::error> schema::drop_table() {
|
||||||
|
auto info = repo_.info<Type>();
|
||||||
|
if (info) {
|
||||||
|
return drop_table(info->get().name());
|
||||||
|
}
|
||||||
|
|
||||||
|
return utils::failure(info.err());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif //MATADOR_SCHEMA_HPP
|
#endif //MATADOR_SCHEMA_HPP
|
||||||
|
|
@ -1,131 +0,0 @@
|
||||||
#ifndef SESSION_INSERT_BUILDER_HPP
|
|
||||||
#define SESSION_INSERT_BUILDER_HPP
|
|
||||||
|
|
||||||
#include "matador/orm/query_builder_exception.hpp"
|
|
||||||
|
|
||||||
#include "matador/query/criteria.hpp"
|
|
||||||
#include "matador/query/query_intermediates.hpp"
|
|
||||||
|
|
||||||
#include "matador/object/repository.hpp"
|
|
||||||
|
|
||||||
#include "matador/utils/cascade_type.hpp"
|
|
||||||
#include "matador/utils/primary_key_attribute.hpp"
|
|
||||||
|
|
||||||
namespace matador::orm {
|
|
||||||
struct entity_insert_data {
|
|
||||||
sql::table table;
|
|
||||||
std::vector<std::string> columns{};
|
|
||||||
std::vector<utils::database_type> values{};
|
|
||||||
};
|
|
||||||
|
|
||||||
enum class insert_build_error : std::uint8_t {
|
|
||||||
Ok = 0,
|
|
||||||
UnknownType,
|
|
||||||
MissingPrimaryKey,
|
|
||||||
UnexpectedError
|
|
||||||
};
|
|
||||||
|
|
||||||
class insert_builder_exception final : public std::exception {
|
|
||||||
public:
|
|
||||||
explicit insert_builder_exception(const insert_build_error error) : error_(error) {
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] insert_build_error error() const { return error_; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
const insert_build_error error_;
|
|
||||||
};
|
|
||||||
|
|
||||||
class session_insert_builder final {
|
|
||||||
public:
|
|
||||||
explicit session_insert_builder(const object::repository &scm)
|
|
||||||
: schema_(scm) {
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class EntityType>
|
|
||||||
utils::result<std::vector<entity_insert_data>, insert_build_error> build(const EntityType &obj) {
|
|
||||||
auto info = schema_.info<EntityType>();
|
|
||||||
if (!info) {
|
|
||||||
return utils::failure(insert_build_error::UnknownType);
|
|
||||||
}
|
|
||||||
table_info_stack_.push({info.value()});
|
|
||||||
entity_insert_data_ = {{sql::table{info.value().get().name()}}};
|
|
||||||
// processed_tables_.insert({info->get().name(), entity_insert_data_.root_table});
|
|
||||||
try {
|
|
||||||
access::process(*this, obj);
|
|
||||||
|
|
||||||
return {utils::ok(std::move(entity_insert_data_))};
|
|
||||||
} catch (const insert_builder_exception &ex) {
|
|
||||||
return {utils::failure(ex.error())};
|
|
||||||
} catch (...) {
|
|
||||||
return {utils::failure(insert_build_error::UnexpectedError)};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class V>
|
|
||||||
void on_primary_key(const char *id, V &x, const utils::primary_key_attribute& /*attr*/ = utils::default_pk_attributes) {
|
|
||||||
push(id, x);
|
|
||||||
}
|
|
||||||
|
|
||||||
void on_revision(const char *id, uint64_t &/*rev*/);
|
|
||||||
|
|
||||||
template<typename Type>
|
|
||||||
void on_attribute(const char *id, Type &x, const utils::field_attributes &/*attr*/ = utils::null_attributes) {
|
|
||||||
push(id, x);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Pointer>
|
|
||||||
void on_belongs_to(const char *id, Pointer &obj, const utils::foreign_attributes &attr) {
|
|
||||||
if (!utils::is_cascade_type_set(attr.cascade(), utils::cascade_type::INSERT)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto info = schema_.info<typename Pointer::value_type::value_type>();
|
|
||||||
if (!info) {
|
|
||||||
throw query_builder_exception{query_build_error::UnknownType};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class Pointer>
|
|
||||||
void on_has_one(const char *id, Pointer &obj, const utils::foreign_attributes &attr) {
|
|
||||||
if (!utils::is_cascade_type_set(attr.cascade(), utils::cascade_type::INSERT)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto info = schema_.info<typename Pointer::value_type::value_type>();
|
|
||||||
if (!info) {
|
|
||||||
throw query_builder_exception{query_build_error::UnknownType};
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class ContainerType>
|
|
||||||
void on_has_many(const char * /*id*/, ContainerType &, const char *join_column,
|
|
||||||
const utils::foreign_attributes &attr) {
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class ContainerType>
|
|
||||||
void on_has_many_to_many(const char *id, ContainerType &/*cont*/, const char *join_column,
|
|
||||||
const char *inverse_join_column, const utils::foreign_attributes &attr) {
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class ContainerType>
|
|
||||||
void on_has_many_to_many(const char *id, ContainerType &/*cont*/, const utils::foreign_attributes &attr) {
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
void push(const std::string &column_name, const utils::database_type &value);
|
|
||||||
|
|
||||||
private:
|
|
||||||
struct table_info {
|
|
||||||
std::reference_wrapper<const object::basic_object_info> info;
|
|
||||||
};
|
|
||||||
|
|
||||||
std::stack<table_info> table_info_stack_;
|
|
||||||
std::unordered_map<std::string, std::shared_ptr<sql::table> > processed_tables_;
|
|
||||||
const object::repository &schema_;
|
|
||||||
std::vector<entity_insert_data> entity_insert_data_;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif //SESSION_INSERT_BUILDER_HPP
|
|
||||||
|
|
@ -2,7 +2,6 @@ add_library(matador-orm STATIC
|
||||||
../../include/matador/orm/error_code.hpp
|
../../include/matador/orm/error_code.hpp
|
||||||
../../include/matador/orm/schema.hpp
|
../../include/matador/orm/schema.hpp
|
||||||
../../include/matador/orm/session.hpp
|
../../include/matador/orm/session.hpp
|
||||||
../../include/matador/orm/session_insert_builder.hpp
|
|
||||||
../../include/matador/orm/session_query_builder.hpp
|
../../include/matador/orm/session_query_builder.hpp
|
||||||
../../include/matador/query/attribute_string_writer.hpp
|
../../include/matador/query/attribute_string_writer.hpp
|
||||||
../../include/matador/query/criteria/abstract_criteria.hpp
|
../../include/matador/query/criteria/abstract_criteria.hpp
|
||||||
|
|
@ -80,7 +79,6 @@ add_library(matador-orm STATIC
|
||||||
orm/query_builder_exception.cpp
|
orm/query_builder_exception.cpp
|
||||||
orm/schema.cpp
|
orm/schema.cpp
|
||||||
orm/session.cpp
|
orm/session.cpp
|
||||||
orm/session_insert_builder.cpp
|
|
||||||
orm/session_query_builder.cpp
|
orm/session_query_builder.cpp
|
||||||
query/attribute_string_writer.cpp
|
query/attribute_string_writer.cpp
|
||||||
query/criteria/between_criteria.cpp
|
query/criteria/between_criteria.cpp
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,18 @@
|
||||||
#include "matador/orm/schema.hpp"
|
#include "matador/orm/schema.hpp"
|
||||||
|
|
||||||
|
#include "matador/orm/error_code.hpp"
|
||||||
|
#include "matador/orm/session.hpp"
|
||||||
#include "matador/query/query.hpp"
|
#include "matador/query/query.hpp"
|
||||||
|
|
||||||
#include "matador/sql/connection_pool.hpp"
|
#include "matador/sql/connection_pool.hpp"
|
||||||
|
|
||||||
namespace matador::orm {
|
namespace matador::orm {
|
||||||
schema::schema( const std::string& name )
|
schema::schema(sql::connection_pool &pool, const std::string& name)
|
||||||
: repo_(name){}
|
: repo_(name)
|
||||||
|
, pool_(pool) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
matador::utils::result<void, matador::utils::error> matador::orm::schema::create(sql::connection_pool &pool) const {
|
matador::utils::result<void, matador::utils::error> matador::orm::schema::create() const {
|
||||||
// Step 1: Build dependency graph
|
// Step 1: Build dependency graph
|
||||||
// std::unordered_map<std::string, std::vector<std::string> > dependency_graph;
|
// std::unordered_map<std::string, std::vector<std::string> > dependency_graph;
|
||||||
// std::unordered_map<std::string, std::pair<int,object::repository::node_ptr>> in_degree;
|
// std::unordered_map<std::string, std::pair<int,object::repository::node_ptr>> in_degree;
|
||||||
|
|
@ -40,7 +43,7 @@ matador::utils::result<void, matador::utils::error> matador::orm::schema::create
|
||||||
// }
|
// }
|
||||||
|
|
||||||
std::vector<std::string> fk_sql_commands;
|
std::vector<std::string> fk_sql_commands;
|
||||||
auto c = pool.acquire();
|
auto c = pool_.acquire();
|
||||||
for (const auto &node: repo_) {
|
for (const auto &node: repo_) {
|
||||||
auto ctx = query::query::create()
|
auto ctx = query::query::create()
|
||||||
.table(node->name(), node->info().definition().columns())
|
.table(node->name(), node->info().definition().columns())
|
||||||
|
|
@ -68,3 +71,31 @@ matador::utils::result<void, matador::utils::error> matador::orm::schema::create
|
||||||
matador::utils::result<void, matador::utils::error> matador::orm::schema::drop() const {
|
matador::utils::result<void, matador::utils::error> matador::orm::schema::drop() const {
|
||||||
return utils::ok<void>();
|
return utils::ok<void>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
matador::utils::result<void, matador::utils::error> matador::orm::schema::drop_table(const std::string& table_name) const {
|
||||||
|
const auto c = pool_.acquire();
|
||||||
|
auto result = query::query::drop()
|
||||||
|
.table(table_name)
|
||||||
|
.execute(*c);
|
||||||
|
if (result.is_error()) {
|
||||||
|
return utils::failure(result.err());
|
||||||
|
}
|
||||||
|
|
||||||
|
return utils::ok<void>();
|
||||||
|
}
|
||||||
|
|
||||||
|
matador::utils::result<std::vector<matador::object::attribute_definition>, matador::utils::error> matador::orm::schema::describe_table(const std::string& table_name) const {
|
||||||
|
const auto c = pool_.acquire();
|
||||||
|
if (!c.valid()) {
|
||||||
|
return utils::failure(make_error(error_code::NoConnectionAvailable, "Failed to acquire connection."));
|
||||||
|
}
|
||||||
|
return utils::ok(c->describe(table_name).release());
|
||||||
|
}
|
||||||
|
|
||||||
|
matador::utils::result<bool, matador::utils::error> matador::orm::schema::table_exists(const std::string& table_name) const {
|
||||||
|
const auto c = pool_.acquire();
|
||||||
|
if (!c.valid()) {
|
||||||
|
return utils::failure(make_error(error_code::NoConnectionAvailable, "Failed to acquire connection."));
|
||||||
|
}
|
||||||
|
return c->exists(repo_.name(), table_name);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +0,0 @@
|
||||||
#include "matador/orm/session_insert_builder.hpp"
|
|
||||||
|
|
||||||
namespace matador::orm {
|
|
||||||
void session_insert_builder::on_revision(const char* id, uint64_t& x) {
|
|
||||||
push(id, x);
|
|
||||||
}
|
|
||||||
|
|
||||||
void session_insert_builder::push(const std::string& column_name, const utils::database_type& value) {
|
|
||||||
entity_insert_data_.back().columns.emplace_back(column_name);
|
|
||||||
entity_insert_data_.back().values.emplace_back(value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,33 @@
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
|
||||||
|
#include "matador/sql/backend_provider.hpp"
|
||||||
|
#include "matador/sql/connection_pool.hpp"
|
||||||
|
|
||||||
|
#include "matador/orm/schema.hpp"
|
||||||
|
|
||||||
|
#include "../orm/backend/test_connection.hpp"
|
||||||
|
#include "../orm/backend/test_backend_service.hpp"
|
||||||
|
|
||||||
|
#include "../models/department.hpp"
|
||||||
|
|
||||||
|
using namespace matador;
|
||||||
|
|
||||||
|
TEST_CASE("Test schema", "[schema]") {
|
||||||
|
using namespace matador::test;
|
||||||
|
sql::backend_provider::instance().register_backend("noop", std::make_unique<test::orm::test_backend_service>());
|
||||||
|
|
||||||
|
sql::connection_pool pool("noop://noop.db", 4);
|
||||||
|
|
||||||
|
matador::orm::schema repo(pool, "NoopSchema");
|
||||||
|
|
||||||
|
auto result = repo.attach<department>("departments")
|
||||||
|
.and_then( [&repo] { return repo.attach<employee>("employees"); } );
|
||||||
|
REQUIRE(result);
|
||||||
|
|
||||||
|
result = repo.create();
|
||||||
|
REQUIRE(result);
|
||||||
|
|
||||||
|
const auto exists_result = repo.table_exists("departments");
|
||||||
|
REQUIRE(exists_result.is_ok());
|
||||||
|
REQUIRE(exists_result.value());
|
||||||
|
}
|
||||||
|
|
@ -13,21 +13,20 @@ add_executable(OrmTests
|
||||||
backend/test_result_reader.hpp
|
backend/test_result_reader.hpp
|
||||||
backend/test_statement.cpp
|
backend/test_statement.cpp
|
||||||
backend/test_statement.hpp
|
backend/test_statement.hpp
|
||||||
orm/SessionInsertBuilderTest.cpp
|
|
||||||
orm/SessionQueryBuilderTest.cpp
|
orm/SessionQueryBuilderTest.cpp
|
||||||
|
query/CriteriaTests.cpp
|
||||||
query/QueryBuilderTest.cpp
|
query/QueryBuilderTest.cpp
|
||||||
query/QueryFixture.cpp
|
query/QueryFixture.cpp
|
||||||
query/QueryFixture.hpp
|
query/QueryFixture.hpp
|
||||||
query/QueryTest.cpp
|
query/QueryTest.cpp
|
||||||
sql/ColumnTest.cpp
|
|
||||||
sql/ColumnGeneratorTest.cpp
|
sql/ColumnGeneratorTest.cpp
|
||||||
|
sql/ColumnTest.cpp
|
||||||
|
sql/ConnectionPoolFixture.hpp
|
||||||
sql/ConnectionPoolTest.cpp
|
sql/ConnectionPoolTest.cpp
|
||||||
sql/FieldTest.cpp
|
sql/FieldTest.cpp
|
||||||
|
sql/StatementCacheTest.cpp
|
||||||
utils/auto_reset_event.cpp
|
utils/auto_reset_event.cpp
|
||||||
utils/auto_reset_event.hpp
|
utils/auto_reset_event.hpp
|
||||||
sql/StatementCacheTest.cpp
|
|
||||||
sql/ConnectionPoolFixture.hpp
|
|
||||||
query/CriteriaTests.cpp
|
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(OrmTests matador-orm matador-core Catch2::Catch2WithMain)
|
target_link_libraries(OrmTests matador-orm matador-core Catch2::Catch2WithMain)
|
||||||
|
|
|
||||||
|
|
@ -1,48 +0,0 @@
|
||||||
#include <iostream>
|
|
||||||
#include <catch2/catch_test_macros.hpp>
|
|
||||||
|
|
||||||
#include "matador/sql/backend_provider.hpp"
|
|
||||||
#include "matador/sql/connection.hpp"
|
|
||||||
#include "matador/sql/column.hpp"
|
|
||||||
#include "matador/sql/table.hpp"
|
|
||||||
|
|
||||||
#include "matador/query/query.hpp"
|
|
||||||
|
|
||||||
#include "matador/orm/session_insert_builder.hpp"
|
|
||||||
|
|
||||||
#include "../backend/test_connection.hpp"
|
|
||||||
#include "../backend/test_backend_service.hpp"
|
|
||||||
|
|
||||||
#include "../../models/airplane.hpp"
|
|
||||||
#include "../../models/author.hpp"
|
|
||||||
#include "../../models/department.hpp"
|
|
||||||
#include "../../models/book.hpp"
|
|
||||||
#include "../../models/flight.hpp"
|
|
||||||
#include "../../models/recipe.hpp"
|
|
||||||
#include "../../models/order.hpp"
|
|
||||||
#include "../../models/student.hpp"
|
|
||||||
|
|
||||||
using namespace matador::object;
|
|
||||||
using namespace matador::orm;
|
|
||||||
using namespace matador::query;
|
|
||||||
using namespace matador::sql;
|
|
||||||
using namespace matador::test;
|
|
||||||
|
|
||||||
TEST_CASE("Create sql insert for entity with eager has one", "[query][entity][insert][builder]") {
|
|
||||||
using namespace matador::test;
|
|
||||||
backend_provider::instance().register_backend("noop", std::make_unique<orm::test_backend_service>());
|
|
||||||
connection db("noop://noop.db");
|
|
||||||
repository scm("noop");
|
|
||||||
auto result = scm.attach<airplane>("airplanes")
|
|
||||||
.and_then( [&scm] { return scm.attach<flight>("flights"); } );
|
|
||||||
REQUIRE(result);
|
|
||||||
|
|
||||||
session_insert_builder eib(scm);
|
|
||||||
|
|
||||||
auto b737 = object_ptr(new airplane{0, "Boeing", "737"});
|
|
||||||
flight f1{0, b737, "F828"};
|
|
||||||
|
|
||||||
// auto data = eib.build(f1);
|
|
||||||
|
|
||||||
// REQUIRE(data.is_ok());
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue