observer progress, added to schema and added schema_observer class
This commit is contained in:
@@ -16,10 +16,13 @@ class table;
|
||||
// ReSharper disable CppNonExplicitConvertingConstructor
|
||||
class column {
|
||||
public:
|
||||
column(const char *name, const std::string& as = ""); // NOLINT(*-explicit-constructor)
|
||||
column(std::string name, std::string as = ""); // NOLINT(*-explicit-constructor)
|
||||
column(const char *name); // NOLINT(*-explicit-constructor)
|
||||
column(std::string name); // NOLINT(*-explicit-constructor)
|
||||
column(std::string name, std::string alias);
|
||||
column(sql::sql_function_t func, std::string name);
|
||||
column(const class table* tab, std::string name, std::string as = "");
|
||||
column(const class table* tab, std::string name);
|
||||
column(const class table* tab, std::string name, std::string alias);
|
||||
column(const class table* tab, std::string name, utils::basic_type type, const utils::field_attributes& attributes);
|
||||
|
||||
[[nodiscard]] bool equals(const column &x) const;
|
||||
|
||||
@@ -45,6 +48,8 @@ private:
|
||||
static query::table* default_table();
|
||||
|
||||
private:
|
||||
friend class table;
|
||||
|
||||
const query::table* table_{nullptr};
|
||||
std::string name_;
|
||||
std::string alias_;
|
||||
|
||||
@@ -6,12 +6,11 @@
|
||||
#include <matador/utils/field_attributes.hpp>
|
||||
#include <matador/utils/foreign_attributes.hpp>
|
||||
|
||||
#include "matador/object/repository.hpp"
|
||||
|
||||
#include "matador/query/fk_value_extractor.hpp"
|
||||
#include "matador/query/internal/column_value_pair.hpp"
|
||||
|
||||
#include "matador/query/table.hpp"
|
||||
#include "matador/query/schema.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
@@ -48,7 +47,7 @@ class column_generator {
|
||||
public:
|
||||
explicit column_generator(const std::string &table_name = "",
|
||||
column_generator_options options = default_column_generator_options);
|
||||
explicit column_generator(const object::repository &repo,
|
||||
explicit column_generator(const schema &repo,
|
||||
const std::string &table_name = "",
|
||||
column_generator_options options = default_column_generator_options);
|
||||
|
||||
@@ -94,7 +93,7 @@ public:
|
||||
if (!repo_) {
|
||||
return;
|
||||
}
|
||||
const auto info = repo_->get().info<typename ContainerType::value_type::value_type>();
|
||||
const auto info = repo_->get().repo().info<typename ContainerType::value_type::value_type>();
|
||||
if (!info) {
|
||||
return;
|
||||
}
|
||||
@@ -130,7 +129,7 @@ private:
|
||||
if (!repo_) {
|
||||
return;
|
||||
}
|
||||
const auto info = repo_->get().info<typename Pointer::value_type>();
|
||||
const auto info = repo_->get().repo().info<typename Pointer::value_type>();
|
||||
if (!info) {
|
||||
return;
|
||||
}
|
||||
@@ -148,7 +147,7 @@ private:
|
||||
void push(const std::string &column_name);
|
||||
|
||||
private:
|
||||
std::optional<std::reference_wrapper<const object::repository>> repo_;
|
||||
std::optional<std::reference_wrapper<const schema>> repo_;
|
||||
std::vector<column> result_;
|
||||
std::stack<std::shared_ptr<table>> table_stack_;
|
||||
std::unordered_set<std::string> seen_tables;
|
||||
@@ -296,7 +295,7 @@ std::vector<internal::column_value_pair> column_value_pairs() {
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
std::vector<column> columns(const object::repository &repo,
|
||||
std::vector<column> columns(const schema &repo,
|
||||
const std::string &table_name = "",
|
||||
const column_generator_options options = default_column_generator_options) {
|
||||
column_generator generator(repo, table_name, options);
|
||||
@@ -304,10 +303,10 @@ std::vector<column> columns(const object::repository &repo,
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
std::vector<column> columns(const object::repository &repo,
|
||||
std::vector<column> columns(const schema &repo,
|
||||
const column_generator_options options) {
|
||||
std::string table_name;
|
||||
if (const auto result = repo.info<Type>()) {
|
||||
if (const auto result = repo.repo().info<Type>()) {
|
||||
table_name = result.value().get().name();
|
||||
}
|
||||
column_generator generator(repo, table_name, options);
|
||||
@@ -316,7 +315,7 @@ std::vector<column> columns(const object::repository &repo,
|
||||
|
||||
template<typename Type>
|
||||
std::vector<column> columns(const Type &obj,
|
||||
const object::repository &repo,
|
||||
const schema &repo,
|
||||
const std::string &table_name = "",
|
||||
const column_generator_options options = default_column_generator_options) {
|
||||
column_generator generator(repo, table_name, options);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef MATADOR_SCHEMA_HPP
|
||||
#define MATADOR_SCHEMA_HPP
|
||||
|
||||
#include "matador/object/observer.hpp"
|
||||
#include "matador/object/repository.hpp"
|
||||
|
||||
#include "matador/sql/query_context.hpp"
|
||||
@@ -28,19 +29,39 @@ private:
|
||||
std::unordered_map<std::string, schema> schema_map_;
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
class schema_observer final : public object::observer<Type> {
|
||||
public:
|
||||
explicit schema_observer(schema& s)
|
||||
: schema_(s) {}
|
||||
void on_attach(object::repository_node &node, const Type &prototype) override;
|
||||
void on_detach(object::repository_node &node, const Type &prototype) override;
|
||||
void on_insert(Type &obj) override;
|
||||
void on_update(Type &obj) override;
|
||||
void on_delete(Type &obj) override;
|
||||
|
||||
private:
|
||||
schema& schema_;
|
||||
};
|
||||
|
||||
class schema final {
|
||||
public:
|
||||
explicit schema(sql::connection_pool &pool);
|
||||
schema(sql::connection_pool &pool, const std::string &name);
|
||||
|
||||
template<typename Type, template <typename> class ObserverType>
|
||||
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name, const std::string &parent = "", std::initializer_list<ObserverType<Type>*> observer = {}) {
|
||||
return repo_.attach<Type>(name, parent, observer);
|
||||
template<typename Type, typename... Observers>
|
||||
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name, Observers&&... observers) {
|
||||
return repo_.attach<Type>(name, schema_observer<Type>{*this}, std::forward<Observers>(observers)...);
|
||||
}
|
||||
|
||||
template<typename Type, typename SuperType, template <typename> class ObserverType>
|
||||
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name, std::initializer_list<ObserverType<Type>*> observer = {}) {
|
||||
return repo_.attach<Type, SuperType>(name, observer);
|
||||
template<typename Type, typename... Observers>
|
||||
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name, const std::string &parent, Observers&&... observers) {
|
||||
return repo_.attach<Type>(name, parent, schema_observer<Type>{*this}, std::forward<Observers>(observers)...);
|
||||
}
|
||||
|
||||
template<typename Type, typename SuperType, typename... Observers>
|
||||
[[nodiscard]] utils::result<void, utils::error> attach(const std::string &name, Observers&&... observers) {
|
||||
return repo_.attach<Type, SuperType>(name, schema_observer<Type>{*this}, std::forward<Observers>(observers)...);
|
||||
}
|
||||
|
||||
[[nodiscard]] utils::result<void, utils::error> create() const;
|
||||
@@ -53,13 +74,20 @@ public:
|
||||
[[nodiscard]] utils::result<std::vector<object::attribute>, utils::error> describe_table(const std::string &table_name) const;
|
||||
[[nodiscard]] utils::result<bool, utils::error> table_exists(const std::string &table_name) const;
|
||||
|
||||
private:
|
||||
[[nodiscard]] sql::query_context build_add_constraint_context( const object::repository_node& node, const object::restriction& cons ) const;
|
||||
[[nodiscard]] sql::query_context build_drop_constraint_context( const object::repository_node& node, const object::restriction& cons ) const;
|
||||
const object::repository &repo() const { return repo_; }
|
||||
|
||||
private:
|
||||
[[nodiscard]] sql::query_context build_add_constraint_context( const object::repository_node& node, const object::restriction& cons ) const;
|
||||
[[nodiscard]] sql::query_context build_drop_constraint_context( const object::repository_node& node, const object::restriction& cons ) const;
|
||||
|
||||
void insert_table(const std::type_index& ti, const object::repository_node &node);
|
||||
|
||||
private:
|
||||
template<typename Type>
|
||||
friend class schema_observer;
|
||||
|
||||
object::repository repo_;
|
||||
std::unordered_map<std::type_index, table> table_map_;
|
||||
std::unordered_map<std::type_index, table> tables_;
|
||||
sql::connection_pool &pool_;
|
||||
};
|
||||
|
||||
@@ -72,6 +100,18 @@ utils::result<void, utils::error> schema::drop_table() {
|
||||
|
||||
return utils::failure(info.err());
|
||||
}
|
||||
|
||||
template <typename Type>
|
||||
void schema_observer<Type>::on_attach(object::repository_node &node, const Type &prototype) {
|
||||
schema_.insert_table(typeid(Type), node);
|
||||
}
|
||||
|
||||
template <typename Type>
|
||||
void schema_observer<Type>::on_detach(object::repository_node &node, const Type &prototype) {}
|
||||
template <typename Type>
|
||||
void schema_observer<Type>::on_insert(Type &obj) {}
|
||||
template <typename Type>
|
||||
void schema_observer<Type>::on_update(Type &obj) {}
|
||||
template <typename Type>
|
||||
void schema_observer<Type>::on_delete(Type &obj) {}
|
||||
} // namespace matador::query
|
||||
#endif //MATADOR_SCHEMA_HPP
|
||||
Reference in New Issue
Block a user