Sqlite: Add insertUpdateRemove

An generic approach to merge values into a database. It is comparing
two ranges. The first range is the Sqlite select range and the second
one which are the new values. New values are inserted, change are
updated and missing values are removed. Unchanged values are not
touched. The compare function is comparing a key. Both ranges are
binary ordered.

Change-Id: I973c83677ea74f8fa62bd7ab8a73ed560c806562
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-05-18 12:14:16 +02:00
parent 6fa399eccf
commit 7b330d3496
6 changed files with 405 additions and 0 deletions

View File

@@ -15,6 +15,7 @@ add_qtc_library(Sqlite
constraints.h
createtablesqlstatementbuilder.cpp createtablesqlstatementbuilder.h
lastchangedrowid.h
sqlitealgorithms.h
sqlitebasestatement.cpp sqlitebasestatement.h
sqlitecolumn.h
sqlitedatabase.cpp sqlitedatabase.h

View File

@@ -25,6 +25,7 @@ SOURCES += \
$$PWD/sqlitebasestatement.cpp
HEADERS += \
$$PWD/constraints.h \
$$PWD/sqlitealgorithms.h \
$$PWD/sqliteblob.h \
$$PWD/sqlitelibraryinitializer.h \
$$PWD/sqlitetimestamp.h \

View File

@@ -0,0 +1,92 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include <utils/smallstringview.h>
namespace Sqlite {
constexpr int compare(Utils::SmallStringView first, Utils::SmallStringView second) noexcept
{
auto difference = std::char_traits<char>::compare(first.data(),
second.data(),
std::min(first.size(), second.size()));
if (difference == 0)
return int(first.size() - second.size());
return difference;
}
template<typename SqliteRange,
typename Range,
typename CompareKey,
typename InsertCallback,
typename UpdateCallback,
typename DeleteCallback>
void insertUpdateDelete(SqliteRange &&sqliteRange,
Range &&values,
CompareKey compareKey,
InsertCallback insertCallback,
UpdateCallback updateCallback,
DeleteCallback deleteCallback)
{
auto currentSqliteIterator = sqliteRange.begin();
auto endSqliteIterator = sqliteRange.end();
auto currentValueIterator = values.begin();
auto endValueIterator = values.end();
while (true) {
bool hasMoreValues = currentValueIterator != endValueIterator;
bool hasMoreSqliteValues = currentSqliteIterator != endSqliteIterator;
if (hasMoreValues && hasMoreSqliteValues) {
const auto &sqliteValue = *currentSqliteIterator;
const auto &value = *currentValueIterator;
auto compare = compareKey(sqliteValue, value);
if (compare == 0) {
updateCallback(sqliteValue, value);
++currentValueIterator;
++currentSqliteIterator;
} else if (compare > 0) {
insertCallback(value);
++currentValueIterator;
} else if (compare < 0) {
deleteCallback(sqliteValue);
++currentSqliteIterator;
}
} else if (hasMoreValues) {
insertCallback(*currentValueIterator);
++currentValueIterator;
} else if (hasMoreSqliteValues) {
deleteCallback(*currentSqliteIterator);
++currentSqliteIterator;
} else {
break;
}
}
}
} // namespace Sqlite