forked from qt-creator/qt-creator
Sqlite: Simplify insertUpdateDelete
That hopefully is clearing up the intention. Change-Id: I89ef34dddaf8453b28361be8875c73d0dd4e9d5b Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
@@ -36,6 +36,36 @@ void insertUpdateDelete(SqliteRange &&sqliteRange,
|
|||||||
auto endValueIterator = values.end();
|
auto endValueIterator = values.end();
|
||||||
auto lastValueIterator = endValueIterator;
|
auto lastValueIterator = endValueIterator;
|
||||||
|
|
||||||
|
auto doUpdate = [&](const auto &sqliteValue, const auto &value) {
|
||||||
|
UpdateChange updateChange = updateCallback(sqliteValue, value);
|
||||||
|
switch (updateChange) {
|
||||||
|
case UpdateChange::Update:
|
||||||
|
lastValueIterator = currentValueIterator;
|
||||||
|
break;
|
||||||
|
case UpdateChange::No:
|
||||||
|
lastValueIterator = endValueIterator;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
++currentSqliteIterator;
|
||||||
|
++currentValueIterator;
|
||||||
|
};
|
||||||
|
|
||||||
|
auto doInsert = [&](const auto &value) {
|
||||||
|
insertCallback(value);
|
||||||
|
++currentValueIterator;
|
||||||
|
};
|
||||||
|
|
||||||
|
auto doDelete = [&](const auto &sqliteValue) {
|
||||||
|
if (lastValueIterator != endValueIterator) {
|
||||||
|
if (compareKey(sqliteValue, *lastValueIterator) != 0)
|
||||||
|
deleteCallback(sqliteValue);
|
||||||
|
lastValueIterator = endValueIterator;
|
||||||
|
} else {
|
||||||
|
deleteCallback(sqliteValue);
|
||||||
|
}
|
||||||
|
++currentSqliteIterator;
|
||||||
|
};
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
bool hasMoreValues = currentValueIterator != endValueIterator;
|
bool hasMoreValues = currentValueIterator != endValueIterator;
|
||||||
bool hasMoreSqliteValues = currentSqliteIterator != endSqliteIterator;
|
bool hasMoreSqliteValues = currentSqliteIterator != endSqliteIterator;
|
||||||
@@ -43,44 +73,16 @@ void insertUpdateDelete(SqliteRange &&sqliteRange,
|
|||||||
auto &&sqliteValue = *currentSqliteIterator;
|
auto &&sqliteValue = *currentSqliteIterator;
|
||||||
auto &&value = *currentValueIterator;
|
auto &&value = *currentValueIterator;
|
||||||
auto compare = compareKey(sqliteValue, value);
|
auto compare = compareKey(sqliteValue, value);
|
||||||
if (compare == 0) {
|
if (compare == 0)
|
||||||
UpdateChange updateChange = updateCallback(sqliteValue, value);
|
doUpdate(sqliteValue, value);
|
||||||
switch (updateChange) {
|
else if (compare > 0)
|
||||||
case UpdateChange::Update:
|
doInsert(value);
|
||||||
lastValueIterator = currentValueIterator;
|
else if (compare < 0)
|
||||||
break;
|
doDelete(sqliteValue);
|
||||||
case UpdateChange::No:
|
|
||||||
lastValueIterator = endValueIterator;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
++currentSqliteIterator;
|
|
||||||
++currentValueIterator;
|
|
||||||
} else if (compare > 0) {
|
|
||||||
insertCallback(value);
|
|
||||||
++currentValueIterator;
|
|
||||||
} else if (compare < 0) {
|
|
||||||
if (lastValueIterator != endValueIterator) {
|
|
||||||
if (compareKey(sqliteValue, *lastValueIterator) != 0)
|
|
||||||
deleteCallback(sqliteValue);
|
|
||||||
lastValueIterator = endValueIterator;
|
|
||||||
} else {
|
|
||||||
deleteCallback(sqliteValue);
|
|
||||||
}
|
|
||||||
++currentSqliteIterator;
|
|
||||||
}
|
|
||||||
} else if (hasMoreValues) {
|
} else if (hasMoreValues) {
|
||||||
insertCallback(*currentValueIterator);
|
doInsert(*currentValueIterator);
|
||||||
++currentValueIterator;
|
|
||||||
} else if (hasMoreSqliteValues) {
|
} else if (hasMoreSqliteValues) {
|
||||||
auto &&sqliteValue = *currentSqliteIterator;
|
doDelete(*currentSqliteIterator);
|
||||||
if (lastValueIterator != endValueIterator) {
|
|
||||||
if (compareKey(sqliteValue, *lastValueIterator) != 0)
|
|
||||||
deleteCallback(sqliteValue);
|
|
||||||
lastValueIterator = endValueIterator;
|
|
||||||
} else {
|
|
||||||
deleteCallback(sqliteValue);
|
|
||||||
}
|
|
||||||
++currentSqliteIterator;
|
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user