added date_type_t, time_type_t and timestamp, ensure allocated postgres resources are released, split repository into basic_repository and repository.
This commit is contained in:
parent
f07a360da2
commit
0c6b5a0a93
|
|
@ -31,8 +31,9 @@ public:
|
||||||
void write_value(size_t pos, const bool &x) override;
|
void write_value(size_t pos, const bool &x) override;
|
||||||
void write_value(size_t pos, const float &x) override;
|
void write_value(size_t pos, const float &x) override;
|
||||||
void write_value(size_t pos, const double &x) override;
|
void write_value(size_t pos, const double &x) override;
|
||||||
void write_value(size_t pos, const time &x ) override;
|
void write_value(size_t pos, const utils::date_type_t &x ) override;
|
||||||
void write_value(size_t pos, const date &x ) override;
|
void write_value(size_t pos, const utils::time_type_t &x ) override;
|
||||||
|
void write_value(size_t pos, const utils::timestamp &x ) override;
|
||||||
void write_value(size_t pos, const char *x) override;
|
void write_value(size_t pos, const char *x) override;
|
||||||
void write_value(size_t pos, const char *x, size_t size) override;
|
void write_value(size_t pos, const char *x, size_t size) override;
|
||||||
void write_value(size_t pos, const std::string &x) override;
|
void write_value(size_t pos, const std::string &x) override;
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,8 @@ namespace matador::backends::postgres {
|
||||||
class postgres_statement final : public sql::statement_impl
|
class postgres_statement final : public sql::statement_impl
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
postgres_statement(PGconn *db, std::string name, const sql::query_context &query);
|
postgres_statement(PGconn *db, PGresult *res, std::string name, const sql::query_context &query);
|
||||||
|
~postgres_statement() override;
|
||||||
|
|
||||||
utils::result<size_t, utils::error> execute(const sql::parameter_binder& bindings) override;
|
utils::result<size_t, utils::error> execute(const sql::parameter_binder& bindings) override;
|
||||||
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> fetch(const sql::parameter_binder& bindings) override;
|
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> fetch(const sql::parameter_binder& bindings) override;
|
||||||
|
|
@ -21,6 +22,7 @@ protected:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PGconn *db_{nullptr};
|
PGconn *db_{nullptr};
|
||||||
|
PGresult *res_ = nullptr;
|
||||||
|
|
||||||
std::string name_;
|
std::string name_;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -86,15 +86,18 @@ utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> postgres_co
|
||||||
PGresult *res = PQexec(conn_, context.sql.c_str());
|
PGresult *res = PQexec(conn_, context.sql.c_str());
|
||||||
|
|
||||||
if (is_result_error(res)) {
|
if (is_result_error(res)) {
|
||||||
return utils::failure(make_error(sql::error_code::FETCH_FAILED, res, conn_, "Failed to fetch", context.sql));
|
const auto err = make_error(sql::error_code::FETCH_FAILED, res, conn_, "Failed to fetch", context.sql);
|
||||||
|
PQclear(res);
|
||||||
|
return utils::failure(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<object::attribute> prototype = context.prototype;
|
std::vector<object::attribute> prototype = context.prototype;
|
||||||
|
|
||||||
const int num_col = PQnfields(res);
|
const int num_col = PQnfields(res);
|
||||||
if (prototype.size() != static_cast<size_t>(num_col)) {
|
if (prototype.size() != static_cast<size_t>(num_col)) {
|
||||||
return utils::failure(make_error(sql::error_code::FETCH_FAILED, res, conn_,
|
const auto err = make_error(sql::error_code::FETCH_FAILED, res, conn_, "Number of received columns doesn't match expected columns.", context.sql);
|
||||||
"Number of received columns doesn't match expected columns.", context.sql));
|
PQclear(res);
|
||||||
|
return utils::failure(err);
|
||||||
}
|
}
|
||||||
for (int i = 0; i < num_col; ++i) {
|
for (int i = 0; i < num_col; ++i) {
|
||||||
if (!prototype.at(i).is_null()) {
|
if (!prototype.at(i).is_null()) {
|
||||||
|
|
@ -127,13 +130,13 @@ utils::result<std::unique_ptr<sql::statement_impl>, utils::error> postgres_conne
|
||||||
const sql::query_context &context) {
|
const sql::query_context &context) {
|
||||||
auto statement_name = generate_statement_name(context);
|
auto statement_name = generate_statement_name(context);
|
||||||
|
|
||||||
const PGresult *result = PQprepare(conn_, statement_name.c_str(), context.sql.c_str(), static_cast<int>(context.bind_vars.size()), nullptr);
|
PGresult *result = PQprepare(conn_, statement_name.c_str(), context.sql.c_str(), static_cast<int>(context.bind_vars.size()), nullptr);
|
||||||
|
|
||||||
if (is_result_error(result)) {
|
if (is_result_error(result)) {
|
||||||
return utils::failure(make_error(sql::error_code::PREPARE_FAILED, result, conn_, "Failed to prepare", context.sql));
|
return utils::failure(make_error(sql::error_code::PREPARE_FAILED, result, conn_, "Failed to prepare", context.sql));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<sql::statement_impl> s(std::make_unique<postgres_statement>(conn_, statement_name, context));
|
std::unique_ptr<sql::statement_impl> s(std::make_unique<postgres_statement>(conn_, result, statement_name, context));
|
||||||
return utils::ok(std::move(s));
|
return utils::ok(std::move(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -277,8 +280,11 @@ utils::result<bool, utils::error> postgres_connection::exists(const std::string
|
||||||
|
|
||||||
const auto result = utils::to<size_t>(PQcmdTuples(res));
|
const auto result = utils::to<size_t>(PQcmdTuples(res));
|
||||||
if (!result) {
|
if (!result) {
|
||||||
|
PQclear(res);
|
||||||
return utils::failure(make_error(sql::error_code::FAILURE, res, conn_, "Failed to convert result value", stmt));
|
return utils::failure(make_error(sql::error_code::FAILURE, res, conn_, "Failed to convert result value", stmt));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PQclear(res);
|
||||||
return utils::ok(*result == 1);
|
return utils::ok(*result == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -50,12 +50,12 @@ postgres_parameter_binder::bind_data::bind_data(const size_t size)
|
||||||
, bytes(size)
|
, bytes(size)
|
||||||
, values(size)
|
, values(size)
|
||||||
, lengths(size)
|
, lengths(size)
|
||||||
, formats(size)
|
, formats(size) {
|
||||||
{}
|
}
|
||||||
|
|
||||||
postgres_parameter_binder::postgres_parameter_binder(const size_t size)
|
postgres_parameter_binder::postgres_parameter_binder(const size_t size)
|
||||||
: bind_data_(size)
|
: bind_data_(size) {
|
||||||
{}
|
}
|
||||||
|
|
||||||
void postgres_parameter_binder::write_value(const size_t pos, const int8_t &x) {
|
void postgres_parameter_binder::write_value(const size_t pos, const int8_t &x) {
|
||||||
detail::bind_value(bind_data_, pos, x);
|
detail::bind_value(bind_data_, pos, x);
|
||||||
|
|
@ -123,18 +123,21 @@ void postgres_parameter_binder::write_value(const size_t pos, const std::string
|
||||||
write_value(pos, x);
|
write_value(pos, x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void postgres_parameter_binder::write_value(const size_t pos, const time &/*x*/) {
|
void postgres_parameter_binder::write_value(const size_t pos, const utils::date_type_t &/*x*/) {
|
||||||
|
// bind_data_.strings[pos] = utils::to_string(x, utils::date_format::ISO8601);
|
||||||
|
bind_data_.values[pos] = bind_data_.strings[pos].data();
|
||||||
|
bind_data_.lengths[pos] = static_cast<int>(bind_data_.strings[pos].size());
|
||||||
|
bind_data_.formats[pos] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void postgres_parameter_binder::write_value(const size_t pos, const utils::time_type_t &/*x*/) {
|
||||||
// bind_data_.strings[pos] = utils::to_string(x, "%Y-%m-%d %T.%f");
|
// bind_data_.strings[pos] = utils::to_string(x, "%Y-%m-%d %T.%f");
|
||||||
bind_data_.values[pos] = bind_data_.strings[pos].data();
|
bind_data_.values[pos] = bind_data_.strings[pos].data();
|
||||||
bind_data_.lengths[pos] = static_cast<int>(bind_data_.strings[pos].size());
|
bind_data_.lengths[pos] = static_cast<int>(bind_data_.strings[pos].size());
|
||||||
bind_data_.formats[pos] = 0;
|
bind_data_.formats[pos] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void postgres_parameter_binder::write_value(const size_t pos, const date &/*x*/) {
|
void postgres_parameter_binder::write_value(size_t pos, const utils::timestamp &x) {
|
||||||
// bind_data_.strings[pos] = utils::to_string(x, utils::date_format::ISO8601);
|
|
||||||
bind_data_.values[pos] = bind_data_.strings[pos].data();
|
|
||||||
bind_data_.lengths[pos] = static_cast<int>(bind_data_.strings[pos].size());
|
|
||||||
bind_data_.formats[pos] = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void postgres_parameter_binder::write_value(const size_t pos, const utils::blob &x) {
|
void postgres_parameter_binder::write_value(const size_t pos, const utils::blob &x) {
|
||||||
|
|
@ -144,10 +147,10 @@ void postgres_parameter_binder::write_value(const size_t pos, const utils::blob
|
||||||
bind_data_.formats[pos] = 1;
|
bind_data_.formats[pos] = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void postgres_parameter_binder::write_value(const size_t /*pos*/, const utils::value &/*x*/, size_t /*size*/) {}
|
void postgres_parameter_binder::write_value(const size_t /*pos*/, const utils::value &/*x*/, size_t /*size*/) {
|
||||||
|
}
|
||||||
|
|
||||||
const postgres_parameter_binder::bind_data &postgres_parameter_binder::params() const {
|
const postgres_parameter_binder::bind_data &postgres_parameter_binder::params() const {
|
||||||
return bind_data_;
|
return bind_data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -153,6 +153,8 @@ void postgres_result_reader::read_value( const char* /*id*/, const size_t index,
|
||||||
unsigned char* unescaped = PQunescapeBytea(data, &length);
|
unsigned char* unescaped = PQunescapeBytea(data, &length);
|
||||||
|
|
||||||
value.assign(unescaped, unescaped+length);
|
value.assign(unescaped, unescaped+length);
|
||||||
|
|
||||||
|
PQfreemem(unescaped);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Type>
|
template <typename Type>
|
||||||
|
|
@ -168,6 +170,8 @@ void set_value<utils::blob>(const char* str, utils::value& value) {
|
||||||
unsigned char* unescaped = PQunescapeBytea(reinterpret_cast<const unsigned char*>(str), &length);
|
unsigned char* unescaped = PQunescapeBytea(reinterpret_cast<const unsigned char*>(str), &length);
|
||||||
|
|
||||||
value = utils::blob(unescaped, unescaped+length);
|
value = utils::blob(unescaped, unescaped+length);
|
||||||
|
|
||||||
|
PQfreemem(unescaped);
|
||||||
}
|
}
|
||||||
|
|
||||||
void postgres_result_reader::read_value(const char * /*id*/, const size_t index, utils::value &val, size_t) {
|
void postgres_result_reader::read_value(const char * /*id*/, const size_t index, utils::value &val, size_t) {
|
||||||
|
|
|
||||||
|
|
@ -5,12 +5,17 @@
|
||||||
|
|
||||||
namespace matador::backends::postgres {
|
namespace matador::backends::postgres {
|
||||||
|
|
||||||
postgres_statement::postgres_statement(PGconn *db, std::string name, const sql::query_context &query)
|
postgres_statement::postgres_statement(PGconn *db, PGresult *res, std::string name, const sql::query_context &query)
|
||||||
: statement_impl(query, 0)
|
: statement_impl(query, 0)
|
||||||
, db_(db)
|
, db_(db)
|
||||||
|
, res_(res)
|
||||||
, name_(std::move(name))
|
, name_(std::move(name))
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
postgres_statement::~postgres_statement() {
|
||||||
|
PQclear(res_);
|
||||||
|
}
|
||||||
|
|
||||||
utils::result<size_t, utils::error> postgres_statement::execute(const sql::parameter_binder& bindings) {
|
utils::result<size_t, utils::error> postgres_statement::execute(const sql::parameter_binder& bindings) {
|
||||||
const auto* postgres_bindings = dynamic_cast<const postgres_parameter_binder*>(&bindings);
|
const auto* postgres_bindings = dynamic_cast<const postgres_parameter_binder*>(&bindings);
|
||||||
if (!postgres_bindings) {
|
if (!postgres_bindings) {
|
||||||
|
|
@ -28,12 +33,14 @@ utils::result<size_t, utils::error> postgres_statement::execute(const sql::param
|
||||||
return utils::failure(make_error(sql::error_code::EXECUTE_FAILED, res, db_, "Failed to execute statement", query_.sql));
|
return utils::failure(make_error(sql::error_code::EXECUTE_FAILED, res, db_, "Failed to execute statement", query_.sql));
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto *tuples = PQcmdTuples(res);
|
size_t value{0};
|
||||||
if (strlen(tuples) == 0) {
|
if (const auto *tuples = PQcmdTuples(res); strlen(tuples) != 0) {
|
||||||
return utils::ok(static_cast<size_t>(0));
|
value = std::stoul(tuples);
|
||||||
}
|
}
|
||||||
|
|
||||||
return utils::ok(static_cast<size_t>(std::stoul(tuples)));
|
PQclear(res);
|
||||||
|
|
||||||
|
return utils::ok(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> postgres_statement::fetch(const sql::parameter_binder& bindings) {
|
utils::result<std::unique_ptr<sql::query_result_impl>, utils::error> postgres_statement::fetch(const sql::parameter_binder& bindings) {
|
||||||
|
|
|
||||||
|
|
@ -67,7 +67,7 @@ private:
|
||||||
friend class object_generator;
|
friend class object_generator;
|
||||||
|
|
||||||
std::string name_;
|
std::string name_;
|
||||||
std::shared_ptr<object> owner_;
|
std::weak_ptr<object> owner_;
|
||||||
utils::basic_type type_{utils::basic_type::Null};
|
utils::basic_type type_{utils::basic_type::Null};
|
||||||
utils::field_attributes options_{};
|
utils::field_attributes options_{};
|
||||||
null_option_type null_option_{null_option_type::NotNull};
|
null_option_type null_option_{null_option_type::NotNull};
|
||||||
|
|
|
||||||
|
|
@ -58,11 +58,11 @@ public:
|
||||||
[[nodiscard]] bool endpoints_empty() const;
|
[[nodiscard]] bool endpoints_empty() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
basic_object_info(std::shared_ptr<repository_node> node, std::shared_ptr<class object> &&obj);
|
basic_object_info(const repository_node& node, const std::shared_ptr<class object> &obj);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::shared_ptr<class object> object_;
|
std::shared_ptr<class object> object_;
|
||||||
std::shared_ptr<repository_node> node_; /**< prototype node of the represented object type */
|
const repository_node& node_; /**< prototype node of the represented object type */
|
||||||
t_endpoint_map relation_endpoints_;
|
t_endpoint_map relation_endpoints_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,156 @@
|
||||||
|
#ifndef MATADOR_BASIC_REPOSITORY_HPP
|
||||||
|
#define MATADOR_BASIC_REPOSITORY_HPP
|
||||||
|
|
||||||
|
#include "matador/object/error_code.hpp"
|
||||||
|
#include "matador/object/object_info.hpp"
|
||||||
|
#include "matador/object/repository_node_iterator.hpp"
|
||||||
|
|
||||||
|
#include "matador/logger/logger.hpp"
|
||||||
|
|
||||||
|
#include "matador/utils/error.hpp"
|
||||||
|
#include "matador/utils/result.hpp"
|
||||||
|
|
||||||
|
#include <typeindex>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
namespace matador::object {
|
||||||
|
utils::error make_error(error_code ec, const std::string &msg);
|
||||||
|
|
||||||
|
class repository_node;
|
||||||
|
class const_repository_node_iterator;
|
||||||
|
|
||||||
|
class basic_repository {
|
||||||
|
public:
|
||||||
|
typedef const_repository_node_iterator const_iterator; /**< Shortcut for the list const iterator. */
|
||||||
|
|
||||||
|
explicit basic_repository(std::string name = "");
|
||||||
|
~basic_repository();
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param node Node to attach
|
||||||
|
* @param parent Name of parent node
|
||||||
|
* @return Attached node
|
||||||
|
*/
|
||||||
|
[[nodiscard]] utils::result<repository_node*, utils::error> attach_node(repository_node *node, const std::string &parent);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Detaches a given node from the schema. If the
|
||||||
|
* node is a parent of other nodes, these nodes are
|
||||||
|
* detached as well.
|
||||||
|
*
|
||||||
|
* @param node Node to detach from schema
|
||||||
|
* @return Result object indicating success or failure
|
||||||
|
*/
|
||||||
|
[[nodiscard]] utils::result<void, utils::error> detach(repository_node *node);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the first schema node.
|
||||||
|
*
|
||||||
|
* @return The first schema node iterator.
|
||||||
|
*/
|
||||||
|
[[nodiscard]] const_iterator begin() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the last schema node.
|
||||||
|
*
|
||||||
|
* @return The last schema node iterator.
|
||||||
|
*/
|
||||||
|
[[nodiscard]] const_iterator end() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the schema contains
|
||||||
|
* no schema nodes.
|
||||||
|
*
|
||||||
|
* @return True if the schema is empty
|
||||||
|
*/
|
||||||
|
[[nodiscard]] bool empty() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the current number of the schema node.
|
||||||
|
*
|
||||||
|
* @return Number of schema nodes
|
||||||
|
*/
|
||||||
|
[[nodiscard]] size_t size() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the name of the schema.
|
||||||
|
*
|
||||||
|
* @return The name of the schema
|
||||||
|
*/
|
||||||
|
[[nodiscard]] std::string name() const;
|
||||||
|
|
||||||
|
[[nodiscard]] bool contains(const std::string &name) const;
|
||||||
|
[[nodiscard]] bool contains(const std::type_index &index) const;
|
||||||
|
template < typename Type >
|
||||||
|
[[nodiscard]] bool contains() const {
|
||||||
|
return contains(std::type_index(typeid(Type)));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[[nodiscard]] utils::result<basic_object_info_ref, utils::error> basic_info(const std::type_index& ti) const;
|
||||||
|
[[nodiscard]] utils::result<basic_object_info_ref, utils::error> basic_info(const std::string &name) const;
|
||||||
|
template<typename Type>
|
||||||
|
[[nodiscard]] utils::result<basic_object_info_ref, utils::error> basic_info() const {
|
||||||
|
return basic_info(std::type_index(typeid(Type)));
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] utils::result<attribute*, utils::error> primary_key_attribute(const std::type_index &ti) const;
|
||||||
|
|
||||||
|
void dump(std::ostream &os) const;
|
||||||
|
static void dump(std::ostream &os, const repository_node& node);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
using t_node_map = std::unordered_map<std::string, repository_node*>;
|
||||||
|
using t_type_index_node_map = std::unordered_map<std::type_index, repository_node*>;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
[[nodiscard]] const_iterator find_node(const std::string &name) const;
|
||||||
|
[[nodiscard]] const_iterator find_node(const std::type_index &type_index) const;
|
||||||
|
template<typename Type>
|
||||||
|
[[nodiscard]] const_iterator find_node() const {
|
||||||
|
return find_node(std::type_index(typeid(Type)));
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] bool has_node(const std::string &name) const;
|
||||||
|
[[nodiscard]] bool has_node(const std::type_index &index) const;
|
||||||
|
[[nodiscard]] bool has_node(const repository_node* node) const;
|
||||||
|
|
||||||
|
static void insert_node(repository_node *parent, repository_node *child);
|
||||||
|
void remove_node(repository_node *node);
|
||||||
|
|
||||||
|
[[nodiscard]] bool expecting_relation_node(const std::string &name) const;
|
||||||
|
void expect_relation_node(const std::string &name, const std::type_index &ti);
|
||||||
|
void remove_expected_relation_node(const std::string &name);
|
||||||
|
|
||||||
|
[[nodiscard]] std::shared_ptr<object> provide_object_in_advance(const std::type_index &ti, const std::shared_ptr<object>& obj);
|
||||||
|
[[nodiscard]] bool has_object_for_type(const std::type_index &ti) const;
|
||||||
|
[[nodiscard]] std::shared_ptr<object> object_for_type(const std::type_index &ti) const;
|
||||||
|
void remove_object_for_type(const std::type_index &ti);
|
||||||
|
|
||||||
|
[[nodiscard]] bool is_node_announced(const std::type_index &ti) const;
|
||||||
|
void push_announce_node(const std::type_index &ti, std::unique_ptr<repository_node> &&node);
|
||||||
|
[[nodiscard]] repository_node* announce_node(const std::type_index &ti) const;
|
||||||
|
[[nodiscard]] std::unique_ptr<repository_node> pop_announce_node(const std::type_index &ti);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
friend class object_generator;
|
||||||
|
template < typename NodeType, template<typename> typename ...Observers >
|
||||||
|
friend class foreign_node_completer;
|
||||||
|
template < typename NodeType, template<typename> typename ...Observers >
|
||||||
|
friend class relation_completer;
|
||||||
|
|
||||||
|
std::string name_;
|
||||||
|
repository_node* root_{nullptr};
|
||||||
|
|
||||||
|
t_node_map nodes_by_name_;
|
||||||
|
t_type_index_node_map nodes_by_type_;
|
||||||
|
logger::logger log_;
|
||||||
|
|
||||||
|
std::unordered_map<std::type_index, std::unique_ptr<repository_node>> announced_node_;
|
||||||
|
std::unordered_map<std::type_index, attribute*> missing_references_{};
|
||||||
|
std::unordered_map<std::string, std::type_index> expected_relation_nodes_;
|
||||||
|
std::unordered_map<std::type_index, std::shared_ptr<object>> object_by_type_{};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
#endif //MATADOR_BASIC_REPOSITORY_HPP
|
||||||
|
|
@ -1,9 +1,8 @@
|
||||||
#ifndef FOREIGN_NODE_COMPLETER_HPP
|
#ifndef FOREIGN_NODE_COMPLETER_HPP
|
||||||
#define FOREIGN_NODE_COMPLETER_HPP
|
#define FOREIGN_NODE_COMPLETER_HPP
|
||||||
|
|
||||||
#include "matador/object/internal/shadow_repository.hpp"
|
#include "matador/object/basic_repository.hpp"
|
||||||
#include "matador/object/internal/observer_list_copy_creator.hpp"
|
#include "matador/object/internal/observer_list_copy_creator.hpp"
|
||||||
#include "matador/object/many_to_many_relation.hpp"
|
|
||||||
#include "matador/object/repository_node.hpp"
|
#include "matador/object/repository_node.hpp"
|
||||||
#include "matador/object/join_columns_collector.hpp"
|
#include "matador/object/join_columns_collector.hpp"
|
||||||
#include "matador/object/object_ptr.hpp"
|
#include "matador/object/object_ptr.hpp"
|
||||||
|
|
@ -32,13 +31,9 @@ public:
|
||||||
*/
|
*/
|
||||||
template<typename NodeType, template<typename> typename ...Observers>
|
template<typename NodeType, template<typename> typename ...Observers>
|
||||||
class foreign_node_completer final {
|
class foreign_node_completer final {
|
||||||
private:
|
|
||||||
using node_ptr = std::shared_ptr<repository_node>;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static void complete(const std::shared_ptr<repository_node> &node, const std::vector<std::unique_ptr<observer<NodeType>>> &observers) {
|
static void complete(repository_node *node, const std::vector<std::unique_ptr<observer<NodeType>>> &observers) {
|
||||||
internal::shadow_repository shadow(node->repo_);
|
foreign_node_completer completer(node->repo_, observers);
|
||||||
foreign_node_completer completer(shadow, observers);
|
|
||||||
|
|
||||||
completer.complete_node(node);
|
completer.complete_node(node);
|
||||||
}
|
}
|
||||||
|
|
@ -78,12 +73,12 @@ public:
|
||||||
static void on_has_many_to_many(const char * /*id*/, CollectionType &/*collection*/, const utils::foreign_attributes &/*attr*/) {}
|
static void on_has_many_to_many(const char * /*id*/, CollectionType &/*collection*/, const utils::foreign_attributes &/*attr*/) {}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit foreign_node_completer(internal::shadow_repository &shadow, const std::vector<std::unique_ptr<observer<NodeType>>> &observers)
|
explicit foreign_node_completer(basic_repository &repo, const std::vector<std::unique_ptr<observer<NodeType>>> &observers)
|
||||||
: repo_(shadow)
|
: repo_(repo)
|
||||||
, log_(logger::create_logger("node_completer"))
|
, log_(logger::create_logger("node_completer"))
|
||||||
, observers_(observers) {}
|
, observers_(observers) {}
|
||||||
|
|
||||||
void complete_node(const std::shared_ptr<repository_node> &node) {
|
void complete_node(repository_node *node) {
|
||||||
nodes_.push(node);
|
nodes_.push(node);
|
||||||
|
|
||||||
NodeType obj;
|
NodeType obj;
|
||||||
|
|
@ -102,8 +97,8 @@ private:
|
||||||
if (repo_.is_node_announced(typeid(Type))) {
|
if (repo_.is_node_announced(typeid(Type))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const auto node = repository_node::make_node<Type>(repo_.repo(), "", []{ return std::make_unique<Type>(); }, std::move(observers));
|
auto node = repository_node::make_node<Type>(repo_, "", []{ return std::make_unique<Type>(); }, std::move(observers));
|
||||||
repo_.push_announce_node(typeid(Type), node);
|
repo_.push_announce_node(typeid(Type), std::move(node));
|
||||||
// if (auto result = repo_.attach_node(node)) {
|
// if (auto result = repo_.attach_node(node)) {
|
||||||
// foreign_node_completer<Type>::complete(result.value(), {});
|
// foreign_node_completer<Type>::complete(result.value(), {});
|
||||||
// }
|
// }
|
||||||
|
|
@ -121,8 +116,8 @@ private:
|
||||||
friend class foreign_node_completer;
|
friend class foreign_node_completer;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::stack<node_ptr> nodes_;
|
std::stack<repository_node*> nodes_;
|
||||||
internal::shadow_repository &repo_;
|
basic_repository &repo_;
|
||||||
logger::logger log_;
|
logger::logger log_;
|
||||||
join_columns_collector join_columns_collector_{};
|
join_columns_collector join_columns_collector_{};
|
||||||
const std::vector<std::unique_ptr<observer<NodeType>>>& observers_;
|
const std::vector<std::unique_ptr<observer<NodeType>>>& observers_;
|
||||||
|
|
@ -153,8 +148,8 @@ void foreign_node_completer<NodeType, Observers...>::on_has_many(const char * /*
|
||||||
template<typename NodeType, template<typename> typename ...Observers>
|
template<typename NodeType, template<typename> typename ...Observers>
|
||||||
void foreign_node_completer<NodeType, Observers...>::register_relation_endpoints(const endpoint_ptr &endpoint,
|
void foreign_node_completer<NodeType, Observers...>::register_relation_endpoints(const endpoint_ptr &endpoint,
|
||||||
const endpoint_ptr &other_endpoint) {
|
const endpoint_ptr &other_endpoint) {
|
||||||
endpoint->node_->info_->register_relation_endpoint(other_endpoint->node_->type_index(), endpoint);
|
endpoint->node().info_->register_relation_endpoint(other_endpoint->node().type_index(), endpoint);
|
||||||
other_endpoint->node_->info_->register_relation_endpoint(endpoint->node_->type_index(), other_endpoint);
|
other_endpoint->node().info_->register_relation_endpoint(endpoint->node().type_index(), other_endpoint);
|
||||||
link_relation_endpoints(endpoint, other_endpoint);
|
link_relation_endpoints(endpoint, other_endpoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,52 +0,0 @@
|
||||||
#ifndef SHADOW_SCHEMA_HPP
|
|
||||||
#define SHADOW_SCHEMA_HPP
|
|
||||||
|
|
||||||
#include "matador/object/basic_object_info.hpp"
|
|
||||||
|
|
||||||
#include "matador/utils/result.hpp"
|
|
||||||
#include "matador/utils/error.hpp"
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
#include <typeindex>
|
|
||||||
|
|
||||||
namespace matador::object {
|
|
||||||
class repository;
|
|
||||||
class repository_node;
|
|
||||||
class object;
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace matador::object::internal {
|
|
||||||
class shadow_repository {
|
|
||||||
private:
|
|
||||||
using node_ptr = std::shared_ptr<repository_node>;
|
|
||||||
|
|
||||||
public:
|
|
||||||
explicit shadow_repository(repository& s);
|
|
||||||
|
|
||||||
[[nodiscard]] repository& repo() const;
|
|
||||||
[[nodiscard]] bool contains(const std::type_index& ti) const;
|
|
||||||
[[nodiscard]] utils::result<basic_object_info_ref, utils::error> basic_info(const std::type_index& ti) const;
|
|
||||||
[[nodiscard]] utils::result<node_ptr, utils::error> find_node(const std::type_index &type_index) const;
|
|
||||||
[[nodiscard]] utils::result<node_ptr, utils::error> find_node(const std::string &name) const;
|
|
||||||
[[nodiscard]] utils::result<node_ptr, utils::error> attach_node(const std::shared_ptr<repository_node> &node) const;
|
|
||||||
[[nodiscard]] utils::result<void, utils::error> detach_node(const std::shared_ptr<repository_node> &node) const;
|
|
||||||
|
|
||||||
[[nodiscard]] bool expecting_relation_node(const std::string &name) const;
|
|
||||||
void expect_relation_node(const std::string &name, const std::type_index &ti) const;
|
|
||||||
void remove_expected_relation_node(const std::string &name) const;
|
|
||||||
|
|
||||||
[[nodiscard]] std::shared_ptr<object> provide_object_in_advance(const std::type_index &ti, const std::shared_ptr<object>& obj) const;
|
|
||||||
[[nodiscard]] bool has_object_for_type(const std::type_index &ti) const;
|
|
||||||
[[nodiscard]] std::shared_ptr<object> object_for_type(const std::type_index &ti) const;
|
|
||||||
void remove_object_for_type(const std::type_index &ti) const;
|
|
||||||
|
|
||||||
[[nodiscard]] bool is_node_announced(const std::type_index &ti) const;
|
|
||||||
void push_announce_node(const std::type_index &ti, const node_ptr &node) const;
|
|
||||||
[[nodiscard]] node_ptr announce_node(const std::type_index &ti) const;
|
|
||||||
[[nodiscard]] node_ptr pop_announce_node(const std::type_index &ti) const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
repository& repo_;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
#endif //SHADOW_SCHEMA_HPP
|
|
||||||
|
|
@ -11,8 +11,6 @@
|
||||||
|
|
||||||
namespace matador::object {
|
namespace matador::object {
|
||||||
|
|
||||||
class repository;
|
|
||||||
|
|
||||||
class object {
|
class object {
|
||||||
public:
|
public:
|
||||||
explicit object(std::string name);
|
explicit object(std::string name);
|
||||||
|
|
@ -23,7 +21,6 @@ public:
|
||||||
[[nodiscard]] const utils::identifier& primary_key() const;
|
[[nodiscard]] const utils::identifier& primary_key() const;
|
||||||
[[nodiscard]] bool has_primary_key() const;
|
[[nodiscard]] bool has_primary_key() const;
|
||||||
|
|
||||||
[[nodiscard]] bool empty() const;
|
|
||||||
[[nodiscard]] const std::string& name() const;
|
[[nodiscard]] const std::string& name() const;
|
||||||
|
|
||||||
void update_name(const std::string& name);
|
void update_name(const std::string& name);
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
#ifndef MATADOR_OBJECT_GENERATOR_HPP
|
#ifndef MATADOR_OBJECT_GENERATOR_HPP
|
||||||
#define MATADOR_OBJECT_GENERATOR_HPP
|
#define MATADOR_OBJECT_GENERATOR_HPP
|
||||||
|
|
||||||
#include "matador/object/internal/shadow_repository.hpp"
|
#include "matador/object/basic_repository.hpp"
|
||||||
#include "matador/object/object.hpp"
|
#include "matador/object/object.hpp"
|
||||||
|
|
||||||
#include "matador/utils/access.hpp"
|
#include "matador/utils/access.hpp"
|
||||||
|
|
@ -10,8 +10,6 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
namespace matador::object {
|
namespace matador::object {
|
||||||
class repository;
|
|
||||||
|
|
||||||
class pk_type_determinator {
|
class pk_type_determinator {
|
||||||
private:
|
private:
|
||||||
pk_type_determinator() = default;
|
pk_type_determinator() = default;
|
||||||
|
|
@ -51,26 +49,26 @@ private:
|
||||||
|
|
||||||
class object_generator {
|
class object_generator {
|
||||||
private:
|
private:
|
||||||
explicit object_generator(const repository& repo, const std::shared_ptr<object> &object);
|
explicit object_generator(basic_repository& repo, const std::shared_ptr<object> &object);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
template < class Type >
|
template < class Type >
|
||||||
static std::shared_ptr<object> generate(const repository &repo, const std::string &name) {
|
static std::shared_ptr<object> generate(basic_repository &repo, const std::string &name) {
|
||||||
return generate(std::make_unique<Type>(), repo, name);
|
return generate(std::make_unique<Type>(), repo, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
template < class Type >
|
template < class Type >
|
||||||
static std::shared_ptr<object> generate(std::unique_ptr<Type>&& t, const repository &repo, const std::string &name) {
|
static std::shared_ptr<object> generate(std::unique_ptr<Type>&& t, basic_repository &repo, const std::string &name) {
|
||||||
const internal::shadow_repository shadow_repo(const_cast<repository&>(repo));
|
|
||||||
const std::type_index ti(typeid(Type));
|
const std::type_index ti(typeid(Type));
|
||||||
if (shadow_repo.has_object_for_type(ti)) {
|
if (repo.has_object_for_type(ti)) {
|
||||||
auto obj = shadow_repo.object_for_type(ti);
|
auto obj = repo.object_for_type(ti);
|
||||||
shadow_repo.remove_object_for_type(ti);
|
repo.remove_object_for_type(ti);
|
||||||
obj->update_name(name);
|
obj->update_name(name);
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto obj = shadow_repo.provide_object_in_advance(ti, std::make_shared<object>(name));
|
auto obj = std::make_shared<object>(name);
|
||||||
|
std::ignore = repo.provide_object_in_advance(ti, obj);
|
||||||
object_generator gen(repo, obj);
|
object_generator gen(repo, obj);
|
||||||
access::process(gen, *t);
|
access::process(gen, *t);
|
||||||
return obj;
|
return obj;
|
||||||
|
|
@ -127,9 +125,9 @@ private:
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
[[nodiscard]] std::shared_ptr<object> fk_object() const;
|
[[nodiscard]] std::shared_ptr<object> fk_object() const;
|
||||||
|
|
||||||
static std::shared_ptr<object> acquire_object(repository &repo, const std::type_index &ti, const std::string& name);
|
static std::shared_ptr<object> acquire_object(basic_repository &repo, const std::type_index &ti, const std::string& name);
|
||||||
private:
|
private:
|
||||||
const repository &repo_;
|
basic_repository &repo_;
|
||||||
std::shared_ptr<object> object_;
|
std::shared_ptr<object> object_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -137,7 +135,6 @@ template<typename ValueType>
|
||||||
void object_generator::on_primary_key(const char *id, ValueType &x, const utils::primary_key_attribute& attr) {
|
void object_generator::on_primary_key(const char *id, ValueType &x, const utils::primary_key_attribute& attr) {
|
||||||
auto &ref = emplace_attribute<ValueType>(id, { attr.size(), utils::constraints::PrimaryKey }, null_option_type::NotNull);
|
auto &ref = emplace_attribute<ValueType>(id, { attr.size(), utils::constraints::PrimaryKey }, null_option_type::NotNull);
|
||||||
prepare_primary_key(ref, utils::identifier(x));
|
prepare_primary_key(ref, utils::identifier(x));
|
||||||
// create_pk_constraint(id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
|
|
@ -167,15 +164,14 @@ void object_generator::create_fk_constraint(const std::string& name) const {
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
std::shared_ptr<object> object_generator::fk_object() const {
|
std::shared_ptr<object> object_generator::fk_object() const {
|
||||||
const auto ti = std::type_index(typeid(Type));
|
const auto ti = std::type_index(typeid(Type));
|
||||||
const internal::shadow_repository shadow_repo(const_cast<repository &>(repo_));
|
if (const auto result = repo_.basic_info(ti)) {
|
||||||
if (const auto result = shadow_repo.basic_info(ti)) {
|
|
||||||
return result->get().object();
|
return result->get().object();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shadow_repo.has_object_for_type(ti)) {
|
if (repo_.has_object_for_type(ti)) {
|
||||||
return shadow_repo.object_for_type(ti);
|
return repo_.object_for_type(ti);
|
||||||
}
|
}
|
||||||
const auto obj = shadow_repo.provide_object_in_advance(ti, std::make_shared<object>(""));
|
const auto obj = repo_.provide_object_in_advance(ti, std::make_shared<object>(""));
|
||||||
object_generator gen(repo_, obj);
|
object_generator gen(repo_, obj);
|
||||||
Type t;
|
Type t;
|
||||||
access::process(gen, t);
|
access::process(gen, t);
|
||||||
|
|
|
||||||
|
|
@ -9,35 +9,20 @@
|
||||||
namespace matador::object {
|
namespace matador::object {
|
||||||
class repository_node;
|
class repository_node;
|
||||||
|
|
||||||
// template<typename Type>
|
|
||||||
// class observer_ptr {
|
|
||||||
// public:
|
|
||||||
// explicit observer_ptr(std::unique_ptr<observer<Type>> &&o)
|
|
||||||
// : observer_(std::move(o)) {}
|
|
||||||
//
|
|
||||||
// operator bool() const { return observer_ != nullptr; }
|
|
||||||
//
|
|
||||||
// observer<Type> *get() const { return observer_.get(); }
|
|
||||||
// observer<Type> &operator*() const { return *observer_; }
|
|
||||||
// observer<Type> *operator->() const { return observer_.get(); }
|
|
||||||
//
|
|
||||||
// private:
|
|
||||||
// std::unique_ptr<observer<Type>> observer_;
|
|
||||||
// };
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
class object_info final : public basic_object_info {
|
class object_info final : public basic_object_info {
|
||||||
public:
|
public:
|
||||||
using create_func = std::function<std::unique_ptr<Type>()>;
|
using create_func = std::function<std::unique_ptr<Type>()>;
|
||||||
|
|
||||||
object_info(const std::shared_ptr<repository_node>& node,
|
object_info(const repository_node& node,
|
||||||
std::shared_ptr<class object> &&obj,
|
const std::shared_ptr<class object> &obj,
|
||||||
std::vector<std::unique_ptr<observer<Type>>>&& observers,
|
std::vector<std::unique_ptr<observer<Type>>>&& observers,
|
||||||
create_func&& creator)
|
create_func&& creator)
|
||||||
: basic_object_info(node, std::move(obj))
|
: basic_object_info(node, obj)
|
||||||
, creator_(std::move(creator))
|
, creator_(std::move(creator))
|
||||||
, observers_(std::move(observers)){}
|
, observers_(std::move(observers)){}
|
||||||
|
|
||||||
explicit object_info(const std::shared_ptr<repository_node>& node)
|
explicit object_info(const repository_node& node)
|
||||||
: basic_object_info(node, {}) {
|
: basic_object_info(node, {}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -46,13 +31,13 @@ public:
|
||||||
|
|
||||||
void on_attach() const override {
|
void on_attach() const override {
|
||||||
for (auto &observer : observers_) {
|
for (auto &observer : observers_) {
|
||||||
observer->on_attach(*node_, prototype_);
|
observer->on_attach(node_, prototype_);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void on_detach() const override {
|
void on_detach() const override {
|
||||||
for (auto &observer : observers_) {
|
for (auto &observer : observers_) {
|
||||||
observer->on_detach(*node_, prototype_);
|
observer->on_detach(node_, prototype_);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
#ifndef RELATION_COMPLETER_HPP
|
#ifndef RELATION_COMPLETER_HPP
|
||||||
#define RELATION_COMPLETER_HPP
|
#define RELATION_COMPLETER_HPP
|
||||||
|
|
||||||
#include "matador/object/internal/shadow_repository.hpp"
|
#include "matador/object/basic_repository.hpp"
|
||||||
#include "matador/object/foreign_node_completer.hpp"
|
#include "matador/object/foreign_node_completer.hpp"
|
||||||
#include "matador/object/many_to_many_relation.hpp"
|
#include "matador/object/many_to_many_relation.hpp"
|
||||||
#include "matador/object/join_columns_collector.hpp"
|
#include "matador/object/join_columns_collector.hpp"
|
||||||
|
|
@ -94,15 +94,11 @@ private:
|
||||||
*/
|
*/
|
||||||
template<typename Type, template<typename> typename... Observers>
|
template<typename Type, template<typename> typename... Observers>
|
||||||
class relation_completer final {
|
class relation_completer final {
|
||||||
private:
|
|
||||||
using node_ptr = std::shared_ptr<repository_node>;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using endpoint_ptr = std::shared_ptr<relation_endpoint>;
|
using endpoint_ptr = std::shared_ptr<relation_endpoint>;
|
||||||
|
|
||||||
static void complete(const std::shared_ptr<repository_node> &node, const std::vector<std::unique_ptr<observer<Type>>> &observers) {
|
static void complete(repository_node *node, const std::vector<std::unique_ptr<observer<Type>>> &observers) {
|
||||||
internal::shadow_repository shadow(node->repo_);
|
relation_completer completer(node->repo_, observers);
|
||||||
relation_completer completer(shadow, observers);
|
|
||||||
|
|
||||||
completer.complete_node_relations(node);
|
completer.complete_node_relations(node);
|
||||||
}
|
}
|
||||||
|
|
@ -131,12 +127,12 @@ public:
|
||||||
void on_has_many_to_many(const char *id, CollectionType &collection, const utils::foreign_attributes &attr);
|
void on_has_many_to_many(const char *id, CollectionType &collection, const utils::foreign_attributes &attr);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit relation_completer(internal::shadow_repository &shadow, const std::vector<std::unique_ptr<observer<Type>>>& observers)
|
explicit relation_completer(basic_repository &repo, const std::vector<std::unique_ptr<observer<Type>>>& observers)
|
||||||
: schema_(shadow)
|
: repo_(repo)
|
||||||
, log_(logger::create_logger("relation_completer"))
|
, log_(logger::create_logger("relation_completer"))
|
||||||
, observers_(observers) {}
|
, observers_(observers) {}
|
||||||
|
|
||||||
void complete_node_relations(const std::shared_ptr<repository_node> &node) {
|
void complete_node_relations(repository_node* node) {
|
||||||
nodes_.push(node);
|
nodes_.push(node);
|
||||||
|
|
||||||
Type obj;
|
Type obj;
|
||||||
|
|
@ -152,11 +148,11 @@ private:
|
||||||
static void link_relation_endpoints(const endpoint_ptr &endpoint,
|
static void link_relation_endpoints(const endpoint_ptr &endpoint,
|
||||||
const endpoint_ptr &other_endpoint);
|
const endpoint_ptr &other_endpoint);
|
||||||
|
|
||||||
node_ptr find_node(const std::type_index &ti) const;
|
[[nodiscard]] repository_node* find_node(const std::type_index &ti) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::stack<node_ptr> nodes_;
|
std::stack<repository_node*> nodes_;
|
||||||
internal::shadow_repository &schema_;
|
basic_repository &repo_;
|
||||||
logger::logger log_;
|
logger::logger log_;
|
||||||
join_columns_collector join_columns_collector_{};
|
join_columns_collector join_columns_collector_{};
|
||||||
const std::vector<std::unique_ptr<observer<Type>>>& observers_;
|
const std::vector<std::unique_ptr<observer<Type>>>& observers_;
|
||||||
|
|
@ -183,13 +179,13 @@ void relation_completer<Type, Observers...>::on_has_many(const char *id, Collect
|
||||||
if (const auto it = foreign_node->info_->find_relation_endpoint(typeid(Type)); it != foreign_node->info().endpoint_end()) {
|
if (const auto it = foreign_node->info_->find_relation_endpoint(typeid(Type)); it != foreign_node->info().endpoint_end()) {
|
||||||
// corresponding belongs_to is available and was called (has_many <-> belongs_to)
|
// corresponding belongs_to is available and was called (has_many <-> belongs_to)
|
||||||
// complete the relation
|
// complete the relation
|
||||||
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasMany, foreign_node);
|
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasMany, *foreign_node);
|
||||||
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
|
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
|
||||||
link_relation_endpoints(local_endpoint, it->second);
|
link_relation_endpoints(local_endpoint, it->second);
|
||||||
} else if (join_column_finder::has_join_column<value_type>(join_column)) {
|
} else if (join_column_finder::has_join_column<value_type>(join_column)) {
|
||||||
// corresponding belongs_to is available but was not called (has_many <-> belongs_to)
|
// corresponding belongs_to is available but was not called (has_many <-> belongs_to)
|
||||||
// prepare the relation
|
// prepare the relation
|
||||||
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasMany, foreign_node);
|
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasMany, *foreign_node);
|
||||||
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
|
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
|
||||||
} else {
|
} else {
|
||||||
// A relation table is necessary
|
// A relation table is necessary
|
||||||
|
|
@ -198,21 +194,23 @@ void relation_completer<Type, Observers...>::on_has_many(const char *id, Collect
|
||||||
// belongs-to relation handles this relation, the many-to-many
|
// belongs-to relation handles this relation, the many-to-many
|
||||||
// relation is maybe detached.
|
// relation is maybe detached.
|
||||||
log_.debug("node '%s' has has many foreign keys '%s' mapped by '%s'", nodes_.top()->name().c_str(), id, join_column);
|
log_.debug("node '%s' has has many foreign keys '%s' mapped by '%s'", nodes_.top()->name().c_str(), id, join_column);
|
||||||
auto node = repository_node::make_node<relation_value_type>(schema_.repo(), id, [join_column] {
|
auto node = repository_node::make_node<relation_value_type>(repo_, id, [join_column] {
|
||||||
return std::make_unique<relation_value_type>("id", join_column);
|
return std::make_unique<relation_value_type>("id", join_column);
|
||||||
}, {});
|
}, {});
|
||||||
const auto result = schema_.attach_node(node);
|
const auto result = repo_.attach_node(node.release(), "");
|
||||||
if (!result) {
|
if (!result) {
|
||||||
// Todo: throw internal error
|
// Todo: throw internal error
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasMany, node);
|
|
||||||
const auto foreign_endpoint = std::make_shared<relation_endpoint>("id", relation_type::BelongsTo, nodes_.top());
|
auto *attached_node = result.value();
|
||||||
|
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasMany, *attached_node);
|
||||||
|
const auto foreign_endpoint = std::make_shared<relation_endpoint>("id", relation_type::BelongsTo, *nodes_.top());
|
||||||
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
|
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
|
||||||
node->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
|
attached_node->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
|
||||||
link_relation_endpoints(local_endpoint, foreign_endpoint);
|
link_relation_endpoints(local_endpoint, foreign_endpoint);
|
||||||
|
|
||||||
const auto foreign_value_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BelongsTo, foreign_node);
|
const auto foreign_value_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BelongsTo, *foreign_node);
|
||||||
node->info_->register_relation_endpoint(typeid(value_type), foreign_value_endpoint);
|
node->info_->register_relation_endpoint(typeid(value_type), foreign_value_endpoint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -226,20 +224,21 @@ void relation_completer<Type, Observers...>::on_has_many(const char *id, Collect
|
||||||
using value_type = typename CollectionType::value_type;
|
using value_type = typename CollectionType::value_type;
|
||||||
using relation_value_type = many_to_relation<Type, value_type>;
|
using relation_value_type = many_to_relation<Type, value_type>;
|
||||||
|
|
||||||
auto node = repository_node::make_node<relation_value_type>(schema_.repo(), id, [join_column] {
|
auto node = repository_node::make_node<relation_value_type>(repo_, id, [join_column] {
|
||||||
return std::make_unique<relation_value_type>(join_column, "value");
|
return std::make_unique<relation_value_type>(join_column, "value");
|
||||||
}, {});
|
}, {});
|
||||||
const auto result = schema_.attach_node(node);
|
const auto result = repo_.attach_node(node.release(), "");
|
||||||
if (!result) {
|
if (!result) {
|
||||||
// Todo: throw internal exception
|
// Todo: throw internal exception
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasMany, node);
|
auto *attached_node = result.value();
|
||||||
const auto foreign_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BelongsTo, nodes_.top());
|
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasMany, *attached_node);
|
||||||
|
const auto foreign_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BelongsTo, *nodes_.top());
|
||||||
|
|
||||||
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
|
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
|
||||||
node->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
|
attached_node->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
|
||||||
link_relation_endpoints(local_endpoint, foreign_endpoint);
|
link_relation_endpoints(local_endpoint, foreign_endpoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -250,11 +249,11 @@ void relation_completer<Type, Observers...>::on_has_many_to_many(const char *id,
|
||||||
const char *join_column,
|
const char *join_column,
|
||||||
const char *inverse_join_column,
|
const char *inverse_join_column,
|
||||||
const utils::foreign_attributes &/*attr*/) {
|
const utils::foreign_attributes &/*attr*/) {
|
||||||
if (!schema_.expecting_relation_node(id)) {
|
if (!repo_.expecting_relation_node(id)) {
|
||||||
schema_.expect_relation_node(id, typeid(typename CollectionType::value_type::value_type));
|
repo_.expect_relation_node(id, typeid(typename CollectionType::value_type::value_type));
|
||||||
} else {
|
} else {
|
||||||
attach_relation_node<typename CollectionType::value_type::value_type>(id, join_column, inverse_join_column);
|
attach_relation_node<typename CollectionType::value_type::value_type>(id, join_column, inverse_join_column);
|
||||||
schema_.remove_expected_relation_node(id);
|
repo_.remove_expected_relation_node(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -278,7 +277,7 @@ void relation_completer<Type, Observers...>::on_has_one(const char *id,
|
||||||
ForeignPointerType &/*obj*/,
|
ForeignPointerType &/*obj*/,
|
||||||
const utils::foreign_attributes &/*attr*/) {
|
const utils::foreign_attributes &/*attr*/) {
|
||||||
using value_type = typename ForeignPointerType::value_type;
|
using value_type = typename ForeignPointerType::value_type;
|
||||||
auto foreign_node = find_node(typeid(value_type));
|
const auto foreign_node = find_node(typeid(value_type));
|
||||||
if (!foreign_node) {
|
if (!foreign_node) {
|
||||||
// Todo: throw internal error or attach node
|
// Todo: throw internal error or attach node
|
||||||
return;
|
return;
|
||||||
|
|
@ -286,7 +285,7 @@ void relation_completer<Type, Observers...>::on_has_one(const char *id,
|
||||||
|
|
||||||
auto local_it = nodes_.top()->info().find_relation_endpoint(typeid(value_type));
|
auto local_it = nodes_.top()->info().find_relation_endpoint(typeid(value_type));
|
||||||
if (local_it == nodes_.top()->info().endpoint_end()) {
|
if (local_it == nodes_.top()->info().endpoint_end()) {
|
||||||
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasOne, foreign_node);
|
const auto local_endpoint = std::make_shared<relation_endpoint>(id, relation_type::HasOne, *foreign_node);
|
||||||
local_it = nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
|
local_it = nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
|
||||||
}
|
}
|
||||||
if (const auto foreign_it = foreign_node->info().find_relation_endpoint(typeid(Type)); foreign_it != foreign_node->info().endpoint_end()) {
|
if (const auto foreign_it = foreign_node->info().find_relation_endpoint(typeid(Type)); foreign_it != foreign_node->info().endpoint_end()) {
|
||||||
|
|
@ -302,7 +301,7 @@ void relation_completer<Type, Observers...>::on_belongs_to(const char *id,
|
||||||
const utils::foreign_attributes & /*attr*/) {
|
const utils::foreign_attributes & /*attr*/) {
|
||||||
using value_type = typename ForeignPointerType::value_type;
|
using value_type = typename ForeignPointerType::value_type;
|
||||||
const auto ti = std::type_index(typeid(value_type));
|
const auto ti = std::type_index(typeid(value_type));
|
||||||
auto foreign_node = find_node(typeid(value_type));
|
const auto foreign_node = find_node(typeid(value_type));
|
||||||
if (!foreign_node) {
|
if (!foreign_node) {
|
||||||
// Todo: throw internal error or attach node
|
// Todo: throw internal error or attach node
|
||||||
return;
|
return;
|
||||||
|
|
@ -313,11 +312,11 @@ void relation_completer<Type, Observers...>::on_belongs_to(const char *id,
|
||||||
if (const auto it = foreign_node->info_->find_relation_endpoint(nodes_.top()->type_index()); it != foreign_node->info().endpoint_end()) {
|
if (const auto it = foreign_node->info_->find_relation_endpoint(nodes_.top()->type_index()); it != foreign_node->info().endpoint_end()) {
|
||||||
// Found corresponding relation endpoint in the foreign node
|
// Found corresponding relation endpoint in the foreign node
|
||||||
if (it->second->is_has_one()) {
|
if (it->second->is_has_one()) {
|
||||||
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BelongsTo, foreign_node);
|
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BelongsTo, *foreign_node);
|
||||||
nodes_.top()->info_->register_relation_endpoint(ti, endpoint);
|
nodes_.top()->info_->register_relation_endpoint(ti, endpoint);
|
||||||
link_relation_endpoints(endpoint, it->second);
|
link_relation_endpoints(endpoint, it->second);
|
||||||
} else if (it->second->is_has_many()) {
|
} else if (it->second->is_has_many()) {
|
||||||
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BelongsTo, foreign_node);
|
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BelongsTo, *foreign_node);
|
||||||
nodes_.top()->info_->register_relation_endpoint(ti, endpoint);
|
nodes_.top()->info_->register_relation_endpoint(ti, endpoint);
|
||||||
link_relation_endpoints(endpoint, it->second);
|
link_relation_endpoints(endpoint, it->second);
|
||||||
} else if (it->second->foreign_endpoint()->node().type_index() == typeid(many_to_many_relation<Type, value_type>)) {
|
} else if (it->second->foreign_endpoint()->node().type_index() == typeid(many_to_many_relation<Type, value_type>)) {
|
||||||
|
|
@ -326,16 +325,15 @@ void relation_completer<Type, Observers...>::on_belongs_to(const char *id,
|
||||||
// "belongs_to"-relation the "many_to_many_relation" can be removed
|
// "belongs_to"-relation the "many_to_many_relation" can be removed
|
||||||
// (detach), and the endpoints must be adjusted
|
// (detach), and the endpoints must be adjusted
|
||||||
const auto foreign_endpoint = it->second->foreign_endpoint();
|
const auto foreign_endpoint = it->second->foreign_endpoint();
|
||||||
const auto detach_result = schema_.detach_node(foreign_endpoint->node_);
|
const auto detach_result = repo_.detach(foreign_endpoint->node_);
|
||||||
foreign_endpoint->node_ = foreign_node;
|
foreign_endpoint->node_ = foreign_node;
|
||||||
// foreign_endpoint->node_ = nodes_.top();
|
|
||||||
nodes_.top()->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
|
nodes_.top()->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
|
||||||
} else {
|
} else {
|
||||||
// check type
|
// check type
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Relation node was not found, create only endpoint.
|
// Relation node was not found, create only endpoint.
|
||||||
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BelongsTo, foreign_node);
|
const auto endpoint = std::make_shared<relation_endpoint>(id, relation_type::BelongsTo, *foreign_node);
|
||||||
nodes_.top()->info_->register_relation_endpoint(ti, endpoint);
|
nodes_.top()->info_->register_relation_endpoint(ti, endpoint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -343,18 +341,17 @@ void relation_completer<Type, Observers...>::on_belongs_to(const char *id,
|
||||||
template<typename Type, template<typename> typename... Observers>
|
template<typename Type, template<typename> typename... Observers>
|
||||||
template<typename NodeType>
|
template<typename NodeType>
|
||||||
void relation_completer<Type, Observers...>::attach_relation_node(const std::string &name, const std::string &join_column, const std::string &inverse_join_column) {
|
void relation_completer<Type, Observers...>::attach_relation_node(const std::string &name, const std::string &join_column, const std::string &inverse_join_column) {
|
||||||
using relation_value_type = many_to_many_relation<NodeType, Type>;
|
using relation_value_type = many_to_many_relation<Type, NodeType>;
|
||||||
using value_type = NodeType;
|
using value_type = NodeType;
|
||||||
|
|
||||||
// Check if the object_ptr type is already inserted in the schema (by id)
|
// Check if the object_ptr type is already inserted in the schema (by id)
|
||||||
auto foreign_node = find_node(typeid(value_type));
|
const auto foreign_node = find_node(typeid(value_type));
|
||||||
if (!foreign_node) {
|
if (!foreign_node) {
|
||||||
// Todo: throw internal error or attach node
|
// Todo: throw internal error or attach node
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto result = schema_.find_node(name);
|
if (const auto it = repo_.find_node(name); it != repo_.end()) {
|
||||||
if (result) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Relation does not exist. Create it.
|
// Relation does not exist. Create it.
|
||||||
|
|
@ -364,26 +361,27 @@ void relation_completer<Type, Observers...>::attach_relation_node(const std::str
|
||||||
|
|
||||||
auto observers = internal::observer_list_copy_creator<Type, relation_value_type, Observers...>::copy_create(observers_);
|
auto observers = internal::observer_list_copy_creator<Type, relation_value_type, Observers...>::copy_create(observers_);
|
||||||
|
|
||||||
auto node = repository_node::make_node<relation_value_type>(schema_.repo(), name, std::move(creator), std::move(observers));
|
auto node = repository_node::make_node<relation_value_type>(repo_, name, std::move(creator), std::move(observers));
|
||||||
result = schema_.attach_node(node);
|
auto result = repo_.attach_node(node.release(), "");
|
||||||
if (!result) {
|
if (!result) {
|
||||||
// Todo: throw internal error
|
// Todo: throw internal error
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto *attached_node = result.value();
|
||||||
foreign_node_completer<relation_value_type>::complete(result.value(), {});
|
foreign_node_completer<relation_value_type>::complete(result.value(), {});
|
||||||
|
|
||||||
const auto local_endpoint = std::make_shared<relation_endpoint>(name, relation_type::HasMany, node);
|
const auto local_endpoint = std::make_shared<relation_endpoint>(name, relation_type::HasMany, *attached_node);
|
||||||
const auto join_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BelongsTo, nodes_.top());
|
const auto join_endpoint = std::make_shared<relation_endpoint>(join_column, relation_type::BelongsTo, *nodes_.top());
|
||||||
const auto inverse_join_endpoint = std::make_shared<relation_endpoint>(inverse_join_column, relation_type::BelongsTo, foreign_node);
|
const auto inverse_join_endpoint = std::make_shared<relation_endpoint>(inverse_join_column, relation_type::BelongsTo, *foreign_node);
|
||||||
const auto foreign_endpoint = std::make_shared<relation_endpoint>(name, relation_type::HasMany, node);
|
const auto foreign_endpoint = std::make_shared<relation_endpoint>(name, relation_type::HasMany, *attached_node);
|
||||||
// register relation endpoint in local node
|
// register relation endpoint in local node
|
||||||
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
|
nodes_.top()->info_->register_relation_endpoint(typeid(value_type), local_endpoint);
|
||||||
// register relation endpoint in foreign node
|
// register relation endpoint in foreign node
|
||||||
foreign_node->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
|
foreign_node->info_->register_relation_endpoint(nodes_.top()->type_index(), foreign_endpoint);
|
||||||
// register endpoints in relation node
|
// register endpoints in relation node
|
||||||
node->info_->register_relation_endpoint(nodes_.top()->type_index(), join_endpoint);
|
attached_node->info_->register_relation_endpoint(nodes_.top()->type_index(), join_endpoint);
|
||||||
node->info_->register_relation_endpoint(typeid(value_type), inverse_join_endpoint);
|
attached_node->info_->register_relation_endpoint(typeid(value_type), inverse_join_endpoint);
|
||||||
// link endpoints
|
// link endpoints
|
||||||
link_relation_endpoints(local_endpoint, join_endpoint);
|
link_relation_endpoints(local_endpoint, join_endpoint);
|
||||||
link_relation_endpoints(foreign_endpoint, inverse_join_endpoint);
|
link_relation_endpoints(foreign_endpoint, inverse_join_endpoint);
|
||||||
|
|
@ -392,8 +390,8 @@ void relation_completer<Type, Observers...>::attach_relation_node(const std::str
|
||||||
template<typename Type, template<typename> typename... Observers>
|
template<typename Type, template<typename> typename... Observers>
|
||||||
void relation_completer<Type, Observers...>::register_relation_endpoints(const endpoint_ptr &endpoint,
|
void relation_completer<Type, Observers...>::register_relation_endpoints(const endpoint_ptr &endpoint,
|
||||||
const endpoint_ptr &other_endpoint) {
|
const endpoint_ptr &other_endpoint) {
|
||||||
endpoint->node_->info_->register_relation_endpoint(other_endpoint->node_->type_index(), endpoint);
|
endpoint->node().info_->register_relation_endpoint(other_endpoint->node().type_index(), endpoint);
|
||||||
other_endpoint->node_->info_->register_relation_endpoint(endpoint->node_->type_index(), other_endpoint);
|
other_endpoint->node().info_->register_relation_endpoint(endpoint->node().type_index(), other_endpoint);
|
||||||
link_relation_endpoints(endpoint, other_endpoint);
|
link_relation_endpoints(endpoint, other_endpoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -404,16 +402,15 @@ void relation_completer<Type, Observers...>::link_relation_endpoints(const endpo
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Type, template <typename> class ... Observers>
|
template<typename Type, template <typename> class ... Observers>
|
||||||
typename relation_completer<Type, Observers...>::node_ptr relation_completer<Type, Observers...>::find_node(const std::type_index &ti) const {
|
repository_node* relation_completer<Type, Observers...>::find_node(const std::type_index &ti) const {
|
||||||
node_ptr foreign_node;
|
if (const auto it = repo_.find_node(ti); it != repo_.end()) {
|
||||||
if (auto result = schema_.find_node(ti); result) {
|
return it.get();
|
||||||
return result.value();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!schema_.is_node_announced(ti)) {
|
if (!repo_.is_node_announced(ti)) {
|
||||||
return {};
|
return nullptr;
|
||||||
}
|
}
|
||||||
return schema_.announce_node(ti);
|
return repo_.announce_node(ti);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif //RELATION_COMPLETER_HPP
|
#endif //RELATION_COMPLETER_HPP
|
||||||
|
|
|
||||||
|
|
@ -24,15 +24,13 @@ static const utils::enum_mapper<relation_type> relation_type_enum({
|
||||||
|
|
||||||
class relation_endpoint {
|
class relation_endpoint {
|
||||||
public:
|
public:
|
||||||
relation_endpoint(std::string field_name, relation_type type, const std::shared_ptr<repository_node>& node);
|
relation_endpoint(std::string field_name, relation_type type, repository_node& node);
|
||||||
|
|
||||||
[[nodiscard]] std::string field_name() const;
|
[[nodiscard]] std::string field_name() const;
|
||||||
[[nodiscard]] relation_type type() const;
|
[[nodiscard]] relation_type type() const;
|
||||||
[[nodiscard]] std::string type_name() const;
|
[[nodiscard]] std::string type_name() const;
|
||||||
[[nodiscard]] const repository_node& node() const;
|
[[nodiscard]] const repository_node& node() const;
|
||||||
|
|
||||||
[[nodiscard]] std::shared_ptr<repository_node> node_ptr() const;
|
|
||||||
|
|
||||||
[[nodiscard]] bool is_has_one() const;
|
[[nodiscard]] bool is_has_one() const;
|
||||||
[[nodiscard]] bool is_has_many() const;
|
[[nodiscard]] bool is_has_many() const;
|
||||||
[[nodiscard]] bool is_belongs_to() const;
|
[[nodiscard]] bool is_belongs_to() const;
|
||||||
|
|
@ -48,8 +46,8 @@ private:
|
||||||
|
|
||||||
std::string field_name_;
|
std::string field_name_;
|
||||||
relation_type type_;
|
relation_type type_;
|
||||||
std::shared_ptr<repository_node> node_;
|
repository_node *node_{nullptr};
|
||||||
std::shared_ptr<relation_endpoint> foreign_endpoint_;
|
std::weak_ptr<relation_endpoint> foreign_endpoint_;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,42 +1,23 @@
|
||||||
#ifndef SCHEMA_HPP
|
#ifndef REPOSITORY_HPP
|
||||||
#define SCHEMA_HPP
|
#define REPOSITORY_HPP
|
||||||
|
|
||||||
#include "matador/logger/log_manager.hpp"
|
#include "matador/object/basic_repository.hpp"
|
||||||
|
|
||||||
#include "matador/object/error_code.hpp"
|
|
||||||
#include "matador/object/foreign_node_completer.hpp"
|
#include "matador/object/foreign_node_completer.hpp"
|
||||||
#include "matador/object/object_info.hpp"
|
|
||||||
#include "matador/object/observer.hpp"
|
#include "matador/object/observer.hpp"
|
||||||
#include "matador/object/relation_completer.hpp"
|
#include "matador/object/relation_completer.hpp"
|
||||||
#include "matador/object/repository_node.hpp"
|
|
||||||
#include "matador/object/repository_node_iterator.hpp"
|
|
||||||
|
|
||||||
#include "matador/utils/result.hpp"
|
#include "matador/utils/result.hpp"
|
||||||
#include "matador/utils/error.hpp"
|
#include "matador/utils/error.hpp"
|
||||||
|
|
||||||
#include "matador/logger/logger.hpp"
|
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
|
|
||||||
namespace matador::object {
|
namespace matador::object {
|
||||||
namespace internal {
|
class repository : public basic_repository {
|
||||||
class shadow_repository;
|
|
||||||
}
|
|
||||||
|
|
||||||
utils::error make_error(error_code ec, const std::string &msg);
|
|
||||||
|
|
||||||
class repository {
|
|
||||||
public:
|
public:
|
||||||
typedef const_repository_node_iterator const_iterator; /**< Shortcut for the list const iterator. */
|
using basic_repository::basic_repository;
|
||||||
|
|
||||||
using node_ptr = std::shared_ptr<repository_node>;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates an empty schema
|
|
||||||
*/
|
|
||||||
explicit repository(std::string name = "");
|
|
||||||
|
|
||||||
template<typename Type, template<typename> typename... Observers>
|
template<typename Type, template<typename> typename... Observers>
|
||||||
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name, Observers<Type>&&... observers) {
|
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name, Observers<Type>&&... observers) {
|
||||||
|
|
@ -46,12 +27,12 @@ public:
|
||||||
template<typename Type, typename SuperType, template<typename> typename... Observers>
|
template<typename Type, typename SuperType, template<typename> typename... Observers>
|
||||||
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name, Observers<Type>&&... observers) {
|
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name, Observers<Type>&&... observers) {
|
||||||
const auto ti = std::type_index(typeid(SuperType));
|
const auto ti = std::type_index(typeid(SuperType));
|
||||||
auto result = find_node(ti);
|
const auto it = find_node(ti);
|
||||||
if (!result) {
|
if (it == end()) {
|
||||||
return utils::failure(make_error(error_code::NodeNotFound, "Parent node '" + std::string(ti.name()) + "' not found"));
|
return utils::failure(make_error(error_code::NodeNotFound, "Parent node '" + std::string(ti.name()) + "' not found"));
|
||||||
}
|
}
|
||||||
|
|
||||||
return attach_type<Type, Observers...>(name, (*result)->name(), std::forward<Observers<Type>>(observers)...);
|
return attach_type<Type, Observers...>(name, it->name(), std::forward<Observers<Type>>(observers)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Type, template<typename> typename... Observers>
|
template<typename Type, template<typename> typename... Observers>
|
||||||
|
|
@ -65,20 +46,22 @@ public:
|
||||||
obs.reserve(sizeof...(Observers));
|
obs.reserve(sizeof...(Observers));
|
||||||
(obs.push_back(std::unique_ptr<observer<Type>>(new Observers<Type>(std::forward<Observers<Type>>(observers)))), ...);
|
(obs.push_back(std::unique_ptr<observer<Type>>(new Observers<Type>(std::forward<Observers<Type>>(observers)))), ...);
|
||||||
// if the type was not found
|
// if the type was not found
|
||||||
std::shared_ptr<repository_node> node;
|
std::unique_ptr<repository_node> node;
|
||||||
if (is_node_announced(typeid(Type))) {
|
if (is_node_announced(typeid(Type))) {
|
||||||
node = pop_announce_node(typeid(Type));
|
node = pop_announce_node(typeid(Type));
|
||||||
node->update_name(name);
|
node->update_name(name);
|
||||||
} else {
|
} else {
|
||||||
node = repository_node::make_node<Type>(*this, name, []{ return std::make_unique<Type>(); }, std::move(obs));
|
node = repository_node::make_node<Type>(*this, name, []{ return std::make_unique<Type>(); }, std::move(obs));
|
||||||
}
|
}
|
||||||
if (auto result = attach_node(node, parent); !result) {
|
auto result = attach_node(node.release(), parent);
|
||||||
|
if (!result) {
|
||||||
return utils::failure(result.err());
|
return utils::failure(result.err());
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto info = node->template info<Type>();
|
repository_node* attached_node = result.value();
|
||||||
foreign_node_completer<Type, Observers...>::complete(node, info.get().observers());
|
const auto info = attached_node->template info<Type>();
|
||||||
relation_completer<Type, Observers...>::complete(node, info.get().observers());
|
foreign_node_completer<Type, Observers...>::complete(attached_node, info.get().observers());
|
||||||
|
relation_completer<Type, Observers...>::complete(attached_node, info.get().observers());
|
||||||
} else if (!has_node(name)) {
|
} else if (!has_node(name)) {
|
||||||
it->second->update_name(name);
|
it->second->update_name(name);
|
||||||
nodes_by_name_[name] = it->second;
|
nodes_by_name_[name] = it->second;
|
||||||
|
|
@ -95,142 +78,16 @@ public:
|
||||||
return utils::ok<void>();
|
return utils::ok<void>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Detaches a given node from the schema. If the
|
|
||||||
* node is a parent of other nodes, these nodes are
|
|
||||||
* detached as well.
|
|
||||||
*
|
|
||||||
* @param node Node to detach from schema
|
|
||||||
* @return Result object indicating success or failure
|
|
||||||
*/
|
|
||||||
[[nodiscard]] utils::result<void, utils::error> detach(const node_ptr &node);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the first schema node.
|
|
||||||
*
|
|
||||||
* @return The first schema node iterator.
|
|
||||||
*/
|
|
||||||
[[nodiscard]] const_iterator begin() const;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the last schema node.
|
|
||||||
*
|
|
||||||
* @return The last schema node iterator.
|
|
||||||
*/
|
|
||||||
[[nodiscard]] const_iterator end() const;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true if the schema contains
|
|
||||||
* no schema nodes.
|
|
||||||
*
|
|
||||||
* @return True if the schema is empty
|
|
||||||
*/
|
|
||||||
[[nodiscard]] bool empty() const;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the current number of the schema node.
|
|
||||||
*
|
|
||||||
* @return Number of schema nodes
|
|
||||||
*/
|
|
||||||
[[nodiscard]] size_t size() const;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the name of the schema.
|
|
||||||
*
|
|
||||||
* @return The name of the schema
|
|
||||||
*/
|
|
||||||
[[nodiscard]] std::string name() const;
|
|
||||||
|
|
||||||
[[nodiscard]] bool contains(const std::string &name) const;
|
|
||||||
[[nodiscard]] bool contains(const std::type_index &index) const;
|
|
||||||
template < typename Type >
|
|
||||||
[[nodiscard]] bool contains() const {
|
|
||||||
return contains(std::type_index(typeid(Type)));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
[[nodiscard]] utils::result<object_info_ref<Type>, utils::error> info() const {
|
[[nodiscard]] utils::result<object_info_ref<Type>, utils::error> info() const {
|
||||||
auto result = find_node(std::type_index(typeid(Type)));
|
const auto it = find_node(std::type_index(typeid(Type)));
|
||||||
if (!result) {
|
if (it == end()) {
|
||||||
return utils::failure(result.err());
|
return utils::failure(make_error(error_code::NodeNotFound, "Parent node '" + std::string(typeid(Type).name()) + "' not found"));
|
||||||
}
|
}
|
||||||
|
|
||||||
return utils::ok(result.value()->info<Type>());
|
return utils::ok(it->info<Type>());
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] utils::result<basic_object_info_ref, utils::error> basic_info(const std::type_index& ti) const;
|
|
||||||
|
|
||||||
[[nodiscard]] utils::result<basic_object_info_ref, utils::error> basic_info(const std::string &name) const;
|
|
||||||
|
|
||||||
template<typename Type>
|
|
||||||
[[nodiscard]] utils::result<basic_object_info_ref, utils::error> basic_info() const {
|
|
||||||
auto result = find_node(std::type_index(typeid(Type)));
|
|
||||||
if (!result) {
|
|
||||||
return utils::failure(result.err());
|
|
||||||
}
|
|
||||||
|
|
||||||
return utils::ok(basic_object_info_ref{result.value()->info()});
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] utils::result<attribute*, utils::error> primary_key_attribute(const std::type_index &type_index) const;
|
|
||||||
|
|
||||||
void dump(std::ostream &os) const;
|
|
||||||
static void dump(std::ostream &os, const node_ptr& node);
|
|
||||||
|
|
||||||
private:
|
|
||||||
using t_node_map = std::unordered_map<std::string, node_ptr>;
|
|
||||||
using t_type_index_node_map = std::unordered_map<std::type_index, node_ptr>;
|
|
||||||
|
|
||||||
[[nodiscard]] utils::result<node_ptr, utils::error> attach_node(const node_ptr &node, const std::string &parent);
|
|
||||||
[[nodiscard]] utils::result<node_ptr, utils::error> find_node(const std::string &name) const;
|
|
||||||
[[nodiscard]] utils::result<node_ptr, utils::error> find_node(const std::type_index &type_index) const;
|
|
||||||
template<typename Type>
|
|
||||||
[[nodiscard]] utils::result<node_ptr, utils::error> find_node() const {
|
|
||||||
return find_node(std::type_index(typeid(Type)));
|
|
||||||
}
|
|
||||||
|
|
||||||
[[nodiscard]] bool has_node(const std::string &name) const;
|
|
||||||
[[nodiscard]] bool has_node(const std::type_index &index) const;
|
|
||||||
[[nodiscard]] bool has_node(const node_ptr& node) const;
|
|
||||||
|
|
||||||
static void insert_node(const node_ptr &parent, const node_ptr &child);
|
|
||||||
void remove_node(const node_ptr &node);
|
|
||||||
|
|
||||||
[[nodiscard]] bool expecting_relation_node(const std::string &name) const;
|
|
||||||
void expect_relation_node(const std::string &name, const std::type_index &ti);
|
|
||||||
void remove_expected_relation_node(const std::string &name);
|
|
||||||
|
|
||||||
[[nodiscard]] std::shared_ptr<object> provide_object_in_advance(const std::type_index &ti, const std::shared_ptr<object>& obj);
|
|
||||||
[[nodiscard]] bool has_object_for_type(const std::type_index &ti) const;
|
|
||||||
[[nodiscard]] std::shared_ptr<object> object_for_type(const std::type_index &ti) const;
|
|
||||||
void remove_object_for_type(const std::type_index &ti);
|
|
||||||
|
|
||||||
[[nodiscard]] bool is_node_announced(const std::type_index &ti) const;
|
|
||||||
void push_announce_node(const std::type_index &ti, const node_ptr &node);
|
|
||||||
[[nodiscard]] node_ptr announce_node(const std::type_index &ti);
|
|
||||||
[[nodiscard]] node_ptr pop_announce_node(const std::type_index &ti);
|
|
||||||
|
|
||||||
private:
|
|
||||||
friend class internal::shadow_repository;
|
|
||||||
friend class repository_node;
|
|
||||||
template < typename NodeType, template<typename> typename ...Observers >
|
|
||||||
friend class foreign_node_completer;
|
|
||||||
friend class object_generator;
|
|
||||||
|
|
||||||
std::string name_;
|
|
||||||
std::shared_ptr<repository_node> root_;
|
|
||||||
|
|
||||||
t_node_map nodes_by_name_;
|
|
||||||
t_type_index_node_map nodes_by_type_;
|
|
||||||
logger::logger log_;
|
|
||||||
|
|
||||||
std::unordered_map<std::type_index, std::shared_ptr<repository_node>> announced_node_;
|
|
||||||
std::unordered_map<std::type_index, attribute*> missing_references_;
|
|
||||||
std::unordered_map<std::string, std::type_index> expected_relation_nodes_;
|
|
||||||
std::unordered_map<std::type_index, std::shared_ptr<object>> object_by_type_;
|
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //SCHEMA_HPP
|
#endif //REPOSITORY_HPP
|
||||||
|
|
|
||||||
|
|
@ -1,34 +1,30 @@
|
||||||
#ifndef SCHEMA_NODE_HPP
|
#ifndef REPOSITORY_NODE_HPP
|
||||||
#define SCHEMA_NODE_HPP
|
#define REPOSITORY_NODE_HPP
|
||||||
|
|
||||||
#include "matador/object/object_generator.hpp"
|
#include "matador/object/object_generator.hpp"
|
||||||
#include "matador/object/object_info.hpp"
|
#include "matador/object/object_info.hpp"
|
||||||
#include "matador/object/internal/observer_list_creator.hpp"
|
#include "matador/object/internal/observer_list_creator.hpp"
|
||||||
|
|
||||||
#include "matador/utils/error.hpp"
|
|
||||||
#include "matador/utils/result.hpp"
|
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
namespace matador::object {
|
namespace matador::object {
|
||||||
|
|
||||||
class basic_object_info;
|
class basic_object_info;
|
||||||
class repository;
|
class basic_repository;
|
||||||
|
|
||||||
class repository_node final {
|
class repository_node final {
|
||||||
public:
|
public:
|
||||||
using node_ptr = std::shared_ptr<repository_node>;
|
using node_ptr = repository_node*;
|
||||||
template< typename Type>
|
template< typename Type>
|
||||||
using creator_func = std::function<std::unique_ptr<Type>()>;
|
using creator_func = std::function<std::unique_ptr<Type>()>;
|
||||||
|
|
||||||
template < typename Type, template<typename> typename... Observers >
|
template < typename Type, template<typename> typename... Observers >
|
||||||
static std::shared_ptr<repository_node> make_node(repository& repo,
|
static std::unique_ptr<repository_node> make_node(basic_repository& repo,
|
||||||
const std::string& name,
|
const std::string& name,
|
||||||
creator_func<Type> creator,
|
creator_func<Type> creator,
|
||||||
std::vector<std::unique_ptr<observer<Type>>>&& observers);
|
std::vector<std::unique_ptr<observer<Type>>>&& observers);
|
||||||
|
|
||||||
static std::shared_ptr<repository_node> make_null_node(repository& repo);
|
explicit repository_node(basic_repository& repo);
|
||||||
|
|
||||||
repository_node(const repository_node& other) = delete;
|
repository_node(const repository_node& other) = delete;
|
||||||
repository_node(repository_node&& other) = default;
|
repository_node(repository_node&& other) = default;
|
||||||
repository_node& operator=(const repository_node& other) = delete;
|
repository_node& operator=(const repository_node& other) = delete;
|
||||||
|
|
@ -50,7 +46,7 @@ public:
|
||||||
return std::ref(static_cast<const object_info<Type>&>(*info_));
|
return std::ref(static_cast<const object_info<Type>&>(*info_));
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] const repository& schema() const;
|
[[nodiscard]] const basic_repository& schema() const;
|
||||||
|
|
||||||
[[nodiscard]] bool has_children() const;
|
[[nodiscard]] bool has_children() const;
|
||||||
|
|
||||||
|
|
@ -58,52 +54,52 @@ public:
|
||||||
void on_detach() const;
|
void on_detach() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit repository_node(repository& repo);
|
repository_node(basic_repository& repo, const std::type_index& ti);
|
||||||
repository_node(repository& repo, const std::type_index& ti);
|
repository_node(basic_repository& repo, std::string name, const std::type_index& ti);
|
||||||
repository_node(repository& repo, std::string name, const std::type_index& ti);
|
|
||||||
|
|
||||||
void unlink();
|
void unlink();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class repository;
|
friend class basic_repository;
|
||||||
template<typename Type, template<typename> typename... Observers>
|
template<typename Type, template<typename> typename... Observers>
|
||||||
friend class relation_completer;
|
friend class relation_completer;
|
||||||
template < typename NodeType, template<typename> typename ...Observers >
|
template < typename NodeType, template<typename> typename ...Observers >
|
||||||
friend class foreign_node_completer;
|
friend class foreign_node_completer;
|
||||||
friend class const_repository_node_iterator;
|
friend class const_repository_node_iterator;
|
||||||
|
|
||||||
repository &repo_;
|
basic_repository &repo_;
|
||||||
std::type_index type_index_;
|
std::type_index type_index_;
|
||||||
std::unique_ptr<basic_object_info> info_;
|
std::unique_ptr<basic_object_info> info_;
|
||||||
|
|
||||||
std::shared_ptr<repository_node> parent_;
|
repository_node* parent_{nullptr};
|
||||||
std::shared_ptr<repository_node> previous_sibling_;
|
repository_node* previous_sibling_{nullptr};
|
||||||
std::shared_ptr<repository_node> next_sibling_;
|
repository_node* next_sibling_{nullptr};
|
||||||
std::shared_ptr<repository_node> first_child_;
|
std::unique_ptr<repository_node> first_child_;
|
||||||
std::shared_ptr<repository_node> last_child_;
|
std::unique_ptr<repository_node> last_child_;
|
||||||
|
|
||||||
std::string name_;
|
std::string name_;
|
||||||
size_t depth_{0};
|
size_t depth_{0};
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename Type, template <typename> class ... Observers>
|
template<typename Type, template <typename> class ... Observers>
|
||||||
std::shared_ptr<repository_node> repository_node::make_node(repository &repo, const std::string &name,
|
std::unique_ptr<repository_node> repository_node::make_node(basic_repository &repo,
|
||||||
creator_func<Type> creator, std::vector<std::unique_ptr<observer<Type>>> &&observers) {
|
const std::string &name,
|
||||||
|
creator_func<Type> creator,
|
||||||
|
std::vector<std::unique_ptr<observer<Type>>> &&observers) {
|
||||||
const std::type_index ti(typeid(Type));
|
const std::type_index ti(typeid(Type));
|
||||||
auto node = std::shared_ptr<repository_node>(new repository_node(repo, name, ti));
|
auto node = std::unique_ptr<repository_node>(new repository_node(repo, name, ti));
|
||||||
|
|
||||||
internal::observer_list_creator<Type, Observers...>::create_missing(observers);
|
internal::observer_list_creator<Type, Observers...>::create_missing(observers);
|
||||||
|
|
||||||
auto obj = object_generator::generate<Type>(creator(), repo, name);
|
auto obj = object_generator::generate<Type>(creator(), repo, name);
|
||||||
auto info = std::make_unique<object_info<Type>>(
|
node->info_.reset(std::make_unique<object_info<Type>>(
|
||||||
node,
|
*node,
|
||||||
std::move(obj),
|
obj,
|
||||||
std::move(observers),
|
std::move(observers),
|
||||||
std::forward<creator_func<Type>>(creator)
|
std::forward<creator_func<Type>>(creator)
|
||||||
);
|
).release());
|
||||||
node->info_ = std::move(info);
|
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif //SCHEMA_NODE_HPP
|
#endif //REPOSITORY_NODE_HPP
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,19 @@
|
||||||
#ifndef SCHEMA_NODE_ITERATOR_HPP
|
#ifndef REPOSITORY_NODE_ITERATOR_HPP
|
||||||
#define SCHEMA_NODE_ITERATOR_HPP
|
#define REPOSITORY_NODE_ITERATOR_HPP
|
||||||
|
|
||||||
#include "matador/object/repository_node.hpp"
|
|
||||||
|
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
|
|
||||||
namespace matador::object {
|
namespace matador::object {
|
||||||
|
|
||||||
|
class repository_node;
|
||||||
|
|
||||||
class const_repository_node_iterator {
|
class const_repository_node_iterator {
|
||||||
public:
|
public:
|
||||||
using iterator_category = std::bidirectional_iterator_tag;
|
using iterator_category = std::bidirectional_iterator_tag;
|
||||||
using difference_type = std::ptrdiff_t;
|
using difference_type = std::ptrdiff_t;
|
||||||
using value_type = std::shared_ptr<repository_node>;
|
using value_type = repository_node;
|
||||||
using pointer = repository_node*;
|
using pointer = repository_node*;
|
||||||
using reference = value_type;
|
using reference = value_type&;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an empty iterator
|
* Creates an empty iterator
|
||||||
|
|
@ -28,7 +28,7 @@ public:
|
||||||
*
|
*
|
||||||
* @param node The schema node of the object
|
* @param node The schema node of the object
|
||||||
*/
|
*/
|
||||||
explicit const_repository_node_iterator(const value_type& node);
|
explicit const_repository_node_iterator(value_type* node);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copy from a given const_object_view_iterator.
|
* Copy from a given const_object_view_iterator.
|
||||||
|
|
@ -123,8 +123,8 @@ private:
|
||||||
void decrement();
|
void decrement();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
value_type node_{};
|
pointer node_{nullptr};
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif //SCHEMA_NODE_ITERATOR_HPP
|
#endif //REPOSITORY_NODE_ITERATOR_HPP
|
||||||
|
|
|
||||||
|
|
@ -25,12 +25,12 @@ public:
|
||||||
[[nodiscard]] bool is_primary_key_constraint() const;
|
[[nodiscard]] bool is_primary_key_constraint() const;
|
||||||
[[nodiscard]] bool is_foreign_key_constraint() const;
|
[[nodiscard]] bool is_foreign_key_constraint() const;
|
||||||
[[nodiscard]] bool is_unique_constraint() const;
|
[[nodiscard]] bool is_unique_constraint() const;
|
||||||
[[nodiscard]] const std::string& ref_table_name() const;
|
[[nodiscard]] std::string ref_table_name() const;
|
||||||
[[nodiscard]] const std::string& ref_column_name() const;
|
[[nodiscard]] std::string ref_column_name() const;
|
||||||
|
|
||||||
friend std::ostream& operator<<(std::ostream& os, const restriction& c);
|
friend std::ostream& operator<<(std::ostream& os, const restriction& c);
|
||||||
|
|
||||||
std::string type_string() const;
|
[[nodiscard]] std::string type_string() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class constraint_builder;
|
friend class constraint_builder;
|
||||||
|
|
@ -38,27 +38,9 @@ private:
|
||||||
friend class object;
|
friend class object;
|
||||||
|
|
||||||
const class attribute& attr_;
|
const class attribute& attr_;
|
||||||
std::shared_ptr<object> owner_;
|
std::weak_ptr<object> owner_;
|
||||||
std::shared_ptr<object> reference_;
|
std::weak_ptr<object> reference_;
|
||||||
utils::constraints options_{utils::constraints::None};
|
utils::constraints options_{utils::constraints::None};
|
||||||
};
|
};
|
||||||
|
|
||||||
// class constraint_builder {
|
|
||||||
// public:
|
|
||||||
// constraint_builder& constraint(std::string name);
|
|
||||||
// constraint_builder& primary_key(std::string name);
|
|
||||||
// constraint_builder& foreign_key(std::string name);
|
|
||||||
// constraint_builder& references(std::string table, std::string column);
|
|
||||||
//
|
|
||||||
// operator class restriction() const;
|
|
||||||
//
|
|
||||||
// private:
|
|
||||||
// std::string constraint_name;
|
|
||||||
// utils::constraints options_{utils::constraints::None};
|
|
||||||
// std::string column_name;
|
|
||||||
// };
|
|
||||||
//
|
|
||||||
// constraint_builder constraint(std::string name);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif //MATADOR_RESTRICTION_HPP
|
#endif //MATADOR_RESTRICTION_HPP
|
||||||
|
|
@ -109,7 +109,7 @@ public:
|
||||||
utils::result<sql::query_result<Type>, utils::error> find(query::criteria_ptr clause = {});
|
utils::result<sql::query_result<Type>, utils::error> find(query::criteria_ptr clause = {});
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
utils::result<void, utils::error> drop_table();
|
utils::result<void, utils::error> drop_table() const;
|
||||||
utils::result<void, utils::error> drop_table(const std::string &table_name) const;
|
utils::result<void, utils::error> drop_table(const std::string &table_name) const;
|
||||||
|
|
||||||
[[nodiscard]] utils::result<sql::query_result<sql::record>, utils::error> fetch_all(const sql::query_context &q) const;
|
[[nodiscard]] utils::result<sql::query_result<sql::record>, utils::error> fetch_all(const sql::query_context &q) const;
|
||||||
|
|
@ -141,13 +141,13 @@ private:
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
utils::result<object::object_ptr<Type>, utils::error> session::insert(Type *obj) {
|
utils::result<object::object_ptr<Type>, utils::error> session::insert(Type *obj) {
|
||||||
auto info = schema_.repo().info<Type>();
|
const auto it = schema_.find(typeid(Type));
|
||||||
if (!info) {
|
if (it == schema_.end()) {
|
||||||
return utils::failure(info.err());
|
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
||||||
}
|
}
|
||||||
|
|
||||||
auto res = query::query::insert()
|
auto res = query::query::insert()
|
||||||
.into(info->get().name(), query::generator::columns<Type>(schema_))
|
.into(it->second.name(), query::generator::columns<Type>(schema_))
|
||||||
.values(query::generator::placeholders<Type>())
|
.values(query::generator::placeholders<Type>())
|
||||||
.prepare(*this);
|
.prepare(*this);
|
||||||
if (!res) {
|
if (!res) {
|
||||||
|
|
@ -214,15 +214,15 @@ private:
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
utils::result<object::object_ptr<Type>, utils::error> session::update( const object::object_ptr<Type>& obj ) {
|
utils::result<object::object_ptr<Type>, utils::error> session::update( const object::object_ptr<Type>& obj ) {
|
||||||
auto info = schema_.repo().info<Type>();
|
const auto it = schema_.find(typeid(Type));
|
||||||
if (!info) {
|
if (it == schema_.end()) {
|
||||||
return utils::failure(info.err());
|
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
||||||
}
|
}
|
||||||
using namespace matador::utils;
|
using namespace matador::utils;
|
||||||
using namespace matador::query;
|
using namespace matador::query;
|
||||||
|
|
||||||
const auto col = table_column(info.value().get().primary_key_attribute()->name());
|
const auto col = table_column(it->second.node().info().primary_key_attribute()->name());
|
||||||
auto res = matador::query::query::update(info->get().name())
|
auto res = matador::query::query::update(it->second.name())
|
||||||
.set(generator::column_value_pairs<Type>())
|
.set(generator::column_value_pairs<Type>())
|
||||||
.where(col == _)
|
.where(col == _)
|
||||||
.prepare(*this);
|
.prepare(*this);
|
||||||
|
|
@ -240,16 +240,16 @@ utils::result<object::object_ptr<Type>, utils::error> session::update( const obj
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
utils::result<void, utils::error> session::remove( const object::object_ptr<Type>& obj ) {
|
utils::result<void, utils::error> session::remove( const object::object_ptr<Type>& obj ) {
|
||||||
auto info = schema_.repo().info<Type>();
|
const auto it = schema_.find(typeid(Type));
|
||||||
if (!info) {
|
if (it == schema_.end()) {
|
||||||
return utils::failure(info.err());
|
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
||||||
}
|
}
|
||||||
using namespace matador::utils;
|
using namespace matador::utils;
|
||||||
using namespace matador::query;
|
using namespace matador::query;
|
||||||
|
|
||||||
const auto col = table_column(info.value().get().primary_key_attribute()->name());
|
const auto col = table_column(it->second.node().info().primary_key_attribute()->name());
|
||||||
auto res = matador::query::query::remove()
|
auto res = matador::query::query::remove()
|
||||||
.from( info->get().name() )
|
.from( it->second.name() )
|
||||||
.where(col == _)
|
.where(col == _)
|
||||||
.prepare(*this);
|
.prepare(*this);
|
||||||
if (!res) {
|
if (!res) {
|
||||||
|
|
@ -299,15 +299,15 @@ utils::result<object::object_ptr<Type>, utils::error> session::find(const Primar
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
utils::result<sql::query_result<Type>, utils::error> session::find(query::criteria_ptr clause) {
|
utils::result<sql::query_result<Type>, utils::error> session::find(query::criteria_ptr clause) {
|
||||||
auto info = schema_.repo().info<Type>();
|
const auto it = schema_.find(typeid(Type));
|
||||||
if (!info) {
|
if (it == schema_.end()) {
|
||||||
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
||||||
}
|
}
|
||||||
|
|
||||||
session_query_builder eqb(schema_, *this);
|
session_query_builder eqb(schema_, *this);
|
||||||
auto data = eqb.build<Type>(std::move(clause));
|
auto data = eqb.build<Type>(std::move(clause));
|
||||||
if (!data.is_ok()) {
|
if (!data.is_ok()) {
|
||||||
return utils::failure(make_error(error_code::FailedToBuildQuery, "Failed to build query for type " + info->get().name() + "."));
|
return utils::failure(make_error(error_code::FailedToBuildQuery, "Failed to build query for type " + it->second.name() + "."));
|
||||||
}
|
}
|
||||||
|
|
||||||
auto result = build_select_query(data.release()).prepare(*this);
|
auto result = build_select_query(data.release()).prepare(*this);
|
||||||
|
|
@ -319,13 +319,12 @@ utils::result<sql::query_result<Type>, utils::error> session::find(query::criter
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
utils::result<void, utils::error> session::drop_table() {
|
utils::result<void, utils::error> session::drop_table() const {
|
||||||
auto info = schema_.repo().info<Type>();
|
const auto it = schema_.find(typeid(Type));
|
||||||
if (info) {
|
if (it == schema_.end()) {
|
||||||
return drop_table(info->get().name());
|
return utils::failure(make_error(error_code::UnknownType, "Failed to determine requested type."));
|
||||||
}
|
}
|
||||||
|
return drop_table(it->second.name());
|
||||||
return utils::failure(info.err());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -278,8 +278,6 @@ private:
|
||||||
struct table_info {
|
struct table_info {
|
||||||
const object::basic_object_info &info;
|
const object::basic_object_info &info;
|
||||||
query::table table;
|
query::table table;
|
||||||
// std::reference_wrapper<const object::basic_object_info> info;
|
|
||||||
// std::shared_ptr<query::table> table;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
std::stack<table_info> table_info_stack_{};
|
std::stack<table_info> table_info_stack_{};
|
||||||
|
|
@ -325,7 +323,7 @@ void session_query_builder::on_foreign_object(const char *id, Pointer &, const u
|
||||||
using namespace matador::utils;
|
using namespace matador::utils;
|
||||||
using namespace matador::query;
|
using namespace matador::query;
|
||||||
// create select query
|
// create select query
|
||||||
auto result = matador::query::query::select(generator::columns<typename Pointer::value_type>(schema_, generator::column_generator_options::ForceLazy))
|
auto result = matador::query::query::select(generator::columns<typename Pointer::value_type>(schema_, foreign_table, generator::column_generator_options::ForceLazy))
|
||||||
.from(foreign_table)
|
.from(foreign_table)
|
||||||
.where(table_column(&foreign_table, info.primary_key_attribute()->name(), "") == _)
|
.where(table_column(&foreign_table, info.primary_key_attribute()->name(), "") == _)
|
||||||
.prepare(executor_);
|
.prepare(executor_);
|
||||||
|
|
|
||||||
|
|
@ -38,8 +38,9 @@ public:
|
||||||
void write_value(size_t pos, const bool& x) override;
|
void write_value(size_t pos, const bool& x) override;
|
||||||
void write_value(size_t pos, const float& x) override;
|
void write_value(size_t pos, const float& x) override;
|
||||||
void write_value(size_t pos, const double& x) override;
|
void write_value(size_t pos, const double& x) override;
|
||||||
void write_value(size_t pos, const time& x) override;
|
void write_value(size_t pos, const utils::date_type_t& x) override;
|
||||||
void write_value(size_t pos, const date& x) override;
|
void write_value(size_t pos, const utils::time_type_t& x) override;
|
||||||
|
void write_value(size_t pos, const utils::timestamp& x) override;
|
||||||
void write_value(size_t pos, const char* x) override;
|
void write_value(size_t pos, const char* x) override;
|
||||||
void write_value(size_t pos, const char* x, size_t size) override;
|
void write_value(size_t pos, const char* x, size_t size) override;
|
||||||
void write_value(size_t pos, const std::string& x) override;
|
void write_value(size_t pos, const std::string& x) override;
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ constexpr auto default_column_generator_options = column_generator_options::Forc
|
||||||
class column_generator {
|
class column_generator {
|
||||||
public:
|
public:
|
||||||
explicit column_generator(const schema &repo,
|
explicit column_generator(const schema &repo,
|
||||||
const std::string &table_name = "",
|
const table* tab,
|
||||||
column_generator_options options = default_column_generator_options);
|
column_generator_options options = default_column_generator_options);
|
||||||
|
|
||||||
template< class Type >
|
template< class Type >
|
||||||
|
|
@ -88,21 +88,19 @@ public:
|
||||||
if (attr.fetch() == utils::fetch_type::Lazy || is_column_generator_option_set(options_, column_generator_options::ForceLazy)) {
|
if (attr.fetch() == utils::fetch_type::Lazy || is_column_generator_option_set(options_, column_generator_options::ForceLazy)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!repo_) {
|
const auto it = repo_.find(typeid(typename ContainerType::value_type::value_type));
|
||||||
return;
|
if (it == repo_.end()) {
|
||||||
}
|
// Todo: throw exception
|
||||||
const auto info = repo_->get().repo().info<typename ContainerType::value_type::value_type>();
|
|
||||||
if (!info) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (seen_tables.count(info->get().name()) == 0) {
|
if (seen_tables.count(it->second.name()) == 0) {
|
||||||
auto it = seen_tables.insert(info->get().name()).first;
|
const auto itt = seen_tables.insert(it->second.name()).first;
|
||||||
table_stack_.push(std::make_shared<table>(info.value().get().name()));
|
table_stack_.push(&it->second.table());
|
||||||
typename ContainerType::value_type::value_type obj;
|
typename ContainerType::value_type::value_type obj;
|
||||||
access::process(*this, obj);
|
access::process(*this, obj);
|
||||||
table_stack_.pop();
|
table_stack_.pop();
|
||||||
seen_tables.erase(it);
|
seen_tables.erase(itt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -124,20 +122,18 @@ private:
|
||||||
if (attr.fetch() == utils::fetch_type::Lazy || is_column_generator_option_set(options_, column_generator_options::ForceLazy)) {
|
if (attr.fetch() == utils::fetch_type::Lazy || is_column_generator_option_set(options_, column_generator_options::ForceLazy)) {
|
||||||
push(id);
|
push(id);
|
||||||
} else {
|
} else {
|
||||||
if (!repo_) {
|
const auto it = repo_.find(typeid(typename Pointer::value_type));
|
||||||
|
if (it == repo_.end()) {
|
||||||
|
// Todo: throw exception
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const auto info = repo_->get().repo().info<typename Pointer::value_type>();
|
if (seen_tables.count(it->second.name()) == 0) {
|
||||||
if (!info) {
|
const auto iit = seen_tables.insert(it->second.name()).first;
|
||||||
return;
|
table_stack_.push(&it->second.table());
|
||||||
}
|
|
||||||
if (seen_tables.count(info->get().name()) == 0) {
|
|
||||||
auto it = seen_tables.insert(info->get().name()).first;
|
|
||||||
table_stack_.push(std::make_shared<table>(info.value().get().name()));
|
|
||||||
typename Pointer::value_type obj;
|
typename Pointer::value_type obj;
|
||||||
access::process(*this, obj);
|
access::process(*this, obj);
|
||||||
table_stack_.pop();
|
table_stack_.pop();
|
||||||
seen_tables.erase(it);
|
seen_tables.erase(iit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -145,9 +141,9 @@ private:
|
||||||
void push(const std::string &column_name);
|
void push(const std::string &column_name);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::optional<std::reference_wrapper<const schema>> repo_;
|
const schema &repo_;
|
||||||
std::vector<table_column> result_;
|
std::vector<table_column> result_;
|
||||||
std::stack<std::shared_ptr<table>> table_stack_;
|
std::stack<const table*> table_stack_;
|
||||||
std::unordered_set<std::string> seen_tables;
|
std::unordered_set<std::string> seen_tables;
|
||||||
int column_index{0};
|
int column_index{0};
|
||||||
column_generator_options options_{false};
|
column_generator_options options_{false};
|
||||||
|
|
@ -294,29 +290,32 @@ std::vector<internal::column_value_pair> column_value_pairs() {
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
std::vector<table_column> columns(const schema &repo,
|
std::vector<table_column> columns(const schema &repo,
|
||||||
const std::string &table_name = "",
|
const table &tab,
|
||||||
const column_generator_options options = default_column_generator_options) {
|
const column_generator_options options = default_column_generator_options) {
|
||||||
column_generator generator(repo, table_name, options);
|
column_generator generator(repo, &tab, options);
|
||||||
return generator.generate<Type>();
|
return generator.generate<Type>();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
std::vector<table_column> columns(const schema &repo,
|
std::vector<table_column> columns(const schema &repo,
|
||||||
const column_generator_options options) {
|
const column_generator_options options = default_column_generator_options) {
|
||||||
std::string table_name;
|
const auto it = repo.find(typeid(Type));
|
||||||
if (const auto result = repo.repo().info<Type>()) {
|
if (it == repo.end()) {
|
||||||
table_name = result.value().get().name();
|
return {};
|
||||||
}
|
}
|
||||||
column_generator generator(repo, table_name, options);
|
column_generator generator(repo, &it->second.table(), options);
|
||||||
return generator.generate<Type>();
|
return generator.generate<Type>();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
std::vector<table_column> columns(const Type &obj,
|
std::vector<table_column> columns(const Type &obj,
|
||||||
const schema &repo,
|
const schema &repo,
|
||||||
const std::string &table_name = "",
|
|
||||||
const column_generator_options options = default_column_generator_options) {
|
const column_generator_options options = default_column_generator_options) {
|
||||||
column_generator generator(repo, table_name, options);
|
const auto it = repo.find(typeid(Type));
|
||||||
|
if (it == repo.end()) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
column_generator generator(repo, &it->second.table(), options);
|
||||||
return generator.generate(obj);
|
return generator.generate(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ public:
|
||||||
return into(tab, generator::columns<Type>(scm));
|
return into(tab, generator::columns<Type>(scm));
|
||||||
}
|
}
|
||||||
query_into_intermediate into(const table &tab, std::initializer_list<table_column> columns);
|
query_into_intermediate into(const table &tab, std::initializer_list<table_column> columns);
|
||||||
query_into_intermediate into(const table &tab, std::vector<table_column> &&columns);
|
query_into_intermediate into(const table &tab, const std::vector<table_column> &columns);
|
||||||
query_into_intermediate into(const table &tab, const std::vector<std::string> &column_names);
|
query_into_intermediate into(const table &tab, const std::vector<std::string> &column_names);
|
||||||
query_into_intermediate into(const table &tab);
|
query_into_intermediate into(const table &tab);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,6 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace matador {
|
|
||||||
class date;
|
|
||||||
class time;
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace matador::sql {
|
namespace matador::sql {
|
||||||
class dialect;
|
class dialect;
|
||||||
struct query_context;
|
struct query_context;
|
||||||
|
|
@ -36,8 +31,9 @@ struct basic_type_to_string_visitor
|
||||||
void operator()(const double &x) { result = writer->to_string(x); }
|
void operator()(const double &x) { result = writer->to_string(x); }
|
||||||
void operator()(const char *x) { result = writer->to_string(x); }
|
void operator()(const char *x) { result = writer->to_string(x); }
|
||||||
void operator()(const std::string &x) { result = writer->to_string(x); }
|
void operator()(const std::string &x) { result = writer->to_string(x); }
|
||||||
void operator()(const matador::date &x) { result = writer->to_string(x); }
|
void operator()(const utils::date_type_t &x) { result = writer->to_string(x); }
|
||||||
void operator()(const matador::time &x) { result = writer->to_string(x); }
|
void operator()(const utils::time_type_t &x) { result = writer->to_string(x); }
|
||||||
|
void operator()(const utils::timestamp &x) { result = writer->to_string(x); }
|
||||||
void operator()(const utils::blob &x) { result = writer->to_string(x); }
|
void operator()(const utils::blob &x) { result = writer->to_string(x); }
|
||||||
|
|
||||||
attribute_string_writer *writer{};
|
attribute_string_writer *writer{};
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ namespace matador::query::internal {
|
||||||
|
|
||||||
class column_value_pair {
|
class column_value_pair {
|
||||||
public:
|
public:
|
||||||
column_value_pair(const table_column &col, utils::database_type value);
|
column_value_pair(table_column col, utils::database_type value);
|
||||||
column_value_pair(std::string name, utils::database_type value);
|
column_value_pair(std::string name, utils::database_type value);
|
||||||
column_value_pair(const char *name, utils::database_type value);
|
column_value_pair(const char *name, utils::database_type value);
|
||||||
column_value_pair(const char *name, utils::placeholder p);
|
column_value_pair(const char *name, utils::placeholder p);
|
||||||
|
|
|
||||||
|
|
@ -92,9 +92,9 @@ public:
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
[[nodiscard]] utils::result<void, utils::error> drop_table(const sql::connection &conn);
|
[[nodiscard]] utils::result<void, utils::error> drop_table(const sql::connection &conn);
|
||||||
[[nodiscard]] utils::result<void, utils::error> drop_table(const std::string &table_name, const sql::connection &conn) const;
|
[[nodiscard]] static utils::result<void, utils::error> drop_table(const std::string &table_name, const sql::connection &conn);
|
||||||
|
|
||||||
[[nodiscard]] utils::result<std::vector<object::attribute>, utils::error> describe_table(const std::string &table_name, const sql::connection &conn) const;
|
[[nodiscard]] static utils::result<std::vector<object::attribute>, utils::error> describe_table(const std::string &table_name, const sql::connection &conn) ;
|
||||||
[[nodiscard]] utils::result<bool, utils::error> table_exists(const std::string &table_name, const sql::connection &conn) const;
|
[[nodiscard]] utils::result<bool, utils::error> table_exists(const std::string &table_name, const sql::connection &conn) const;
|
||||||
|
|
||||||
iterator begin();
|
iterator begin();
|
||||||
|
|
@ -127,7 +127,7 @@ private:
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
utils::result<void, utils::error> schema::drop_table(const sql::connection &conn) {
|
utils::result<void, utils::error> schema::drop_table(const sql::connection &conn) {
|
||||||
auto info = repo_.info<Type>();
|
auto info = repo_.basic_info<Type>();
|
||||||
if (info) {
|
if (info) {
|
||||||
return drop_table(info->get().name(), conn);
|
return drop_table(info->get().name(), conn);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -71,8 +71,9 @@ public:
|
||||||
void write_value(size_t pos, const bool &x) override;
|
void write_value(size_t pos, const bool &x) override;
|
||||||
void write_value(size_t pos, const float &x) override;
|
void write_value(size_t pos, const float &x) override;
|
||||||
void write_value(size_t pos, const double &x) override;
|
void write_value(size_t pos, const double &x) override;
|
||||||
void write_value(size_t pos, const time &x ) override;
|
void write_value(size_t pos, const utils::date_type_t &x ) override;
|
||||||
void write_value(size_t pos, const date &x ) override;
|
void write_value(size_t pos, const utils::time_type_t &x ) override;
|
||||||
|
void write_value(size_t pos, const utils::timestamp &x ) override;
|
||||||
void write_value(size_t pos, const char *x) override;
|
void write_value(size_t pos, const char *x) override;
|
||||||
void write_value(size_t pos, const char *x, size_t size) override;
|
void write_value(size_t pos, const char *x, size_t size) override;
|
||||||
void write_value(size_t pos, const std::string &x) override;
|
void write_value(size_t pos, const std::string &x) override;
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,6 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace matador {
|
|
||||||
class date;
|
|
||||||
class time;
|
|
||||||
}
|
|
||||||
namespace matador::utils {
|
namespace matador::utils {
|
||||||
|
|
||||||
class value;
|
class value;
|
||||||
|
|
@ -29,8 +25,9 @@ public:
|
||||||
virtual void write_value(size_t pos, const bool &x) = 0;
|
virtual void write_value(size_t pos, const bool &x) = 0;
|
||||||
virtual void write_value(size_t pos, const float &x) = 0;
|
virtual void write_value(size_t pos, const float &x) = 0;
|
||||||
virtual void write_value(size_t pos, const double &x) = 0;
|
virtual void write_value(size_t pos, const double &x) = 0;
|
||||||
virtual void write_value(size_t pos, const time &x) = 0;
|
virtual void write_value(size_t pos, const date_type_t &x) = 0;
|
||||||
virtual void write_value(size_t pos, const date &x) = 0;
|
virtual void write_value(size_t pos, const time_type_t &x) = 0;
|
||||||
|
virtual void write_value(size_t pos, const timestamp &x) = 0;
|
||||||
virtual void write_value(size_t pos, const char *x) = 0;
|
virtual void write_value(size_t pos, const char *x) = 0;
|
||||||
virtual void write_value(size_t pos, const char *x, size_t size) = 0;
|
virtual void write_value(size_t pos, const char *x, size_t size) = 0;
|
||||||
virtual void write_value(size_t pos, const std::string &x) = 0;
|
virtual void write_value(size_t pos, const std::string &x) = 0;
|
||||||
|
|
|
||||||
|
|
@ -6,11 +6,6 @@
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
// namespace matador {
|
|
||||||
//class date;
|
|
||||||
//class time;
|
|
||||||
//}
|
|
||||||
|
|
||||||
namespace matador::utils {
|
namespace matador::utils {
|
||||||
|
|
||||||
template < typename Type >
|
template < typename Type >
|
||||||
|
|
@ -36,8 +31,9 @@ public:
|
||||||
void operator()(double &x) { this->convert(x); }
|
void operator()(double &x) { this->convert(x); }
|
||||||
void operator()(const char *x) { this->convert(x); }
|
void operator()(const char *x) { this->convert(x); }
|
||||||
void operator()(std::string &x) { this->convert(x); }
|
void operator()(std::string &x) { this->convert(x); }
|
||||||
// void operator()(date &x) { this->convert(result, x); }
|
void operator()(date_type_t &x) { this->convert(x); }
|
||||||
// void operator()(time &x) { this->convert(result, x); }
|
void operator()(time_type_t &x) { this->convert(x); }
|
||||||
|
void operator()(timestamp &x) { this->convert(x); }
|
||||||
void operator()(blob &x) { this->convert(x); }
|
void operator()(blob &x) { this->convert(x); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ enum class basic_type : uint8_t {
|
||||||
Varchar, /*!< Data type varchar */
|
Varchar, /*!< Data type varchar */
|
||||||
Text, /*!< Data type text */
|
Text, /*!< Data type text */
|
||||||
Date, /*!< Data type date */
|
Date, /*!< Data type date */
|
||||||
|
DateTime, /*!< Data type datetime */
|
||||||
Time, /*!< Data type time */
|
Time, /*!< Data type time */
|
||||||
Blob, /*!< Data type blob */
|
Blob, /*!< Data type blob */
|
||||||
Null, /*!< Data type null */
|
Null, /*!< Data type null */
|
||||||
|
|
|
||||||
|
|
@ -3,39 +3,36 @@
|
||||||
|
|
||||||
#include "matador/utils/types.hpp"
|
#include "matador/utils/types.hpp"
|
||||||
#include "matador/utils/result.hpp"
|
#include "matador/utils/result.hpp"
|
||||||
//#include "matador/utils/date.hpp"
|
|
||||||
//#include "matador/utils/time.hpp"
|
|
||||||
|
|
||||||
//#include "matador/utils/placeholder.hpp"
|
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <charconv>
|
#include <charconv>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <iomanip>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Conversion matrix
|
* Conversion matrix
|
||||||
* from> | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | bool | float | double | string | date | time | blob | placeholder | null
|
* from> | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | bool | float | double | string | date | time | timestamp | blob | null
|
||||||
* to | | | | | | | | | | | | | | | | |
|
* to | | | | | | | | | | | | | | | | |
|
||||||
* ------------+------+-------+-------+-------+-------+--------+--------+--------+------+-------+--------+--------+------+------+------+-------------+-----
|
* ------------+------+-------+-------+-------+-------+--------+--------+--------+------+-------+--------+--------+------+------+-----------+------+-----
|
||||||
* int8 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
|
* int8 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | ok | try | N/A
|
||||||
* int16 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
|
* int16 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | ok | try | N/A
|
||||||
* int32 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
|
* int32 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | ok | try | N/A
|
||||||
* int64 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
|
* int64 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | ok | try | N/A
|
||||||
* uint8 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
|
* uint8 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | ok | try | N/A
|
||||||
* uint16 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
|
* uint16 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | ok | try | N/A
|
||||||
* uint32 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
|
* uint32 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | ok | try | N/A
|
||||||
* uint64 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | try | N/A | N/A
|
* uint64 | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | ok | ok | ok | try | N/A
|
||||||
* bool | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | N/A | N/A | try | N/A | N/A
|
* bool | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | N/A | N/A | ok | try | N/A
|
||||||
* float | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | N/A | N/A | try | N/A | N/A
|
* float | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | N/A | N/A | ok | try | N/A
|
||||||
* double | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | N/A | N/A | try | N/A | N/A
|
* double | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | try | N/A | N/A | ok | try | N/A
|
||||||
* string | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | N/A | N/A | N/A
|
* string | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | N/A | N/A
|
||||||
* date | ok | ok | ok | ok | ok | ok | ok | ok | N/A | N/A | N/A | try | ok | ok | N/A | N/A | N/A
|
* date | ok | ok | ok | ok | ok | ok | ok | ok | N/A | N/A | N/A | try | ok | ok | ok | N/A | N/A
|
||||||
* time | ok | ok | ok | ok | ok | ok | ok | ok | N/A | N/A | N/A | try | ok | ok | N/A | N/A | N/A
|
* time | ok | ok | ok | ok | ok | ok | ok | ok | N/A | N/A | N/A | try | ok | ok | ok | N/A | N/A
|
||||||
* blob | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | N/A | N/A
|
* timestamp | ok | ok | ok | ok | ok | ok | ok | ok | N/A | N/A | N/A | try | ok | ok | ok | N/A | N/A
|
||||||
* placeholder | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | ok | N/A
|
* blob | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | ok | N/A
|
||||||
* null | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | ok
|
* null | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | N/A | ok | ok
|
||||||
*
|
*
|
||||||
* from integral to date/time works, value is interpreted as std::chrono::timepoint
|
* from integral to date/time works, value is interpreted as std::chrono::timepoint
|
||||||
* from integral to blob works, bytes of integral data will converted to blob
|
* from integral to blob works, bytes of integral data will converted to blob
|
||||||
|
|
@ -97,7 +94,6 @@ result<DestType, conversion_error> to(const SourceType &source, std::enable_if_t
|
||||||
return ok(std::string(buffer.data(), ptr));
|
return ok(std::string(buffer.data(), ptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
// return failure({ec, "couldn't convert value to std::string"});
|
|
||||||
return failure(conversion_error::NotConvertable/*, "couldn't convert value to std::string"}*/);
|
return failure(conversion_error::NotConvertable/*, "couldn't convert value to std::string"}*/);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -110,7 +106,6 @@ result<DestType, conversion_error> to(const SourceType &source, std::enable_if_t
|
||||||
return ok(std::string(buffer.data(), ptr));
|
return ok(std::string(buffer.data(), ptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
// return failure({ec, "couldn't convert value to std::string"});
|
|
||||||
return failure(conversion_error::NotConvertable/*, "couldn't convert value to std::string"}*/);
|
return failure(conversion_error::NotConvertable/*, "couldn't convert value to std::string"}*/);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -257,6 +252,105 @@ result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_
|
||||||
return failure(conversion_error::NotConvertable);
|
return failure(conversion_error::NotConvertable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template < typename DestType >
|
||||||
|
result<DestType, conversion_error> to(const std::string &source, std::enable_if_t<std::is_same_v<DestType, date_type_t>>* = nullptr) {
|
||||||
|
std::tm result{};
|
||||||
|
std::istringstream iss(source);
|
||||||
|
iss >> std::get_time(&result, "%Y-%m-%d %H:%M:%S");
|
||||||
|
return ok(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename DestType >
|
||||||
|
result<DestType, conversion_error> to(const std::string &source, std::enable_if_t<std::is_same_v<DestType, time_type_t>>* = nullptr) {
|
||||||
|
std::tm result{};
|
||||||
|
std::istringstream iss(source);
|
||||||
|
iss >> std::get_time(&result, "%Y-%m-%d %H:%M:%S");
|
||||||
|
return ok(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename DestType >
|
||||||
|
result<DestType, conversion_error> to(const std::string &source, std::enable_if_t<std::is_same_v<DestType, timestamp>>* = nullptr) {
|
||||||
|
std::tm result{};
|
||||||
|
std::istringstream iss(source);
|
||||||
|
iss >> std::get_time(&result, "%Y-%m-%d %H:%M:%S");
|
||||||
|
return ok(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename DestType, typename SourceType >
|
||||||
|
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_integral_v<DestType> && !std::is_same_v<bool, DestType> && std::is_same_v<date_type_t, SourceType>>* = nullptr) {
|
||||||
|
return failure(conversion_error::NotConvertable);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename DestType, typename SourceType >
|
||||||
|
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_integral_v<DestType> && !std::is_same_v<bool, DestType> && std::is_same_v<time_type_t, SourceType>>* = nullptr) {
|
||||||
|
return failure(conversion_error::NotConvertable);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename DestType, typename SourceType >
|
||||||
|
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_integral_v<DestType> && !std::is_same_v<bool, DestType> && std::is_same_v<timestamp, SourceType>>* = nullptr) {
|
||||||
|
return failure(conversion_error::NotConvertable);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename DestType, typename SourceType >
|
||||||
|
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_same_v<bool, DestType> && std::is_same_v<date_type_t, SourceType>>* = nullptr) {
|
||||||
|
return failure(conversion_error::NotConvertable);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename DestType, typename SourceType >
|
||||||
|
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_same_v<bool, DestType> && std::is_same_v<time_type_t, SourceType>>* = nullptr) {
|
||||||
|
return failure(conversion_error::NotConvertable);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename DestType, typename SourceType >
|
||||||
|
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_same_v<bool, DestType> && std::is_same_v<timestamp, SourceType>>* = nullptr) {
|
||||||
|
return failure(conversion_error::NotConvertable);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename DestType, typename SourceType >
|
||||||
|
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_floating_point_v<DestType> && std::is_same_v<date_type_t, SourceType>>* = nullptr) {
|
||||||
|
return failure(conversion_error::NotConvertable);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename DestType, typename SourceType >
|
||||||
|
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_floating_point_v<DestType> && std::is_same_v<time_type_t, SourceType>>* = nullptr) {
|
||||||
|
return failure(conversion_error::NotConvertable);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename DestType, typename SourceType >
|
||||||
|
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_floating_point_v<DestType> && std::is_same_v<timestamp, SourceType>>* = nullptr) {
|
||||||
|
return failure(conversion_error::NotConvertable);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename DestType, typename SourceType >
|
||||||
|
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_same_v<blob, DestType> && std::is_same_v<date_type_t, SourceType>>* = nullptr) {
|
||||||
|
return failure(conversion_error::NotConvertable);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename DestType, typename SourceType >
|
||||||
|
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_same_v<blob, DestType> && std::is_same_v<time_type_t, SourceType>>* = nullptr) {
|
||||||
|
return failure(conversion_error::NotConvertable);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename DestType, typename SourceType >
|
||||||
|
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_same_v<blob, DestType> && std::is_same_v<timestamp, SourceType>>* = nullptr) {
|
||||||
|
return failure(conversion_error::NotConvertable);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename DestType, typename SourceType >
|
||||||
|
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_same_v<std::string, DestType> && std::is_same_v<date_type_t, SourceType>>* = nullptr) {
|
||||||
|
return failure(conversion_error::NotConvertable);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename DestType, typename SourceType >
|
||||||
|
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_same_v<std::string, DestType> && std::is_same_v<time_type_t, SourceType>>* = nullptr) {
|
||||||
|
return failure(conversion_error::NotConvertable);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename DestType, typename SourceType >
|
||||||
|
result<DestType, conversion_error> to(const SourceType &/*source*/, std::enable_if_t<std::is_same_v<std::string, DestType> && std::is_same_v<timestamp, SourceType>>* = nullptr) {
|
||||||
|
return failure(conversion_error::NotConvertable);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //MATADOR_CONVERT_HPP
|
#endif //MATADOR_CONVERT_HPP
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
namespace matador::utils {
|
namespace matador::utils {
|
||||||
|
|
||||||
|
|
@ -12,6 +13,10 @@ enum class basic_type : uint8_t;
|
||||||
|
|
||||||
using byte = unsigned char;
|
using byte = unsigned char;
|
||||||
using blob = std::vector<byte>;
|
using blob = std::vector<byte>;
|
||||||
|
using timestamp = std::chrono::system_clock::time_point;
|
||||||
|
|
||||||
|
struct date_type_t;
|
||||||
|
struct time_type_t;
|
||||||
|
|
||||||
using database_type = std::variant<
|
using database_type = std::variant<
|
||||||
uint8_t, uint16_t, uint32_t, uint64_t,
|
uint8_t, uint16_t, uint32_t, uint64_t,
|
||||||
|
|
@ -21,12 +26,61 @@ using database_type = std::variant<
|
||||||
const char*,
|
const char*,
|
||||||
std::string,
|
std::string,
|
||||||
blob,
|
blob,
|
||||||
|
timestamp,
|
||||||
|
date_type_t,
|
||||||
|
time_type_t,
|
||||||
nullptr_t>;
|
nullptr_t>;
|
||||||
|
|
||||||
struct null_type_t {};
|
struct null_type_t {};
|
||||||
|
|
||||||
|
struct date_type_t {
|
||||||
|
int32_t year{};
|
||||||
|
uint8_t month{}; // 1-12
|
||||||
|
uint8_t day{};
|
||||||
|
};
|
||||||
|
|
||||||
|
struct time_type_t {
|
||||||
|
uint8_t hour{};
|
||||||
|
uint8_t minute{};
|
||||||
|
uint8_t second{};
|
||||||
|
uint32_t microsecond{};
|
||||||
|
};
|
||||||
|
|
||||||
void initialize_by_basic_type(basic_type type, database_type &val);
|
void initialize_by_basic_type(basic_type type, database_type &val);
|
||||||
|
|
||||||
|
|
||||||
|
// Equality
|
||||||
|
constexpr bool operator==(const date_type_t& a, const date_type_t& b) noexcept {
|
||||||
|
return a.year == b.year &&
|
||||||
|
a.month == b.month &&
|
||||||
|
a.day == b.day;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr bool operator!=(const date_type_t& a, const date_type_t& b) noexcept;
|
||||||
|
|
||||||
|
// Ordering
|
||||||
|
constexpr bool operator<(const date_type_t& a, const date_type_t& b) noexcept;
|
||||||
|
constexpr bool operator>(const date_type_t& a, const date_type_t& b) noexcept;
|
||||||
|
constexpr bool operator<=(const date_type_t& a, const date_type_t& b) noexcept;
|
||||||
|
constexpr bool operator>=(const date_type_t& a, const date_type_t& b) noexcept;
|
||||||
|
|
||||||
|
|
||||||
|
// Equality
|
||||||
|
constexpr bool operator==(const time_type_t &a, const time_type_t &b) noexcept {
|
||||||
|
return a.hour == b.hour &&
|
||||||
|
a.minute == b.minute &&
|
||||||
|
a.second == b.second &&
|
||||||
|
a.microsecond == b.microsecond;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr bool operator!=(const time_type_t& a, const time_type_t& b) noexcept;
|
||||||
|
|
||||||
|
// Ordering
|
||||||
|
constexpr bool operator<(const time_type_t& a, const time_type_t& b) noexcept;
|
||||||
|
constexpr bool operator>(const time_type_t& a, const time_type_t& b) noexcept;
|
||||||
|
constexpr bool operator<=(const time_type_t& a, const time_type_t& b) noexcept;
|
||||||
|
constexpr bool operator>=(const time_type_t& a, const time_type_t& b) noexcept;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif //MATADOR_TYPES_HPP
|
#endif //MATADOR_TYPES_HPP
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,6 @@ add_library(matador-core STATIC
|
||||||
../../include/matador/object/basic_object_info.hpp
|
../../include/matador/object/basic_object_info.hpp
|
||||||
../../include/matador/object/error_code.hpp
|
../../include/matador/object/error_code.hpp
|
||||||
../../include/matador/object/foreign_node_completer.hpp
|
../../include/matador/object/foreign_node_completer.hpp
|
||||||
../../include/matador/object/internal/shadow_repository.hpp
|
|
||||||
../../include/matador/object/many_to_many_relation.hpp
|
../../include/matador/object/many_to_many_relation.hpp
|
||||||
../../include/matador/object/object.hpp
|
../../include/matador/object/object.hpp
|
||||||
../../include/matador/object/object_generator.hpp
|
../../include/matador/object/object_generator.hpp
|
||||||
|
|
@ -81,7 +80,6 @@ add_library(matador-core STATIC
|
||||||
object/basic_object_info.cpp
|
object/basic_object_info.cpp
|
||||||
object/error_code.cpp
|
object/error_code.cpp
|
||||||
object/foreign_node_completer.cpp
|
object/foreign_node_completer.cpp
|
||||||
object/internal/shadow_repository.cpp
|
|
||||||
object/object.cpp
|
object/object.cpp
|
||||||
object/object_generator.cpp
|
object/object_generator.cpp
|
||||||
object/observer.cpp
|
object/observer.cpp
|
||||||
|
|
@ -110,6 +108,8 @@ add_library(matador-core STATIC
|
||||||
utils/version.cpp
|
utils/version.cpp
|
||||||
../../include/matador/object/internal/observer_list_creator.hpp
|
../../include/matador/object/internal/observer_list_creator.hpp
|
||||||
../../include/matador/object/internal/observer_list_copy_creator.hpp
|
../../include/matador/object/internal/observer_list_copy_creator.hpp
|
||||||
|
../../include/matador/object/basic_repository.hpp
|
||||||
|
object/basic_repository.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(matador-core ${CMAKE_DL_LIBS})
|
target_link_libraries(matador-core ${CMAKE_DL_LIBS})
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,8 @@ void attribute::name( const std::string& n ) {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string attribute::full_name() const {
|
std::string attribute::full_name() const {
|
||||||
return owner_ ? owner_->name() + "." + name_ : name_;
|
const auto owner = owner_.lock();
|
||||||
|
return owner ? owner->name() + "." + name_ : name_;
|
||||||
}
|
}
|
||||||
|
|
||||||
const utils::field_attributes &attribute::attributes() const {
|
const utils::field_attributes &attribute::attributes() const {
|
||||||
|
|
@ -48,7 +49,7 @@ utils::basic_type attribute::type() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<object> attribute::owner() const {
|
std::shared_ptr<object> attribute::owner() const {
|
||||||
return owner_;
|
return owner_.lock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void attribute::change_type(const utils::basic_type type, const utils::field_attributes& attr) {
|
void attribute::change_type(const utils::basic_type type, const utils::field_attributes& attr) {
|
||||||
|
|
|
||||||
|
|
@ -5,16 +5,16 @@
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
namespace matador::object {
|
namespace matador::object {
|
||||||
basic_object_info::basic_object_info(std::shared_ptr<repository_node> node, std::shared_ptr<class object>&& obj)
|
basic_object_info::basic_object_info(const repository_node& node, const std::shared_ptr<class object>& obj)
|
||||||
: object_(std::move(obj))
|
: object_(obj)
|
||||||
, node_(std::move(node)) {}
|
, node_(node) {}
|
||||||
|
|
||||||
std::type_index basic_object_info::type_index() const {
|
std::type_index basic_object_info::type_index() const {
|
||||||
return node_->type_index();
|
return node_.type_index();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string basic_object_info::name() const {
|
std::string basic_object_info::name() const {
|
||||||
return node_->name();
|
return node_.name();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<class object> basic_object_info::object() const {
|
std::shared_ptr<class object> basic_object_info::object() const {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,274 @@
|
||||||
|
#include "matador/object/basic_repository.hpp"
|
||||||
|
#include "matador/object/repository_node.hpp"
|
||||||
|
#include "matador/object/repository_node_iterator.hpp"
|
||||||
|
|
||||||
|
#include "matador/logger/log_manager.hpp"
|
||||||
|
|
||||||
|
#include <stack>
|
||||||
|
|
||||||
|
namespace matador::object {
|
||||||
|
utils::error make_error(const error_code ec, const std::string &msg) {
|
||||||
|
return utils::error(ec, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
basic_repository::basic_repository(std::string name)
|
||||||
|
: name_(std::move(name))
|
||||||
|
, root_(new repository_node(*this))
|
||||||
|
, log_(logger::create_logger("schema")) {
|
||||||
|
root_->first_child_ = std::make_unique<repository_node>(*this);
|
||||||
|
root_->last_child_ = std::make_unique<repository_node>(*this);
|
||||||
|
root_->first_child_->next_sibling_ = root_->last_child_.get();
|
||||||
|
root_->last_child_->previous_sibling_ = root_->first_child_.get();
|
||||||
|
root_->info_ = std::make_unique<null_info>(*root_);
|
||||||
|
}
|
||||||
|
|
||||||
|
basic_repository::~basic_repository() {
|
||||||
|
if (!root_) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
while (root_->first_child_->next_sibling_ != root_->last_child_.get()) {
|
||||||
|
remove_node(root_->first_child_->next_sibling_);
|
||||||
|
}
|
||||||
|
delete root_;
|
||||||
|
}
|
||||||
|
|
||||||
|
utils::result<void, utils::error> basic_repository::detach(repository_node *node) {
|
||||||
|
log_.debug("detach node '%s' (type: %s)", node->name().c_str(), node->type_index().name());
|
||||||
|
|
||||||
|
remove_node(node);
|
||||||
|
|
||||||
|
node->on_detach();
|
||||||
|
|
||||||
|
return utils::ok<void>();
|
||||||
|
}
|
||||||
|
|
||||||
|
basic_repository::const_iterator basic_repository::begin() const {
|
||||||
|
return const_iterator(root_->first_child_->next_sibling_);
|
||||||
|
}
|
||||||
|
|
||||||
|
basic_repository::const_iterator basic_repository::end() const {
|
||||||
|
return const_iterator(root_->last_child_.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
bool basic_repository::empty() const {
|
||||||
|
return root_->first_child_.get() == root_->last_child_->previous_sibling_;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t basic_repository::size() const {
|
||||||
|
return static_cast<size_t>(std::distance(begin(), end()));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string basic_repository::name() const {
|
||||||
|
return name_;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool basic_repository::contains( const std::string& name ) const {
|
||||||
|
return nodes_by_name_.count(name) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool basic_repository::contains( const std::type_index& index ) const {
|
||||||
|
return nodes_by_type_.count(index) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
utils::result<basic_object_info_ref, utils::error> basic_repository::basic_info(const std::type_index &ti) const {
|
||||||
|
const auto it = find_node(ti);
|
||||||
|
if (it == end()) {
|
||||||
|
return utils::failure(make_error(error_code::NodeNotFound, "Node '" + std::string(ti.name()) + "' not found."));
|
||||||
|
}
|
||||||
|
|
||||||
|
return utils::ok(basic_object_info_ref{it->info()});
|
||||||
|
}
|
||||||
|
|
||||||
|
utils::result<basic_object_info_ref, utils::error> basic_repository::basic_info(const std::string& name) const {
|
||||||
|
const auto it = find_node(name);
|
||||||
|
if (it == end()) {
|
||||||
|
return utils::failure(make_error(error_code::NodeNotFound, "Node '" + name + "' not found."));
|
||||||
|
}
|
||||||
|
|
||||||
|
return utils::ok(basic_object_info_ref{it->info()});
|
||||||
|
}
|
||||||
|
|
||||||
|
utils::result<attribute*, utils::error> basic_repository::primary_key_attribute(const std::type_index &ti) const {
|
||||||
|
const auto it = find_node(ti);
|
||||||
|
if (it == end()) {
|
||||||
|
return utils::failure(make_error(error_code::NodeNotFound, "Node '" + std::string(ti.name()) + "' not found."));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!it->info().has_primary_key()) {
|
||||||
|
return utils::failure(make_error(error_code::NodeAlreadyExists, "Object '" + it->name() + "' does not have a primary key."));
|
||||||
|
}
|
||||||
|
return utils::ok(it->info().primary_key_attribute());
|
||||||
|
}
|
||||||
|
|
||||||
|
void basic_repository::dump(std::ostream &os) const {
|
||||||
|
for (const auto &node : *this) {
|
||||||
|
dump(os, node);
|
||||||
|
}
|
||||||
|
os << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void basic_repository::dump( std::ostream& os, const repository_node& node ) {
|
||||||
|
os << "node [" << node.name() << "] (" << node.type_index().name() << ")\n";
|
||||||
|
for (auto it = node.info().endpoint_begin(); it != node.info().endpoint_end(); ++it) {
|
||||||
|
os << " " << node.name() << "::" << it->second->field_name() << " (" << it->second->type_name() << ")";
|
||||||
|
if (it->second->foreign_endpoint()) {
|
||||||
|
os << " <---> " << it->second->node().name() << "::" << it->second->foreign_endpoint()->field_name() << " (" << it->second->foreign_endpoint()->type_name() << ")\n";
|
||||||
|
} else {
|
||||||
|
os << " -> " << it->second->node().name() << " (type: " << it->second->node().type_index().name() << ")\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
utils::result<repository_node*, utils::error> basic_repository::attach_node(repository_node *node, const std::string &parent) {
|
||||||
|
if (has_node(node)) {
|
||||||
|
return utils::failure(make_error(error_code::NodeAlreadyExists, "Node '" + node->name() + "' already exists."));
|
||||||
|
}
|
||||||
|
|
||||||
|
log_.info("attach: insert node '%s' (type: %s)", node->name().c_str(), node->type_index().name());
|
||||||
|
|
||||||
|
// set node to root node
|
||||||
|
auto parent_node = root_;
|
||||||
|
if (!parent.empty()) {
|
||||||
|
const auto result = find_node(parent);
|
||||||
|
if (result == end()) {
|
||||||
|
return utils::failure(make_error(error_code::NodeNotFound, "Parent node '" + parent + "' not found."));
|
||||||
|
}
|
||||||
|
parent_node = result.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
insert_node(parent_node, node);
|
||||||
|
|
||||||
|
// Todo: check return value
|
||||||
|
nodes_by_name_.insert({node->name(), node});
|
||||||
|
nodes_by_type_.insert({node->type_index(), node});
|
||||||
|
|
||||||
|
node->on_attach();
|
||||||
|
|
||||||
|
return utils::ok(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
basic_repository::const_iterator basic_repository::find_node(const std::string &name) const {
|
||||||
|
// first search in the prototype map
|
||||||
|
const auto i = nodes_by_name_.find(name);
|
||||||
|
if (i == nodes_by_name_.end()) {
|
||||||
|
return end();
|
||||||
|
}
|
||||||
|
return const_iterator(i->second);
|
||||||
|
}
|
||||||
|
|
||||||
|
basic_repository::const_iterator basic_repository::find_node(const std::type_index &type_index) const {
|
||||||
|
const auto i = nodes_by_type_.find(type_index);
|
||||||
|
if (i == nodes_by_type_.end()) {
|
||||||
|
return end();
|
||||||
|
}
|
||||||
|
return const_iterator(i->second);
|
||||||
|
}
|
||||||
|
|
||||||
|
void basic_repository::insert_node(repository_node *parent, repository_node *child) {
|
||||||
|
child->parent_ = parent;
|
||||||
|
child->previous_sibling_ = parent->last_child_->previous_sibling_;
|
||||||
|
child->next_sibling_ = parent->last_child_.get();
|
||||||
|
/*
|
||||||
|
* +-----------------------------<- (first) parent (last) -> ----------------------------+
|
||||||
|
* | |
|
||||||
|
* first (next) -> <- (prev) child_1 (next) -> <- (prev) new_child (next) -> <- (prev) last
|
||||||
|
* ^^^^^^^ inserted ^^^^^^
|
||||||
|
*/
|
||||||
|
if (const auto prev_sib = parent->last_child_->previous_sibling_) {
|
||||||
|
prev_sib->next_sibling_ = child;
|
||||||
|
}
|
||||||
|
parent->last_child_->previous_sibling_ = child;
|
||||||
|
// set depth
|
||||||
|
// child->depth = depth + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void basic_repository::remove_node(repository_node *node) {
|
||||||
|
// auto next = node->next();
|
||||||
|
|
||||||
|
std::stack<repository_node*> nodes_to_remove;
|
||||||
|
nodes_to_remove.push(node);
|
||||||
|
|
||||||
|
while (!nodes_to_remove.empty()) {
|
||||||
|
const auto current = nodes_to_remove.top();
|
||||||
|
|
||||||
|
if (current->has_children()) {
|
||||||
|
// Push all children to the stack (from right to left to maintain order)
|
||||||
|
auto child = current->last_child_->previous_sibling_;
|
||||||
|
while (child && child != current->first_child_.get()) {
|
||||||
|
nodes_to_remove.push(child);
|
||||||
|
child = child->previous_sibling_;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// No children left, safe to remove this node
|
||||||
|
nodes_to_remove.pop();
|
||||||
|
current->unlink();
|
||||||
|
nodes_by_name_.erase(current->name());
|
||||||
|
nodes_by_type_.erase(current->type_index());
|
||||||
|
delete current;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool basic_repository::has_node(const std::string &name) const {
|
||||||
|
return nodes_by_name_.count(name) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool basic_repository::has_node(const std::type_index &index) const {
|
||||||
|
return nodes_by_type_.count(index) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool basic_repository::has_node(const repository_node* node) const {
|
||||||
|
return nodes_by_name_.count(node->name()) > 0 || nodes_by_type_.count(node->type_index()) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool basic_repository::expecting_relation_node( const std::string& name ) const {
|
||||||
|
return expected_relation_nodes_.count(name) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void basic_repository::expect_relation_node(const std::string &name, const std::type_index &ti) {
|
||||||
|
expected_relation_nodes_.insert({name, ti});
|
||||||
|
}
|
||||||
|
|
||||||
|
void basic_repository::remove_expected_relation_node( const std::string& name ) {
|
||||||
|
expected_relation_nodes_.erase(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<object> basic_repository::provide_object_in_advance(const std::type_index &ti, const std::shared_ptr<object>& obj) {
|
||||||
|
return object_by_type_.insert({ti, obj}).first->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool basic_repository::has_object_for_type(const std::type_index &ti) const {
|
||||||
|
return object_by_type_.count(ti) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<object> basic_repository::object_for_type(const std::type_index &ti) const {
|
||||||
|
return object_by_type_.at(ti);
|
||||||
|
}
|
||||||
|
|
||||||
|
void basic_repository::remove_object_for_type(const std::type_index &ti) {
|
||||||
|
object_by_type_.erase(ti);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool basic_repository::is_node_announced(const std::type_index &ti) const {
|
||||||
|
return announced_node_.count(ti) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void basic_repository::push_announce_node(const std::type_index &ti, std::unique_ptr<repository_node> &&node) {
|
||||||
|
announced_node_.insert({ti, std::move(node)});
|
||||||
|
}
|
||||||
|
|
||||||
|
repository_node* basic_repository::announce_node(const std::type_index &ti) const {
|
||||||
|
return announced_node_.find(ti)->second.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<repository_node> basic_repository::pop_announce_node(const std::type_index &ti) {
|
||||||
|
const auto it = announced_node_.find(ti);
|
||||||
|
if (it == announced_node_.end()) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
auto node = std::move(it->second);
|
||||||
|
announced_node_.erase(it);
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,81 +0,0 @@
|
||||||
#include "matador/object/internal/shadow_repository.hpp"
|
|
||||||
|
|
||||||
#include "matador/object/repository.hpp"
|
|
||||||
#include "matador/object/repository_node.hpp"
|
|
||||||
|
|
||||||
namespace matador::object::internal {
|
|
||||||
shadow_repository::shadow_repository(repository& s)
|
|
||||||
: repo_(s) {}
|
|
||||||
|
|
||||||
repository& shadow_repository::repo() const {
|
|
||||||
return repo_;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool shadow_repository::contains( const std::type_index& ti ) const {
|
|
||||||
return repo_.contains(ti);
|
|
||||||
}
|
|
||||||
|
|
||||||
utils::result<basic_object_info_ref, utils::error> shadow_repository::basic_info(const std::type_index& ti) const {
|
|
||||||
return repo_.basic_info(ti);
|
|
||||||
}
|
|
||||||
|
|
||||||
utils::result<shadow_repository::node_ptr, utils::error> shadow_repository::find_node( const std::type_index& type_index ) const {
|
|
||||||
return repo_.find_node(type_index);
|
|
||||||
}
|
|
||||||
|
|
||||||
utils::result<shadow_repository::node_ptr, utils::error> shadow_repository::find_node( const std::string& name ) const {
|
|
||||||
return repo_.find_node(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
utils::result<shadow_repository::node_ptr, utils::error> shadow_repository::attach_node( const std::shared_ptr<repository_node>& node ) const {
|
|
||||||
return repo_.attach_node(node, "");
|
|
||||||
}
|
|
||||||
|
|
||||||
utils::result<void, utils::error> shadow_repository::detach_node( const std::shared_ptr<repository_node>& node ) const {
|
|
||||||
return repo_.detach(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool shadow_repository::expecting_relation_node(const std::string &name) const {
|
|
||||||
return repo_.expecting_relation_node(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
void shadow_repository::expect_relation_node(const std::string &name, const std::type_index &ti) const {
|
|
||||||
repo_.expect_relation_node(name, ti);
|
|
||||||
}
|
|
||||||
|
|
||||||
void shadow_repository::remove_expected_relation_node(const std::string &name) const {
|
|
||||||
repo_.remove_expected_relation_node(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<object> shadow_repository::provide_object_in_advance(const std::type_index& ti, const std::shared_ptr<object>& obj) const {
|
|
||||||
return repo_.provide_object_in_advance(ti, obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool shadow_repository::has_object_for_type(const std::type_index& ti) const {
|
|
||||||
return repo_.has_object_for_type(ti);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<object> shadow_repository::object_for_type(const std::type_index& ti) const {
|
|
||||||
return repo_.object_for_type(ti);
|
|
||||||
}
|
|
||||||
|
|
||||||
void shadow_repository::remove_object_for_type(const std::type_index& ti) const {
|
|
||||||
repo_.remove_object_for_type(ti);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool shadow_repository::is_node_announced(const std::type_index &ti) const {
|
|
||||||
return repo_.is_node_announced(ti);
|
|
||||||
}
|
|
||||||
|
|
||||||
void shadow_repository::push_announce_node(const std::type_index &ti, const node_ptr &node) const {
|
|
||||||
repo_.push_announce_node(ti, node);
|
|
||||||
}
|
|
||||||
|
|
||||||
shadow_repository::node_ptr shadow_repository::announce_node(const std::type_index &ti) const {
|
|
||||||
return repo_.announce_node(ti);
|
|
||||||
}
|
|
||||||
|
|
||||||
shadow_repository::node_ptr shadow_repository::pop_announce_node(const std::type_index &ti) const {
|
|
||||||
return repo_.pop_announce_node(ti);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -5,11 +5,11 @@
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
namespace matador::object {
|
namespace matador::object {
|
||||||
object_generator::object_generator(const repository& repo, const std::shared_ptr<object>& object)
|
object_generator::object_generator(basic_repository& repo, const std::shared_ptr<object>& object)
|
||||||
: repo_(repo)
|
: repo_(repo)
|
||||||
, object_(object) {}
|
, object_(object) {}
|
||||||
|
|
||||||
std::shared_ptr<object> object_generator::acquire_object(repository &repo, const std::type_index &ti, const std::string& name) {
|
std::shared_ptr<object> object_generator::acquire_object(basic_repository &repo, const std::type_index &ti, const std::string& name) {
|
||||||
if (repo.has_object_for_type(ti)) {
|
if (repo.has_object_for_type(ti)) {
|
||||||
auto obj = repo.object_for_type(ti);
|
auto obj = repo.object_for_type(ti);
|
||||||
repo.remove_object_for_type(ti);
|
repo.remove_object_for_type(ti);
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,10 @@
|
||||||
#include "matador/object/repository_node.hpp"
|
#include "matador/object/repository_node.hpp"
|
||||||
|
|
||||||
namespace matador::object {
|
namespace matador::object {
|
||||||
relation_endpoint::relation_endpoint(std::string field_name, const relation_type type, const std::shared_ptr<repository_node> &node)
|
relation_endpoint::relation_endpoint(std::string field_name, const relation_type type, repository_node &node)
|
||||||
: field_name_(std::move(field_name))
|
: field_name_(std::move(field_name))
|
||||||
, type_(type)
|
, type_(type)
|
||||||
, node_(node) {
|
, node_(&node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string relation_endpoint::field_name() const {
|
std::string relation_endpoint::field_name() const {
|
||||||
|
|
@ -25,10 +25,6 @@ const repository_node &relation_endpoint::node() const {
|
||||||
return *node_;
|
return *node_;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<repository_node> relation_endpoint::node_ptr() const {
|
|
||||||
return node_;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool relation_endpoint::is_has_one() const {
|
bool relation_endpoint::is_has_one() const {
|
||||||
return type_ == relation_type::HasOne;
|
return type_ == relation_type::HasOne;
|
||||||
}
|
}
|
||||||
|
|
@ -42,7 +38,7 @@ bool relation_endpoint::is_belongs_to() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<relation_endpoint> relation_endpoint::foreign_endpoint() const {
|
std::shared_ptr<relation_endpoint> relation_endpoint::foreign_endpoint() const {
|
||||||
return foreign_endpoint_;
|
return foreign_endpoint_.lock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void relation_endpoint::link_foreign_endpoint( const std::shared_ptr<relation_endpoint>& endpoint ) {
|
void relation_endpoint::link_foreign_endpoint( const std::shared_ptr<relation_endpoint>& endpoint ) {
|
||||||
|
|
|
||||||
|
|
@ -1,255 +0,0 @@
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
#include "matador/object/repository.hpp"
|
|
||||||
|
|
||||||
namespace matador::object {
|
|
||||||
utils::error make_error(const error_code ec, const std::string &msg) {
|
|
||||||
return utils::error(ec, msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
repository::repository(std::string name)
|
|
||||||
: name_(std::move(name))
|
|
||||||
, root_(repository_node::make_null_node(*this))
|
|
||||||
, log_(logger::create_logger("schema")) {
|
|
||||||
root_->first_child_ = std::shared_ptr<repository_node>(new repository_node(*this));
|
|
||||||
root_->last_child_ = std::shared_ptr<repository_node>(new repository_node(*this));
|
|
||||||
root_->first_child_->next_sibling_ = root_->last_child_;
|
|
||||||
root_->last_child_->previous_sibling_ = root_->first_child_;
|
|
||||||
}
|
|
||||||
|
|
||||||
utils::result<void, utils::error> repository::detach(const node_ptr &node) {
|
|
||||||
log_.debug("detach node '%s' (type: %s)", node->name().c_str(), node->type_index().name());
|
|
||||||
|
|
||||||
remove_node(node);
|
|
||||||
|
|
||||||
node->on_detach();
|
|
||||||
|
|
||||||
return utils::ok<void>();
|
|
||||||
}
|
|
||||||
|
|
||||||
repository::const_iterator repository::begin() const {
|
|
||||||
return const_iterator(root_->first_child_->next_sibling_);
|
|
||||||
}
|
|
||||||
|
|
||||||
repository::const_iterator repository::end() const {
|
|
||||||
return const_iterator(root_->last_child_);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool repository::empty() const {
|
|
||||||
return root_->first_child_ == root_->last_child_->previous_sibling_;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t repository::size() const {
|
|
||||||
return static_cast<size_t>(std::distance(begin(), end()));
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string repository::name() const {
|
|
||||||
return name_;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool repository::contains( const std::string& name ) const {
|
|
||||||
return nodes_by_name_.count(name) > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool repository::contains( const std::type_index& index ) const {
|
|
||||||
return nodes_by_type_.count(index) > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
utils::result<basic_object_info_ref, utils::error> repository::basic_info(const std::type_index &ti) const {
|
|
||||||
auto result = find_node(ti);
|
|
||||||
if (!result) {
|
|
||||||
return utils::failure(result.err());
|
|
||||||
}
|
|
||||||
|
|
||||||
return utils::ok(basic_object_info_ref{result.value()->info()});
|
|
||||||
}
|
|
||||||
|
|
||||||
utils::result<basic_object_info_ref, utils::error> repository::basic_info(const std::string& name) const {
|
|
||||||
auto result = find_node(name);
|
|
||||||
if (!result) {
|
|
||||||
return utils::failure(result.err());
|
|
||||||
}
|
|
||||||
|
|
||||||
return utils::ok(basic_object_info_ref{result.value()->info()});
|
|
||||||
}
|
|
||||||
|
|
||||||
utils::result<attribute*, utils::error> repository::primary_key_attribute(const std::type_index &type_index) const {
|
|
||||||
const auto result = find_node(type_index);
|
|
||||||
if (!result) {
|
|
||||||
return utils::failure(result.err());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!result.value()->info().has_primary_key()) {
|
|
||||||
return utils::failure(make_error(error_code::NodeAlreadyExists, "Object '" + result.value()->name() + "' does not have a primary key."));
|
|
||||||
}
|
|
||||||
return utils::ok((*result)->info().primary_key_attribute());
|
|
||||||
}
|
|
||||||
|
|
||||||
void repository::dump(std::ostream &os) const {
|
|
||||||
for (const auto &node : *this) {
|
|
||||||
dump(os, node);
|
|
||||||
}
|
|
||||||
os << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
void repository::dump( std::ostream& os, const node_ptr& node ) {
|
|
||||||
os << "node [" << node->name() << "] (" << node->type_index().name() << ")\n";
|
|
||||||
for (auto it = node->info().endpoint_begin(); it != node->info().endpoint_end(); ++it) {
|
|
||||||
os << " " << node->name() << "::" << it->second->field_name() << " (" << it->second->type_name() << ")";
|
|
||||||
if (it->second->foreign_endpoint()) {
|
|
||||||
os << " <---> " << it->second->node().name() << "::" << it->second->foreign_endpoint()->field_name() << " (" << it->second->foreign_endpoint()->type_name() << ")\n";
|
|
||||||
} else {
|
|
||||||
os << " -> " << it->second->node().name() << " (type: " << it->second->node().type_index().name() << ")\n";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
utils::result<repository::node_ptr, utils::error> repository::attach_node(const std::shared_ptr<repository_node> &node, const std::string &parent) {
|
|
||||||
if (has_node(node)) {
|
|
||||||
return utils::failure(make_error(error_code::NodeAlreadyExists, "Node '" + node->name() + "' already exists."));
|
|
||||||
}
|
|
||||||
|
|
||||||
log_.info("attach: insert node '%s' (type: %s)", node->name().c_str(), node->type_index().name());
|
|
||||||
|
|
||||||
// set node to root node
|
|
||||||
auto parent_node = root_;
|
|
||||||
if (!parent.empty()) {
|
|
||||||
auto result = find_node(parent);
|
|
||||||
if (!result.is_ok() && result.err().ec() != error_code::NodeNotFound) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
parent_node = *result;
|
|
||||||
}
|
|
||||||
|
|
||||||
insert_node(parent_node, node);
|
|
||||||
|
|
||||||
// Todo: check return value
|
|
||||||
nodes_by_name_.insert({node->name(), node});
|
|
||||||
nodes_by_type_.insert({node->type_index(), node});
|
|
||||||
|
|
||||||
node->on_attach();
|
|
||||||
|
|
||||||
return utils::ok(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
utils::result<repository::node_ptr, utils::error> repository::find_node(const std::string &name) const {
|
|
||||||
// first search in the prototype map
|
|
||||||
const auto i = nodes_by_name_.find(name);
|
|
||||||
if (i == nodes_by_name_.end()) {
|
|
||||||
return utils::failure(make_error(error_code::NodeNotFound, "Couldn't find node by name '" + name + "'"));
|
|
||||||
}
|
|
||||||
return utils::ok(i->second);
|
|
||||||
}
|
|
||||||
|
|
||||||
utils::result<repository::node_ptr, utils::error> repository::find_node(const std::type_index &type_index) const {
|
|
||||||
const auto i = nodes_by_type_.find(type_index);
|
|
||||||
if (i == nodes_by_type_.end()) {
|
|
||||||
return utils::failure(make_error(error_code::NodeNotFound,
|
|
||||||
"Couldn't find node by type '" + std::string(type_index.name()) + "'"));
|
|
||||||
}
|
|
||||||
return utils::ok(i->second);
|
|
||||||
}
|
|
||||||
|
|
||||||
void repository::insert_node(const node_ptr &parent, const node_ptr &child) {
|
|
||||||
child->parent_ = parent;
|
|
||||||
child->previous_sibling_ = parent->last_child_->previous_sibling_;
|
|
||||||
child->next_sibling_ = parent->last_child_;
|
|
||||||
/*
|
|
||||||
* +-----------------------------<- (first) parent (last) -> ----------------------------+
|
|
||||||
* | |
|
|
||||||
* first (next) -> <- (prev) child_1 (next) -> <- (prev) new_child (next) -> <- (prev) last
|
|
||||||
* ^^^^^^^ inserted ^^^^^^
|
|
||||||
*/
|
|
||||||
parent->last_child_->previous_sibling_->next_sibling_ = child;
|
|
||||||
parent->last_child_->previous_sibling_ = child;
|
|
||||||
// set depth
|
|
||||||
// child->depth = depth + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void repository::remove_node(const node_ptr &node) {
|
|
||||||
auto next = node->next();
|
|
||||||
|
|
||||||
std::stack<node_ptr> nodes_to_remove;
|
|
||||||
nodes_to_remove.push(node);
|
|
||||||
|
|
||||||
while (!nodes_to_remove.empty()) {
|
|
||||||
const auto current = nodes_to_remove.top();
|
|
||||||
|
|
||||||
if (current->has_children()) {
|
|
||||||
// Push all children to the stack (from right to left to maintain order)
|
|
||||||
auto child = current->last_child_->previous_sibling_;
|
|
||||||
while (child != current->first_child_) {
|
|
||||||
nodes_to_remove.push(child);
|
|
||||||
child = child->previous_sibling_;
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// No children left, safe to remove this node
|
|
||||||
nodes_to_remove.pop();
|
|
||||||
current->unlink();
|
|
||||||
nodes_by_name_.erase(current->name());
|
|
||||||
nodes_by_type_.erase(current->type_index());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool repository::has_node(const std::string &name) const {
|
|
||||||
return nodes_by_name_.count(name) > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool repository::has_node(const std::type_index &index) const {
|
|
||||||
return nodes_by_type_.count(index) > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool repository::has_node(const node_ptr& node) const {
|
|
||||||
return nodes_by_name_.count(node->name()) > 0 || nodes_by_type_.count(node->type_index()) > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool repository::expecting_relation_node( const std::string& name ) const {
|
|
||||||
return expected_relation_nodes_.count(name) > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void repository::expect_relation_node(const std::string &name, const std::type_index &ti) {
|
|
||||||
expected_relation_nodes_.insert({name, ti});
|
|
||||||
}
|
|
||||||
|
|
||||||
void repository::remove_expected_relation_node( const std::string& name ) {
|
|
||||||
expected_relation_nodes_.erase(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<object> repository::provide_object_in_advance(const std::type_index &ti, const std::shared_ptr<object>& obj) {
|
|
||||||
return object_by_type_.insert({ti, obj}).first->second;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool repository::has_object_for_type(const std::type_index &ti) const {
|
|
||||||
return object_by_type_.count(ti) > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<object> repository::object_for_type(const std::type_index &ti) const {
|
|
||||||
return object_by_type_.at(ti);
|
|
||||||
}
|
|
||||||
|
|
||||||
void repository::remove_object_for_type(const std::type_index &ti) {
|
|
||||||
object_by_type_.erase(ti);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool repository::is_node_announced(const std::type_index &ti) const {
|
|
||||||
return announced_node_.count(ti) > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void repository::push_announce_node(const std::type_index &ti, const node_ptr &node) {
|
|
||||||
announced_node_.insert({ti, node});
|
|
||||||
}
|
|
||||||
|
|
||||||
repository::node_ptr repository::announce_node(const std::type_index &ti) {
|
|
||||||
return announced_node_.find(ti)->second;
|
|
||||||
}
|
|
||||||
|
|
||||||
repository::node_ptr repository::pop_announce_node(const std::type_index &ti) {
|
|
||||||
const auto it = announced_node_.find(ti);
|
|
||||||
if (it != announced_node_.end()) {
|
|
||||||
announced_node_.erase(it);
|
|
||||||
}
|
|
||||||
return it->second;
|
|
||||||
}
|
|
||||||
} // namespace matador::object
|
|
||||||
|
|
@ -1,34 +1,28 @@
|
||||||
|
#include <memory>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
#include "matador/object/repository.hpp"
|
#include "matador/object/basic_repository.hpp"
|
||||||
#include "matador/object/repository_node.hpp"
|
#include "matador/object/repository_node.hpp"
|
||||||
|
|
||||||
namespace matador::object {
|
namespace matador::object {
|
||||||
repository_node::repository_node(repository &repo)
|
repository_node::repository_node(basic_repository &repo)
|
||||||
: repo_(repo)
|
: repo_(repo)
|
||||||
, type_index_(typeid(detail::null_type)){
|
, type_index_(typeid(detail::null_type)){
|
||||||
}
|
}
|
||||||
|
|
||||||
repository_node::repository_node(repository &repo, const std::type_index& ti)
|
repository_node::repository_node(basic_repository &repo, const std::type_index& ti)
|
||||||
: repo_(repo)
|
: repo_(repo)
|
||||||
, type_index_(ti) {
|
, type_index_(ti) {
|
||||||
}
|
}
|
||||||
|
|
||||||
repository_node::repository_node(repository &repo, std::string name, const std::type_index& ti)
|
repository_node::repository_node(basic_repository &repo, std::string name, const std::type_index& ti)
|
||||||
: repo_(repo)
|
: repo_(repo)
|
||||||
, type_index_(ti)
|
, type_index_(ti)
|
||||||
, first_child_(std::shared_ptr<repository_node>(new repository_node(repo)))
|
, first_child_(std::make_unique<repository_node>(repo))
|
||||||
, last_child_(std::shared_ptr<repository_node>(new repository_node(repo)))
|
, last_child_(std::make_unique<repository_node>(repo))
|
||||||
, name_(std::move(name)) {
|
, name_(std::move(name)) {
|
||||||
first_child_->next_sibling_ = last_child_;
|
first_child_->next_sibling_ = last_child_.get();
|
||||||
last_child_->previous_sibling_ = first_child_;
|
last_child_->previous_sibling_ = first_child_.get();
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<repository_node> repository_node::make_null_node(repository &repo) {
|
|
||||||
auto node = std::shared_ptr<repository_node>(new repository_node(repo));
|
|
||||||
node->info_ = std::make_unique<null_info>(node);
|
|
||||||
|
|
||||||
return node;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string repository_node::name() const {
|
std::string repository_node::name() const {
|
||||||
|
|
@ -48,32 +42,35 @@ void repository_node::update_name(const std::string& name) {
|
||||||
info_->update_name(name);
|
info_->update_name(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
const repository& repository_node::schema() const {
|
const basic_repository& repository_node::schema() const {
|
||||||
return repo_;
|
return repo_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool repository_node::has_children() const {
|
bool repository_node::has_children() const {
|
||||||
return first_child_->next_sibling_ != last_child_;
|
return first_child_->next_sibling_ != last_child_.get();
|
||||||
}
|
}
|
||||||
void repository_node::on_attach() const {
|
void repository_node::on_attach() const {
|
||||||
info_->on_attach();
|
info_->on_attach();
|
||||||
}
|
}
|
||||||
|
|
||||||
void repository_node::on_detach() const {}
|
void repository_node::on_detach() const {
|
||||||
|
info_->on_detach();
|
||||||
|
}
|
||||||
|
|
||||||
repository_node::node_ptr repository_node::next() const {
|
repository_node::node_ptr repository_node::next() const {
|
||||||
// if we have a child, child is the next iterator to return
|
// if we have a child, child is the next iterator to return
|
||||||
// (if we don't iterate over the siblings)
|
// (if we don't iterate over the siblings)
|
||||||
if (first_child_ && first_child_->next_sibling_ != last_child_) {
|
if (first_child_ && first_child_->next_sibling_ != last_child_.get()) {
|
||||||
return first_child_->next_sibling_;
|
return first_child_->next_sibling_;
|
||||||
}
|
}
|
||||||
// if there is no child, we check for sibling
|
// if there is no child, we check for sibling
|
||||||
// if there is a sibling, this is our next iterator to return
|
// if there is a sibling, this is our next iterator to return
|
||||||
// if not, we go back to the parent
|
// if not, we go back to the parent
|
||||||
auto *node = this;
|
auto *node = this;
|
||||||
while (node->parent_ && node->next_sibling_ == node->parent_->last_child_) {
|
while (node->parent_ && node->next_sibling_ == node->parent_->last_child_.get()) {
|
||||||
node = node->parent_.get();
|
node = node->parent_;
|
||||||
}
|
}
|
||||||
return node->parent_ ? node->next_sibling_ : node->last_child_;
|
return node->parent_ ? node->next_sibling_ : node->last_child_.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
repository_node::node_ptr repository_node::prev() const {
|
repository_node::node_ptr repository_node::prev() const {
|
||||||
|
|
@ -83,7 +80,7 @@ repository_node::node_ptr repository_node::prev() const {
|
||||||
// child as our iterator
|
// child as our iterator
|
||||||
if (previous_sibling_ && previous_sibling_->previous_sibling_) {
|
if (previous_sibling_ && previous_sibling_->previous_sibling_) {
|
||||||
auto node = previous_sibling_;
|
auto node = previous_sibling_;
|
||||||
while (node->last_child_ && node->first_child_->next_sibling_ != node->last_child_) {
|
while (node->last_child_ && node->first_child_->next_sibling_ != node->last_child_.get()) {
|
||||||
node = node->last_child_->previous_sibling_;
|
node = node->last_child_->previous_sibling_;
|
||||||
}
|
}
|
||||||
return node;
|
return node;
|
||||||
|
|
@ -96,7 +93,7 @@ repository_node::node_ptr repository_node::prev() const {
|
||||||
void repository_node::unlink() {
|
void repository_node::unlink() {
|
||||||
previous_sibling_->next_sibling_ = next_sibling_;
|
previous_sibling_->next_sibling_ = next_sibling_;
|
||||||
next_sibling_->previous_sibling_ = previous_sibling_;
|
next_sibling_->previous_sibling_ = previous_sibling_;
|
||||||
next_sibling_.reset();
|
next_sibling_ = nullptr;
|
||||||
previous_sibling_.reset();
|
previous_sibling_ = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,78 +1,66 @@
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
#include "matador/object/repository_node_iterator.hpp"
|
#include "matador/object/repository_node_iterator.hpp"
|
||||||
|
#include "matador/object/repository_node.hpp"
|
||||||
|
|
||||||
namespace matador::object {
|
namespace matador::object {
|
||||||
|
const_repository_node_iterator::const_repository_node_iterator(value_type* node)
|
||||||
|
: node_(node) {
|
||||||
|
}
|
||||||
|
|
||||||
const_repository_node_iterator::const_repository_node_iterator(const value_type& node)
|
bool const_repository_node_iterator::operator==(const const_repository_node_iterator &i) const {
|
||||||
: node_(node)
|
|
||||||
{}
|
|
||||||
|
|
||||||
bool const_repository_node_iterator::operator==(const const_repository_node_iterator &i) const
|
|
||||||
{
|
|
||||||
return (node_ == i.node_);
|
return (node_ == i.node_);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool const_repository_node_iterator::operator!=(const const_repository_node_iterator &i) const
|
bool const_repository_node_iterator::operator!=(const const_repository_node_iterator &i) const {
|
||||||
{
|
|
||||||
return !operator==(i);
|
return !operator==(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
const_repository_node_iterator& const_repository_node_iterator::operator++()
|
const_repository_node_iterator &const_repository_node_iterator::operator++() {
|
||||||
{
|
|
||||||
increment();
|
increment();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
const_repository_node_iterator const_repository_node_iterator::operator++(int) {
|
const_repository_node_iterator const_repository_node_iterator::operator++(int) {
|
||||||
const value_type tmp = node_;
|
value_type* tmp = node_;
|
||||||
increment();
|
increment();
|
||||||
return const_repository_node_iterator(tmp);
|
return const_repository_node_iterator(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
const_repository_node_iterator& const_repository_node_iterator::operator--()
|
const_repository_node_iterator &const_repository_node_iterator::operator--() {
|
||||||
{
|
|
||||||
decrement();
|
decrement();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
const_repository_node_iterator const_repository_node_iterator::operator--(int)
|
const_repository_node_iterator const_repository_node_iterator::operator--(int) {
|
||||||
{
|
value_type* tmp = node_;
|
||||||
const value_type tmp = node_;
|
|
||||||
decrement();
|
decrement();
|
||||||
return const_repository_node_iterator(tmp);
|
return const_repository_node_iterator(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
const_repository_node_iterator::pointer const_repository_node_iterator::operator->() const
|
const_repository_node_iterator::pointer const_repository_node_iterator::operator->() const {
|
||||||
{
|
|
||||||
return node_.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
const_repository_node_iterator::reference const_repository_node_iterator::operator*() const
|
|
||||||
{
|
|
||||||
return node_;
|
return node_;
|
||||||
}
|
}
|
||||||
|
|
||||||
const_repository_node_iterator::pointer const_repository_node_iterator::get() const
|
const_repository_node_iterator::reference const_repository_node_iterator::operator*() const {
|
||||||
{
|
return *node_;
|
||||||
return node_.get();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void const_repository_node_iterator::increment()
|
const_repository_node_iterator::pointer const_repository_node_iterator::get() const {
|
||||||
{
|
return node_;
|
||||||
|
}
|
||||||
|
|
||||||
|
void const_repository_node_iterator::increment() {
|
||||||
if (!node_) {
|
if (!node_) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
node_ = node_->next();
|
node_ = node_->next();
|
||||||
}
|
}
|
||||||
void const_repository_node_iterator::decrement()
|
|
||||||
{
|
void const_repository_node_iterator::decrement() {
|
||||||
if (!node_) {
|
if (!node_) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
node_ = node_->prev();
|
node_ = node_->prev();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -15,7 +15,7 @@ std::string restriction::column_name() const {
|
||||||
return attr_.name();
|
return attr_.name();
|
||||||
}
|
}
|
||||||
std::shared_ptr<object> restriction::owner() const {
|
std::shared_ptr<object> restriction::owner() const {
|
||||||
return owner_;
|
return owner_.lock();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool restriction::is_primary_key_constraint() const {
|
bool restriction::is_primary_key_constraint() const {
|
||||||
|
|
@ -30,12 +30,14 @@ bool restriction::is_unique_constraint() const {
|
||||||
return utils::is_constraint_set(options_, utils::constraints::Unique);
|
return utils::is_constraint_set(options_, utils::constraints::Unique);
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& restriction::ref_table_name() const {
|
std::string restriction::ref_table_name() const {
|
||||||
return reference_->name();
|
const auto ref = reference_.lock();
|
||||||
|
return ref ? ref->name() : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& restriction::ref_column_name() const {
|
std::string restriction::ref_column_name() const {
|
||||||
return reference_->primary_key_attribute()->name();
|
const auto ref = reference_.lock();
|
||||||
|
return ref ? ref->primary_key_attribute()->name() : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostream & operator<<(std::ostream &os, const class restriction &c) {
|
std::ostream & operator<<(std::ostream &os, const class restriction &c) {
|
||||||
|
|
@ -58,43 +60,4 @@ std::string restriction::type_string() const {
|
||||||
}
|
}
|
||||||
return "Unknown";
|
return "Unknown";
|
||||||
}
|
}
|
||||||
|
|
||||||
// constraint_builder & constraint_builder::constraint(std::string name) {
|
|
||||||
// constraint_name = std::move(name);
|
|
||||||
// return *this;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// constraint_builder & constraint_builder::primary_key(std::string name) {
|
|
||||||
// column_name = std::move(name);
|
|
||||||
// options_ |= utils::constraints::PrimaryKey;
|
|
||||||
// return *this;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// constraint_builder & constraint_builder::foreign_key(std::string name) {
|
|
||||||
// column_name = std::move(name);
|
|
||||||
// options_ |= utils::constraints::ForeignKey;
|
|
||||||
// return *this;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// constraint_builder & constraint_builder::references(std::string table, std::string column) {
|
|
||||||
// ref_table_name = std::move(table);
|
|
||||||
// ref_column_name = std::move(column);
|
|
||||||
//
|
|
||||||
// return *this;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// constraint_builder::operator class restriction() const {
|
|
||||||
// class restriction c;
|
|
||||||
// c.name_ = constraint_name;
|
|
||||||
// c.attr_ = column_name;
|
|
||||||
// c.options_ = options_;
|
|
||||||
// c.ref_column_name_ = ref_column_name;
|
|
||||||
// c.ref_table_name_ = ref_table_name;
|
|
||||||
// return c;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// constraint_builder constraint(std::string name) {
|
|
||||||
// constraint_builder builder;
|
|
||||||
// return builder.constraint(std::move(name));
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,55 @@
|
||||||
#include "matador/utils/basic_types.hpp"
|
#include "matador/utils/basic_types.hpp"
|
||||||
|
|
||||||
namespace matador::utils {
|
namespace matador::utils {
|
||||||
|
// constexpr bool operator==(const date_type_t& a, const date_type_t& b) noexcept
|
||||||
|
|
||||||
void initialize_by_basic_type(const basic_type type, database_type &val)
|
constexpr bool operator!=(const date_type_t &a, const date_type_t &b) noexcept {
|
||||||
{
|
return !(a == b);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr bool operator<(const date_type_t &a, const date_type_t &b) noexcept {
|
||||||
|
return (a.year < b.year) ||
|
||||||
|
(a.year == b.year && a.month < b.month) ||
|
||||||
|
(a.year == b.year && a.month == b.month && a.day < b.day);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr bool operator>(const date_type_t &a, const date_type_t &b) noexcept {
|
||||||
|
return b < a;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr bool operator<=(const date_type_t &a, const date_type_t &b) noexcept {
|
||||||
|
return !(b < a);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr bool operator>=(const date_type_t &a, const date_type_t &b) noexcept {
|
||||||
|
return !(a < b);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr bool operator!=(const time_type_t &a, const time_type_t &b) noexcept {
|
||||||
|
return !(a == b);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr bool operator<(const time_type_t &a, const time_type_t &b) noexcept {
|
||||||
|
return (a.hour < b.hour) ||
|
||||||
|
(a.hour == b.hour && a.minute < b.minute) ||
|
||||||
|
(a.hour == b.hour && a.minute == b.minute && a.second < b.second) ||
|
||||||
|
(a.hour == b.hour && a.minute == b.minute && a.second == b.second &&
|
||||||
|
a.microsecond < b.microsecond);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr bool operator>(const time_type_t &a, const time_type_t &b) noexcept {
|
||||||
|
return b < a;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr bool operator<=(const time_type_t &a, const time_type_t &b) noexcept {
|
||||||
|
return !(b < a);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr bool operator>=(const time_type_t &a, const time_type_t &b) noexcept {
|
||||||
|
return !(a < b);
|
||||||
|
}
|
||||||
|
|
||||||
|
void initialize_by_basic_type(const basic_type type, database_type &val) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case basic_type::Int8:
|
case basic_type::Int8:
|
||||||
val.emplace<int8_t>();
|
val.emplace<int8_t>();
|
||||||
|
|
@ -43,19 +89,21 @@ void initialize_by_basic_type(const basic_type type, database_type &val)
|
||||||
case basic_type::Text:
|
case basic_type::Text:
|
||||||
val.emplace<std::string>();
|
val.emplace<std::string>();
|
||||||
break;
|
break;
|
||||||
// case basic_type::type_date:
|
case basic_type::Date:
|
||||||
// val.emplace<date>();
|
val.emplace<date_type_t>();
|
||||||
// break;
|
break;
|
||||||
// case basic_type::type_time:
|
case basic_type::Time:
|
||||||
// val.emplace<time>();
|
val.emplace<time_type_t>();
|
||||||
// break;
|
break;
|
||||||
|
case basic_type::DateTime:
|
||||||
|
val.emplace<timestamp>();
|
||||||
|
break;
|
||||||
case basic_type::Blob:
|
case basic_type::Blob:
|
||||||
val.emplace<utils::blob>();
|
val.emplace<blob>();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
val.emplace<nullptr_t>();
|
val.emplace<nullptr_t>();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -4,9 +4,6 @@
|
||||||
|
|
||||||
namespace matador::utils {
|
namespace matador::utils {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
void initialize_by_basic_type(basic_type type, database_type &val);
|
|
||||||
|
|
||||||
size_t determine_size(const std::string &val) {
|
size_t determine_size(const std::string &val) {
|
||||||
return val.size();
|
return val.size();
|
||||||
}
|
}
|
||||||
|
|
@ -18,25 +15,22 @@ size_t determine_size(const char *val) {
|
||||||
size_t determine_size(const blob &val) {
|
size_t determine_size(const blob &val) {
|
||||||
return val.size();
|
return val.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
value::value(const basic_type data_type, const size_t size)
|
value::value(const basic_type data_type, const size_t size)
|
||||||
: size_(size)
|
: size_(size)
|
||||||
, type_(data_type)
|
, type_(data_type) {
|
||||||
{
|
|
||||||
initialize_by_basic_type(type_, value_);
|
initialize_by_basic_type(type_, value_);
|
||||||
}
|
}
|
||||||
|
|
||||||
value::value(value &&x) noexcept
|
value::value(value &&x) noexcept
|
||||||
: value_(std::move(x.value_))
|
: value_(std::move(x.value_))
|
||||||
, type_(x.type_)
|
, type_(x.type_) {
|
||||||
{
|
|
||||||
x.value_ = nullptr;
|
x.value_ = nullptr;
|
||||||
x.type_ = basic_type::Null;
|
x.type_ = basic_type::Null;
|
||||||
}
|
}
|
||||||
|
|
||||||
value &value::operator=(value &&x) noexcept
|
value &value::operator=(value &&x) noexcept {
|
||||||
{
|
|
||||||
value_ = std::move(x.value_);
|
value_ = std::move(x.value_);
|
||||||
type_ = x.type_;
|
type_ = x.type_;
|
||||||
x.value_ = nullptr;
|
x.value_ = nullptr;
|
||||||
|
|
@ -45,13 +39,11 @@ value &value::operator=(value &&x) noexcept
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string value::str() const
|
std::string value::str() const {
|
||||||
{
|
|
||||||
return as<std::string>().value_or("");
|
return as<std::string>().value_or("");
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t value::size() const
|
size_t value::size() const {
|
||||||
{
|
|
||||||
return size_;
|
return size_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -61,107 +53,42 @@ basic_type value::type() const {
|
||||||
|
|
||||||
void value::type(const basic_type t) {
|
void value::type(const basic_type t) {
|
||||||
type_ = t;
|
type_ = t;
|
||||||
detail::initialize_by_basic_type(type_, value_);
|
initialize_by_basic_type(type_, value_);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool value::is_integer() const
|
bool value::is_integer() const {
|
||||||
{
|
|
||||||
return type_ >= basic_type::Int8 && type_ <= basic_type::UInt64;
|
return type_ >= basic_type::Int8 && type_ <= basic_type::UInt64;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool value::is_floating_point() const
|
bool value::is_floating_point() const {
|
||||||
{
|
|
||||||
return type_ == basic_type::Float || type_ == basic_type::Double;
|
return type_ == basic_type::Float || type_ == basic_type::Double;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool value::is_bool() const
|
bool value::is_bool() const {
|
||||||
{
|
|
||||||
return type_ == basic_type::Boolean;
|
return type_ == basic_type::Boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool value::is_string() const
|
bool value::is_string() const {
|
||||||
{
|
|
||||||
return type_ == basic_type::Text;
|
return type_ == basic_type::Text;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool value::is_varchar() const
|
bool value::is_varchar() const {
|
||||||
{
|
|
||||||
return type_ == basic_type::Varchar;
|
return type_ == basic_type::Varchar;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool value::is_date() const
|
bool value::is_date() const {
|
||||||
{
|
|
||||||
return type_ == basic_type::Date;
|
return type_ == basic_type::Date;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool value::is_time() const
|
bool value::is_time() const {
|
||||||
{
|
|
||||||
return type_ == basic_type::Time;
|
return type_ == basic_type::Time;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool value::is_blob() const
|
bool value::is_blob() const {
|
||||||
{
|
|
||||||
return type_ == basic_type::Blob;
|
return type_ == basic_type::Blob;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool value::is_null() const
|
bool value::is_null() const {
|
||||||
{
|
|
||||||
return type_ == basic_type::Null;
|
return type_ == basic_type::Null;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace detail {
|
|
||||||
void initialize_by_basic_type(const basic_type type, database_type &val) {
|
|
||||||
switch (type) {
|
|
||||||
case basic_type::Int8:
|
|
||||||
val.emplace<int8_t>();
|
|
||||||
break;
|
|
||||||
case basic_type::Int16:
|
|
||||||
val.emplace<int16_t>();
|
|
||||||
break;
|
|
||||||
case basic_type::Int32:
|
|
||||||
val.emplace<int32_t>();
|
|
||||||
break;
|
|
||||||
case basic_type::Int64:
|
|
||||||
val.emplace<int64_t>();
|
|
||||||
break;
|
|
||||||
case basic_type::UInt8:
|
|
||||||
val.emplace<uint8_t>();
|
|
||||||
break;
|
|
||||||
case basic_type::UInt16:
|
|
||||||
val.emplace<uint16_t>();
|
|
||||||
break;
|
|
||||||
case basic_type::UInt32:
|
|
||||||
val.emplace<uint32_t>();
|
|
||||||
break;
|
|
||||||
case basic_type::UInt64:
|
|
||||||
val.emplace<uint64_t>();
|
|
||||||
break;
|
|
||||||
case basic_type::Boolean:
|
|
||||||
val.emplace<bool>();
|
|
||||||
break;
|
|
||||||
case basic_type::Float:
|
|
||||||
val.emplace<float>();
|
|
||||||
break;
|
|
||||||
case basic_type::Double:
|
|
||||||
val.emplace<double>();
|
|
||||||
break;
|
|
||||||
case basic_type::Varchar:
|
|
||||||
case basic_type::Text:
|
|
||||||
val.emplace<std::string>();
|
|
||||||
break;
|
|
||||||
// case basic_type::type_date:
|
|
||||||
// val.emplace<date>();
|
|
||||||
// break;
|
|
||||||
// case basic_type::type_time:
|
|
||||||
// val.emplace<time>();
|
|
||||||
// break;
|
|
||||||
case basic_type::Blob:
|
|
||||||
val.emplace<utils::blob>();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
val.emplace<nullptr_t>();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
@ -67,12 +67,15 @@ void attribute_string_writer::write_value(size_t /*pos*/, const double& x ) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void attribute_string_writer::write_value(size_t /*pos*/, const time& /*x*/ ) {
|
void attribute_string_writer::write_value(size_t /*pos*/, const utils::date_type_t& /*x*/ ) {
|
||||||
|
// result_ = "'" + dialect_.prepare_literal(utils::to_string(x)) + "'";
|
||||||
|
}
|
||||||
|
|
||||||
|
void attribute_string_writer::write_value(size_t /*pos*/, const utils::time_type_t& /*x*/ ) {
|
||||||
// result_ = "'" + dialect_.prepare_literal(utils::to_string(x, "%F %T.%f")) + "'";
|
// result_ = "'" + dialect_.prepare_literal(utils::to_string(x, "%F %T.%f")) + "'";
|
||||||
}
|
}
|
||||||
|
|
||||||
void attribute_string_writer::write_value(size_t /*pos*/, const date& /*x*/ ) {
|
void attribute_string_writer::write_value(size_t /*pos*/, const utils::timestamp &/*x*/) {
|
||||||
// result_ = "'" + dialect_.prepare_literal(utils::to_string(x)) + "'";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void attribute_string_writer::write_value(size_t pos, const char* x ) {
|
void attribute_string_writer::write_value(size_t pos, const char* x ) {
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
#include "matador/query/generator.hpp"
|
#include "matador/query/generator.hpp"
|
||||||
|
|
||||||
namespace matador::query::generator {
|
namespace matador::query::generator {
|
||||||
column_generator::column_generator(const schema& repo, const std::string& table_name, const column_generator_options options)
|
column_generator::column_generator(const schema& repo, const table* tab, const column_generator_options options)
|
||||||
: repo_(std::cref(repo))
|
: repo_(std::cref(repo))
|
||||||
, options_(options) {
|
, options_(options) {
|
||||||
table_stack_.push(table_name.empty() ? std::make_shared<table>() : std::make_shared<table>(table_name));
|
table_stack_.push(tab);
|
||||||
}
|
}
|
||||||
|
|
||||||
void column_generator::on_revision(const char* id, uint64_t&) {
|
void column_generator::on_revision(const char* id, uint64_t&) {
|
||||||
|
|
@ -15,9 +15,9 @@ void column_generator::push( const std::string& column_name ) {
|
||||||
if (is_column_generator_option_set(options_, column_generator_options::GenerateAlias)) {
|
if (is_column_generator_option_set(options_, column_generator_options::GenerateAlias)) {
|
||||||
char str[4];
|
char str[4];
|
||||||
snprintf(str, 4, "c%02d", ++column_index);
|
snprintf(str, 4, "c%02d", ++column_index);
|
||||||
result_.emplace_back(table_stack_.top().get(), column_name, str);
|
result_.emplace_back(table_stack_.top(), column_name, str);
|
||||||
} else {
|
} else {
|
||||||
result_.emplace_back(table_stack_.top().get(), column_name);
|
result_.emplace_back(table_stack_.top(), column_name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,10 +11,10 @@ query_insert_intermediate::query_insert_intermediate() {
|
||||||
}
|
}
|
||||||
|
|
||||||
query_into_intermediate query_insert_intermediate::into(const table &tab, const std::initializer_list<table_column> columns) {
|
query_into_intermediate query_insert_intermediate::into(const table &tab, const std::initializer_list<table_column> columns) {
|
||||||
return into(tab, std::move(std::vector<table_column>{columns}));
|
return into(tab, {columns});
|
||||||
}
|
}
|
||||||
|
|
||||||
query_into_intermediate query_insert_intermediate::into(const table &tab, std::vector<table_column> &&columns) {
|
query_into_intermediate query_insert_intermediate::into(const table &tab, const std::vector<table_column> &columns) {
|
||||||
context_->parts.push_back(std::make_unique<internal::query_into_part>(tab, columns));
|
context_->parts.push_back(std::make_unique<internal::query_into_part>(tab, columns));
|
||||||
return {context_};
|
return {context_};
|
||||||
}
|
}
|
||||||
|
|
@ -25,7 +25,7 @@ query_into_intermediate query_insert_intermediate::into(const table &tab, const
|
||||||
for (const auto &col_name : column_names) {
|
for (const auto &col_name : column_names) {
|
||||||
columns.emplace_back(col_name);
|
columns.emplace_back(col_name);
|
||||||
}
|
}
|
||||||
return into(tab, std::move(columns));
|
return into(tab, columns);
|
||||||
}
|
}
|
||||||
|
|
||||||
query_into_intermediate query_insert_intermediate::into(const table &tab) {
|
query_into_intermediate query_insert_intermediate::into(const table &tab) {
|
||||||
|
|
|
||||||
|
|
@ -9,8 +9,8 @@ column_value_pair::column_value_pair(std::string name, utils::database_type valu
|
||||||
, value_(std::move(value)) {
|
, value_(std::move(value)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
column_value_pair::column_value_pair(const table_column &col, utils::database_type value)
|
column_value_pair::column_value_pair(table_column col, utils::database_type value)
|
||||||
: column_(col)
|
: column_(std::move(col))
|
||||||
, value_(std::move(value)) {
|
, value_(std::move(value)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -36,6 +36,7 @@ bool operator==( const column_value_pair& lhs, const column_value_pair& rhs ) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator!=( const column_value_pair& lhs, const column_value_pair& rhs ) {
|
bool operator!=( const column_value_pair& lhs, const column_value_pair& rhs ) {
|
||||||
|
using namespace matador::utils;
|
||||||
return !( lhs == rhs );
|
return !( lhs == rhs );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -70,8 +70,8 @@ utils::result<void, utils::error> schema::create(const sql::connection &conn) co
|
||||||
// create plain tables without constraints
|
// create plain tables without constraints
|
||||||
for (const auto &node: repo_) {
|
for (const auto &node: repo_) {
|
||||||
auto ctx = query::query::create()
|
auto ctx = query::query::create()
|
||||||
.table(node->name())
|
.table(node.name())
|
||||||
.columns(node->info().attributes())
|
.columns(node.info().attributes())
|
||||||
// .constraints(node->info().constraints())
|
// .constraints(node->info().constraints())
|
||||||
.compile(conn);
|
.compile(conn);
|
||||||
|
|
||||||
|
|
@ -83,11 +83,11 @@ utils::result<void, utils::error> schema::create(const sql::connection &conn) co
|
||||||
|
|
||||||
// create primary key constraints
|
// create primary key constraints
|
||||||
for (const auto &node: repo_) {
|
for (const auto &node: repo_) {
|
||||||
for (const auto &cons: node->info().constraints()) {
|
for (const auto &cons: node.info().constraints()) {
|
||||||
if (!cons.is_primary_key_constraint()) {
|
if (!cons.is_primary_key_constraint()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
auto ctx = build_add_constraint_context(*node, cons, conn);
|
auto ctx = build_add_constraint_context(node, cons, conn);
|
||||||
|
|
||||||
std::cout << ctx.sql << std::endl;
|
std::cout << ctx.sql << std::endl;
|
||||||
if (auto result = conn.execute(ctx.sql); !result) {
|
if (auto result = conn.execute(ctx.sql); !result) {
|
||||||
|
|
@ -97,11 +97,11 @@ utils::result<void, utils::error> schema::create(const sql::connection &conn) co
|
||||||
}
|
}
|
||||||
// create table constraints
|
// create table constraints
|
||||||
for (const auto &node: repo_) {
|
for (const auto &node: repo_) {
|
||||||
for (const auto &cons: node->info().constraints()) {
|
for (const auto &cons: node.info().constraints()) {
|
||||||
if (cons.is_primary_key_constraint()) {
|
if (cons.is_primary_key_constraint()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
auto ctx = build_add_constraint_context(*node, cons, conn);
|
auto ctx = build_add_constraint_context(node, cons, conn);
|
||||||
|
|
||||||
std::cout << ctx.sql << std::endl;
|
std::cout << ctx.sql << std::endl;
|
||||||
if (auto result = conn.execute(ctx.sql); !result) {
|
if (auto result = conn.execute(ctx.sql); !result) {
|
||||||
|
|
@ -115,12 +115,12 @@ utils::result<void, utils::error> schema::create(const sql::connection &conn) co
|
||||||
utils::result<void, utils::error> schema::drop(const sql::connection &conn) const {
|
utils::result<void, utils::error> schema::drop(const sql::connection &conn) const {
|
||||||
// drop table primary key constraints
|
// drop table primary key constraints
|
||||||
for (const auto &node: repo_) {
|
for (const auto &node: repo_) {
|
||||||
for (const auto &cons: node->info().constraints()) {
|
for (const auto &cons: node.info().constraints()) {
|
||||||
if (cons.is_primary_key_constraint()) {
|
if (cons.is_primary_key_constraint()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
auto ctx = query::query::alter()
|
auto ctx = query::query::alter()
|
||||||
.table(node->name())
|
.table(node.name())
|
||||||
.drop_constraint(cons)
|
.drop_constraint(cons)
|
||||||
.compile(conn);
|
.compile(conn);
|
||||||
|
|
||||||
|
|
@ -133,12 +133,12 @@ utils::result<void, utils::error> schema::drop(const sql::connection &conn) cons
|
||||||
|
|
||||||
// drop table constraints
|
// drop table constraints
|
||||||
for (const auto &node: repo_) {
|
for (const auto &node: repo_) {
|
||||||
for (const auto &cons: node->info().constraints()) {
|
for (const auto &cons: node.info().constraints()) {
|
||||||
if (!cons.is_primary_key_constraint()) {
|
if (!cons.is_primary_key_constraint()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
auto ctx = query::query::alter()
|
auto ctx = query::query::alter()
|
||||||
.table(node->name())
|
.table(node.name())
|
||||||
.drop_constraint(cons)
|
.drop_constraint(cons)
|
||||||
.compile(conn);
|
.compile(conn);
|
||||||
|
|
||||||
|
|
@ -152,7 +152,7 @@ utils::result<void, utils::error> schema::drop(const sql::connection &conn) cons
|
||||||
// drop table
|
// drop table
|
||||||
for (const auto &node: repo_) {
|
for (const auto &node: repo_) {
|
||||||
auto ctx = query::query::drop()
|
auto ctx = query::query::drop()
|
||||||
.table(node->name())
|
.table(node.name())
|
||||||
.compile(conn);
|
.compile(conn);
|
||||||
|
|
||||||
std::cout << ctx.sql << std::endl;
|
std::cout << ctx.sql << std::endl;
|
||||||
|
|
@ -164,7 +164,7 @@ utils::result<void, utils::error> schema::drop(const sql::connection &conn) cons
|
||||||
return utils::ok<void>();
|
return utils::ok<void>();
|
||||||
}
|
}
|
||||||
|
|
||||||
utils::result<void, utils::error> schema::drop_table(const std::string &table_name, const sql::connection &conn) const {
|
utils::result<void, utils::error> schema::drop_table(const std::string &table_name, const sql::connection &conn) {
|
||||||
auto result = query::query::drop()
|
auto result = query::query::drop()
|
||||||
.table(table_name)
|
.table(table_name)
|
||||||
.execute(conn);
|
.execute(conn);
|
||||||
|
|
@ -176,17 +176,11 @@ utils::result<void, utils::error> schema::drop_table(const std::string &table_na
|
||||||
}
|
}
|
||||||
|
|
||||||
utils::result<std::vector<object::attribute>, utils::error>
|
utils::result<std::vector<object::attribute>, utils::error>
|
||||||
schema::describe_table(const std::string &table_name, const sql::connection &conn) const {
|
schema::describe_table(const std::string &table_name, const sql::connection &conn) {
|
||||||
// if (!conn.valid()) {
|
|
||||||
// return utils::failure(utils::error(sql::error_code::FAILURE, "Failed to acquire connection."));
|
|
||||||
// }
|
|
||||||
return utils::ok(conn.describe(table_name).release());
|
return utils::ok(conn.describe(table_name).release());
|
||||||
}
|
}
|
||||||
|
|
||||||
utils::result<bool, utils::error> schema::table_exists(const std::string &table_name, const sql::connection &conn) const {
|
utils::result<bool, utils::error> schema::table_exists(const std::string &table_name, const sql::connection &conn) const {
|
||||||
// if (!c.valid()) {
|
|
||||||
// return utils::failure(utils::error(sql::error_code::FAILURE, "Failed to acquire connection."));
|
|
||||||
// }
|
|
||||||
return conn.exists(repo_.name(), table_name);
|
return conn.exists(repo_.name(), table_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,119 +1,98 @@
|
||||||
#include "matador/query/value_extractor.hpp"
|
#include "matador/query/value_extractor.hpp"
|
||||||
|
|
||||||
namespace matador::query {
|
namespace matador::query {
|
||||||
|
|
||||||
value_extractor::value_extractor(std::vector<utils::database_type> &values)
|
value_extractor::value_extractor(std::vector<utils::database_type> &values)
|
||||||
: values_(values)
|
: values_(values) {
|
||||||
{}
|
}
|
||||||
|
|
||||||
void value_extractor::on_revision(const char *, uint64_t &rev)
|
void value_extractor::on_revision(const char *, uint64_t &rev) {
|
||||||
{
|
|
||||||
utils::data_type_traits<uint64_t>::bind_value(*this, 0, rev);
|
utils::data_type_traits<uint64_t>::bind_value(*this, 0, rev);
|
||||||
}
|
}
|
||||||
|
|
||||||
void value_extractor::on_attribute(const char *, char *x, const utils::field_attributes &attr)
|
void value_extractor::on_attribute(const char *, char *x, const utils::field_attributes &attr) {
|
||||||
{
|
|
||||||
utils::data_type_traits<char *>::bind_value(*this, 0, x, attr.size());
|
utils::data_type_traits<char *>::bind_value(*this, 0, x, attr.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
void value_extractor::on_attribute(const char *, std::string &x, const utils::field_attributes &attr)
|
void value_extractor::on_attribute(const char *, std::string &x, const utils::field_attributes &attr) {
|
||||||
{
|
|
||||||
utils::data_type_traits<std::string>::bind_value(*this, 0, x, attr.size());
|
utils::data_type_traits<std::string>::bind_value(*this, 0, x, attr.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
void value_extractor::write_value(size_t /*pos*/, const int8_t &x)
|
void value_extractor::write_value(size_t /*pos*/, const int8_t &x) {
|
||||||
{
|
|
||||||
values_.emplace_back(x);
|
values_.emplace_back(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void value_extractor::write_value(size_t /*pos*/, const int16_t &x)
|
void value_extractor::write_value(size_t /*pos*/, const int16_t &x) {
|
||||||
{
|
|
||||||
values_.emplace_back(x);
|
values_.emplace_back(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void value_extractor::write_value(size_t /*pos*/, const int32_t &x)
|
void value_extractor::write_value(size_t /*pos*/, const int32_t &x) {
|
||||||
{
|
|
||||||
values_.emplace_back(x);
|
values_.emplace_back(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void value_extractor::write_value(size_t /*pos*/, const int64_t &x)
|
void value_extractor::write_value(size_t /*pos*/, const int64_t &x) {
|
||||||
{
|
|
||||||
values_.emplace_back(x);
|
values_.emplace_back(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void value_extractor::write_value(size_t /*pos*/, const uint8_t &x)
|
void value_extractor::write_value(size_t /*pos*/, const uint8_t &x) {
|
||||||
{
|
|
||||||
values_.emplace_back(x);
|
values_.emplace_back(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void value_extractor::write_value(size_t /*pos*/, const uint16_t &x)
|
void value_extractor::write_value(size_t /*pos*/, const uint16_t &x) {
|
||||||
{
|
|
||||||
values_.emplace_back(x);
|
values_.emplace_back(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void value_extractor::write_value(size_t /*pos*/, const uint32_t &x)
|
void value_extractor::write_value(size_t /*pos*/, const uint32_t &x) {
|
||||||
{
|
|
||||||
values_.emplace_back(x);
|
values_.emplace_back(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void value_extractor::write_value(size_t /*pos*/, const uint64_t &x)
|
void value_extractor::write_value(size_t /*pos*/, const uint64_t &x) {
|
||||||
{
|
|
||||||
values_.emplace_back(x);
|
values_.emplace_back(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void value_extractor::write_value(size_t /*pos*/, const bool &x)
|
void value_extractor::write_value(size_t /*pos*/, const bool &x) {
|
||||||
{
|
|
||||||
values_.emplace_back(x);
|
values_.emplace_back(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void value_extractor::write_value(size_t /*pos*/, const float &x)
|
void value_extractor::write_value(size_t /*pos*/, const float &x) {
|
||||||
{
|
|
||||||
values_.emplace_back(x);
|
values_.emplace_back(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void value_extractor::write_value(size_t /*pos*/, const double &x)
|
void value_extractor::write_value(size_t /*pos*/, const double &x) {
|
||||||
{
|
|
||||||
values_.emplace_back(x);
|
values_.emplace_back(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void value_extractor::write_value(size_t /*pos*/, const time &/*x*/)
|
void value_extractor::write_value(size_t /*pos*/, const utils::date_type_t &/*x*/) {
|
||||||
{
|
|
||||||
// values_.emplace_back(x);
|
// values_.emplace_back(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void value_extractor::write_value(size_t /*pos*/, const date &/*x*/)
|
void value_extractor::write_value(size_t /*pos*/, const utils::time_type_t &/*x*/) {
|
||||||
{
|
|
||||||
// values_.emplace_back(x);
|
// values_.emplace_back(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void value_extractor::write_value(size_t /*pos*/, const char *x)
|
void value_extractor::write_value(size_t /*pos*/, const utils::timestamp &/*x*/) {
|
||||||
{
|
}
|
||||||
|
|
||||||
|
void value_extractor::write_value(size_t /*pos*/, const char *x) {
|
||||||
values_.emplace_back(x);
|
values_.emplace_back(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void value_extractor::write_value(size_t /*pos*/, const char *x, size_t /*size*/)
|
void value_extractor::write_value(size_t /*pos*/, const char *x, size_t /*size*/) {
|
||||||
{
|
|
||||||
values_.emplace_back(x);
|
values_.emplace_back(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void value_extractor::write_value(size_t /*pos*/, const std::string &x)
|
void value_extractor::write_value(size_t /*pos*/, const std::string &x) {
|
||||||
{
|
|
||||||
values_.emplace_back(x);
|
values_.emplace_back(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void value_extractor::write_value(size_t /*pos*/, const std::string &x, size_t /*size*/)
|
void value_extractor::write_value(size_t /*pos*/, const std::string &x, size_t /*size*/) {
|
||||||
{
|
|
||||||
values_.emplace_back(x);
|
values_.emplace_back(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void value_extractor::write_value(size_t /*pos*/, const utils::blob &x)
|
void value_extractor::write_value(size_t /*pos*/, const utils::blob &x) {
|
||||||
{
|
|
||||||
values_.emplace_back(x);
|
values_.emplace_back(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void value_extractor::write_value(size_t /*pos*/, const utils::value &/*x*/, size_t /*size*/)
|
void value_extractor::write_value(size_t /*pos*/, const utils::value &/*x*/, size_t /*size*/) {
|
||||||
{
|
|
||||||
// values_.emplace_back(x);
|
// values_.emplace_back(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -85,7 +85,7 @@ utils::result<std::optional<record>, utils::error> statement::fetch_one() const
|
||||||
return utils::ok(std::optional<record>{std::nullopt});
|
return utils::ok(std::optional<record>{std::nullopt});
|
||||||
}
|
}
|
||||||
|
|
||||||
return utils::ok(std::optional{*first.release()});
|
return utils::ok(std::optional{*first.get()});
|
||||||
}
|
}
|
||||||
|
|
||||||
void statement::reset() const {
|
void statement::reset() const {
|
||||||
|
|
|
||||||
|
|
@ -428,8 +428,8 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with foreign key and for single
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship", "[query][join][many_to_many]") {
|
TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship", "[query][join][many_to_many]") {
|
||||||
auto result = repo.attach<recipe>("recipes")
|
auto result = repo.attach<ingredient>("ingredients")
|
||||||
.and_then( [this] { return repo.attach<ingredient>("ingredients"); } );
|
.and_then( [this] { return repo.attach<recipe>("recipes"); } );
|
||||||
|
|
||||||
auto obj = object_generator::generate<recipe>(repo.repo(), "recipes");
|
auto obj = object_generator::generate<recipe>(repo.repo(), "recipes");
|
||||||
auto res = query::create()
|
auto res = query::create()
|
||||||
|
|
@ -455,7 +455,10 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
|
||||||
REQUIRE(db.exists("ingredients"));
|
REQUIRE(db.exists("ingredients"));
|
||||||
tables_to_drop.emplace("ingredients");
|
tables_to_drop.emplace("ingredients");
|
||||||
|
|
||||||
obj = object_generator::generate<recipe_ingredient>(repo.repo(), "recipe_ingredients");
|
const auto it = repo.find("recipe_ingredients");
|
||||||
|
REQUIRE(it != repo.end());
|
||||||
|
obj = it->second.node().info().object();
|
||||||
|
// obj = object_generator::generate<recipe_ingredient>(repo.repo(), "recipe_ingredients");
|
||||||
res = query::create()
|
res = query::create()
|
||||||
.table(obj->name())
|
.table(obj->name())
|
||||||
.columns(obj->attributes())
|
.columns(obj->attributes())
|
||||||
|
|
@ -512,16 +515,17 @@ TEST_CASE_METHOD(QueryFixture, "Select statement with many to many relationship"
|
||||||
{ 9, 3 }
|
{ 9, 3 }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
using namespace matador::query::meta;
|
||||||
|
|
||||||
for (const auto & [recipe_id, ingredient_id]: recipe_ingredients) {
|
for (const auto & [recipe_id, ingredient_id]: recipe_ingredients) {
|
||||||
res = query::insert()
|
res = query::insert()
|
||||||
.into("recipe_ingredients", generator::columns<recipe_ingredient>(repo))
|
.into("recipe_ingredients", RECIPE_INGREDIENT)
|
||||||
.values({recipe_id, ingredient_id})
|
.values({recipe_id, ingredient_id})
|
||||||
.execute(db);
|
.execute(db);
|
||||||
REQUIRE(res.is_ok());
|
REQUIRE(res.is_ok());
|
||||||
REQUIRE(*res == 1);
|
REQUIRE(*res == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
using namespace matador::query::meta;
|
|
||||||
|
|
||||||
const auto r = RECIPE.as("r");
|
const auto r = RECIPE.as("r");
|
||||||
const auto ri= RECIPE_INGREDIENT.as("ri");
|
const auto ri= RECIPE_INGREDIENT.as("ri");
|
||||||
|
|
|
||||||
|
|
@ -48,15 +48,15 @@ TEST_CASE("Test add type to prototype tree", "[schema_node][add]") {
|
||||||
|
|
||||||
REQUIRE(repo.empty());
|
REQUIRE(repo.empty());
|
||||||
|
|
||||||
auto res = repo.attach<person>("person");
|
// auto res = repo.attach<person>("person");
|
||||||
REQUIRE(res.is_ok());
|
// REQUIRE(res.is_ok());
|
||||||
res = repo.attach<student, person>("student");
|
// res = repo.attach<student, person>("student");
|
||||||
REQUIRE(res.is_ok());
|
// REQUIRE(res.is_ok());
|
||||||
res = repo.attach<teacher, person>("teacher");
|
// res = repo.attach<teacher, person>("teacher");
|
||||||
REQUIRE(res.is_ok());
|
// REQUIRE(res.is_ok());
|
||||||
|
//
|
||||||
REQUIRE(!repo.empty());
|
// REQUIRE(!repo.empty());
|
||||||
REQUIRE(repo.size() == 3);
|
// REQUIRE(repo.size() == 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("Test next and previous of schema node", "[schema_node][next][previous]") {
|
TEST_CASE("Test next and previous of schema node", "[schema_node][next][previous]") {
|
||||||
|
|
|
||||||
|
|
@ -50,10 +50,10 @@ struct recipe
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class recipe_ingredient : public object::many_to_many_relation<recipe, ingredient> {
|
// class recipe_ingredient : public object::many_to_many_relation<recipe, ingredient> {
|
||||||
public:
|
// public:
|
||||||
recipe_ingredient() : many_to_many_relation("recipe_id", "ingredient_id") {}
|
// recipe_ingredient() : many_to_many_relation("recipe_id", "ingredient_id") {}
|
||||||
};
|
// };
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,8 +12,9 @@ void test_parameter_binder::write_value(size_t /*pos*/, const uint64_t &/*x*/) {
|
||||||
void test_parameter_binder::write_value(size_t /*pos*/, const bool &/*x*/) {}
|
void test_parameter_binder::write_value(size_t /*pos*/, const bool &/*x*/) {}
|
||||||
void test_parameter_binder::write_value(size_t /*pos*/, const float &/*x*/) {}
|
void test_parameter_binder::write_value(size_t /*pos*/, const float &/*x*/) {}
|
||||||
void test_parameter_binder::write_value(size_t /*pos*/, const double &/*x*/) {}
|
void test_parameter_binder::write_value(size_t /*pos*/, const double &/*x*/) {}
|
||||||
void test_parameter_binder::write_value(size_t /*pos*/, const time &/*x*/) {}
|
void test_parameter_binder::write_value(size_t /*pos*/, const utils::date_type_t &/*x*/) {}
|
||||||
void test_parameter_binder::write_value(size_t /*pos*/, const date &/*x*/) {}
|
void test_parameter_binder::write_value(size_t /*pos*/, const utils::time_type_t &/*x*/) {}
|
||||||
|
void test_parameter_binder::write_value(size_t /*pos*/, const utils::timestamp &/*x*/) {}
|
||||||
void test_parameter_binder::write_value(size_t /*pos*/, const char * /*x*/) {}
|
void test_parameter_binder::write_value(size_t /*pos*/, const char * /*x*/) {}
|
||||||
void test_parameter_binder::write_value(size_t /*pos*/, const char * /*x*/, size_t /*size*/) {}
|
void test_parameter_binder::write_value(size_t /*pos*/, const char * /*x*/, size_t /*size*/) {}
|
||||||
void test_parameter_binder::write_value(size_t /*pos*/, const std::string &/*x*/) {}
|
void test_parameter_binder::write_value(size_t /*pos*/, const std::string &/*x*/) {}
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,9 @@ public:
|
||||||
void write_value(size_t pos, const bool &x) override;
|
void write_value(size_t pos, const bool &x) override;
|
||||||
void write_value(size_t pos, const float &x) override;
|
void write_value(size_t pos, const float &x) override;
|
||||||
void write_value(size_t pos, const double &x) override;
|
void write_value(size_t pos, const double &x) override;
|
||||||
void write_value(size_t pos, const time &x) override;
|
void write_value(size_t pos, const utils::date_type_t &x) override;
|
||||||
void write_value(size_t pos, const date &x) override;
|
void write_value(size_t pos, const utils::time_type_t &x) override;
|
||||||
|
void write_value(size_t pos, const utils::timestamp &x) override;
|
||||||
void write_value(size_t pos, const char *x) override;
|
void write_value(size_t pos, const char *x) override;
|
||||||
void write_value(size_t pos, const char *x, size_t size) override;
|
void write_value(size_t pos, const char *x, size_t size) override;
|
||||||
void write_value(size_t pos, const std::string &x) override;
|
void write_value(size_t pos, const std::string &x) override;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue