Skip to content
Draft
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
6 changes: 3 additions & 3 deletions dev/statement_serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -1599,7 +1599,7 @@ namespace sqlite_orm {
mpl::conjunction<mpl::not_<mpl::always<is_without_rowid>>,
mpl::disjunction_fn<is_primary_key, is_generated_always>>>(
[&table, &columnNames](auto& column) {
if (exists_in_composite_primary_key(table, column)) {
if (!is_without_rowid::value && exists_in_composite_primary_key(table, column)) {
return;
}

Expand All @@ -1621,7 +1621,7 @@ namespace sqlite_orm {
mpl::conjunction<mpl::not_<mpl::always<is_without_rowid>>,
mpl::disjunction_fn<is_primary_key, is_generated_always>>{},
[&table](auto& column) {
return exists_in_composite_primary_key(table, column);
return !is_without_rowid::value && exists_in_composite_primary_key(table, column);
},
context,
get_ref(statement.object))
Expand Down Expand Up @@ -1771,7 +1771,7 @@ namespace sqlite_orm {
mpl::conjunction<mpl::not_<mpl::always<is_without_rowid>>,
mpl::disjunction_fn<is_primary_key, is_generated_always>>>(
[&table, &columnNames](auto& column) {
if (exists_in_composite_primary_key(table, column)) {
if (!is_without_rowid::value && exists_in_composite_primary_key(table, column)) {
return;
}

Expand Down
8 changes: 5 additions & 3 deletions dev/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -1531,15 +1531,17 @@ namespace sqlite_orm {

auto processObject = [&table = this->get_table<object_type>(),
bindValue = field_value_binder{stmt}](auto& object) mutable {
using is_without_rowid = typename std::remove_reference_t<decltype(table)>::is_without_rowid;
using table_type = polyfill::remove_cvref_t<decltype(table)>;
using is_without_rowid = typename table_type::is_without_rowid;

table.template for_each_column_excluding<
mpl::conjunction<mpl::not_<mpl::always<is_without_rowid>>,
mpl::disjunction_fn<is_primary_key, is_generated_always>>>(
call_as_template_base<column_field>([&table, &bindValue, &object](auto& column) {
if (!exists_in_composite_primary_key(table, column)) {
bindValue(polyfill::invoke(column.member_pointer, object));
if (!is_without_rowid::value && exists_in_composite_primary_key(table, column)) {
return;
}
bindValue(polyfill::invoke(column.member_pointer, object));
}));
};

Expand Down
14 changes: 8 additions & 6 deletions include/sqlite_orm/sqlite_orm.h
Original file line number Diff line number Diff line change
Expand Up @@ -22681,7 +22681,7 @@ namespace sqlite_orm {
mpl::conjunction<mpl::not_<mpl::always<is_without_rowid>>,
mpl::disjunction_fn<is_primary_key, is_generated_always>>>(
[&table, &columnNames](auto& column) {
if (exists_in_composite_primary_key(table, column)) {
if (!is_without_rowid::value && exists_in_composite_primary_key(table, column)) {
return;
}

Expand All @@ -22703,7 +22703,7 @@ namespace sqlite_orm {
mpl::conjunction<mpl::not_<mpl::always<is_without_rowid>>,
mpl::disjunction_fn<is_primary_key, is_generated_always>>{},
[&table](auto& column) {
return exists_in_composite_primary_key(table, column);
return !is_without_rowid::value && exists_in_composite_primary_key(table, column);
},
context,
get_ref(statement.object))
Expand Down Expand Up @@ -22853,7 +22853,7 @@ namespace sqlite_orm {
mpl::conjunction<mpl::not_<mpl::always<is_without_rowid>>,
mpl::disjunction_fn<is_primary_key, is_generated_always>>>(
[&table, &columnNames](auto& column) {
if (exists_in_composite_primary_key(table, column)) {
if (!is_without_rowid::value && exists_in_composite_primary_key(table, column)) {
return;
}

Expand Down Expand Up @@ -25678,15 +25678,17 @@ namespace sqlite_orm {

auto processObject = [&table = this->get_table<object_type>(),
bindValue = field_value_binder{stmt}](auto& object) mutable {
using is_without_rowid = typename std::remove_reference_t<decltype(table)>::is_without_rowid;
using table_type = polyfill::remove_cvref_t<decltype(table)>;
using is_without_rowid = typename table_type::is_without_rowid;

table.template for_each_column_excluding<
mpl::conjunction<mpl::not_<mpl::always<is_without_rowid>>,
mpl::disjunction_fn<is_primary_key, is_generated_always>>>(
call_as_template_base<column_field>([&table, &bindValue, &object](auto& column) {
if (!exists_in_composite_primary_key(table, column)) {
bindValue(polyfill::invoke(column.member_pointer, object));
if (!is_without_rowid::value && exists_in_composite_primary_key(table, column)) {
return;
}
bindValue(polyfill::invoke(column.member_pointer, object));
}));
};

Expand Down
39 changes: 37 additions & 2 deletions tests/statement_serializer_tests/statements/insert_replace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,20 @@ TEST_CASE("statement_serializer insert/replace") {
int id = 0;
std::string name;
};
struct UserData1 {
int userId = 0;
};
struct UserData2 {
int userId = 0;
};
auto table = make_table("users", make_column("id", &User::id), make_column("name", &User::name));
auto table2 =
make_table("users_backup", make_column("id", &UserBackup::id), make_column("name", &UserBackup::name));
using db_objects_t = internal::db_objects_tuple<decltype(table), decltype(table2)>;
auto dbObjects = db_objects_t{table, table2};
auto table3 = make_table("user_data1", make_column("user_id", &UserData1::userId, primary_key())).without_rowid();
auto table4 = make_table("user_data2", make_column("user_id", &UserData2::userId), primary_key(&UserData2::userId))
.without_rowid();
std::tuple dbObjects = {table, table2, table3, table4};
using db_objects_t = decltype(dbObjects);
using context_t = internal::serializer_context<db_objects_t>;
context_t context{dbObjects};
std::string value;
Expand Down Expand Up @@ -138,6 +147,8 @@ TEST_CASE("statement_serializer insert/replace") {
}
SECTION("insert") {
User user{5, "Gambit"};
UserData1 userData1{5};
UserData2 userData2{5};
SECTION("crud") {
auto statement = insert(user);
SECTION("question marks") {
Expand All @@ -150,6 +161,18 @@ TEST_CASE("statement_serializer insert/replace") {
}
value = serialize(statement, context);
}
SECTION("crud without rowid 1") {
context.replace_bindable_with_question = false;
auto statement = insert(userData1);
expected = R"(INSERT INTO "user_data1" ("user_id") VALUES (5))";
value = serialize(statement, context);
}
SECTION("crud without rowid 2") {
context.replace_bindable_with_question = false;
auto statement = insert(userData2);
expected = R"(INSERT INTO "user_data2" ("user_id") VALUES (5))";
value = serialize(statement, context);
}
SECTION("explicit") {
SECTION("one column") {
auto statement = insert(user, columns(&User::id));
Expand Down Expand Up @@ -385,6 +408,8 @@ TEST_CASE("statement_serializer insert/replace") {
context.replace_bindable_with_question = false;

std::vector<User> users(1);
std::vector<UserData1> userData1(1);
std::vector<UserData2> userData2(1);
SECTION("objects") {
auto expression = insert_range<User>(users.begin(), users.end());
// deduced object type
Expand Down Expand Up @@ -427,6 +452,16 @@ TEST_CASE("statement_serializer insert/replace") {
expected = R"(INSERT INTO "users" ("id", "name") VALUES (?, ?))";
}
}
SECTION("without rowid 1") {
auto expression = insert_range<UserData1>(userData1.begin(), userData1.end());
value = serialize(expression, context);
expected = R"(INSERT INTO "user_data1" ("user_id") VALUES (?))";
}
SECTION("without rowid 2") {
auto expression = insert_range<UserData2>(userData2.begin(), userData2.end());
value = serialize(expression, context);
expected = R"(INSERT INTO "user_data2" ("user_id") VALUES (?))";
}
}
}
REQUIRE(value == expected);
Expand Down