Compare commits
2 Commits
d4a8801cd2
...
c4f8731262
| Author | SHA1 | Date |
|---|---|---|
|
|
c4f8731262 | |
|
|
4fcbe9c064 |
|
|
@ -2,7 +2,7 @@
|
|||
#define BASIC_PROTOTYPE_INFO_HPP
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/constraint.hpp"
|
||||
#include "matador/object/restriction.hpp"
|
||||
#include "matador/object/relation_endpoint.hpp"
|
||||
|
||||
#include "matador/utils/identifier.hpp"
|
||||
|
|
@ -26,15 +26,15 @@ public:
|
|||
|
||||
[[nodiscard]] std::type_index type_index() const;
|
||||
[[nodiscard]] std::string name() const;
|
||||
[[nodiscard]] const class object& object() const;
|
||||
[[nodiscard]] std::shared_ptr<class object> object() const;
|
||||
[[nodiscard]] const std::list<attribute>& attributes() const;
|
||||
[[nodiscard]] const std::list<class constraint>& constraints() const;
|
||||
[[nodiscard]] const std::list<class restriction>& constraints() const;
|
||||
|
||||
[[nodiscard]] bool has_primary_key() const;
|
||||
[[nodiscard]] const utils::identifier& primary_key() const;
|
||||
[[nodiscard]] attribute* primary_key_attribute() const;
|
||||
|
||||
void update_name(const std::string& name);
|
||||
void update_name(const std::string& name) const;
|
||||
|
||||
endpoint_iterator register_relation_endpoint(const std::type_index &type, const std::shared_ptr<relation_endpoint> &endpoint);
|
||||
void unregister_relation_endpoint(const std::type_index &type);
|
||||
|
|
@ -55,10 +55,10 @@ public:
|
|||
[[nodiscard]] bool endpoints_empty() const;
|
||||
|
||||
protected:
|
||||
basic_object_info(std::shared_ptr<repository_node> node, std::unique_ptr<class object> &&obj);
|
||||
basic_object_info(std::shared_ptr<repository_node> node, std::shared_ptr<class object> &&obj);
|
||||
|
||||
protected:
|
||||
std::unique_ptr<class object> object_;
|
||||
std::shared_ptr<class object> object_;
|
||||
std::shared_ptr<repository_node> node_; /**< prototype node of the represented object type */
|
||||
t_endpoint_map relation_endpoints_;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef MATADOR_CONSTRAINTS_GENERATOR_HPP
|
||||
#define MATADOR_CONSTRAINTS_GENERATOR_HPP
|
||||
|
||||
#include "matador/object/constraint.hpp"
|
||||
#include "matador/object/restriction.hpp"
|
||||
|
||||
#include "matador/utils/access.hpp"
|
||||
#include "matador/utils/field_attributes.hpp"
|
||||
|
|
@ -17,20 +17,20 @@ class repository;
|
|||
|
||||
class constraints_generator final {
|
||||
private:
|
||||
constraints_generator(std::list<class constraint> &constraints, const repository &repo, object &obj);
|
||||
constraints_generator(std::list<class restriction> &constraints, const repository &repo, const std::shared_ptr<object> &obj);
|
||||
|
||||
public:
|
||||
constraints_generator() = delete;
|
||||
|
||||
template < typename Type >
|
||||
static std::list<class constraint> generate(const repository &repo, object &obj) {
|
||||
static std::list<class restriction> generate(const repository &repo, const std::shared_ptr<object> &obj) {
|
||||
Type t;
|
||||
return generate(t, repo, obj);
|
||||
}
|
||||
|
||||
template < typename Type >
|
||||
static std::list<class constraint> generate(const Type& t, const repository &repo, object &obj) {
|
||||
std::list<class constraint> constraints;
|
||||
static std::list<class restriction> generate(const Type& t, const repository &repo, const std::shared_ptr<object> &obj) {
|
||||
std::list<class restriction> constraints;
|
||||
constraints_generator gen(constraints, repo, obj);
|
||||
access::process(gen, t);
|
||||
return constraints;
|
||||
|
|
@ -69,9 +69,9 @@ private:
|
|||
[[nodiscard]] std::list<attribute>::iterator find_attribute_by_name(const std::string &name) const;
|
||||
|
||||
private:
|
||||
std::list<class constraint> &constraints_;
|
||||
std::list<class restriction> &constraints_;
|
||||
const repository &repo_;
|
||||
object &obj_;
|
||||
std::shared_ptr<object> obj_;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
#define MATADOR_OBJECT_HPP
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/constraint.hpp"
|
||||
#include "matador/object/restriction.hpp"
|
||||
|
||||
#include "matador/utils/identifier.hpp"
|
||||
|
||||
|
|
@ -19,8 +19,8 @@ public:
|
|||
|
||||
static const attribute& create_attribute(std::string name, object& obj);
|
||||
|
||||
void add_attribute(attribute attr);
|
||||
void add_constraint(class constraint c);
|
||||
// void add_attribute(attribute attr);
|
||||
// void add_constraint(class constraint c);
|
||||
|
||||
[[nodiscard]] attribute* primary_key_attribute() const;
|
||||
[[nodiscard]] const utils::identifier& primary_key() const;
|
||||
|
|
@ -37,7 +37,7 @@ public:
|
|||
|
||||
[[nodiscard]] bool has_constraints() const;
|
||||
[[nodiscard]] size_t constraint_count() const;
|
||||
[[nodiscard]] const std::list<class constraint>& constraints() const;
|
||||
[[nodiscard]] const std::list<class restriction>& constraints() const;
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& os, const object& obj);
|
||||
|
||||
|
|
@ -52,7 +52,7 @@ private:
|
|||
attribute* pk_attribute_{nullptr};
|
||||
utils::identifier pk_identifier_;
|
||||
std::list<attribute> attributes_;
|
||||
std::list<class constraint> constraints_;
|
||||
std::list<class restriction> constraints_;
|
||||
};
|
||||
}
|
||||
#endif //MATADOR_OBJECT_HPP
|
||||
|
|
@ -50,23 +50,22 @@ private:
|
|||
|
||||
class object_generator {
|
||||
private:
|
||||
explicit object_generator(const repository& repo, std::unique_ptr<object> &&object);
|
||||
explicit object_generator(repository& repo, const std::shared_ptr<object> &object);
|
||||
|
||||
public:
|
||||
template < class Type >
|
||||
static std::unique_ptr<object> generate(const repository &repo, const std::string &name, const std::string &alias = "") {
|
||||
static std::shared_ptr<object> generate(repository &repo, const std::string &name, const std::string &alias = "") {
|
||||
return generate(std::make_unique<Type>(), repo, name, alias);
|
||||
}
|
||||
|
||||
template < class Type >
|
||||
static std::unique_ptr<object> generate(std::unique_ptr<Type>&& t, const repository &repo, const std::string &name, const std::string &alias = "") {
|
||||
object_generator gen(repo, std::make_unique<object>(name, alias));
|
||||
static std::shared_ptr<object> generate(std::unique_ptr<Type>&& t, repository &repo, const std::string &name, const std::string &alias = "") {
|
||||
auto obj = std::make_shared<object>(name, alias);
|
||||
object_generator gen(repo, obj);
|
||||
access::process(gen, *t);
|
||||
return gen.move();
|
||||
return obj;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::unique_ptr<object> move() { return std::move(object_); }
|
||||
|
||||
template < class Type >
|
||||
void on_primary_key(const char *, Type &x, const utils::primary_key_attribute& attr = utils::default_pk_attributes);
|
||||
void on_revision(const char *id, uint64_t &rev);
|
||||
|
|
@ -114,9 +113,11 @@ private:
|
|||
|
||||
void prepare_primary_key(attribute &ref, utils::identifier &&pk) const;
|
||||
|
||||
std::shared_ptr<class object> fk_object(const std::type_index& ti) const;
|
||||
|
||||
private:
|
||||
const repository &repo_;
|
||||
std::unique_ptr<object> object_;
|
||||
repository &repo_;
|
||||
std::shared_ptr<object> object_;
|
||||
};
|
||||
|
||||
template<typename ValueType>
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ public:
|
|||
using create_func = std::function<std::unique_ptr<Type>()>;
|
||||
|
||||
object_info(const std::shared_ptr<repository_node>& node,
|
||||
std::unique_ptr<class object> &&obj,
|
||||
std::shared_ptr<class object> &&obj,
|
||||
create_func&& creator)
|
||||
: basic_object_info(node, std::move(obj))
|
||||
, creator_(std::move(creator)){
|
||||
|
|
|
|||
|
|
@ -177,16 +177,22 @@ private:
|
|||
static void insert_node(const node_ptr &parent, const node_ptr &child);
|
||||
void remove_node(const node_ptr &node);
|
||||
|
||||
bool expecting_relation_node(const std::string &name) const;
|
||||
[[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);
|
||||
|
||||
private:
|
||||
friend class internal::shadow_repository;
|
||||
friend class repository_node;
|
||||
friend class attribute_generator;
|
||||
template < typename NodeType >
|
||||
friend class foreign_node_completer;
|
||||
friend class object_generator;
|
||||
|
||||
std::string name_;
|
||||
std::shared_ptr<repository_node> root_;
|
||||
|
|
@ -197,6 +203,7 @@ private:
|
|||
|
||||
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_;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,22 +15,22 @@ class constraint_builder;
|
|||
class constraint_generator;
|
||||
class object;
|
||||
|
||||
class constraint {
|
||||
class restriction {
|
||||
public:
|
||||
constraint() = default;
|
||||
explicit constraint(std::string name);
|
||||
restriction() = default;
|
||||
explicit restriction(std::string name);
|
||||
|
||||
[[nodiscard]] const std::string& name() const;
|
||||
[[nodiscard]] const class attribute* attribute() const;
|
||||
[[nodiscard]] std::string column_name() const;
|
||||
[[nodiscard]] const object* owner() const;
|
||||
[[nodiscard]] std::shared_ptr<object> owner() const;
|
||||
[[nodiscard]] bool is_primary_key_constraint() const;
|
||||
[[nodiscard]] bool is_foreign_key_constraint() const;
|
||||
[[nodiscard]] bool is_unique_constraint() const;
|
||||
[[nodiscard]] const std::string& ref_table_name() const;
|
||||
[[nodiscard]] const std::string& ref_column_name() const;
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& os, const constraint& c);
|
||||
friend std::ostream& operator<<(std::ostream& os, const restriction& c);
|
||||
|
||||
private:
|
||||
friend class constraint_builder;
|
||||
|
|
@ -40,7 +40,8 @@ private:
|
|||
|
||||
std::string name_;
|
||||
std::variant<class attribute*, std::string> attr_;
|
||||
object *owner_{nullptr};
|
||||
std::shared_ptr<object> owner_;
|
||||
std::shared_ptr<object> reference_;
|
||||
utils::constraints options_{utils::constraints::None};
|
||||
std::string ref_table_name_{};
|
||||
std::string ref_column_name_{};
|
||||
|
|
@ -53,12 +54,11 @@ public:
|
|||
constraint_builder& foreign_key(std::string name);
|
||||
constraint_builder& references(std::string table, std::string column);
|
||||
|
||||
operator class constraint() const;
|
||||
operator class restriction() const;
|
||||
|
||||
private:
|
||||
std::string constraint_name;
|
||||
utils::constraints options_{utils::constraints::None};
|
||||
std::shared_ptr<object> reference_;
|
||||
std::string column_name;
|
||||
std::string ref_table_name;
|
||||
std::string ref_column_name;
|
||||
|
|
@ -52,8 +52,8 @@ public:
|
|||
[[nodiscard]] utils::result<bool, utils::error> table_exists(const std::string &table_name) const;
|
||||
|
||||
private:
|
||||
sql::query_context build_add_constraint_context( const object::repository_node& node, const class object::constraint& cons ) const;
|
||||
sql::query_context build_drop_constraint_context( const object::repository_node& node, const class object::constraint& cons ) const;
|
||||
sql::query_context build_add_constraint_context( const object::repository_node& node, const class object::restriction& cons ) const;
|
||||
sql::query_context build_drop_constraint_context( const object::repository_node& node, const class object::restriction& cons ) const;
|
||||
|
||||
private:
|
||||
object::repository repo_;
|
||||
|
|
|
|||
|
|
@ -145,7 +145,7 @@ private:
|
|||
friend class query_select;
|
||||
|
||||
static query::fetchable_query build_select_query(entity_query_data &&data);
|
||||
static sql::query_context build_add_constraint_context(const std::string& table_name, const class object::constraint& cons, const sql::connection_ptr &conn) ;
|
||||
static sql::query_context build_add_constraint_context(const std::string& table_name, const class object::restriction& cons, const sql::connection_ptr &conn) ;
|
||||
|
||||
private:
|
||||
mutable sql::statement_cache cache_;
|
||||
|
|
|
|||
|
|
@ -64,9 +64,9 @@ using Boolean = typed_data_type<bool>;
|
|||
using Varchar = sized_typed_data_type<std::string>;
|
||||
using Blob = sized_typed_data_type<std::vector<std::byte>>;
|
||||
|
||||
class constraint {
|
||||
class restriction {
|
||||
public:
|
||||
explicit constraint(std::string name)
|
||||
explicit restriction(std::string name)
|
||||
: name_(std::move(name)) {}
|
||||
|
||||
[[nodiscard]] const std::string& name() const;
|
||||
|
|
@ -126,7 +126,7 @@ public:
|
|||
constraint_builder& references(std::string table, std::string column);
|
||||
|
||||
// ReSharper disable once CppNonExplicitConversionOperator
|
||||
operator object::constraint() const; // NOLINT(*-explicit-constructor)
|
||||
operator object::restriction() const; // NOLINT(*-explicit-constructor)
|
||||
|
||||
private:
|
||||
std::string constraint_name;
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ public:
|
|||
|
||||
[[nodiscard]] bool equals(const column &x) const;
|
||||
|
||||
column& as(std::string a);
|
||||
column as(std::string a);
|
||||
|
||||
[[nodiscard]] const std::string& name() const;
|
||||
[[nodiscard]] const std::string& alias() const;
|
||||
|
|
@ -33,7 +33,8 @@ public:
|
|||
[[nodiscard]] std::shared_ptr<class table> table() const;
|
||||
void table(const std::shared_ptr<query::table>& t);
|
||||
|
||||
operator const std::string&() const;
|
||||
// ReSharper disable once CppNonExplicitConversionOperator
|
||||
operator const std::string&() const; // NOLINT(*-explicit-constructor)
|
||||
|
||||
private:
|
||||
std::shared_ptr<query::table> table_;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
#define MATADOR_QUERY_ALTER_TABLE_INTERMEDIATE_HPP
|
||||
|
||||
#include "executable_query.hpp"
|
||||
#include "matador/object/constraint.hpp"
|
||||
#include "matador/object/restriction.hpp"
|
||||
#include "matador/query/intermediates/query_intermediate.hpp"
|
||||
|
||||
namespace matador::query {
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
#include "matador/query/intermediates/executable_query.hpp"
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/constraint.hpp"
|
||||
#include "matador/object/restriction.hpp"
|
||||
|
||||
#include <list>
|
||||
|
||||
|
|
@ -16,8 +16,8 @@ class query_create_table_columns_intermediate : public executable_query {
|
|||
public:
|
||||
using executable_query::executable_query;
|
||||
|
||||
executable_query constraints(std::initializer_list<class object::constraint> constraints);
|
||||
executable_query constraints(const std::list<class object::constraint> &constraints);
|
||||
executable_query constraints(std::initializer_list<class object::restriction> constraints);
|
||||
executable_query constraints(const std::list<class object::restriction> &constraints);
|
||||
};
|
||||
|
||||
class query_create_table_intermediate : public query_intermediate {
|
||||
|
|
@ -26,6 +26,8 @@ public:
|
|||
|
||||
query_create_table_columns_intermediate columns(std::initializer_list<object::attribute> columns);
|
||||
query_create_table_columns_intermediate columns(const std::list<object::attribute> &columns);
|
||||
query_create_table_columns_intermediate columns(std::initializer_list<column> columns);
|
||||
query_create_table_columns_intermediate columns(const std::list<column> &columns);
|
||||
};
|
||||
|
||||
class query_create_intermediate : public query_intermediate {
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
#include "matador/query/table.hpp"
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
#include "matador/object/constraint.hpp"
|
||||
#include "matador/object/restriction.hpp"
|
||||
|
||||
#include "matador/utils/placeholder.hpp"
|
||||
|
||||
|
|
@ -392,15 +392,15 @@ private:
|
|||
|
||||
class query_create_table_constraints_part final : public query_part {
|
||||
public:
|
||||
explicit query_create_table_constraints_part(const std::list<class object::constraint> &constraints);
|
||||
explicit query_create_table_constraints_part(const std::list<class object::restriction> &constraints);
|
||||
|
||||
[[nodiscard]] const std::list<class object::constraint>& constraints() const;
|
||||
[[nodiscard]] const std::list<class object::restriction>& constraints() const;
|
||||
|
||||
private:
|
||||
void accept(query_part_visitor &visitor) override;
|
||||
|
||||
private:
|
||||
std::list<class object::constraint> constraints_;
|
||||
std::list<class object::restriction> constraints_;
|
||||
};
|
||||
|
||||
class query_create_schema_part final : public query_part {
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ namespace matador::query {
|
|||
|
||||
class column;
|
||||
|
||||
// ReSharper disable CppNonExplicitConvertingConstructor
|
||||
class table {
|
||||
public:
|
||||
table() = default;
|
||||
|
|
@ -18,7 +19,7 @@ public:
|
|||
table(std::string name, std::string as);
|
||||
table(std::string name, std::string as, const std::vector<class column> &columns);
|
||||
|
||||
table& as(const std::string &a);
|
||||
table as(const std::string &a);
|
||||
|
||||
[[nodiscard]] bool operator==(const table &x) const;
|
||||
|
||||
|
|
@ -30,9 +31,8 @@ public:
|
|||
[[nodiscard]] const std::string& alias() const;
|
||||
[[nodiscard]] const std::vector<class column>& columns() const;
|
||||
|
||||
// [[nodiscard]] class column column(const std::string &name) const;
|
||||
|
||||
operator const std::vector<query::column>&() const;
|
||||
// ReSharper disable once CppNonExplicitConversionOperator
|
||||
operator const std::vector<query::column>&() const; // NOLINT(*-explicit-constructor)
|
||||
private:
|
||||
friend class column;
|
||||
|
||||
|
|
|
|||
|
|
@ -16,13 +16,13 @@ add_library(matador-core STATIC
|
|||
../../include/matador/object/attribute.hpp
|
||||
../../include/matador/object/attribute_generator.hpp
|
||||
../../include/matador/object/basic_object_info.hpp
|
||||
../../include/matador/object/constraint.hpp
|
||||
../../include/matador/object/constraints_generator.hpp
|
||||
../../include/matador/object/error_code.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/object.hpp
|
||||
../../include/matador/object/object_generator.hpp
|
||||
../../include/matador/object/object_info.hpp
|
||||
../../include/matador/object/object_proxy.hpp
|
||||
../../include/matador/object/object_ptr.hpp
|
||||
|
|
@ -32,6 +32,7 @@ add_library(matador-core STATIC
|
|||
../../include/matador/object/repository.hpp
|
||||
../../include/matador/object/repository_node.hpp
|
||||
../../include/matador/object/repository_node_iterator.hpp
|
||||
../../include/matador/object/restriction.hpp
|
||||
../../include/matador/sql/statement_cache.hpp
|
||||
../../include/matador/utils/access.hpp
|
||||
../../include/matador/utils/attribute_reader.hpp
|
||||
|
|
@ -80,15 +81,17 @@ add_library(matador-core STATIC
|
|||
object/attribute.cpp
|
||||
object/attribute_generator.cpp
|
||||
object/basic_object_info.cpp
|
||||
object/constraint.cpp
|
||||
object/constraints_generator.cpp
|
||||
object/error_code.cpp
|
||||
object/foreign_node_completer.cpp
|
||||
object/internal/shadow_repository.cpp
|
||||
object/object.cpp
|
||||
object/object_generator.cpp
|
||||
object/relation_endpoint.cpp
|
||||
object/repository.cpp
|
||||
object/repository_node.cpp
|
||||
object/repository_node_iterator.cpp
|
||||
object/restriction.cpp
|
||||
utils/default_type_traits.cpp
|
||||
utils/error.cpp
|
||||
utils/errors.cpp
|
||||
|
|
@ -107,9 +110,6 @@ add_library(matador-core STATIC
|
|||
utils/uuid.cpp
|
||||
utils/value.cpp
|
||||
utils/version.cpp
|
||||
object/object.cpp
|
||||
../../include/matador/object/object_generator.hpp
|
||||
object/object_generator.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(matador-core ${CMAKE_DL_LIBS})
|
||||
|
|
|
|||
|
|
@ -5,22 +5,7 @@
|
|||
#include <algorithm>
|
||||
|
||||
namespace matador::object {
|
||||
// basic_object_info::basic_object_info(std::shared_ptr<repository_node> node,
|
||||
// const std::vector<attribute> &attributes,
|
||||
// utils::identifier &&pk,
|
||||
// const std::shared_ptr<attribute> &pk_as_fk_column)
|
||||
// : node_(std::move(node))
|
||||
// , attributes_(attributes)
|
||||
// , identifier_(std::move(pk))
|
||||
// , pk_as_fk_column_(pk_as_fk_column) {
|
||||
// }
|
||||
//
|
||||
// basic_object_info::basic_object_info(std::shared_ptr<repository_node> node,
|
||||
// const std::vector<attribute> &attributes)
|
||||
// : node_(std::move(node))
|
||||
// , attributes_(attributes) {}
|
||||
|
||||
basic_object_info::basic_object_info(std::shared_ptr<repository_node> node, std::unique_ptr<class object>&& obj)
|
||||
basic_object_info::basic_object_info(std::shared_ptr<repository_node> node, std::shared_ptr<class object>&& obj)
|
||||
: object_(std::move(obj))
|
||||
, node_(std::move(node)) {}
|
||||
|
||||
|
|
@ -32,15 +17,15 @@ std::string basic_object_info::name() const {
|
|||
return node_->name();
|
||||
}
|
||||
|
||||
const class object & basic_object_info::object() const {
|
||||
return *object_;
|
||||
std::shared_ptr<class object> basic_object_info::object() const {
|
||||
return object_;
|
||||
}
|
||||
|
||||
const std::list<attribute>& basic_object_info::attributes() const {
|
||||
return object_->attributes();
|
||||
}
|
||||
|
||||
const std::list<class constraint>& basic_object_info::constraints() const {
|
||||
const std::list<class restriction>& basic_object_info::constraints() const {
|
||||
return object_->constraints();
|
||||
}
|
||||
|
||||
|
|
@ -56,7 +41,7 @@ attribute* basic_object_info::primary_key_attribute() const {
|
|||
return object_->primary_key_attribute();
|
||||
}
|
||||
|
||||
void basic_object_info::update_name(const std::string& name) {
|
||||
void basic_object_info::update_name(const std::string& name) const {
|
||||
object_->update_name(name);
|
||||
}
|
||||
|
||||
|
|
@ -112,4 +97,4 @@ std::size_t basic_object_info::endpoints_size() const {
|
|||
bool basic_object_info::endpoints_empty() const {
|
||||
return relation_endpoints_.empty();
|
||||
}
|
||||
} // namespace matador::object
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,18 +5,18 @@
|
|||
#include <algorithm>
|
||||
|
||||
namespace matador::object {
|
||||
constraints_generator::constraints_generator(std::list<class constraint> &constraints, const repository& repo, object &obj)
|
||||
constraints_generator::constraints_generator(std::list<class restriction> &constraints, const repository& repo, const std::shared_ptr<object> &obj)
|
||||
: constraints_(constraints)
|
||||
, repo_(repo)
|
||||
, obj_(obj) {}
|
||||
|
||||
void constraints_generator::create_pk_constraint(const std::string& name) const {
|
||||
class constraint pk_constraint("PK_" + obj_.name());
|
||||
class restriction pk_constraint("PK_" + obj_->name());
|
||||
pk_constraint.options_ |= utils::constraints::PrimaryKey;
|
||||
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(obj_.attributes_)) {
|
||||
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(obj_->attributes_)) {
|
||||
pk_constraint.attr_ = &*pk_attr;
|
||||
}
|
||||
pk_constraint.owner_ = &obj_;
|
||||
pk_constraint.owner_ = obj_;
|
||||
constraints_.emplace_back(std::move(pk_constraint));
|
||||
}
|
||||
|
||||
|
|
@ -26,28 +26,28 @@ void constraints_generator::create_fk_constraint(const std::type_index& ti, cons
|
|||
return;
|
||||
}
|
||||
const auto *pk_attribute = result.value().get().primary_key_attribute();
|
||||
class constraint pk_constraint("FK_" + obj_.name() + "_" + name);
|
||||
class restriction pk_constraint("FK_" + obj_->name() + "_" + name);
|
||||
pk_constraint.options_ |= utils::constraints::ForeignKey;
|
||||
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(obj_.attributes_)) {
|
||||
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(obj_->attributes_)) {
|
||||
pk_constraint.attr_ = &*pk_attr;
|
||||
}
|
||||
pk_constraint.owner_ = &obj_;
|
||||
pk_constraint.owner_ = obj_;
|
||||
pk_constraint.ref_column_name_ = pk_attribute->name();
|
||||
pk_constraint.ref_table_name_ = pk_attribute->owner() ? pk_attribute->owner()->name() : "";
|
||||
constraints_.emplace_back(std::move(pk_constraint));
|
||||
}
|
||||
|
||||
void constraints_generator::create_unique_constraint(const std::string& name) const {
|
||||
class constraint pk_constraint("UK_" + obj_.name() + "_" + name);
|
||||
class restriction pk_constraint("UK_" + obj_->name() + "_" + name);
|
||||
pk_constraint.options_ |= utils::constraints::Unique;
|
||||
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(obj_.attributes_)) {
|
||||
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(obj_->attributes_)) {
|
||||
pk_constraint.attr_ = &*pk_attr;
|
||||
}
|
||||
pk_constraint.owner_ = &obj_;
|
||||
pk_constraint.owner_ = obj_;
|
||||
}
|
||||
|
||||
std::list<attribute>::iterator constraints_generator::find_attribute_by_name(const std::string& name) const {
|
||||
return std::find_if(std::begin(obj_.attributes_), std::end(obj_.attributes_), [&name](const attribute& elem) {
|
||||
return std::find_if(std::begin(obj_->attributes_), std::end(obj_->attributes_), [&name](const attribute& elem) {
|
||||
return elem.name() == name;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,15 +11,15 @@ const attribute& object::create_attribute( std::string name, object& obj ) {
|
|||
return obj.attributes_.emplace_back(std::move(attr));
|
||||
}
|
||||
|
||||
void object::add_attribute( attribute attr ) {
|
||||
auto &ref = attributes_.emplace_back(std::move(attr));
|
||||
ref.owner_ = this;
|
||||
}
|
||||
|
||||
void object::add_constraint( class constraint c ) {
|
||||
auto &ref = constraints_.emplace_back(std::move(c));
|
||||
ref.owner_ = this;
|
||||
}
|
||||
// void object::add_attribute( attribute attr ) {
|
||||
// auto &ref = attributes_.emplace_back(std::move(attr));
|
||||
// ref.owner_ = this;
|
||||
// }
|
||||
//
|
||||
// void object::add_constraint( class constraint c ) {
|
||||
// auto &ref = constraints_.emplace_back(std::move(c));
|
||||
// ref.owner_ = this;
|
||||
// }
|
||||
|
||||
attribute* object::primary_key_attribute() const {
|
||||
return pk_attribute_;
|
||||
|
|
@ -72,7 +72,7 @@ size_t object::constraint_count() const {
|
|||
return constraints_.size();
|
||||
}
|
||||
|
||||
const std::list<class constraint>& object::constraints() const {
|
||||
const std::list<class restriction>& object::constraints() const {
|
||||
return constraints_;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,52 +5,48 @@
|
|||
#include <algorithm>
|
||||
|
||||
namespace matador::object {
|
||||
object_generator::object_generator( const repository& repo, std::unique_ptr<object>&& object )
|
||||
object_generator::object_generator(repository& repo, const std::shared_ptr<object>& object)
|
||||
: repo_(repo)
|
||||
, object_(std::move(object)) {}
|
||||
, object_(object) {}
|
||||
|
||||
void object_generator::on_revision(const char* id, uint64_t& rev) {
|
||||
on_attribute(id, rev);
|
||||
}
|
||||
|
||||
void object_generator::create_pk_constraint(const std::string& name) const {
|
||||
class constraint pk_constraint("PK_" + object_->name());
|
||||
class restriction pk_constraint("PK_" + object_->name());
|
||||
pk_constraint.options_ |= utils::constraints::PrimaryKey;
|
||||
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(object_->attributes_)) {
|
||||
pk_constraint.attr_ = &*pk_attr;
|
||||
}
|
||||
pk_constraint.owner_ = object_.get();
|
||||
pk_constraint.owner_ = object_;
|
||||
object_->constraints_.emplace_back(std::move(pk_constraint));
|
||||
}
|
||||
|
||||
void object_generator::create_fk_constraint(const std::type_index& ti, const std::string& name) const {
|
||||
const auto result = repo_.basic_info(ti);
|
||||
if (!result) {
|
||||
repo_.provide_object_in_advance(ti, std::make_shared<object>(""));
|
||||
repo_.has_object_for_type(ti);
|
||||
repo_.object_for_type(ti);
|
||||
repo_.remove_object_for_type(ti);
|
||||
return;
|
||||
}
|
||||
const auto *pk_attribute = result->get().primary_key_attribute();
|
||||
class constraint pk_constraint("FK_" + object_->name() + "_" + name);
|
||||
const auto obj = fk_object(ti);
|
||||
const auto *pk_attribute = obj->primary_key_attribute();
|
||||
class restriction pk_constraint("FK_" + object_->name() + "_" + name);
|
||||
pk_constraint.options_ |= utils::constraints::ForeignKey;
|
||||
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(object_->attributes_)) {
|
||||
pk_constraint.attr_ = &*pk_attr;
|
||||
}
|
||||
pk_constraint.owner_ = object_.get();
|
||||
pk_constraint.owner_ = object_;
|
||||
pk_constraint.reference_ = obj;
|
||||
if (pk_attribute) {
|
||||
pk_constraint.ref_column_name_ = pk_attribute->name();
|
||||
pk_constraint.ref_table_name_ = pk_attribute->owner() ? pk_attribute->owner()->name() : "";
|
||||
}
|
||||
object_->constraints_.emplace_back(std::move(pk_constraint));
|
||||
}
|
||||
|
||||
void object_generator::create_unique_constraint(const std::string& name) const {
|
||||
class constraint pk_constraint("UK_" + object_->name() + "_" + name);
|
||||
class restriction pk_constraint("UK_" + object_->name() + "_" + name);
|
||||
pk_constraint.options_ |= utils::constraints::Unique;
|
||||
if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(object_->attributes_)) {
|
||||
pk_constraint.attr_ = &*pk_attr;
|
||||
}
|
||||
pk_constraint.owner_ = object_.get();
|
||||
pk_constraint.owner_ = object_;
|
||||
}
|
||||
|
||||
std::list<attribute>::iterator object_generator::find_attribute_by_name(const std::string& name) const {
|
||||
|
|
@ -63,4 +59,13 @@ void object_generator::prepare_primary_key(attribute& ref, utils::identifier &&p
|
|||
object_->pk_attribute_ = &ref;
|
||||
object_->pk_identifier_ = std::move(pk);
|
||||
}
|
||||
|
||||
std::shared_ptr<class object> object_generator::fk_object(const std::type_index& ti) const {
|
||||
const auto result = repo_.basic_info(ti);
|
||||
if (result) {
|
||||
return result->get().object();
|
||||
}
|
||||
|
||||
return repo_.provide_object_in_advance(ti, std::make_shared<object>(""));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -213,4 +213,20 @@ void repository::expect_relation_node(const std::string &name, const std::type_i
|
|||
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);
|
||||
}
|
||||
} // namespace matador::object
|
||||
|
|
|
|||
|
|
@ -1,15 +1,16 @@
|
|||
#include "matador/object/constraint.hpp"
|
||||
#include "matador/object/restriction.hpp"
|
||||
|
||||
#include "matador/object/attribute.hpp"
|
||||
|
||||
namespace matador::object {
|
||||
constraint::constraint(std::string name)
|
||||
restriction::restriction(std::string name)
|
||||
: name_(std::move(name)) {}
|
||||
|
||||
const std::string & constraint::name() const {
|
||||
const std::string & restriction::name() const {
|
||||
return name_;
|
||||
}
|
||||
|
||||
const class attribute* constraint::attribute() const {
|
||||
const class attribute* restriction::attribute() const {
|
||||
if (std::holds_alternative<class attribute*>(attr_)) {
|
||||
return std::get<class attribute*>(attr_);
|
||||
}
|
||||
|
|
@ -17,7 +18,7 @@ const class attribute* constraint::attribute() const {
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
std::string constraint::column_name() const {
|
||||
std::string restriction::column_name() const {
|
||||
if (std::holds_alternative<class attribute*>(attr_)) {
|
||||
return std::get<class attribute*>(attr_)->name();
|
||||
}
|
||||
|
|
@ -28,31 +29,31 @@ std::string constraint::column_name() const {
|
|||
return "";
|
||||
}
|
||||
|
||||
const object* constraint::owner() const {
|
||||
std::shared_ptr<object> restriction::owner() const {
|
||||
return owner_;
|
||||
}
|
||||
|
||||
bool constraint::is_primary_key_constraint() const {
|
||||
bool restriction::is_primary_key_constraint() const {
|
||||
return utils::is_constraint_set(options_, utils::constraints::PrimaryKey);
|
||||
}
|
||||
|
||||
bool constraint::is_foreign_key_constraint() const {
|
||||
bool restriction::is_foreign_key_constraint() const {
|
||||
return utils::is_constraint_set(options_, utils::constraints::ForeignKey);
|
||||
}
|
||||
|
||||
bool constraint::is_unique_constraint() const {
|
||||
bool restriction::is_unique_constraint() const {
|
||||
return utils::is_constraint_set(options_, utils::constraints::Unique);
|
||||
}
|
||||
|
||||
const std::string& constraint::ref_table_name() const {
|
||||
const std::string& restriction::ref_table_name() const {
|
||||
return ref_table_name_;
|
||||
}
|
||||
|
||||
const std::string& constraint::ref_column_name() const {
|
||||
const std::string& restriction::ref_column_name() const {
|
||||
return ref_column_name_;
|
||||
}
|
||||
|
||||
std::ostream & operator<<(std::ostream &os, const class constraint &c) {
|
||||
std::ostream & operator<<(std::ostream &os, const class restriction &c) {
|
||||
os << "constraint " << c.name_ << " for column " << c.column_name();
|
||||
return os;
|
||||
}
|
||||
|
|
@ -81,8 +82,8 @@ constraint_builder & constraint_builder::references(std::string table, std::stri
|
|||
return *this;
|
||||
}
|
||||
|
||||
constraint_builder::operator class constraint() const {
|
||||
class constraint c;
|
||||
constraint_builder::operator class restriction() const {
|
||||
class restriction c;
|
||||
c.name_ = constraint_name;
|
||||
c.attr_ = column_name;
|
||||
c.options_ = options_;
|
||||
|
|
@ -181,7 +181,7 @@ utils::result<bool, utils::error> schema::table_exists(const std::string& table_
|
|||
return c->exists(repo_.name(), table_name);
|
||||
}
|
||||
|
||||
sql::query_context schema::build_add_constraint_context( const object::repository_node& node, const class object::constraint& cons ) const {
|
||||
sql::query_context schema::build_add_constraint_context( const object::repository_node& node, const class object::restriction& cons ) const {
|
||||
if (cons.is_foreign_key_constraint()) {
|
||||
return query::query::alter()
|
||||
.table(node.name())
|
||||
|
|
@ -200,7 +200,7 @@ sql::query_context schema::build_add_constraint_context( const object::repositor
|
|||
return {};
|
||||
}
|
||||
|
||||
sql::query_context schema::build_drop_constraint_context( const object::repository_node& node, const class object::constraint& cons ) const {
|
||||
sql::query_context schema::build_drop_constraint_context( const object::repository_node& node, const class object::restriction& cons ) const {
|
||||
return query::query::alter()
|
||||
.table(node.name())
|
||||
.drop_constraint(cons.name())
|
||||
|
|
|
|||
|
|
@ -76,7 +76,7 @@ utils::result<void, utils::error> session::create_schema() const {
|
|||
return utils::ok<void>();
|
||||
}
|
||||
|
||||
sql::query_context session::build_add_constraint_context(const std::string& table_name, const class object::constraint& cons, const sql::connection_ptr &conn) {
|
||||
sql::query_context session::build_add_constraint_context(const std::string& table_name, const class object::restriction& cons, const sql::connection_ptr &conn) {
|
||||
if (cons.is_foreign_key_constraint()) {
|
||||
return query::query::alter()
|
||||
.table(table_name)
|
||||
|
|
|
|||
|
|
@ -48,8 +48,8 @@ constraint_builder& constraint_builder::references( std::string table, std::stri
|
|||
return *this;
|
||||
}
|
||||
|
||||
constraint_builder::operator object::constraint() const {
|
||||
return object::constraint{constraint_name};
|
||||
constraint_builder::operator object::restriction() const {
|
||||
return object::restriction{constraint_name};
|
||||
}
|
||||
|
||||
constraint_builder constraint( std::string name ) {
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ bool column::equals(const column &x) const {
|
|||
function_ == x.function_;
|
||||
}
|
||||
|
||||
column &column::as(std::string a) {
|
||||
column column::as(std::string a) {
|
||||
alias_ = std::move(a);
|
||||
return *this;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,11 +18,11 @@ executable_query query_create_intermediate::schema( const std::string& schema_na
|
|||
return {context_};
|
||||
}
|
||||
|
||||
executable_query query_create_table_columns_intermediate::constraints( std::initializer_list<class object::constraint> constraints ) {
|
||||
executable_query query_create_table_columns_intermediate::constraints(const std::initializer_list<class object::restriction> constraints) {
|
||||
return this->constraints(std::list(constraints));
|
||||
}
|
||||
|
||||
executable_query query_create_table_columns_intermediate::constraints( const std::list<class object::constraint>& constraints ) {
|
||||
executable_query query_create_table_columns_intermediate::constraints(const std::list<class object::restriction>& constraints) {
|
||||
|
||||
context_->parts.push_back(std::make_unique<internal::query_create_table_constraints_part>(constraints));
|
||||
return {context_};
|
||||
|
|
@ -37,4 +37,12 @@ query_create_table_columns_intermediate query_create_table_intermediate::columns
|
|||
context_->parts.push_back(std::make_unique<internal::query_create_table_columns_part>(columns));
|
||||
return {context_};
|
||||
}
|
||||
|
||||
query_create_table_columns_intermediate query_create_table_intermediate::columns(std::initializer_list<column> columns) {
|
||||
return {context_};
|
||||
}
|
||||
|
||||
query_create_table_columns_intermediate query_create_table_intermediate::columns(const std::list<column>& columns) {
|
||||
return {context_};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -370,11 +370,11 @@ void query_create_table_columns_part::accept(query_part_visitor& visitor) {
|
|||
visitor.visit(*this);
|
||||
}
|
||||
|
||||
query_create_table_constraints_part::query_create_table_constraints_part(const std::list<class object::constraint>& constraints)
|
||||
query_create_table_constraints_part::query_create_table_constraints_part(const std::list<class object::restriction>& constraints)
|
||||
: query_part( sql::dialect_token::Constraint )
|
||||
, constraints_(constraints) {}
|
||||
|
||||
const std::list<class object::constraint>& query_create_table_constraints_part::constraints() const {
|
||||
const std::list<class object::restriction>& query_create_table_constraints_part::constraints() const {
|
||||
return constraints_;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -294,7 +294,7 @@ void query_compiler::visit(internal::query_create_part &/*create_part*/)
|
|||
}
|
||||
|
||||
std::string build_create_column(const object::attribute &col, const sql::dialect &d);
|
||||
std::string build_constraint(const class object::constraint &cons, const sql::dialect &d);
|
||||
std::string build_constraint(const class object::restriction &cons, const sql::dialect &d);
|
||||
|
||||
void query_compiler::visit(internal::query_create_table_part &part)
|
||||
{
|
||||
|
|
@ -392,7 +392,7 @@ std::string build_create_column(const object::attribute &col, const sql::dialect
|
|||
return result;
|
||||
}
|
||||
|
||||
std::string build_constraint(const class object::constraint& cons, const sql::dialect& d) {
|
||||
std::string build_constraint(const class object::restriction& cons, const sql::dialect& d) {
|
||||
std::string result;
|
||||
if (!cons.name().empty()) {
|
||||
result.append(d.constraint()).append(" ").append(cons.name()).append(" ");
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ bool table::operator==( const table& x ) const {
|
|||
return name_ == x.name_;
|
||||
}
|
||||
|
||||
table & table::as(const std::string &a) {
|
||||
table table::as(const std::string &a) {
|
||||
alias_ = a;
|
||||
return *this;
|
||||
}
|
||||
|
|
@ -49,10 +49,6 @@ const std::vector<column>& table::columns() const {
|
|||
return columns_;
|
||||
}
|
||||
|
||||
// column table::column(const std::string& name) const {
|
||||
// return {std::shared_ptr<table>(const_cast<table*>(this)), name};
|
||||
// }
|
||||
|
||||
table::operator const std::vector<query::column>&() const {
|
||||
return columns_;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue