Sqlite: Fix insertUpdateDelete

Like you can read in https://www.sqlite.org/isolation.html after an
update they same value can be show up for an iterator advancement. This
would be lead to an delete. So the last value for update is saved and
then compared in the delete method. If they are equal the delete is
skipped.

Change-Id: Ic0aa6619f6a4a520eac77be4e5a83cbe533d102d
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
Marco Bubke
2021-12-01 16:28:15 +01:00
parent 815383bf0a
commit c1ca2a7103
3 changed files with 74 additions and 25 deletions

View File

@@ -27,6 +27,8 @@
#include <utils/smallstringview.h>
#include <type_traits>
namespace Sqlite {
constexpr int compare(Utils::SmallStringView first, Utils::SmallStringView second) noexcept
@@ -41,6 +43,8 @@ constexpr int compare(Utils::SmallStringView first, Utils::SmallStringView secon
return difference;
}
enum class UpdateChange { No, Update };
template<typename SqliteRange,
typename Range,
typename CompareKey,
@@ -58,6 +62,7 @@ void insertUpdateDelete(SqliteRange &&sqliteRange,
auto endSqliteIterator = sqliteRange.end();
auto currentValueIterator = values.begin();
auto endValueIterator = values.end();
std::optional<std::decay_t<decltype(*currentValueIterator)>> lastValue;
while (true) {
bool hasMoreValues = currentValueIterator != endValueIterator;
@@ -67,21 +72,42 @@ void insertUpdateDelete(SqliteRange &&sqliteRange,
auto &&value = *currentValueIterator;
auto compare = compareKey(sqliteValue, value);
if (compare == 0) {
updateCallback(sqliteValue, value);
++currentValueIterator;
UpdateChange updateChange = updateCallback(sqliteValue, value);
switch (updateChange) {
case UpdateChange::Update:
lastValue = value;
break;
case UpdateChange::No:
lastValue.reset();
break;
}
++currentSqliteIterator;
++currentValueIterator;
} else if (compare > 0) {
insertCallback(value);
++currentValueIterator;
} else if (compare < 0) {
deleteCallback(sqliteValue);
if (lastValue) {
if (compareKey(sqliteValue, *lastValue) != 0)
deleteCallback(sqliteValue);
lastValue.reset();
} else {
deleteCallback(sqliteValue);
}
++currentSqliteIterator;
}
} else if (hasMoreValues) {
insertCallback(*currentValueIterator);
++currentValueIterator;
} else if (hasMoreSqliteValues) {
deleteCallback(*currentSqliteIterator);
auto &&sqliteValue = *currentSqliteIterator;
if (lastValue) {
if (compareKey(sqliteValue, *lastValue) != 0)
deleteCallback(sqliteValue);
lastValue.reset();
} else {
deleteCallback(sqliteValue);
}
++currentSqliteIterator;
} else {
break;