schema progress
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
#ifndef OBJECT_PTR_HPP
|
||||
#define OBJECT_PTR_HPP
|
||||
|
||||
namespace matador::object {
|
||||
template <typename Type>
|
||||
class object_ptr {
|
||||
public:
|
||||
Type* operator->() { return ptr; };
|
||||
Type& operator*() { return *ptr; };
|
||||
|
||||
private:
|
||||
Type* ptr{nullptr};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //OBJECT_PTR_HPP
|
||||
@@ -18,7 +18,10 @@ class schema {
|
||||
public:
|
||||
typedef const_schema_node_iterator const_iterator; /**< Shortcut for the list const iterator. */
|
||||
|
||||
schema();
|
||||
/**
|
||||
* Creates an empty schema
|
||||
*/
|
||||
explicit schema( const std::string& name = "");
|
||||
|
||||
template <typename Type>
|
||||
utils::result<void, utils::error> attach(const std::string name, const std::string &parent = "") {
|
||||
@@ -36,6 +39,11 @@ public:
|
||||
utils::result<void, utils::error> attach(const std::string name) {
|
||||
auto node = schema_node::make_node<Type>(*this, name);
|
||||
|
||||
auto result = attach_node(node, std::type_index(typeid(ParentType)));
|
||||
if (!result) {
|
||||
return utils::failure(result.err());
|
||||
}
|
||||
|
||||
return utils::ok<void>();
|
||||
}
|
||||
|
||||
@@ -53,26 +61,46 @@ public:
|
||||
*/
|
||||
[[nodiscard]] const_iterator end() const;
|
||||
|
||||
/**
|
||||
* Returns true if the schema contains
|
||||
* no schema nodes.
|
||||
*
|
||||
* @return True if schema is empty
|
||||
*/
|
||||
[[nodiscard]] bool empty() const;
|
||||
|
||||
/**
|
||||
* Returns the current number of 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;
|
||||
|
||||
private:
|
||||
using t_node_map = std::unordered_map<std::string, std::shared_ptr<schema_node>>;
|
||||
// type_index -> [name -> prototype]
|
||||
using t_type_index_node_map = std::unordered_map<std::type_index, t_node_map>;
|
||||
using node_ptr = std::shared_ptr<schema_node>;
|
||||
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>;
|
||||
|
||||
utils::result<std::shared_ptr<schema_node>, utils::error> attach_node(const std::shared_ptr<schema_node> &node,
|
||||
const std::string &parent);
|
||||
utils::result<std::shared_ptr<schema_node>, utils::error> find_parent(const std::string &name) const;
|
||||
utils::result<std::shared_ptr<schema_node>, utils::error> find_node(const std::string &name) const;
|
||||
[[nodiscard]] utils::result<std::shared_ptr<schema_node>, utils::error> attach_node(const std::shared_ptr<schema_node> &node,
|
||||
const std::string &parent);
|
||||
[[nodiscard]] utils::result<std::shared_ptr<schema_node>, utils::error> attach_node(const std::shared_ptr<schema_node> &node,
|
||||
const std::type_index &type_index);
|
||||
[[nodiscard]] utils::result<std::shared_ptr<schema_node>, utils::error> find_node(const std::string &name) const;
|
||||
[[nodiscard]] utils::result<std::shared_ptr<schema_node>, utils::error> find_node(const std::type_index &type_index) const;
|
||||
|
||||
void push_back_child(const node_ptr &parent, const node_ptr &child);
|
||||
[[nodiscard]] bool has_node(const std::type_index& index, const std::string &name) const;
|
||||
|
||||
bool has_node(const std::type_index& index, const std::string &name) const;
|
||||
static void push_back_child(const node_ptr &parent, const node_ptr &child);
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
std::shared_ptr<schema_node> root_;
|
||||
|
||||
t_node_map node_map_;
|
||||
|
||||
@@ -12,6 +12,8 @@ class schema;
|
||||
|
||||
class schema_node final {
|
||||
public:
|
||||
using node_ptr = std::shared_ptr<schema_node>;
|
||||
|
||||
template < typename Type >
|
||||
static std::shared_ptr<schema_node> make_node(schema& tree, const std::string& name) {
|
||||
return std::shared_ptr<schema_node>(new schema_node(tree, name, static_cast<Type*>(nullptr)));
|
||||
@@ -41,6 +43,9 @@ public:
|
||||
*/
|
||||
void insert(const std::shared_ptr<schema_node> &child);
|
||||
|
||||
[[nodiscard]] node_ptr next() const;
|
||||
[[nodiscard]] node_ptr prev() const;
|
||||
|
||||
private:
|
||||
explicit schema_node(schema& tree);
|
||||
template < typename Type >
|
||||
|
||||
@@ -81,7 +81,7 @@ public:
|
||||
*
|
||||
* @return Returns iterator before incrementing.
|
||||
*/
|
||||
const_schema_node_iterator operator++(int);
|
||||
const_schema_node_iterator operator++( int );
|
||||
|
||||
/**
|
||||
* Pre increments the iterator
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
#include <string>
|
||||
|
||||
namespace matador::sql {
|
||||
class schema;
|
||||
class connection_impl;
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef QUERY_QUERY_HELPER_HPP
|
||||
#define QUERY_QUERY_HELPER_HPP
|
||||
|
||||
#include "matador/utils/macro_map.hpp"
|
||||
|
||||
#include "matador/sql/table.hpp"
|
||||
#include "matador/sql/column.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <ostream>
|
||||
|
||||
#define FIELD(x) const sql::column x{*this, #x, ""};
|
||||
|
||||
#define QUERY_HELPER(C, ...) \
|
||||
namespace matador::qh { \
|
||||
namespace internal { \
|
||||
struct C##_query : sql::table { \
|
||||
C##_query() : table(#C) {} \
|
||||
MAP(FIELD, __VA_ARGS__) \
|
||||
}; } \
|
||||
static const internal:: C##_query C; \
|
||||
}
|
||||
|
||||
#endif //QUERY_QUERY_HELPER_HPP
|
||||
@@ -0,0 +1,42 @@
|
||||
#ifndef QUERY_MACRO_MAP_HPP
|
||||
#define QUERY_MACRO_MAP_HPP
|
||||
|
||||
#define EVAL0(...) __VA_ARGS__
|
||||
#define EVAL1(...) EVAL0(EVAL0(EVAL0(__VA_ARGS__)))
|
||||
#define EVAL2(...) EVAL1(EVAL1(EVAL1(__VA_ARGS__)))
|
||||
#define EVAL3(...) EVAL2(EVAL2(EVAL2(__VA_ARGS__)))
|
||||
#define EVAL4(...) EVAL3(EVAL3(EVAL3(__VA_ARGS__)))
|
||||
#define EVAL(...) EVAL4(EVAL4(EVAL4(__VA_ARGS__)))
|
||||
|
||||
#define MAP_END(...)
|
||||
#define MAP_OUT
|
||||
#define MAP_COMMA ,
|
||||
|
||||
#define MAP_GET_END2() 0, MAP_END
|
||||
#define MAP_GET_END1(...) MAP_GET_END2
|
||||
#define MAP_GET_END(...) MAP_GET_END1
|
||||
#define MAP_NEXT0(test, next, ...) next MAP_OUT
|
||||
#define MAP_NEXT1(test, next) MAP_NEXT0(test, next, 0)
|
||||
#define MAP_NEXT(test, next) MAP_NEXT1(MAP_GET_END test, next)
|
||||
|
||||
#define MAP0(f, x, peek, ...) f(x) MAP_NEXT(peek, MAP1)(f, peek, __VA_ARGS__)
|
||||
#define MAP1(f, x, peek, ...) f(x) MAP_NEXT(peek, MAP0)(f, peek, __VA_ARGS__)
|
||||
|
||||
#define MAP_LIST_NEXT1(test, next) MAP_NEXT0(test, MAP_COMMA next, 0)
|
||||
#define MAP_LIST_NEXT(test, next) MAP_LIST_NEXT1(MAP_GET_END test, next)
|
||||
|
||||
#define MAP_LIST0(f, x, peek, ...) f(x) MAP_LIST_NEXT(peek, MAP_LIST1)(f, peek, __VA_ARGS__)
|
||||
#define MAP_LIST1(f, x, peek, ...) f(x) MAP_LIST_NEXT(peek, MAP_LIST0)(f, peek, __VA_ARGS__)
|
||||
|
||||
/**
|
||||
* Applies the function macro `f` to each of the remaining parameters.
|
||||
*/
|
||||
#define MAP(f, ...) EVAL(MAP1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
|
||||
|
||||
/**
|
||||
* Applies the function macro `f` to each of the remaining parameters and
|
||||
* inserts commas between the results.
|
||||
*/
|
||||
#define MAP_LIST(f, ...) EVAL(MAP_LIST1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
|
||||
|
||||
#endif //QUERY_MACRO_MAP_HPP
|
||||
Reference in New Issue
Block a user