From dc46fc5fb01dff71d63b1008d5dd46885cb06fa3 Mon Sep 17 00:00:00 2001 From: bergmann Date: Wed, 26 Sep 2018 19:51:10 +0200 Subject: [PATCH] * fixed bug in mariadb driver (init stage2 and filters) --- .../driver/mariadb/schema/filter.h | 4 +-- .../driver/mariadb/schema/table.cpp | 26 +++++++++---------- test/cpphibernate_init.cpp | 20 +++++++------- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/include/cpphibernate/driver/mariadb/schema/filter.h b/include/cpphibernate/driver/mariadb/schema/filter.h index 92c9523..c1cc432 100644 --- a/include/cpphibernate/driver/mariadb/schema/filter.h +++ b/include/cpphibernate/driver/mariadb/schema/filter.h @@ -30,8 +30,8 @@ beg_namespace_cpphibernate_driver_mariadb using field_set_type = std::set; using table_set_type = std::set; - size_t cache_id { 0 }; - bool exclusive; + size_t cache_id { 0 }; + bool exclusive { true }; field_set_type fields; table_set_type tables; diff --git a/src/cpphibernate/driver/mariadb/schema/table.cpp b/src/cpphibernate/driver/mariadb/schema/table.cpp index 85cfca1..ce02b85 100644 --- a/src/cpphibernate/driver/mariadb/schema/table.cpp +++ b/src/cpphibernate/driver/mariadb/schema/table.cpp @@ -308,11 +308,10 @@ struct select_query_builder_t if (_filter.is_excluded(ctx.table)) return ret; - ret = true; - /* primary key */ assert(ctx.table.primary_key_field); add_field(*ctx.table.primary_key_field, real_alias); + ret = true; /* data fields */ for (auto& ptr : ctx.table.data_fields) @@ -320,7 +319,9 @@ struct select_query_builder_t assert(ptr); auto& field = *ptr; if (!_filter.is_excluded(field)) + { add_field(field, real_alias); + } } /* foreign table one */ @@ -401,17 +402,13 @@ struct select_query_builder_t << "`"; auto it = joins.insert(joins.end(), ss.str()); - if (add_table({ + if (!add_table({ derived_table, derived_alias, false, true, ctx.is_dynamic, })) - { - ret = true; - } - else { joins.erase(it); } @@ -826,7 +823,7 @@ std::string build_init_stage2_query(const table_t& table) << "`" << incindent << indent - << "FOREIGN KEY (`" + << "FOREIGN KEY IF NOT EXISTS (`" << ref_key_info.field_name << "`)" << indent @@ -863,7 +860,7 @@ std::string build_init_stage2_query(const table_t& table) << "`" << incindent << indent - << "FOREIGN KEY (`" + << "FOREIGN KEY IF NOT EXISTS (`" << ref_key_info.table_name << "_id_" << field_info.field_name @@ -876,9 +873,12 @@ std::string build_init_stage2_query(const table_t& table) << "` (`" << ref_key_info.field_name << "`)" - << indent - << "ON DELETE SET NULL" - << indent + << indent; + if (field_info.value_is_nullable) + os << "ON DELETE SET NULL"; + else + os << "ON DELETE CASCADE"; + os << indent << "ON UPDATE NO ACTION" << decindent; } @@ -902,7 +902,7 @@ std::string build_init_stage2_query(const table_t& table) << "`" << incindent << indent - << "FOREIGN KEY (`" + << "FOREIGN KEY IF NOT EXISTS (`" << field_info.table_name << "_id_" << field_info.field_name diff --git a/test/cpphibernate_init.cpp b/test/cpphibernate_init.cpp index ee892ab..1e36dbb 100644 --- a/test/cpphibernate_init.cpp +++ b/test/cpphibernate_init.cpp @@ -153,53 +153,53 @@ TEST(CppHibernateTests, init) expect_query(mock, "ALTER TABLE `tbl_test3`\n" " ADD CONSTRAINT `fk_tbl_test3_to_tbl_derived3_id_test3_list`\n" - " FOREIGN KEY (`tbl_derived3_id_test3_list`)\n" + " FOREIGN KEY IF NOT EXISTS (`tbl_derived3_id_test3_list`)\n" " REFERENCES `test`.`tbl_derived3` (`tbl_derived3_id`)\n" " ON DELETE SET NULL\n" " ON UPDATE NO ACTION,\n" " ADD CONSTRAINT `fk_tbl_test3_to_tbl_derived3_id_test3_vector`\n" - " FOREIGN KEY (`tbl_derived3_id_test3_vector`)\n" + " FOREIGN KEY IF NOT EXISTS (`tbl_derived3_id_test3_vector`)\n" " REFERENCES `test`.`tbl_derived3` (`tbl_derived3_id`)\n" " ON DELETE SET NULL\n" " ON UPDATE NO ACTION"); expect_query(mock, "ALTER TABLE `tbl_derived1`\n" " ADD CONSTRAINT `fk_tbl_derived1_to_tbl_base_id`\n" - " FOREIGN KEY (`tbl_base_id`)\n" + " FOREIGN KEY IF NOT EXISTS (`tbl_base_id`)\n" " REFERENCES `test`.`tbl_base` (`tbl_base_id`)\n" " ON DELETE CASCADE\n" " ON UPDATE CASCADE,\n" " ADD CONSTRAINT `fk_tbl_derived1_to_tbl_test1_id_test1_data`\n" - " FOREIGN KEY (`tbl_test1_id_test1_data`)\n" + " FOREIGN KEY IF NOT EXISTS (`tbl_test1_id_test1_data`)\n" " REFERENCES `test`.`tbl_test1` (`tbl_test1_id`)\n" - " ON DELETE SET NULL\n" + " ON DELETE CASCADE\n" " ON UPDATE NO ACTION"); expect_query(mock, "ALTER TABLE `tbl_derived2`\n" " ADD CONSTRAINT `fk_tbl_derived2_to_tbl_base_id`\n" - " FOREIGN KEY (`tbl_base_id`)\n" + " FOREIGN KEY IF NOT EXISTS (`tbl_base_id`)\n" " REFERENCES `test`.`tbl_base` (`tbl_base_id`)\n" " ON DELETE CASCADE\n" " ON UPDATE CASCADE,\n" " ADD CONSTRAINT `fk_tbl_derived2_to_tbl_test2_id_test2_nullable`\n" - " FOREIGN KEY (`tbl_test2_id_test2_nullable`)\n" + " FOREIGN KEY IF NOT EXISTS (`tbl_test2_id_test2_nullable`)\n" " REFERENCES `test`.`tbl_test2` (`tbl_test2_id`)\n" " ON DELETE SET NULL\n" " ON UPDATE NO ACTION,\n" " ADD CONSTRAINT `fk_tbl_derived2_to_tbl_test2_id_test2_ptr_u`\n" - " FOREIGN KEY (`tbl_test2_id_test2_ptr_u`)\n" + " FOREIGN KEY IF NOT EXISTS (`tbl_test2_id_test2_ptr_u`)\n" " REFERENCES `test`.`tbl_test2` (`tbl_test2_id`)\n" " ON DELETE SET NULL\n" " ON UPDATE NO ACTION,\n" " ADD CONSTRAINT `fk_tbl_derived2_to_tbl_test2_id_test2_ptr_s`\n" - " FOREIGN KEY (`tbl_test2_id_test2_ptr_s`)\n" + " FOREIGN KEY IF NOT EXISTS (`tbl_test2_id_test2_ptr_s`)\n" " REFERENCES `test`.`tbl_test2` (`tbl_test2_id`)\n" " ON DELETE SET NULL\n" " ON UPDATE NO ACTION"); expect_query(mock, "ALTER TABLE `tbl_derived3`\n" " ADD CONSTRAINT `fk_tbl_derived3_to_tbl_derived2_id`\n" - " FOREIGN KEY (`tbl_derived2_id`)\n" + " FOREIGN KEY IF NOT EXISTS (`tbl_derived2_id`)\n" " REFERENCES `test`.`tbl_derived2` (`tbl_derived2_id`)\n" " ON DELETE CASCADE\n" " ON UPDATE CASCADE");