added more tests
This commit is contained in:
@@ -0,0 +1,176 @@
|
||||
#include "matador/object/attribute_definition.hpp"
|
||||
|
||||
#include <ostream>
|
||||
#include <utility>
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
attribute_definition::attribute_definition(const char *name)
|
||||
: name_(name)
|
||||
, attributes_(utils::null_attributes)
|
||||
{}
|
||||
|
||||
attribute_definition::attribute_definition(std::string name)
|
||||
: name_(std::move(name))
|
||||
, attributes_(utils::null_attributes)
|
||||
{}
|
||||
|
||||
attribute_definition::attribute_definition(std::string name, const utils::basic_type type, const utils::field_attributes& attr, const null_option null_opt, const size_t index)
|
||||
: name_(std::move(name))
|
||||
, index_(index)
|
||||
, attributes_(attr)
|
||||
, null_option_(null_opt)
|
||||
, value_(type, attr.size())
|
||||
{}
|
||||
|
||||
attribute_definition::attribute_definition(std::string name,
|
||||
const utils::basic_type type,
|
||||
const size_t index,
|
||||
std::string ref_table,
|
||||
std::string ref_column,
|
||||
const utils::field_attributes& attr, const null_option null_opt)
|
||||
: name_(std::move(name))
|
||||
, index_(index)
|
||||
, attributes_(attr)
|
||||
, null_option_(null_opt)
|
||||
, value_(type, attr.size())
|
||||
, ref_table_(std::move(ref_table))
|
||||
, ref_column_(std::move(ref_column))
|
||||
{}
|
||||
|
||||
const std::string &attribute_definition::name() const
|
||||
{
|
||||
return name_;
|
||||
}
|
||||
|
||||
std::string attribute_definition::full_name() const
|
||||
{
|
||||
return table_ + "." + name_;
|
||||
}
|
||||
|
||||
std::string attribute_definition::table_name() const
|
||||
{
|
||||
return table_;
|
||||
}
|
||||
|
||||
int attribute_definition::index() const
|
||||
{
|
||||
return index_;
|
||||
}
|
||||
|
||||
const utils::field_attributes &attribute_definition::attributes() const
|
||||
{
|
||||
return attributes_;
|
||||
}
|
||||
|
||||
bool attribute_definition::is_nullable() const
|
||||
{
|
||||
return null_option_ == null_option::NULLABLE;
|
||||
}
|
||||
|
||||
utils::basic_type attribute_definition::type() const
|
||||
{
|
||||
return value_.type();
|
||||
}
|
||||
|
||||
const std::string &attribute_definition::ref_table() const
|
||||
{
|
||||
return ref_table_;
|
||||
}
|
||||
|
||||
const std::string &attribute_definition::ref_column() const
|
||||
{
|
||||
return ref_column_;
|
||||
}
|
||||
|
||||
bool attribute_definition::is_foreign_reference() const
|
||||
{
|
||||
return !ref_column_.empty() && !ref_table_.empty();
|
||||
}
|
||||
|
||||
bool attribute_definition::is_integer() const
|
||||
{
|
||||
return value_.is_integer();
|
||||
}
|
||||
|
||||
bool attribute_definition::is_floating_point() const
|
||||
{
|
||||
return value_.is_floating_point();
|
||||
}
|
||||
|
||||
bool attribute_definition::is_bool() const
|
||||
{
|
||||
return value_.is_bool();
|
||||
}
|
||||
|
||||
bool attribute_definition::is_string() const
|
||||
{
|
||||
return value_.is_string();
|
||||
}
|
||||
|
||||
bool attribute_definition::is_varchar() const
|
||||
{
|
||||
return value_.is_varchar();
|
||||
}
|
||||
|
||||
bool attribute_definition::is_date() const
|
||||
{
|
||||
return value_.is_date();
|
||||
}
|
||||
|
||||
bool attribute_definition::is_time() const
|
||||
{
|
||||
return value_.is_time();
|
||||
}
|
||||
|
||||
bool attribute_definition::is_blob() const
|
||||
{
|
||||
return value_.is_blob();
|
||||
}
|
||||
|
||||
bool attribute_definition::is_null() const
|
||||
{
|
||||
return value_.is_null();
|
||||
}
|
||||
|
||||
void attribute_definition::type(utils::basic_type /*type*/)
|
||||
{
|
||||
// type_ = type;
|
||||
// utils::initialize_by_utils::basic_type(type, value_);
|
||||
}
|
||||
|
||||
std::string attribute_definition::str() const
|
||||
{
|
||||
return value_.str();
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream &out, const attribute_definition &col)
|
||||
{
|
||||
out << col.str();
|
||||
return out;
|
||||
}
|
||||
|
||||
attribute_definition make_column(const std::string &name, utils::basic_type type, utils::field_attributes attr, null_option null_opt)
|
||||
{
|
||||
return {name, type, attr, null_opt};
|
||||
}
|
||||
|
||||
template<>
|
||||
attribute_definition make_column<std::string>(const std::string &name, utils::field_attributes attr, null_option null_opt)
|
||||
{
|
||||
return make_column(name, utils::data_type_traits<std::string>::type(attr.size()), attr, null_opt);
|
||||
}
|
||||
|
||||
template<>
|
||||
attribute_definition make_pk_column<std::string>(const std::string &name, size_t size)
|
||||
{
|
||||
return make_column<std::string>(name, {size, utils::constraints::FOREIGN_KEY});
|
||||
}
|
||||
|
||||
template<>
|
||||
[[maybe_unused]] attribute_definition make_fk_column<std::string>(const std::string &name, size_t size, const std::string &ref_table,
|
||||
const std::string &ref_column)
|
||||
{
|
||||
return {name, utils::data_type_traits<std::string>::type(size), 0, ref_table, ref_column, {size, utils::constraints::FOREIGN_KEY}, null_option::NOT_NULL};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#include "matador/object/attribute_definition_generator.hpp"
|
||||
#include "matador/object/schema.hpp"
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
attribute_definition_generator::attribute_definition_generator(std::vector<object::attribute_definition> &columns, const schema &repo)
|
||||
: columns_(columns)
|
||||
, repo_(repo)
|
||||
{}
|
||||
|
||||
void attribute_definition_generator::on_primary_key(const char *id, std::string &pk, size_t size)
|
||||
{
|
||||
on_attribute(id, pk, { size, utils::constraints::PRIMARY_KEY });
|
||||
}
|
||||
|
||||
void attribute_definition_generator::on_revision(const char *id, unsigned long long int &x)
|
||||
{
|
||||
on_attribute(id, x);
|
||||
}
|
||||
|
||||
std::pair<std::string, std::string> attribute_definition_generator::determine_foreign_ref(const std::type_index &ti)
|
||||
{
|
||||
return repo_.reference(ti).value();
|
||||
}
|
||||
|
||||
void fk_attribute_generator::on_primary_key(const char *, std::string &, const size_t size)
|
||||
{
|
||||
type_ = utils::data_type_traits<std::string>::type(size);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,9 +6,10 @@
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
basic_object_info::basic_object_info(schema_node &node, const std::type_index type_index)
|
||||
basic_object_info::basic_object_info(schema_node &node, const std::type_index type_index, object_definition &&definition)
|
||||
: node_(node)
|
||||
, type_index_(type_index) {}
|
||||
, type_index_(type_index)
|
||||
, definition_(std::move(definition)) {}
|
||||
|
||||
std::type_index basic_object_info::type_index() const {
|
||||
return type_index_;
|
||||
@@ -17,4 +18,9 @@ std::type_index basic_object_info::type_index() const {
|
||||
std::string basic_object_info::name() const {
|
||||
return node_.name();
|
||||
}
|
||||
|
||||
const object_definition& basic_object_info::definition() const {
|
||||
return definition_;
|
||||
}
|
||||
|
||||
} // namespace matador::object
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
#include "matador/object/object_definition.hpp"
|
||||
|
||||
namespace matador::object {
|
||||
object_definition::object_definition(std::initializer_list<object::attribute_definition> columns)
|
||||
: columns_(columns)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
object_definition::object_definition(const std::vector<object::attribute_definition> &columns)
|
||||
: columns_(columns)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
object_definition::object_definition(const object_definition &x)
|
||||
: columns_(x.columns_)
|
||||
, pk_index_(x.pk_index_)
|
||||
{
|
||||
for (auto& col : columns_) {
|
||||
add_to_map(col, col.index());
|
||||
}
|
||||
}
|
||||
|
||||
object_definition &object_definition::operator=(const object_definition &x)
|
||||
{
|
||||
if (&x == this) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
columns_ = x.columns_;
|
||||
columns_by_name_.clear();
|
||||
pk_index_ = x.pk_index_;
|
||||
for (auto& col : columns_) {
|
||||
add_to_map(col, col.index());
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool object_definition::has_primary_key() const
|
||||
{
|
||||
return pk_index_ > -1;
|
||||
}
|
||||
|
||||
std::optional<object::attribute_definition> object_definition::primary_key() const
|
||||
{
|
||||
if (!has_primary_key()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return columns_[pk_index_];
|
||||
}
|
||||
|
||||
void object_definition::append(attribute_definition col)
|
||||
{
|
||||
auto &ref = columns_.emplace_back(std::move(col));
|
||||
add_to_map(ref, columns_.size()-1);
|
||||
}
|
||||
|
||||
const std::vector<object::attribute_definition> &object_definition::columns() const
|
||||
{
|
||||
return columns_;
|
||||
}
|
||||
|
||||
const attribute_definition &object_definition::at(const std::string &name) const
|
||||
{
|
||||
return columns_by_name_.at(name).first;
|
||||
}
|
||||
|
||||
const attribute_definition &object_definition::at( const size_t index) const
|
||||
{
|
||||
return columns_.at(index);
|
||||
}
|
||||
|
||||
object_definition::iterator object_definition::find(const std::string &column_name)
|
||||
{
|
||||
auto it = columns_by_name_.find(column_name);
|
||||
return it != columns_by_name_.end() ? columns_.begin() + it->second.second : columns_.end();
|
||||
}
|
||||
|
||||
object_definition::const_iterator object_definition::find(const std::string &column_name) const {
|
||||
auto it = columns_by_name_.find(column_name);
|
||||
return it != columns_by_name_.end() ? columns_.begin() + it->second.second : columns_.end();
|
||||
}
|
||||
|
||||
object_definition::iterator object_definition::begin()
|
||||
{
|
||||
return columns_.begin();
|
||||
}
|
||||
|
||||
object_definition::const_iterator object_definition::begin() const
|
||||
{
|
||||
return columns_.begin();
|
||||
}
|
||||
|
||||
object_definition::const_iterator object_definition::cbegin() const
|
||||
{
|
||||
return columns_.cbegin();
|
||||
}
|
||||
|
||||
object_definition::iterator object_definition::end()
|
||||
{
|
||||
return columns_.end();
|
||||
}
|
||||
|
||||
object_definition::const_iterator object_definition::end() const
|
||||
{
|
||||
return columns_.end();
|
||||
}
|
||||
|
||||
object_definition::const_iterator object_definition::cend() const
|
||||
{
|
||||
return columns_.cend();
|
||||
}
|
||||
|
||||
size_t object_definition::size() const
|
||||
{
|
||||
return columns_.size();
|
||||
}
|
||||
|
||||
bool object_definition::empty() const
|
||||
{
|
||||
return columns_.empty();
|
||||
}
|
||||
|
||||
void object_definition::clear()
|
||||
{
|
||||
columns_.clear();
|
||||
columns_by_name_.clear();
|
||||
}
|
||||
|
||||
void object_definition::init()
|
||||
{
|
||||
size_t index{0};
|
||||
for(auto &col : columns_) {
|
||||
add_to_map(col, index++);
|
||||
}
|
||||
}
|
||||
|
||||
void object_definition::add_to_map(attribute_definition &col, size_t index)
|
||||
{
|
||||
columns_by_name_.emplace(col.name(), column_index_pair {std::ref(col), index});
|
||||
if (is_constraint_set(col.attributes().options(), utils::constraints::PRIMARY_KEY)) {
|
||||
pk_index_ = static_cast<int>(index);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -35,6 +35,10 @@ std::string schema::name() const {
|
||||
return name_;
|
||||
}
|
||||
|
||||
utils::result<std::pair<std::string, std::string>, utils::error> schema::reference( const std::type_index& type_index ) const {
|
||||
return utils::ok(std::pair<std::string, std::string>{});
|
||||
}
|
||||
|
||||
utils::result<std::shared_ptr<schema_node>, utils::error> schema::attach_node(const std::shared_ptr<schema_node> &node,
|
||||
const std::string &parent) {
|
||||
if (has_node(node->type_index(), node->name())) {
|
||||
|
||||
@@ -4,9 +4,9 @@
|
||||
|
||||
namespace matador::object {
|
||||
|
||||
schema_node::schema_node(schema &tree)
|
||||
schema_node::schema_node(object::schema &tree)
|
||||
: schema_(tree)
|
||||
, info_(std::make_unique<null_info>(*this))
|
||||
, info_(std::make_unique<null_info>(*this, object_definition{}))
|
||||
{}
|
||||
|
||||
std::string schema_node::name() const {
|
||||
@@ -17,45 +17,8 @@ std::type_index schema_node::type_index() const {
|
||||
return info_->type_index();
|
||||
}
|
||||
|
||||
void schema_node::append(const std::shared_ptr<schema_node> &sibling) {
|
||||
sibling->parent_ = parent_;
|
||||
|
||||
// sibling->previous_sibling_ = this;
|
||||
// sibling->next_sibling_ = next_sibling_;
|
||||
// next_sibling_ = sibling;
|
||||
// sibling->next_sibling_->previous_sibling_ = sibling;
|
||||
// sibling->depth = depth;
|
||||
|
||||
// if (!parent_) {
|
||||
// sibling->op_first = new object_proxy();
|
||||
// sibling->op_last = sibling->op_marker = new object_proxy();
|
||||
// sibling->op_first->link(sibling->op_last);
|
||||
// } else {
|
||||
// throw object_exception("failed to add node as sibling: node has no parent");
|
||||
// }
|
||||
}
|
||||
|
||||
void schema_node::insert(const std::shared_ptr<schema_node> &child) {
|
||||
// child->parent_ = this;
|
||||
// child->previous_sibling_ = last_child_->previous_sibling_;
|
||||
// child->next_sibling_ = last_child_;
|
||||
// last_child_->previous_sibling_->next_sibling_ = child;
|
||||
// last_child_->previous_sibling_ = child;
|
||||
// set depth
|
||||
// child->depth = depth + 1;
|
||||
// set object proxy pointer
|
||||
// 1. first
|
||||
// if (op_first->next() == op_last) {
|
||||
// // node hasn't any serializable (proxy)
|
||||
// child->op_first = op_first;
|
||||
// } else {
|
||||
// // node has some objects (proxy)
|
||||
// child->op_first = op_last->prev();
|
||||
// }
|
||||
// // 2. marker
|
||||
// child->op_marker = op_last;
|
||||
// // 3. last
|
||||
// child->op_last = op_last;
|
||||
const basic_object_info& schema_node::basic_info() const {
|
||||
return *info_;
|
||||
}
|
||||
|
||||
schema_node::node_ptr schema_node::next() const {
|
||||
|
||||
Reference in New Issue
Block a user