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. 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/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; diff --git a/tests/test_ormpp.cpp b/tests/test_ormpp.cpp index b5a2f747..e7025ee5 100644 --- a/tests/test_ormpp.cpp +++ b/tests/test_ormpp.cpp @@ -950,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