fixed core- and orm-tests. postgres-tests needs some work
This commit is contained in:
@@ -7,11 +7,11 @@
|
||||
|
||||
namespace matador::object {
|
||||
attribute_definition::attribute_definition(const char *name)
|
||||
: attribute_definition(name, utils::basic_type::type_null, {utils::null_attributes}) {
|
||||
: attribute_definition(name, utils::basic_type::type_null, "", {utils::null_attributes}) {
|
||||
}
|
||||
|
||||
attribute_definition::attribute_definition(std::string name)
|
||||
: attribute_definition(std::move(name), utils::basic_type::type_null, {utils::null_attributes}) {
|
||||
: attribute_definition(std::move(name), utils::basic_type::type_null, "", {utils::null_attributes}) {
|
||||
}
|
||||
|
||||
attribute_definition::attribute_definition(std::string name,
|
||||
@@ -19,7 +19,7 @@ attribute_definition::attribute_definition(std::string name,
|
||||
const utils::field_attributes &attr,
|
||||
const null_option_type null_opt,
|
||||
const int index)
|
||||
: attribute_definition(std::move(name), type, {attr, null_opt, index}) {
|
||||
: attribute_definition(std::move(name), type, "", {attr, null_opt, index}) {
|
||||
}
|
||||
|
||||
attribute_definition::attribute_definition(std::string name,
|
||||
@@ -28,16 +28,20 @@ attribute_definition::attribute_definition(std::string name,
|
||||
const std::shared_ptr<attribute_definition> &ref_column,
|
||||
const utils::field_attributes &attr,
|
||||
const null_option_type null_opt)
|
||||
: attribute_definition(std::move(name), type, {attr, null_opt, index}, {}, ref_column) {
|
||||
: attribute_definition(std::move(name), type, "", {attr, null_opt, index}, ref_column) {
|
||||
}
|
||||
|
||||
attribute_definition::attribute_definition( std::string name, const utils::basic_type type, const std::shared_ptr<attribute_definition>& ref_column )
|
||||
: attribute_definition(std::move(name), type, {}, {}, ref_column) {
|
||||
}
|
||||
|
||||
attribute_definition::attribute_definition(std::string name, const utils::basic_type type, const attribute_options& options, const std::shared_ptr<object_definition>& obj, const std::shared_ptr<attribute_definition>& ref_column)
|
||||
: name_( std::move( name ) )
|
||||
, object_( obj )
|
||||
attribute_definition::attribute_definition(std::string name,
|
||||
const utils::basic_type type,
|
||||
std::string table_name,
|
||||
const attribute_options& options,
|
||||
const std::shared_ptr<attribute_definition>& ref_column)
|
||||
: name_(std::move(name))
|
||||
, table_name_(std::move(table_name))
|
||||
, options_( options )
|
||||
, type_( type )
|
||||
, reference_column_( ref_column ) {
|
||||
@@ -52,17 +56,17 @@ void attribute_definition::name( const std::string& n ) {
|
||||
}
|
||||
|
||||
std::string attribute_definition::full_name() const {
|
||||
return object_ ? object_->name() + "." + name_ : name_;
|
||||
}
|
||||
|
||||
std::shared_ptr<object_definition> attribute_definition::object() const {
|
||||
return object_;
|
||||
}
|
||||
|
||||
void attribute_definition::object(const std::shared_ptr<object_definition>& obj) {
|
||||
object_ = obj;
|
||||
return !table_name_.empty() ? table_name_ + "." + name_ : name_;
|
||||
}
|
||||
|
||||
// std::shared_ptr<object_definition> attribute_definition::object() const {
|
||||
// return object_;
|
||||
// }
|
||||
//
|
||||
// void attribute_definition::object(const std::shared_ptr<object_definition>& obj) {
|
||||
// object_ = obj;
|
||||
// }
|
||||
//
|
||||
int attribute_definition::index() const {
|
||||
return options_.index;
|
||||
}
|
||||
@@ -83,9 +87,17 @@ utils::basic_type attribute_definition::type() const {
|
||||
return type_;
|
||||
}
|
||||
|
||||
const std::string& attribute_definition::table_name() const {
|
||||
return table_name_;
|
||||
}
|
||||
|
||||
void attribute_definition::table_name( const std::string& name ) {
|
||||
table_name_ = name;
|
||||
}
|
||||
|
||||
void attribute_definition::change_type(const utils::basic_type type, const utils::field_attributes& attr) {
|
||||
options_.attributes = attr;
|
||||
type_ = type;
|
||||
options_.attributes = attr;
|
||||
type_ = type;
|
||||
}
|
||||
|
||||
std::shared_ptr<attribute_definition> attribute_definition::reference_column() const {
|
||||
@@ -160,14 +172,14 @@ 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, std::make_shared<object_definition>(ref_table_name), utils::basic_type::type_varchar, utils::constraints::FOREIGN_KEY),
|
||||
std::make_shared<attribute_definition>(ref_column_name, utils::basic_type::type_varchar, ref_table_name, attribute_options{utils::constraints::FOREIGN_KEY}),
|
||||
{ 0, utils::constraints::FOREIGN_KEY }, null_option_type::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);
|
||||
const auto ref_column = std::make_shared<attribute_definition>(ref_column_name, utils::basic_type::type_varchar, ref_table_name, attribute_options{utils::constraints::FOREIGN_KEY});
|
||||
return {
|
||||
name, utils::data_type_traits<std::string>::type(size), 0, ref_column,
|
||||
{size, utils::constraints::FOREIGN_KEY}, null_option_type::NOT_NULL
|
||||
|
||||
@@ -5,28 +5,30 @@
|
||||
#include <algorithm>
|
||||
|
||||
namespace matador::object {
|
||||
// basic_object_info::basic_object_info(std::shared_ptr<repository_node> node,
|
||||
// utils::identifier &&pk,
|
||||
// const std::shared_ptr<attribute_definition> &pk_column,
|
||||
// const std::shared_ptr<object_definition> &definition)
|
||||
// : node_(std::move(node))
|
||||
// , definition_(definition)
|
||||
// , identifier_(std::move(pk))
|
||||
// , pk_column_(pk_column) {
|
||||
// }
|
||||
|
||||
basic_object_info::basic_object_info(std::shared_ptr<repository_node> node,
|
||||
const std::vector<attribute_definition> &attributes,
|
||||
utils::identifier &&pk,
|
||||
const std::shared_ptr<attribute_definition> &pk_column,
|
||||
const std::shared_ptr<object_definition> &definition)
|
||||
const std::shared_ptr<attribute_definition> &pk_as_fk_column)
|
||||
: node_(std::move(node))
|
||||
, definition_(definition)
|
||||
, attributes_(attributes)
|
||||
, identifier_(std::move(pk))
|
||||
, pk_column_(pk_column) {
|
||||
, pk_as_fk_column_(pk_as_fk_column) {
|
||||
}
|
||||
|
||||
basic_object_info::basic_object_info(std::shared_ptr<repository_node> node,
|
||||
utils::identifier &&pk,
|
||||
const std::shared_ptr<attribute_definition> &pk_column)
|
||||
const std::vector<attribute_definition> &attributes)
|
||||
: node_(std::move(node))
|
||||
, identifier_(std::move(pk))
|
||||
, pk_column_(pk_column) {
|
||||
}
|
||||
|
||||
basic_object_info::basic_object_info(std::shared_ptr<repository_node> node,
|
||||
const std::shared_ptr<object_definition> &definition)
|
||||
: node_(std::move(node))
|
||||
, definition_(definition) {}
|
||||
, attributes_(attributes) {}
|
||||
|
||||
std::type_index basic_object_info::type_index() const {
|
||||
return node_->type_index();
|
||||
@@ -36,12 +38,12 @@ std::string basic_object_info::name() const {
|
||||
return node_->name();
|
||||
}
|
||||
|
||||
std::shared_ptr<object_definition> basic_object_info::definition() const {
|
||||
return definition_;
|
||||
const std::vector<attribute_definition>& basic_object_info::attributes() const {
|
||||
return attributes_;
|
||||
}
|
||||
|
||||
std::shared_ptr<attribute_definition> basic_object_info::reference_column() const {
|
||||
return pk_column_;
|
||||
return pk_as_fk_column_;
|
||||
}
|
||||
|
||||
bool basic_object_info::has_primary_key() const {
|
||||
|
||||
@@ -4,17 +4,17 @@
|
||||
#include "matador/object/repository_node.hpp"
|
||||
|
||||
namespace matador::object {
|
||||
repository_node::repository_node(object::repository &repo)
|
||||
repository_node::repository_node(repository &repo)
|
||||
: repo_(repo)
|
||||
, type_index_(typeid(detail::null_type)){
|
||||
}
|
||||
|
||||
repository_node::repository_node(object::repository &repo, const std::type_index& ti)
|
||||
repository_node::repository_node(repository &repo, const std::type_index& ti)
|
||||
: repo_(repo)
|
||||
, type_index_(ti) {
|
||||
}
|
||||
|
||||
repository_node::repository_node(object::repository &repo, std::string name, const std::type_index& ti)
|
||||
repository_node::repository_node(repository &repo, std::string name, const std::type_index& ti)
|
||||
: repo_(repo)
|
||||
, type_index_(ti)
|
||||
, first_child_(std::shared_ptr<repository_node>(new repository_node(repo)))
|
||||
@@ -24,9 +24,9 @@ repository_node::repository_node(object::repository &repo, std::string name, con
|
||||
last_child_->previous_sibling_ = first_child_;
|
||||
}
|
||||
|
||||
std::shared_ptr<repository_node> repository_node::make_null_node(object::repository &repo) {
|
||||
std::shared_ptr<repository_node> repository_node::make_null_node(repository &repo) {
|
||||
auto node = std::shared_ptr<repository_node>(new repository_node(repo));
|
||||
node->info_ = std::make_unique<null_info>(node, std::shared_ptr<attribute_definition>{});
|
||||
node->info_ = std::make_unique<null_info>(node/*, std::shared_ptr<attribute_definition>{}*/);
|
||||
|
||||
return node;
|
||||
}
|
||||
@@ -45,8 +45,8 @@ const basic_object_info &repository_node::info() const {
|
||||
|
||||
void repository_node::update_name(const std::string& name) {
|
||||
name_ = name;
|
||||
if (info_->reference_column() && info_->reference_column()->object() != nullptr) {
|
||||
info_->reference_column()->object()->name_ = name;
|
||||
if (info_->reference_column()) {
|
||||
info_->reference_column()->table_name(name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,20 +104,23 @@ utils::result<repository_node::node_ptr, utils::error> repository_node::make_and
|
||||
return repo.attach_node(node, "");
|
||||
}
|
||||
|
||||
std::shared_ptr<attribute_definition> repository_node::determine_reference_column(const std::type_index& ti, const std::shared_ptr<object_definition>& obj, const primary_key_info& pk_info, repository& repo) {
|
||||
std::shared_ptr<attribute_definition> repository_node::determine_reference_column(const std::type_index& ti,
|
||||
const std::string& table_name,
|
||||
const primary_key_info& pk_info,
|
||||
repository& repo) {
|
||||
const auto it = repo.missing_references_.find(ti);
|
||||
if (it == repo.missing_references_.end()) {
|
||||
return std::make_shared<attribute_definition>(pk_info.pk_column_name, pk_info.type, attribute_options{utils::constraints::FOREIGN_KEY});
|
||||
return std::make_shared<attribute_definition>(pk_info.pk_column_name, pk_info.type, table_name, attribute_options{utils::constraints::FOREIGN_KEY});
|
||||
}
|
||||
|
||||
auto ref_column = it->second;
|
||||
repo.missing_references_.erase(it);
|
||||
ref_column->name(pk_info.pk_column_name);
|
||||
ref_column->object(obj);
|
||||
ref_column->table_name(table_name);
|
||||
ref_column->change_type(pk_info.type);
|
||||
ref_column->attributes() = utils::constraints::FOREIGN_KEY;
|
||||
|
||||
if (obj->name().empty()) {
|
||||
if (table_name.empty()) {
|
||||
repo.missing_references_.insert({ti, ref_column});
|
||||
}
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ matador::utils::result<void, matador::utils::error> matador::orm::schema::create
|
||||
// std::cout << result.sql << std::endl;
|
||||
for (const auto &node: repo_) {
|
||||
auto ctx = query::query::create()
|
||||
.table(node->name(), node->info().definition()->columns())
|
||||
.table(node->name(), node->info().attributes())
|
||||
.compile(*c);
|
||||
|
||||
for ( const auto& [sql, command] : ctx.additional_commands ) {
|
||||
|
||||
@@ -52,7 +52,7 @@ utils::result<void, utils::error> session::create_schema() const {
|
||||
auto c = cache_.pool().acquire();
|
||||
for (const auto &node: *schema_) {
|
||||
auto ctx = query::query::create()
|
||||
.table(node->name(), node->info().definition()->columns())
|
||||
.table(node->name(), node->info().attributes())
|
||||
.compile(*c);
|
||||
|
||||
for ( const auto& [sql, command] : ctx.additional_commands ) {
|
||||
@@ -100,7 +100,9 @@ utils::result<sql::query_result<sql::record>, utils::error> session::fetch_all(c
|
||||
}
|
||||
// adjust columns from given query
|
||||
for (auto &col: q.prototype) {
|
||||
if (const auto rit = it->second.find(col.name()); rit != it->second.end()) {
|
||||
if (const auto rit = std::find_if(it->second.begin(), it->second.end(), [&col](const auto &value) {
|
||||
return value.name() == col.name();
|
||||
}); rit != it->second.end()) {
|
||||
const_cast<object::attribute_definition &>(col).change_type(rit->type());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,14 +138,14 @@ void query_compiler::visit(internal::query_select_part &part)
|
||||
}
|
||||
|
||||
void query_compiler::visit(internal::query_from_part &part) {
|
||||
query_.table_name = part.table().alias();
|
||||
query_.sql += " " + build_table_name(part.token(), *dialect_, query_.table_name);
|
||||
query_.table_name = part.table().name();
|
||||
query_.sql += " " + build_table_name(part.token(), *dialect_, part.table());
|
||||
query_.table_aliases.insert({query_.table_name, part.table().alias()});
|
||||
}
|
||||
|
||||
void query_compiler::visit(internal::query_join_part &part)
|
||||
{
|
||||
query_.sql += " " + query_compiler::build_table_name(part.token(), *dialect_, part.table());
|
||||
query_.sql += " " + build_table_name(part.token(), *dialect_, part.table());
|
||||
}
|
||||
|
||||
void query_compiler::visit(internal::query_on_part &part) {
|
||||
@@ -335,7 +335,7 @@ void query_compiler::visit(internal::query_create_table_part &part)
|
||||
fk_cmd += " CONSTRAINT FK_" + query_.table_name;
|
||||
fk_cmd += "_" + column;
|
||||
fk_cmd += " FOREIGN KEY (" + dialect_->prepare_identifier_string(column) + ")";
|
||||
fk_cmd += " REFERENCES " + reference_column->object()->name() + "(" + reference_column->name() + ")";
|
||||
fk_cmd += " REFERENCES " + reference_column->table_name() + "(" + reference_column->name() + ")";
|
||||
query_.additional_commands.push_back({fk_cmd, sql::sql_command::SQL_ALTER_TABLE});
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "matador/sql/interface/connection_impl.hpp"
|
||||
|
||||
#include "matador/utils/string.hpp"
|
||||
#include "matador/utils/value.hpp"
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user