Sqlite: Add update hook and use it to get the last changed id

Sqlite has a function to get the last inserted rowid but very often you
want to get the updated rowid too.

Change-Id: Ie276a5039682813ad16597433996a2959f54d9ba
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Marco Bubke
2020-05-12 12:54:18 +02:00
parent fded815021
commit a4b00a7742
12 changed files with 330 additions and 13 deletions

View File

@@ -344,13 +344,11 @@ int indexOfPragma(Utils::SmallStringView pragma, const Utils::SmallStringView (&
}
}
constexpr const Utils::SmallStringView journalModeStrings[] = {
"delete",
"truncate",
"persist",
"memory",
"wal"
};
const Utils::SmallStringView journalModeStrings[] = {"delete",
"truncate",
"persist",
"memory",
"wal"};
Utils::SmallStringView DatabaseBackend::journalModeToPragma(JournalMode journalMode)
{
@@ -367,11 +365,7 @@ JournalMode DatabaseBackend::pragmaToJournalMode(Utils::SmallStringView pragma)
return static_cast<JournalMode>(index);
}
constexpr const Utils::SmallStringView textEncodingStrings[] = {
"UTF-8",
"UTF-16le",
"UTF-16be"
};
const Utils::SmallStringView textEncodingStrings[] = {"UTF-8", "UTF-16le", "UTF-16be"};
Utils::SmallStringView DatabaseBackend::textEncodingToPragma(TextEncoding textEncoding)
{
@@ -426,6 +420,29 @@ void DatabaseBackend::walCheckpointFull()
}
}
namespace {
void updateCallback(
void *callback, int type, char const *database, char const *table, sqlite3_int64 row)
{
auto &function = *reinterpret_cast<DatabaseBackend::UpdateCallback *>(callback);
function(static_cast<ChangeType>(type), database, table, row);
}
} // namespace
void DatabaseBackend::setUpdateHook(UpdateCallback &callback)
{
if (callback)
sqlite3_update_hook(m_databaseHandle, updateCallback, &callback);
else
sqlite3_update_hook(m_databaseHandle, nullptr, nullptr);
}
void DatabaseBackend::resetUpdateHook()
{
sqlite3_update_hook(m_databaseHandle, nullptr, nullptr);
}
void DatabaseBackend::throwExceptionStatic(const char *whatHasHappens)
{
throw Exception(whatHasHappens);