added schema creation to session class
This commit is contained in:
parent
0f13884f06
commit
78c5f64b34
|
|
@ -39,6 +39,7 @@ using namespace matador;
|
||||||
TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]") {
|
TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]") {
|
||||||
|
|
||||||
ses.attach<matador::test::airplane>("airplane");
|
ses.attach<matador::test::airplane>("airplane");
|
||||||
|
ses.create_schema();
|
||||||
ses.insert<test::airplane>(1, "Boeing", "A380");
|
ses.insert<test::airplane>(1, "Boeing", "A380");
|
||||||
|
|
||||||
REQUIRE(true);
|
REQUIRE(true);
|
||||||
|
|
|
||||||
|
|
@ -200,7 +200,8 @@ class query_create_intermediate : public query_start_intermediate
|
||||||
public:
|
public:
|
||||||
explicit query_create_intermediate(connection &db);
|
explicit query_create_intermediate(connection &db);
|
||||||
|
|
||||||
query_execute_finish table(const std::string &table, std::initializer_list<column> columns);
|
query_execute_finish table(const std::string &table_name, std::initializer_list<column> columns);
|
||||||
|
query_execute_finish table(const std::string &table_name, const std::vector<column> &columns);
|
||||||
template<class Type>
|
template<class Type>
|
||||||
query_execute_finish table(const std::string &table_name)
|
query_execute_finish table(const std::string &table_name)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,8 @@ public:
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
void attach(const std::string &table_name);
|
void attach(const std::string &table_name);
|
||||||
|
|
||||||
|
void create_schema();
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
entity<Type> insert(Type *obj);
|
entity<Type> insert(Type *obj);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,15 +11,23 @@
|
||||||
|
|
||||||
namespace matador::sql {
|
namespace matador::sql {
|
||||||
|
|
||||||
|
class connection;
|
||||||
|
|
||||||
struct table_info
|
struct table_info
|
||||||
{
|
{
|
||||||
std::string name;
|
std::string name;
|
||||||
record prototype;
|
record prototype;
|
||||||
|
void create(connection &conn) const;
|
||||||
|
void drop(connection &conn) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
class table_repository
|
class table_repository
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
using repository = std::unordered_map<std::type_index, table_info>;
|
||||||
|
using iterator = repository::iterator;
|
||||||
|
using const_iterator = repository::const_iterator;
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
const table_info& attach(const std::string &table_name)
|
const table_info& attach(const std::string &table_name)
|
||||||
{
|
{
|
||||||
|
|
@ -52,8 +60,14 @@ public:
|
||||||
|
|
||||||
[[nodiscard]] bool exists(const std::type_index &ti) const;
|
[[nodiscard]] bool exists(const std::type_index &ti) const;
|
||||||
|
|
||||||
|
iterator begin();
|
||||||
|
const_iterator begin() const;
|
||||||
|
iterator end();
|
||||||
|
const_iterator end() const;
|
||||||
|
|
||||||
|
bool empty() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
using repository = std::unordered_map<std::type_index, table_info>;
|
|
||||||
repository repository_;
|
repository repository_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -153,9 +153,14 @@ query_create_intermediate::query_create_intermediate(connection &db)
|
||||||
builder_.create();
|
builder_.create();
|
||||||
}
|
}
|
||||||
|
|
||||||
query_execute_finish query_create_intermediate::table(const std::string &table, std::initializer_list<column> columns)
|
query_execute_finish query_create_intermediate::table(const std::string &table_name, std::initializer_list<column> columns)
|
||||||
{
|
{
|
||||||
return {connection_, builder_.table(table, columns)};
|
return table(table_name, std::vector<column>{columns});
|
||||||
|
}
|
||||||
|
|
||||||
|
query_execute_finish query_create_intermediate::table(const std::string &table_name, const std::vector<column> &columns)
|
||||||
|
{
|
||||||
|
return {connection_, builder_.table(table_name, columns)};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<table_repository> query_create_intermediate::tables() const
|
std::shared_ptr<table_repository> query_create_intermediate::tables() const
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,14 @@ session::session(connection_pool<connection> &pool)
|
||||||
: pool_(pool)
|
: pool_(pool)
|
||||||
, dialect_(backend_provider::instance().connection_dialect(pool_.info().type)) {}
|
, dialect_(backend_provider::instance().connection_dialect(pool_.info().type)) {}
|
||||||
|
|
||||||
|
void session::create_schema()
|
||||||
|
{
|
||||||
|
auto c = pool_.acquire();
|
||||||
|
for (const auto &t : table_repository_) {
|
||||||
|
t.second.create(*c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void session::drop_table(const std::string &table_name)
|
void session::drop_table(const std::string &table_name)
|
||||||
{
|
{
|
||||||
auto c = pool_.acquire();
|
auto c = pool_.acquire();
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,20 @@
|
||||||
#include "matador/sql/table_repository.hpp"
|
#include "matador/sql/table_repository.hpp"
|
||||||
|
#include "matador/sql/connection.hpp"
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace matador::sql {
|
namespace matador::sql {
|
||||||
|
|
||||||
|
void table_info::create(connection &conn) const
|
||||||
|
{
|
||||||
|
conn.create().table(name, prototype.columns()).execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
void table_info::drop(connection &conn) const
|
||||||
|
{
|
||||||
|
conn.drop().table(name).execute();
|
||||||
|
}
|
||||||
|
|
||||||
const table_info& table_repository::attach(const std::type_index ti, const table_info& table)
|
const table_info& table_repository::attach(const std::type_index ti, const table_info& table)
|
||||||
{
|
{
|
||||||
return repository_.try_emplace(ti, table).first->second;
|
return repository_.try_emplace(ti, table).first->second;
|
||||||
|
|
@ -36,4 +47,28 @@ bool table_repository::exists(const std::type_index &ti) const
|
||||||
return repository_.count(ti) > 0;
|
return repository_.count(ti) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
table_repository::iterator table_repository::begin()
|
||||||
|
{
|
||||||
|
return repository_.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
table_repository::const_iterator table_repository::begin() const
|
||||||
|
{
|
||||||
|
return repository_.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
table_repository::iterator table_repository::end()
|
||||||
|
{
|
||||||
|
return repository_.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
table_repository::const_iterator table_repository::end() const
|
||||||
|
{
|
||||||
|
return repository_.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool table_repository::empty() const
|
||||||
|
{
|
||||||
|
return repository_.empty();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue