column, placeholder and column_value generator progress
This commit is contained in:
@@ -6,28 +6,69 @@
|
||||
#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/sql/table.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace matador::query::generator {
|
||||
class column_generator final {
|
||||
enum class column_generator_options {
|
||||
None = 0,
|
||||
GenerateAlias = 1 << 0,
|
||||
ForceLazy = 1 << 1
|
||||
};
|
||||
|
||||
template<typename EnumType>
|
||||
class enum_flags {
|
||||
public:
|
||||
template < class V >
|
||||
explicit enum_flags(EnumType value) : value_(value) {}
|
||||
private:
|
||||
EnumType value_;
|
||||
};
|
||||
|
||||
inline column_generator_options operator|(column_generator_options a, column_generator_options b) { return static_cast<column_generator_options>(static_cast<unsigned int>(a) | static_cast<unsigned int>(b)); }
|
||||
inline column_generator_options operator&(column_generator_options a, column_generator_options b) { return static_cast<column_generator_options>(static_cast<unsigned int>(a) & static_cast<unsigned int>(b)); }
|
||||
inline column_generator_options& operator|= (column_generator_options& a, column_generator_options b) { return reinterpret_cast<column_generator_options&>(reinterpret_cast<int&>(a) |= static_cast<int>(b)); }
|
||||
inline column_generator_options& operator&= (column_generator_options& a, column_generator_options b) { return reinterpret_cast<column_generator_options&>(reinterpret_cast<int&>(a) &= static_cast<int>(b)); }
|
||||
|
||||
inline bool is_column_generator_option_set(const column_generator_options source, const column_generator_options needle) { return static_cast<int>(source & needle) > 0; }
|
||||
|
||||
constexpr auto default_column_generator_options = column_generator_options::ForceLazy;
|
||||
|
||||
class column_generator2 {
|
||||
public:
|
||||
explicit column_generator2(const std::string &table_name = "",
|
||||
column_generator_options options = default_column_generator_options);
|
||||
explicit column_generator2(const object::repository &repo,
|
||||
const std::string &table_name = "",
|
||||
column_generator_options options = default_column_generator_options);
|
||||
|
||||
template< class Type >
|
||||
std::vector<sql::column> generate() {
|
||||
Type obj;
|
||||
return generate(obj);
|
||||
}
|
||||
|
||||
template< class Type >
|
||||
std::vector<sql::column> generate(const Type &obj) {
|
||||
result_.clear();
|
||||
access::process(*this, obj);
|
||||
|
||||
return result_;
|
||||
}
|
||||
|
||||
template < class V >
|
||||
void on_primary_key(const char *id, V &, const utils::primary_key_attribute& /*attr*/ = utils::default_pk_attributes) {
|
||||
if (has_many_to_many_) {
|
||||
return;
|
||||
}
|
||||
push(id);
|
||||
}
|
||||
void on_revision(const char *id, uint64_t &/*rev*/);
|
||||
|
||||
template<typename Type>
|
||||
void on_attribute(const char *id, Type &, const utils::field_attributes &/*attr*/ = utils::null_attributes) {
|
||||
if (has_many_to_many_) {
|
||||
return;
|
||||
}
|
||||
push(id);
|
||||
}
|
||||
|
||||
@@ -42,32 +83,29 @@ public:
|
||||
|
||||
template<class ContainerType>
|
||||
void on_has_many(const char * /*id*/, ContainerType &, const char *, const utils::foreign_attributes &attr) {
|
||||
if (has_many_to_many_) {
|
||||
if (attr.fetch() == utils::fetch_type::LAZY || is_column_generator_option_set(options_, column_generator_options::ForceLazy)) {
|
||||
return;
|
||||
}
|
||||
if (attr.fetch() == utils::fetch_type::LAZY || force_lazy_) {
|
||||
if (!repo_) {
|
||||
return;
|
||||
}
|
||||
const auto info = table_schema_.info<typename ContainerType::value_type::value_type>();
|
||||
const auto info = repo_->get().info<typename ContainerType::value_type::value_type>();
|
||||
if (!info) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (seen_tables.count(info->get().name()) == 0) {
|
||||
auto it = seen_tables.insert(info->get().name()).first;
|
||||
table_name_stack_.push(info.value().get().name());
|
||||
table_stack_.push(std::make_shared<sql::table>(info.value().get().name()));
|
||||
typename ContainerType::value_type::value_type obj;
|
||||
access::process(*this, obj);
|
||||
table_name_stack_.pop();
|
||||
table_stack_.pop();
|
||||
seen_tables.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
template<class ContainerType>
|
||||
void on_has_many_to_many(const char * /*id*/, ContainerType & /*cont*/, const char *join_column, const char *inverse_join_column, const utils::foreign_attributes &/*attr*/) {
|
||||
if (!has_many_to_many_) {
|
||||
return;
|
||||
}
|
||||
push(join_column);
|
||||
push(inverse_join_column);
|
||||
}
|
||||
@@ -75,7 +113,41 @@ public:
|
||||
template<class ContainerType>
|
||||
static void on_has_many_to_many(const char * /*id*/, ContainerType & /*cont*/, const utils::foreign_attributes &/*attr*/) {}
|
||||
|
||||
private:
|
||||
template<class Pointer>
|
||||
void on_foreign_key(const char *id, Pointer &, const utils::foreign_attributes &attr) {
|
||||
if (attr.fetch() == utils::fetch_type::LAZY || is_column_generator_option_set(options_, column_generator_options::ForceLazy)) {
|
||||
push(id);
|
||||
} else {
|
||||
if (!repo_) {
|
||||
return;
|
||||
}
|
||||
const auto info = repo_->get().info<typename Pointer::value_type>();
|
||||
if (!info) {
|
||||
return;
|
||||
}
|
||||
if (seen_tables.count(info->get().name()) == 0) {
|
||||
auto it = seen_tables.insert(info->get().name()).first;
|
||||
table_stack_.push(std::make_shared<sql::table>(info.value().get().name()));
|
||||
typename Pointer::value_type obj;
|
||||
access::process(*this, obj);
|
||||
table_stack_.pop();
|
||||
seen_tables.erase(it);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void push(const std::string &column_name);
|
||||
|
||||
private:
|
||||
std::optional<std::reference_wrapper<const object::repository>> repo_;
|
||||
std::vector<sql::column> result_;
|
||||
std::stack<std::shared_ptr<sql::table>> table_stack_;
|
||||
std::unordered_set<std::string> seen_tables;
|
||||
int column_index{0};
|
||||
column_generator_options options_{false};
|
||||
};
|
||||
|
||||
class placeholder_generator final {
|
||||
public:
|
||||
template< class Type >
|
||||
@@ -174,17 +246,43 @@ std::vector<utils::placeholder> placeholders(const Type &obj) {
|
||||
return generator.generate(obj);
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
std::vector<internal::column_value_pair> column_value_pairs() {
|
||||
column_value_generator generator;
|
||||
return generator.generate<Type>();
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
std::vector<internal::column_value_pair> column_value_pairs(const Type &obj) {
|
||||
column_value_generator generator;
|
||||
return generator.generate(obj);
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
std::vector<sql::column> columns(const object::repository &repo,
|
||||
const std::string &table_name = "",
|
||||
const column_generator_options options = default_column_generator_options) {
|
||||
column_generator2 generator(repo, table_name, options);
|
||||
return generator.generate<Type>();
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
std::vector<sql::column> columns(const Type &obj,
|
||||
const object::repository &repo,
|
||||
const std::string &table_name = "",
|
||||
const column_generator_options options = default_column_generator_options) {
|
||||
column_generator2 generator(repo, table_name, options);
|
||||
return generator.generate(obj);
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
std::vector<sql::column> columns(const std::string &table_name = "",
|
||||
const column_generator_options options = default_column_generator_options) {
|
||||
column_generator2 generator(table_name, options);
|
||||
return generator.generate<Type>();
|
||||
}
|
||||
|
||||
template<typename Type>
|
||||
std::vector<sql::column> columns(const Type &obj,
|
||||
const std::string &table_name = "",
|
||||
const column_generator_options options = default_column_generator_options) {
|
||||
column_generator2 generator(table_name, options);
|
||||
return generator.generate(obj);
|
||||
}
|
||||
|
||||
}
|
||||
#endif //MATADOR_GENERATOR_HPP
|
||||
@@ -15,11 +15,14 @@ public:
|
||||
column_value_pair(const char *name, utils::database_type value);
|
||||
column_value_pair(const char *name, utils::placeholder p);
|
||||
|
||||
[[nodiscard]] const std::string& name() const;
|
||||
friend bool operator==(const column_value_pair &lhs, const column_value_pair &rhs);
|
||||
friend bool operator!=(const column_value_pair &lhs, const column_value_pair &rhs);
|
||||
|
||||
[[nodiscard]] const sql::column& col() const;
|
||||
[[nodiscard]] const std::variant<utils::placeholder, utils::database_type>& value() const;
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
sql::column column_;
|
||||
std::variant<utils::placeholder, utils::database_type> value_;
|
||||
};
|
||||
|
||||
|
||||
@@ -320,7 +320,7 @@ class query_set_part final : public query_part
|
||||
public:
|
||||
explicit query_set_part(const std::vector<internal::column_value_pair>& key_value_pairs);
|
||||
|
||||
[[nodiscard]] const std::vector<internal::column_value_pair>& key_values() const;
|
||||
[[nodiscard]] const std::vector<internal::column_value_pair>& column_values() const;
|
||||
|
||||
private:
|
||||
void accept(query_part_visitor &visitor) override;
|
||||
|
||||
Reference in New Issue
Block a user