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 2 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
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
11 changes: 11 additions & 0 deletions tests/test_ormpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,17 @@ TEST_CASE("orm_aop") {
// REQUIRE(r);
}

#ifdef ORMPP_ENABLE_SQLITE3
TEST_CASE("test create table with unique") {
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

#ifdef ORMPP_ENABLE_MYSQL
struct image {
int id;
Expand Down