Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix use unique to create table bug #107

Merged
merged 6 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,13 @@ postgres.create_datatable<person>(key1, not_null);
sqlite.create_datatable<person>(key1);
```

注意:目前只支持了key和not null属性,并且只支持单键,还不支持组合键,将在下一个版本中支持组合键。
注意:目前只支持了key、unique和not null属性,并且只支持单键,还不支持组合键,将在下一个版本中支持组合键。
```
mysql.create_datatable<person>(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.

Expand Down
7 changes: 6 additions & 1 deletion include/mysql.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,12 @@ class mysql {
}
else if constexpr (std::is_same_v<decltype(item), ormpp_unique>) {
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, ")");
Expand Down
13 changes: 9 additions & 4 deletions include/sqlite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_; }
Expand Down Expand Up @@ -282,13 +282,14 @@ class sqlite {

auto tp = sort_tuple(std::make_tuple(std::forward<Args>(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<decltype(item), ormpp_not_null>) {
if (item.fields.find(field_name.data()) == item.fields.end())
return;
Expand Down Expand Up @@ -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 {
Expand All @@ -343,6 +344,10 @@ class sqlite {
sql += ", ";
}

if (!unique_field.empty()) {
append(sql, unique_field.data());
}

sql += ")";

return sql;
Expand Down
21 changes: 20 additions & 1 deletion tests/test_ormpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -950,4 +950,23 @@ TEST_CASE("orm_mysql_blob_tuple") {
REQUIRE(img.bin.size() == size);
REQUIRE(time == img_ex.time);
}
#endif
#endif

TEST_CASE("test create table with unique") {
#ifdef ORMPP_ENABLE_MYSQL
dbng<mysql> mysql;
if (mysql.connect(ip, "root", password, db)) {
mysql.execute("drop table if exists person");
CHECK(mysql.create_datatable<person>(ormpp_auto_key{"id"},
ormpp_unique{"name"}));
}
#endif
#ifdef ORMPP_ENABLE_SQLITE3
dbng<sqlite> sqlite;
if (sqlite.connect(db)) {
sqlite.execute("drop table if exists person");
CHECK(sqlite.create_datatable<person>(ormpp_auto_key{"id"},
ormpp_unique{"name"}));
}
#endif
}