forked from qt-creator/qt-creator
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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user