From 4349e53cb0fea8aac749e2053d27490c8ee87ffa Mon Sep 17 00:00:00 2001 From: Jacyking <791026912@qq.com> Date: Tue, 11 Jul 2023 11:46:26 +0800 Subject: [PATCH 1/6] fix use unique to create table bug --- include/sqlite.hpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/include/sqlite.hpp b/include/sqlite.hpp index caef44c1..62bdcb53 100644 --- a/include/sqlite.hpp +++ b/include/sqlite.hpp @@ -18,7 +18,7 @@ class sqlite { void set_last_error(std::string last_error) { last_error_ = std::move(last_error); - // std::cout << last_error_ << std::endl;//todo, write to log file + std::cout << last_error_ << std::endl; // todo, write to log file } std::string get_last_error() const { return last_error_; } @@ -282,13 +282,14 @@ class sqlite { auto tp = sort_tuple(std::make_tuple(std::forward(args)...)); const size_t arr_size = arr.size(); + std::string unique_field; for (size_t i = 0; i < arr_size; ++i) { auto field_name = arr[i]; bool has_add_field = false; for_each0( tp, - [&sql, &i, &has_add_field, field_name, type_name_arr, name, - this](auto item) { + [&sql, &i, &has_add_field, &unique_field, field_name, type_name_arr, + name, this](auto item) { if constexpr (std::is_same_v) { if (item.fields.find(field_name.data()) == item.fields.end()) return; @@ -326,7 +327,7 @@ class sqlite { append(sql, field_name.data(), " ", type_name_arr[i]); } - append(sql, ", UNIQUE(", item.fields, ")"); + append(unique_field, ", UNIQUE(", item.fields, ")"); has_add_field = true; } else { @@ -343,6 +344,10 @@ class sqlite { sql += ", "; } + if (!unique_field.empty()) { + append(sql, unique_field.data()); + } + sql += ")"; return sql; From 20c3c5ce41c522d1e16f996bd9cecf7b32e1c6d5 Mon Sep 17 00:00:00 2001 From: Jacyking <791026912@qq.com> Date: Tue, 11 Jul 2023 13:47:44 +0800 Subject: [PATCH 2/6] add unit test "create table with unique on sqlite" --- tests/test_ormpp.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/test_ormpp.cpp b/tests/test_ormpp.cpp index b5a2f747..bad5672c 100644 --- a/tests/test_ormpp.cpp +++ b/tests/test_ormpp.cpp @@ -878,6 +878,17 @@ TEST_CASE("orm_aop") { // REQUIRE(r); } +#ifdef ORMPP_ENABLE_SQLITE3 +TEST_CASE("test create table with unique") { + dbng sqlite; + if (sqlite.connect(db)) { + sqlite.execute("drop table if exists person"); + CHECK(sqlite.create_datatable(ormpp_auto_key{"id"}, + ormpp_unique{"name"})); + } +} +#endif + #ifdef ORMPP_ENABLE_MYSQL struct image { int id; From 23b77f6faecd0b1ae6365946ecfdc3cf87ffe6c9 Mon Sep 17 00:00:00 2001 From: Jacyking <791026912@qq.com> Date: Tue, 11 Jul 2023 14:07:52 +0800 Subject: [PATCH 3/6] test create table with unique --- tests/test_ormpp.cpp | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/tests/test_ormpp.cpp b/tests/test_ormpp.cpp index bad5672c..e7025ee5 100644 --- a/tests/test_ormpp.cpp +++ b/tests/test_ormpp.cpp @@ -878,17 +878,6 @@ TEST_CASE("orm_aop") { // REQUIRE(r); } -#ifdef ORMPP_ENABLE_SQLITE3 -TEST_CASE("test create table with unique") { - dbng sqlite; - if (sqlite.connect(db)) { - sqlite.execute("drop table if exists person"); - CHECK(sqlite.create_datatable(ormpp_auto_key{"id"}, - ormpp_unique{"name"})); - } -} -#endif - #ifdef ORMPP_ENABLE_MYSQL struct image { int id; @@ -961,4 +950,23 @@ TEST_CASE("orm_mysql_blob_tuple") { REQUIRE(img.bin.size() == size); REQUIRE(time == img_ex.time); } -#endif \ No newline at end of file +#endif + +TEST_CASE("test create table with unique") { +#ifdef ORMPP_ENABLE_MYSQL + dbng mysql; + if (mysql.connect(ip, "root", password, db)) { + mysql.execute("drop table if exists person"); + CHECK(mysql.create_datatable(ormpp_auto_key{"id"}, + ormpp_unique{"name"})); + } +#endif +#ifdef ORMPP_ENABLE_SQLITE3 + dbng sqlite; + if (sqlite.connect(db)) { + sqlite.execute("drop table if exists person"); + CHECK(sqlite.create_datatable(ormpp_auto_key{"id"}, + ormpp_unique{"name"})); + } +#endif +} \ No newline at end of file From 8ff92c070df658e1c25061672f44ab499e5b1c1f Mon Sep 17 00:00:00 2001 From: Jacyking <791026912@qq.com> Date: Tue, 11 Jul 2023 16:17:22 +0800 Subject: [PATCH 4/6] fix BLOB/TEXT column 'name' used in key specification without a key length --- include/type_mapping.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/type_mapping.hpp b/include/type_mapping.hpp index 2551e4e9..85bd7930 100644 --- a/include/type_mapping.hpp +++ b/include/type_mapping.hpp @@ -75,7 +75,9 @@ inline constexpr auto type_to_name(identity) noexcept { return "BIGINT"sv; } inline constexpr auto type_to_name(identity) noexcept { return "BLOB"sv; } -inline auto type_to_name(identity) noexcept { return "TEXT"sv; } +inline auto type_to_name(identity) noexcept { + return "varchar(65535)"sv; +} template inline auto type_to_name(identity>) noexcept { std::string s = "varchar(" + std::to_string(N) + ")"; From f67c6ac3d2286fd1b3ebb3844ea148a4de33a94e Mon Sep 17 00:00:00 2001 From: Jacyking <791026912@qq.com> Date: Tue, 11 Jul 2023 16:44:11 +0800 Subject: [PATCH 5/6] fix BLOB/TEXT column 'name' used in key specification without a key length --- include/mysql.hpp | 7 ++++++- include/type_mapping.hpp | 4 +--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/include/mysql.hpp b/include/mysql.hpp index 89531b84..837f6ab2 100644 --- a/include/mysql.hpp +++ b/include/mysql.hpp @@ -606,7 +606,12 @@ class mysql { } else if constexpr (std::is_same_v) { if (!has_add_field) { - append(sql, field_name.data(), " ", type_name_arr[i]); + if (type_name_arr[i] == "TEXT") { + append(sql, field_name.data(), " ", "varchar(512)"); + } + else { + append(sql, field_name.data(), " ", type_name_arr[i]); + } } append(sql, ", UNIQUE(", item.fields, ")"); diff --git a/include/type_mapping.hpp b/include/type_mapping.hpp index 85bd7930..2551e4e9 100644 --- a/include/type_mapping.hpp +++ b/include/type_mapping.hpp @@ -75,9 +75,7 @@ inline constexpr auto type_to_name(identity) noexcept { return "BIGINT"sv; } inline constexpr auto type_to_name(identity) noexcept { return "BLOB"sv; } -inline auto type_to_name(identity) noexcept { - return "varchar(65535)"sv; -} +inline auto type_to_name(identity) noexcept { return "TEXT"sv; } template inline auto type_to_name(identity>) noexcept { std::string s = "varchar(" + std::to_string(N) + ")"; From 415c3a6a2c5e9bbb53325a8a619d0747e4cbc716 Mon Sep 17 00:00:00 2001 From: Jacyking <791026912@qq.com> Date: Tue, 11 Jul 2023 17:06:11 +0800 Subject: [PATCH 6/6] [no ci] --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 62bec3d3..80f3db7a 100644 --- a/README.md +++ b/README.md @@ -263,7 +263,13 @@ postgres.create_datatable(key1, not_null); sqlite.create_datatable(key1); ``` -注意:目前只支持了key和not null属性,并且只支持单键,还不支持组合键,将在下一个版本中支持组合键。 +注意:目前只支持了key、unique和not null属性,并且只支持单键,还不支持组合键,将在下一个版本中支持组合键。 +``` +mysql.create_datatable(ormpp_unique{"name"}); +当在mysql中使用由unique声明的std::string成员创建表时, +由于"BLOB/TEXT column 'NAME' used in key specification without a key length", +故在创建表时,如果是由unique声明的std::string成员对应的数据类型则为VARCHAR(512),否则则为TEXT +``` 返回值:bool,成功返回true,失败返回false.