builder progress (attribute, object and constraint class in todo.md)
This commit is contained in:
parent
34eaeca0bb
commit
78abf16f28
|
|
@ -93,6 +93,23 @@ private:
|
||||||
std::shared_ptr<attribute> reference_column_;
|
std::shared_ptr<attribute> reference_column_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class object {
|
||||||
|
public:
|
||||||
|
using iterator = std::vector<attribute>::iterator;
|
||||||
|
using const_iterator = std::vector<attribute>::const_iterator;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<attribute> columns_;
|
||||||
|
std::string name_;
|
||||||
|
std::string alias_;
|
||||||
|
};
|
||||||
|
|
||||||
|
class constraint {
|
||||||
|
public:
|
||||||
|
private:
|
||||||
|
std::shared_ptr<attribute> column_;
|
||||||
|
};
|
||||||
/**
|
/**
|
||||||
* User defined literal to have a shortcut creating a column object
|
* User defined literal to have a shortcut creating a column object
|
||||||
* @param name Name of the column
|
* @param name Name of the column
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,9 @@ private:
|
||||||
using node_ptr = std::shared_ptr<repository_node>;
|
using node_ptr = std::shared_ptr<repository_node>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit shadow_repository(object::repository& s);
|
explicit shadow_repository(repository& s);
|
||||||
|
|
||||||
[[nodiscard]] object::repository& repo() const;
|
[[nodiscard]] repository& repo() const;
|
||||||
[[nodiscard]] bool contains(const std::type_index& ti) const;
|
[[nodiscard]] bool contains(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::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> find_node(const std::string &name) const;
|
||||||
|
|
@ -28,7 +28,7 @@ public:
|
||||||
[[nodiscard]] utils::result<void, utils::error> detach_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;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
object::repository& schema_;
|
repository& repo_;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
#endif //SHADOW_SCHEMA_HPP
|
#endif //SHADOW_SCHEMA_HPP
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ public:
|
||||||
|
|
||||||
template < typename Type >
|
template < typename Type >
|
||||||
static std::shared_ptr<repository_node> make_node(repository& repo, const std::string& name) {
|
static std::shared_ptr<repository_node> make_node(repository& repo, const std::string& name) {
|
||||||
auto node = std::shared_ptr<repository_node>(new repository_node(repo, name, typeid(Type)));
|
auto node = std::make_shared<repository_node>( repo, name, typeid( Type ) );
|
||||||
|
|
||||||
primary_key_resolver resolver;
|
primary_key_resolver resolver;
|
||||||
auto pk_info = resolver.resolve<Type>();
|
auto pk_info = resolver.resolve<Type>();
|
||||||
|
|
@ -74,7 +74,7 @@ public:
|
||||||
void update_name(const std::string& name);
|
void update_name(const std::string& name);
|
||||||
|
|
||||||
template <typename Type>
|
template <typename Type>
|
||||||
object_info_ref<Type> info() const {
|
[[nodiscard]] object_info_ref<Type> info() const {
|
||||||
return std::ref(static_cast<const object_info<Type>&>(*info_));
|
return std::ref(static_cast<const object_info<Type>&>(*info_));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
#include "matador/utils/basic_types.hpp"
|
#include "matador/utils/basic_types.hpp"
|
||||||
#include "matador/utils/constraints.hpp"
|
#include "matador/utils/constraints.hpp"
|
||||||
|
#include "matador/utils/data_type_traits.hpp"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
@ -23,26 +24,25 @@ private:
|
||||||
std::string alias_;
|
std::string alias_;
|
||||||
};
|
};
|
||||||
|
|
||||||
// class attribute {
|
class data_type {
|
||||||
// public:
|
public:
|
||||||
// attribute(std::string name, const utils::basic_type type)
|
template<typename Type>
|
||||||
// : name_(std::move(name))
|
data_type() : type_(utils::data_type_traits<Type>()) {}
|
||||||
// , type_(type) {}
|
template<typename Type>
|
||||||
//
|
explicit data_type(const size_t size) : type_(utils::data_type_traits<Type>()), size_(size) {}
|
||||||
// [[nodiscard]] const std::string& name() const;
|
|
||||||
// [[nodiscard]] utils::basic_type type() const;
|
[[nodiscard]] const utils::basic_type& type() const { return type_; }
|
||||||
// [[nodiscard]] const attribute_options& options() const;
|
[[nodiscard]] size_t size() const { return size_; }
|
||||||
//
|
|
||||||
// private:
|
private:
|
||||||
// friend class entity;
|
utils::basic_type type_{};
|
||||||
//
|
size_t size_{0};
|
||||||
// std::string name_;
|
};
|
||||||
// std::string alias_;
|
|
||||||
// utils::basic_type type_{};
|
// using BigInt = data_type<int64_t>();
|
||||||
// attribute_options options_;
|
// using Real = data_type<float>;
|
||||||
//
|
// using double_ = data_type<double>;
|
||||||
// entity* owner_{nullptr};
|
// using string = data_type<std::string>;
|
||||||
// };
|
|
||||||
|
|
||||||
class constraint {
|
class constraint {
|
||||||
public:
|
public:
|
||||||
|
|
@ -70,13 +70,17 @@ namespace matador::query {
|
||||||
|
|
||||||
class column_builder {
|
class column_builder {
|
||||||
public:
|
public:
|
||||||
explicit column_builder(std::string column_name);
|
explicit column_builder(std::string column_name, utils::basic_type type, size_t size = 0);
|
||||||
|
|
||||||
// ReSharper disable once CppNonExplicitConversionOperator
|
// ReSharper disable once CppNonExplicitConversionOperator
|
||||||
operator query::column() const; // NOLINT(*-explicit-constructor)
|
operator query::column() const; // NOLINT(*-explicit-constructor)
|
||||||
|
|
||||||
|
column_builder& not_null();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string column_name;
|
std::string column_name_;
|
||||||
|
utils::basic_type type_{};
|
||||||
|
utils::constraints constraints_{};
|
||||||
};
|
};
|
||||||
|
|
||||||
class table_builder {
|
class table_builder {
|
||||||
|
|
@ -113,7 +117,7 @@ private:
|
||||||
|
|
||||||
constraint_builder constraint(std::string name);
|
constraint_builder constraint(std::string name);
|
||||||
table_builder table(std::string name);
|
table_builder table(std::string name);
|
||||||
column_builder column(std::string name);
|
column_builder column(std::string name, utils::basic_type type, size_t size = 0);
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif //MATADOR_BUILDER_HPP
|
#endif //MATADOR_BUILDER_HPP
|
||||||
|
|
@ -3,7 +3,6 @@
|
||||||
|
|
||||||
#include "matador/sql/sql_functions.hpp"
|
#include "matador/sql/sql_functions.hpp"
|
||||||
|
|
||||||
#include <functional>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
@ -40,6 +39,7 @@ private:
|
||||||
std::shared_ptr<query::table> table_;
|
std::shared_ptr<query::table> table_;
|
||||||
std::string name;
|
std::string name;
|
||||||
std::string alias_;
|
std::string alias_;
|
||||||
|
|
||||||
sql::sql_function_t function_{sql::sql_function_t::None};
|
sql::sql_function_t function_{sql::sql_function_t::None};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,8 @@
|
||||||
#include "matador/query/table.hpp"
|
#include "matador/query/table.hpp"
|
||||||
#include "matador/query/query_part.hpp"
|
#include "matador/query/query_part.hpp"
|
||||||
|
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
namespace matador::query {
|
namespace matador::query {
|
||||||
|
|
||||||
struct query_data {
|
struct query_data {
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,11 @@
|
||||||
#define QUERY_QUERY_DATA_HPP
|
#define QUERY_QUERY_DATA_HPP
|
||||||
|
|
||||||
#include "matador/object/attribute.hpp"
|
#include "matador/object/attribute.hpp"
|
||||||
#include "matador/query/table.hpp"
|
|
||||||
|
|
||||||
#include "matador/utils/types.hpp"
|
#include "matador/utils/types.hpp"
|
||||||
|
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
namespace matador::sql {
|
namespace matador::sql {
|
||||||
|
|
||||||
enum class sql_command {
|
enum class sql_command {
|
||||||
|
|
|
||||||
|
|
@ -4,30 +4,30 @@
|
||||||
#include "matador/object/repository_node.hpp"
|
#include "matador/object/repository_node.hpp"
|
||||||
|
|
||||||
namespace matador::object::internal {
|
namespace matador::object::internal {
|
||||||
shadow_repository::shadow_repository( object::repository& s )
|
shadow_repository::shadow_repository( repository& s )
|
||||||
: schema_(s) {}
|
: repo_(s) {}
|
||||||
|
|
||||||
repository& shadow_repository::repo() const {
|
repository& shadow_repository::repo() const {
|
||||||
return schema_;
|
return repo_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool shadow_repository::contains( const std::type_index& ti ) const {
|
bool shadow_repository::contains( const std::type_index& ti ) const {
|
||||||
return schema_.contains(ti);
|
return repo_.contains(ti);
|
||||||
}
|
}
|
||||||
|
|
||||||
utils::result<shadow_repository::node_ptr, utils::error> shadow_repository::find_node( const std::type_index& type_index ) const {
|
utils::result<shadow_repository::node_ptr, utils::error> shadow_repository::find_node( const std::type_index& type_index ) const {
|
||||||
return schema_.find_node(type_index);
|
return repo_.find_node(type_index);
|
||||||
}
|
}
|
||||||
|
|
||||||
utils::result<shadow_repository::node_ptr, utils::error> shadow_repository::find_node( const std::string& name ) const {
|
utils::result<shadow_repository::node_ptr, utils::error> shadow_repository::find_node( const std::string& name ) const {
|
||||||
return schema_.find_node(name);
|
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 {
|
utils::result<shadow_repository::node_ptr, utils::error> shadow_repository::attach_node( const std::shared_ptr<repository_node>& node ) const {
|
||||||
return schema_.attach_node(node, "");
|
return repo_.attach_node(node, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
utils::result<void, utils::error> shadow_repository::detach_node( const std::shared_ptr<repository_node>& node ) const {
|
utils::result<void, utils::error> shadow_repository::detach_node( const std::shared_ptr<repository_node>& node ) const {
|
||||||
return schema_.detach(node);
|
return repo_.detach(node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2,11 +2,17 @@
|
||||||
|
|
||||||
namespace matador::query {
|
namespace matador::query {
|
||||||
|
|
||||||
column_builder::column_builder(std::string column_name)
|
column_builder::column_builder(std::string column_name, const utils::basic_type type, const size_t size)
|
||||||
: column_name( std::move(column_name) ) {}
|
: column_name_(std::move(column_name))
|
||||||
|
, type_(type){}
|
||||||
|
|
||||||
column_builder::operator class query::column() const {
|
column_builder::operator class query::column() const {
|
||||||
return {column_name};
|
return {column_name_};
|
||||||
|
}
|
||||||
|
|
||||||
|
column_builder& column_builder::not_null() {
|
||||||
|
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
table_builder::table_builder(std::string name)
|
table_builder::table_builder(std::string name)
|
||||||
|
|
@ -55,8 +61,8 @@ table_builder table( std::string name ) {
|
||||||
return table_builder(std::move(name));
|
return table_builder(std::move(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
column_builder column( std::string name ) {
|
column_builder column(std::string name, const utils::basic_type type, size_t size) {
|
||||||
return column_builder(std::move(name));
|
return column_builder(std::move(name), type, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
361
todo.md
361
todo.md
|
|
@ -20,12 +20,245 @@
|
||||||
|
|
||||||
## book class
|
## book class
|
||||||
```cpp
|
```cpp
|
||||||
class book : public matador::sql::aliasable_table<Book> {
|
class Book : public matador::object::typed_object<Book> {
|
||||||
|
public:
|
||||||
|
using typed_object::as;
|
||||||
|
|
||||||
|
Book() : typed_object("book") {}
|
||||||
|
explicit Book(std::string alias)
|
||||||
|
: typed_object("book", std::move(alias)) {}
|
||||||
|
|
||||||
|
const matador::object::attribute id = create_attribute("id", *this);
|
||||||
|
const matador::object::attribute title = create_attribute("title", *this);
|
||||||
|
const matador::object::attribute year = create_attribute("year", *this);
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
## aliasable_table
|
||||||
|
```cpp
|
||||||
|
class object;
|
||||||
|
|
||||||
|
class attribute {
|
||||||
|
public:
|
||||||
|
explicit attribute(std::string name); // NOLINT(*-explicit-constructor)
|
||||||
|
|
||||||
|
attribute(const attribute&) = default;
|
||||||
|
attribute& operator=(const attribute&) = default;
|
||||||
|
attribute(attribute&&) noexcept = default;
|
||||||
|
attribute& operator=(attribute&&) noexcept = default;
|
||||||
|
|
||||||
|
attribute() = default;
|
||||||
|
attribute(std::string name,
|
||||||
|
utils::basic_type type,
|
||||||
|
const utils::field_attributes& attr = utils::null_attributes,
|
||||||
|
utils::null_option_type null_opt = utils::null_option_type::NotNull);
|
||||||
|
|
||||||
|
[[nodiscard]] const std::string& name() const { return name_; }
|
||||||
|
void name(const std::string& n);
|
||||||
|
[[nodiscard]] const utils::field_attributes& attributes() const;
|
||||||
|
[[nodiscard]] utils::field_attributes& attributes();
|
||||||
|
|
||||||
|
[[nodiscard]] bool is_nullable() const;
|
||||||
|
[[nodiscard]] utils::basic_type type() const;
|
||||||
|
|
||||||
|
[[nodiscard]] bool is_integer() const;
|
||||||
|
[[nodiscard]] bool is_floating_point() const;
|
||||||
|
[[nodiscard]] bool is_bool() const;
|
||||||
|
[[nodiscard]] bool is_string() const;
|
||||||
|
[[nodiscard]] bool is_varchar() const;
|
||||||
|
[[nodiscard]] bool is_date() const;
|
||||||
|
[[nodiscard]] bool is_time() const;
|
||||||
|
[[nodiscard]] bool is_blob() const;
|
||||||
|
[[nodiscard]] bool is_null() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
friend class object;
|
||||||
|
|
||||||
|
std::string name_;
|
||||||
|
std::string alias_;
|
||||||
|
utils::basic_type type_{utils::basic_type::type_null};
|
||||||
|
utils::field_attributes attributes_;
|
||||||
|
utils::null_option_type null_option_type_{utils::null_option_type::NotNull};
|
||||||
|
|
||||||
|
object* parent_{nullptr};
|
||||||
|
};
|
||||||
|
|
||||||
|
class constraint {
|
||||||
|
public:
|
||||||
|
constraint() = default;
|
||||||
|
|
||||||
|
explicit constraint(std::string name) : name_(std::move(name)) {}
|
||||||
|
[[nodiscard]] const std::string& name() const { return name_; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
friend class object;
|
||||||
|
|
||||||
|
std::string name_;
|
||||||
|
std::string attribute_name_;
|
||||||
|
utils::constraints options_{utils::constraints::None};
|
||||||
|
|
||||||
|
object* parent_{nullptr};
|
||||||
|
};
|
||||||
|
|
||||||
|
class object {
|
||||||
|
public:
|
||||||
|
using iterator = std::vector<attribute>::iterator;
|
||||||
|
using const_iterator = std::vector<attribute>::const_iterator;
|
||||||
|
|
||||||
|
explicit object(std::string name, std::string alias = "");
|
||||||
|
|
||||||
|
void add_attribute(attribute attr) {
|
||||||
|
auto &ref = attributes_.emplace_back(std::move(attr));
|
||||||
|
ref.parent_ = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const attribute& create_attribute(std::string name, object& obj) {
|
||||||
|
attribute attr{std::move(name)};
|
||||||
|
attr.parent_ = &obj;
|
||||||
|
return obj.attributes_.emplace_back(std::move(attr));
|
||||||
|
}
|
||||||
|
|
||||||
|
iterator begin() { return attributes_.begin(); }
|
||||||
|
iterator end() { return attributes_.end(); }
|
||||||
|
|
||||||
|
[[nodiscard]] bool empty() const { return attributes_.empty(); }
|
||||||
|
[[nodiscard]] size_t size() const { return attributes_.size(); }
|
||||||
|
|
||||||
|
[[nodiscard]] const std::string& name() const { return name_; }
|
||||||
|
[[nodiscard]] const std::string& alias() const { return alias_; }
|
||||||
|
[[nodiscard]] const std::vector<attribute>& columns() const { return attributes_; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string name_;
|
||||||
|
std::string alias_;
|
||||||
|
|
||||||
|
std::vector<attribute> attributes_;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename Type>
|
||||||
|
class typed_object : public object {
|
||||||
|
public:
|
||||||
|
using object::object;
|
||||||
|
|
||||||
|
Type as(std::string alias) { return Type{std::move(alias)}; }
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
struct column_builder {
|
||||||
|
explicit column_builder(std::string column_name)
|
||||||
|
: column_name( std::move(column_name) ) {
|
||||||
|
}
|
||||||
|
|
||||||
|
column_builder& not_null() {
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
column_builder& primary_key() {
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
operator matador::sql::column() const {
|
||||||
|
return matador::sql::column{column_name};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string column_name;
|
||||||
|
};
|
||||||
|
|
||||||
|
column_builder column(std::string name) {
|
||||||
|
return column_builder(std::move(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace matador::sql {
|
||||||
|
struct constraint {
|
||||||
|
std::string name;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
struct constraint_builder {
|
||||||
|
constraint_builder& constraint(std::string name) {
|
||||||
|
constraint_name = std::move(name);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
constraint_builder& primary_key(std::string name) {
|
||||||
|
pk_column_name = std::move(name);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
constraint_builder& foreign_key(std::string name) {
|
||||||
|
fk_column_name = std::move(name);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
constraint_builder& references(std::string table, std::string column) {
|
||||||
|
this->table_name = std::move(table);
|
||||||
|
this->column_name = std::move(column);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
operator matador::sql::constraint() const {
|
||||||
|
return matador::sql::constraint{constraint_name};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string constraint_name;
|
||||||
|
std::string pk_column_name;
|
||||||
|
std::string fk_column_name;
|
||||||
|
std::string table_name;
|
||||||
|
std::string column_name;
|
||||||
|
};
|
||||||
|
|
||||||
|
constraint_builder constraint(std::string name) {
|
||||||
|
constraint_builder builder;
|
||||||
|
return builder.constraint(std::move(name));
|
||||||
|
}
|
||||||
|
void foo(const std::initializer_list<matador::sql::column> columns) {
|
||||||
|
for (const auto& column : columns) {
|
||||||
|
std::cout << column.name() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void foo(const std::vector<matador::sql::column>& columns) {
|
||||||
|
for (const auto& column : columns) {
|
||||||
|
std::cout << column.name() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Type>
|
||||||
|
std::string column_prefix(matador::sql::aliasable_table<Type> *tab) {
|
||||||
|
if (!tab || tab->empty()) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
if (!tab->alias().empty()) {
|
||||||
|
return tab->alias();
|
||||||
|
}
|
||||||
|
return tab->name();
|
||||||
|
}
|
||||||
|
struct table_builder {
|
||||||
|
explicit table_builder(std::string name)
|
||||||
|
: table_name( std::move(name) ) {}
|
||||||
|
|
||||||
|
table_builder& as(std::string table_alias) {
|
||||||
|
this->alias = std::move(table_alias);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
operator matador::query::query_table() const {
|
||||||
|
return {matador::sql::table{table_name}, alias};
|
||||||
|
}
|
||||||
|
std::string table_name;
|
||||||
|
std::string alias;
|
||||||
|
};
|
||||||
|
|
||||||
|
table_builder table(std::string name) {
|
||||||
|
return table_builder(std::move(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
class Book : public matador::sql::aliasable_table<Book> {
|
||||||
public:
|
public:
|
||||||
using aliasable_table::as;
|
using aliasable_table::as;
|
||||||
|
|
||||||
Book() : aliasable_table("book", "") {}
|
Book() : aliasable_table("book", "") {}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class aliasable_table;
|
friend class aliasable_table;
|
||||||
|
|
||||||
|
|
@ -37,56 +270,100 @@ public:
|
||||||
matador::sql::column title = create_column("title", *this);
|
matador::sql::column title = create_column("title", *this);
|
||||||
matador::sql::column year = create_column("year", *this);
|
matador::sql::column year = create_column("year", *this);
|
||||||
};
|
};
|
||||||
```
|
|
||||||
|
|
||||||
## aliasable_table
|
struct base_node {
|
||||||
```cpp
|
base_node() = delete;
|
||||||
class table {
|
base_node(std::string name)
|
||||||
public:
|
: name(std::move(name)) {}
|
||||||
using iterator = std::vector<column>::iterator;
|
virtual ~base_node() = default;
|
||||||
using const_iterator = std::vector<column>::const_iterator;
|
|
||||||
|
|
||||||
explicit table(std::string name) : name_(std::move(name)) {}
|
std::string name;
|
||||||
table(std::string name, std::vector<column> columns)
|
};
|
||||||
: name_(std::move(name)), columns_(std::move(columns)) {}
|
template < typename Type>
|
||||||
|
struct node final {
|
||||||
|
virtual ~node() = default;
|
||||||
|
|
||||||
void add_column(column column) {
|
Type type;
|
||||||
column.table_ = shared_from_this();
|
};
|
||||||
columns_.emplace_back(std::move(column));
|
|
||||||
}
|
|
||||||
|
|
||||||
static const column& create_column(std::string name, table& t) {
|
template < template<typename> typename NodeType = node, typename BaseType = base_node >
|
||||||
column c{std::move(name)};
|
struct repository {
|
||||||
c.table_ = &t;
|
template<typename Type>
|
||||||
t.columns_.emplace_back(std::move(c));
|
NodeType<Type> find(std::string name);
|
||||||
return t.columns_.back();
|
|
||||||
}
|
|
||||||
operator const std::vector<column>&() const { return columns_; }
|
|
||||||
table as_table() const { return {name_, columns_}; }
|
|
||||||
|
|
||||||
iterator begin() { return columns_.begin(); }
|
std::unordered_map<std::string, BaseType> nodes;
|
||||||
iterator end() { return columns_.end(); }
|
};
|
||||||
|
|
||||||
[[nodiscard]] bool empty() const { return columns_.empty(); }
|
struct base_schema_node : base_node {
|
||||||
[[nodiscard]] size_t size() const { return columns_.size(); }
|
base_schema_node() = delete;
|
||||||
|
base_schema_node(std::string name, std::string table)
|
||||||
|
: base_node(std::move(name)), table_name(std::move(table)) {}
|
||||||
|
|
||||||
[[nodiscard]] const std::string& name() const { return name_; }
|
std::string table_name;
|
||||||
[[nodiscard]] const std::vector<column>& columns() const { return columns_; }
|
|
||||||
private:
|
|
||||||
std::string name_;
|
|
||||||
std::vector<column> columns_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template < typename Type >
|
template < typename Type >
|
||||||
class aliasable_table : public table {
|
struct schema_node final : base_schema_node {
|
||||||
public:
|
schema_node() = default;
|
||||||
aliasable_table(std::string name, std::string alias)
|
schema_node(std::string name, std::string table)
|
||||||
: table(std::move(name))
|
: base_schema_node(std::move(name), std::move(table)) {}
|
||||||
, alias_(std::move(alias)) {}
|
|
||||||
|
|
||||||
Type as(std::string alias) { return Type{std::move(alias)}; }
|
Type type;
|
||||||
|
|
||||||
private:
|
|
||||||
std::string alias_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
repository repo1;
|
||||||
|
repository<schema_node, base_schema_node> repo;
|
||||||
|
|
||||||
|
Book BOOK;
|
||||||
|
|
||||||
|
const auto b = BOOK.as("b");
|
||||||
|
|
||||||
|
foo(b);
|
||||||
|
foo(BOOK);
|
||||||
|
foo({
|
||||||
|
BOOK.id,
|
||||||
|
b.title,
|
||||||
|
column("col1").primary_key(),
|
||||||
|
column("col2").not_null()
|
||||||
|
});
|
||||||
|
matador::sql::schema schema("test");
|
||||||
|
|
||||||
|
schema.add_table(BOOK.as_table());
|
||||||
|
// query::create()
|
||||||
|
// .table(BOOK)
|
||||||
|
// .columns({
|
||||||
|
// BOOK.id,
|
||||||
|
// BOOK.title,
|
||||||
|
// column("year"),
|
||||||
|
// column("author_id")
|
||||||
|
// })
|
||||||
|
// .constraints({
|
||||||
|
// constraint( "PK" ).primary_key("id"),
|
||||||
|
// constraint( "FK" ).foreign_key("author_id").references("author", "id")
|
||||||
|
// });
|
||||||
|
//
|
||||||
|
// query::create()
|
||||||
|
// .table("book", {
|
||||||
|
// column("id"),
|
||||||
|
// column("title"),
|
||||||
|
// column("year"),
|
||||||
|
// column("author_id")
|
||||||
|
// }, {
|
||||||
|
// constraint( "PK" ).primary_key("id"),
|
||||||
|
// constraint( "FK" ).foreign_key("author_id").references("author", "id")
|
||||||
|
// });
|
||||||
|
//
|
||||||
|
// matador::query::query_table book = table("book").as("b");
|
||||||
|
//
|
||||||
|
// query::select({
|
||||||
|
// column(book.column("id")),
|
||||||
|
// column(book.column("title"))
|
||||||
|
// })
|
||||||
|
// .from(book);
|
||||||
|
//
|
||||||
|
// query::insert()
|
||||||
|
// .into(b, {b.title, b.year})
|
||||||
|
// .values({"title", 2021});
|
||||||
|
}
|
||||||
```
|
```
|
||||||
Loading…
Reference in New Issue