schema node analyze progress
This commit is contained in:
@@ -5,23 +5,47 @@
|
||||
|
||||
namespace matador::object {
|
||||
attribute_definition::attribute_definition(const char *name)
|
||||
: name_(name)
|
||||
, attributes_(utils::null_attributes) {
|
||||
: name_(name)
|
||||
, attributes_(utils::null_attributes) {
|
||||
}
|
||||
|
||||
attribute_definition::attribute_definition(std::string name)
|
||||
: name_(std::move(name))
|
||||
, attributes_(utils::null_attributes) {
|
||||
: name_(std::move(name))
|
||||
, attributes_(utils::null_attributes) {
|
||||
}
|
||||
|
||||
attribute_definition::attribute_definition(std::string name,
|
||||
std::string table_name,
|
||||
const utils::basic_type type,
|
||||
const utils::field_attributes& attr)
|
||||
: name_(std::move(name))
|
||||
, table_(std::move(table_name))
|
||||
, index_(0)
|
||||
, attributes_(attr)
|
||||
, value_(type, attr.size()) {
|
||||
}
|
||||
|
||||
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()) {
|
||||
: name_(std::move(name))
|
||||
, index_(index)
|
||||
, attributes_(attr)
|
||||
, null_option_(null_opt)
|
||||
, value_(type, attr.size()) {
|
||||
}
|
||||
|
||||
attribute_definition::attribute_definition(std::string name,
|
||||
std::string table_name,
|
||||
const utils::basic_type type,
|
||||
const utils::field_attributes &attr, const null_option null_opt,
|
||||
const size_t index)
|
||||
: name_(std::move(name))
|
||||
, table_(std::move(table_name))
|
||||
, index_(index)
|
||||
, attributes_(attr)
|
||||
, null_option_(null_opt)
|
||||
, value_(type, attr.size()) {
|
||||
}
|
||||
|
||||
attribute_definition::attribute_definition(std::string name,
|
||||
@@ -29,12 +53,12 @@ attribute_definition::attribute_definition(std::string name,
|
||||
const size_t index,
|
||||
const std::shared_ptr<attribute_definition> &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())
|
||||
, reference_column_(ref_column) {
|
||||
: name_(std::move(name))
|
||||
, index_(index)
|
||||
, attributes_(attr)
|
||||
, null_option_(null_opt)
|
||||
, value_(type, attr.size())
|
||||
, reference_column_(ref_column) {
|
||||
}
|
||||
|
||||
const std::string &attribute_definition::name() const {
|
||||
@@ -140,7 +164,25 @@ attribute_definition make_pk_column<std::string>(const std::string &name, size_t
|
||||
}
|
||||
|
||||
template<>
|
||||
[[maybe_unused]] attribute_definition make_fk_column<std::string>(const std::string &name, size_t size, const std::shared_ptr<attribute_definition> &ref_column) {
|
||||
attribute_definition make_fk_column<std::string>(const std::string &name, size_t size, const std::shared_ptr<attribute_definition> &ref_column) {
|
||||
return {
|
||||
name, utils::data_type_traits<std::string>::type(size), 0, ref_column,
|
||||
{size, utils::constraints::FOREIGN_KEY}, null_option::NOT_NULL
|
||||
};
|
||||
}
|
||||
|
||||
template<>
|
||||
attribute_definition make_fk_column<std::string>( const std::string& name, const std::string& ref_table_name, const std::string& ref_column_name ) {
|
||||
return {
|
||||
name, utils::basic_type::type_varchar, 0,
|
||||
std::make_shared<attribute_definition>(ref_column_name, ref_table_name, utils::basic_type::type_varchar, utils::constraints::FOREIGN_KEY),
|
||||
{ 0, utils::constraints::FOREIGN_KEY }, null_option::NOT_NULL
|
||||
};
|
||||
}
|
||||
|
||||
template<>
|
||||
attribute_definition make_fk_column<std::string>(const std::string &name, size_t size, const std::string &ref_table_name, const std::string &ref_column_name) {
|
||||
const auto ref_column = std::make_shared<attribute_definition>(ref_column_name, ref_table_name, utils::basic_type::type_varchar, utils::constraints::FOREIGN_KEY);
|
||||
return {
|
||||
name, utils::data_type_traits<std::string>::type(size), 0, ref_column,
|
||||
{size, utils::constraints::FOREIGN_KEY}, null_option::NOT_NULL
|
||||
|
||||
@@ -5,22 +5,26 @@
|
||||
#include <algorithm>
|
||||
|
||||
namespace matador::object {
|
||||
basic_object_info::basic_object_info(schema_node &node,
|
||||
const std::type_index type_index,
|
||||
basic_object_info::basic_object_info(const std::type_index type_index,
|
||||
object_definition &&definition,
|
||||
std::shared_ptr<attribute_definition> &&reference_column)
|
||||
: node_(node)
|
||||
, type_index_(type_index)
|
||||
: type_index_(type_index)
|
||||
, definition_(std::move(definition))
|
||||
, reference_column_(std::move(reference_column)) {
|
||||
}
|
||||
|
||||
basic_object_info::basic_object_info(const std::type_index type_index,
|
||||
std::shared_ptr<attribute_definition> &&reference_column)
|
||||
: type_index_(type_index)
|
||||
, reference_column_(std::move(reference_column)) {
|
||||
}
|
||||
|
||||
std::type_index basic_object_info::type_index() const {
|
||||
return type_index_;
|
||||
}
|
||||
|
||||
std::string basic_object_info::name() const {
|
||||
return node_.name();
|
||||
return node_->name();
|
||||
}
|
||||
|
||||
const object_definition &basic_object_info::definition() const {
|
||||
|
||||
@@ -2,21 +2,18 @@
|
||||
|
||||
namespace matador::object {
|
||||
object_definition::object_definition(const std::initializer_list<attribute_definition> columns)
|
||||
: columns_(columns)
|
||||
{
|
||||
: columns_(columns) {
|
||||
init();
|
||||
}
|
||||
|
||||
object_definition::object_definition(const std::vector<attribute_definition> &columns)
|
||||
: columns_(columns)
|
||||
{
|
||||
: columns_(columns) {
|
||||
init();
|
||||
}
|
||||
|
||||
object_definition::object_definition(const object_definition &x)
|
||||
: columns_(x.columns_)
|
||||
, pk_index_(x.pk_index_)
|
||||
{
|
||||
, pk_index_(x.pk_index_) {
|
||||
for (auto& col : columns_) {
|
||||
add_to_map(col, col.index());
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ utils::result<std::shared_ptr<attribute_definition>, utils::error> schema::refer
|
||||
return utils::failure(make_error(error_code::Failure, "Primary key not found"));
|
||||
}
|
||||
|
||||
return utils::ok(std::make_pair(node->name(), node->basic_info().definition().primary_key()->name()));
|
||||
return utils::ok(std::make_shared<attribute_definition>(node->name(), node->basic_info().definition().primary_key()->name(), utils::basic_type::type_null, utils::constraints::FOREIGN_KEY));
|
||||
}
|
||||
|
||||
utils::result<std::shared_ptr<schema_node>, utils::error> schema::attach_node(const std::shared_ptr<schema_node> &node,
|
||||
|
||||
@@ -5,13 +5,12 @@
|
||||
namespace matador::object {
|
||||
schema_node::schema_node(schema &tree)
|
||||
: schema_(tree)
|
||||
, info_(std::make_unique<null_info>(*this, object_definition{})) {
|
||||
, info_(std::make_unique<null_info>(object_definition{}, std::shared_ptr<attribute_definition>{})) {
|
||||
}
|
||||
|
||||
schema_node::schema_node(schema &tree, std::string name, std::unique_ptr<basic_object_info> &&info)
|
||||
: schema_(tree)
|
||||
, info_(std::move(info))
|
||||
// , info_(std::make_unique<object_info<Type>>(*this, object_definition(attribute_definition_generator::generate<Type>(schema_))))
|
||||
, first_child_(std::shared_ptr<schema_node>(new schema_node(tree)))
|
||||
, last_child_(std::shared_ptr<schema_node>(new schema_node(tree)))
|
||||
, name_(std::move(name)) {
|
||||
|
||||
@@ -229,11 +229,9 @@ void query_compiler::visit(internal::query_create_part &/*create_part*/)
|
||||
query_.sql = dialect_->token_at(sql::dialect_token::CREATE);
|
||||
}
|
||||
|
||||
struct fk_context
|
||||
{
|
||||
struct fk_context {
|
||||
std::string column;
|
||||
std::string ref_table;
|
||||
std::string ref_column;
|
||||
std::shared_ptr<object::attribute_definition> reference_column;
|
||||
};
|
||||
|
||||
struct column_context
|
||||
@@ -273,7 +271,7 @@ void query_compiler::visit(internal::query_create_table_part &create_table_part)
|
||||
result += ", CONSTRAINT FK_" + create_table_part.table().name;
|
||||
result += "_" + fk.column;
|
||||
result += " FOREIGN KEY (" + fk.column + ")";
|
||||
result += " REFERENCES " + fk.ref_table + "(" + fk.ref_column + ")";
|
||||
result += " REFERENCES " + fk.reference_column->table_name() + "(" + fk.reference_column->name() + ")";
|
||||
}
|
||||
|
||||
result += ")";
|
||||
@@ -340,7 +338,7 @@ std::string build_create_column(const object::attribute_definition &col, const s
|
||||
context.primary_keys.emplace_back(col.name());
|
||||
}
|
||||
if (is_constraint_set(col.attributes().options(), utils::constraints::FOREIGN_KEY)) {
|
||||
context.foreign_contexts.push_back({col.name(), col.ref_table(), col.ref_column()});
|
||||
context.foreign_contexts.push_back({col.name(), col.reference_column()});
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user