diff --git a/src/libs/sqlite/lastchangedrowid.h b/src/libs/sqlite/lastchangedrowid.h index 62e534b57f4..f140faa59ec 100644 --- a/src/libs/sqlite/lastchangedrowid.h +++ b/src/libs/sqlite/lastchangedrowid.h @@ -36,6 +36,29 @@ namespace Sqlite { class LastChangedRowId { public: + LastChangedRowId(DatabaseInterface &database) + : database(database) + + { + callback = [=](ChangeType, char const *database, char const *table, long long rowId) { + this->lastRowId = rowId; + }; + + database.setUpdateHook(callback); + } + + LastChangedRowId(DatabaseInterface &database, Utils::SmallStringView databaseName) + : database(database) + + { + callback = [=](ChangeType, char const *database, char const *table, long long rowId) { + if (databaseName == database) + this->lastRowId = rowId; + }; + + database.setUpdateHook(callback); + } + LastChangedRowId(DatabaseInterface &database, Utils::SmallStringView databaseName, Utils::SmallStringView tableName) diff --git a/tests/unit/unittest/lastchangedrowid-test.cpp b/tests/unit/unittest/lastchangedrowid-test.cpp index da33dd6098e..d23d81ae739 100644 --- a/tests/unit/unittest/lastchangedrowid-test.cpp +++ b/tests/unit/unittest/lastchangedrowid-test.cpp @@ -287,4 +287,159 @@ TEST_F(LastChangedRowIdWithThreeTables, TakeLastRowIdResetsRowIdToMinusOne) ASSERT_THAT(id, -1); } +class LastChangedRowIdWithNoDatabaseAndTable : public testing::Test +{ +protected: + NiceMock mockSqliteDatabase; + Sqlite::LastChangedRowId lastRowId{mockSqliteDatabase}; +}; + +TEST_F(LastChangedRowIdWithNoDatabaseAndTable, SetUpdateHookInContructor) +{ + EXPECT_CALL(mockSqliteDatabase, setUpdateHook(_)); + + Sqlite::LastChangedRowId lastRowId{mockSqliteDatabase, "main", "foo"}; +} + +TEST_F(LastChangedRowIdWithNoDatabaseAndTable, ResetUpdateHookInDestructor) +{ + EXPECT_CALL(mockSqliteDatabase, resetUpdateHook()); +} + +TEST_F(LastChangedRowIdWithNoDatabaseAndTable, GetMinusOneAsRowIdIfNoCallbackWasCalled) +{ + ASSERT_THAT(lastRowId.lastRowId, -1); +} + +TEST_F(LastChangedRowIdWithNoDatabaseAndTable, CallbackSetsLastRowId) +{ + lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 42); + + ASSERT_THAT(lastRowId.lastRowId, 42); +} + +TEST_F(LastChangedRowIdWithNoDatabaseAndTable, CallbackDoNotChecksDatabaseName) +{ + lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 33); + + lastRowId.callback(Sqlite::ChangeType::Update, "temp", "foo", 42); + + ASSERT_THAT(lastRowId.lastRowId, 42); +} + +TEST_F(LastChangedRowIdWithNoDatabaseAndTable, CallbackDoNotChecksTableName) +{ + lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 33); + + lastRowId.callback(Sqlite::ChangeType::Update, "main", "bar", 42); + + ASSERT_THAT(lastRowId.lastRowId, 42); +} + +TEST_F(LastChangedRowIdWithNoDatabaseAndTable, LastCallSetsRowId) +{ + lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 42); + lastRowId.callback(Sqlite::ChangeType::Insert, "main", "foo", 33); + + lastRowId.callback(Sqlite::ChangeType::Delete, "main", "foo", 66); + + ASSERT_THAT(lastRowId.lastRowId, 66); +} + +TEST_F(LastChangedRowIdWithNoDatabaseAndTable, TakeLastRowId) +{ + lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 42); + + auto id = lastRowId.takeLastRowId(); + + ASSERT_THAT(id, 42); +} + +TEST_F(LastChangedRowIdWithNoDatabaseAndTable, TakeLastRowIdResetsRowIdToMinusOne) +{ + lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 42); + lastRowId.takeLastRowId(); + + auto id = lastRowId.takeLastRowId(); + + ASSERT_THAT(id, -1); +} + +class LastChangedRowIdWithNoTable : public testing::Test +{ +protected: + NiceMock mockSqliteDatabase; + Sqlite::LastChangedRowId lastRowId{mockSqliteDatabase}; +}; + +TEST_F(LastChangedRowIdWithNoTable, SetUpdateHookInContructor) +{ + EXPECT_CALL(mockSqliteDatabase, setUpdateHook(_)); + + Sqlite::LastChangedRowId lastRowId{mockSqliteDatabase, "main", "foo"}; +} + +TEST_F(LastChangedRowIdWithNoTable, ResetUpdateHookInDestructor) +{ + EXPECT_CALL(mockSqliteDatabase, resetUpdateHook()); +} + +TEST_F(LastChangedRowIdWithNoTable, GetMinusOneAsRowIdIfNoCallbackWasCalled) +{ + ASSERT_THAT(lastRowId.lastRowId, -1); +} + +TEST_F(LastChangedRowIdWithNoTable, CallbackSetsLastRowId) +{ + lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 42); + + ASSERT_THAT(lastRowId.lastRowId, 42); +} + +TEST_F(LastChangedRowIdWithNoTable, CallbackChecksDatabaseName) +{ + lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 33); + + lastRowId.callback(Sqlite::ChangeType::Update, "temp", "foo", 42); + + ASSERT_THAT(lastRowId.lastRowId, 33); +} + +TEST_F(LastChangedRowIdWithNoTable, CallbackDoNotChecksTableName) +{ + lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 33); + + lastRowId.callback(Sqlite::ChangeType::Update, "main", "bar", 42); + + ASSERT_THAT(lastRowId.lastRowId, 42); +} + +TEST_F(LastChangedRowIdWithNoTable, LastCallSetsRowId) +{ + lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 42); + lastRowId.callback(Sqlite::ChangeType::Insert, "main", "foo", 33); + + lastRowId.callback(Sqlite::ChangeType::Delete, "main", "foo", 66); + + ASSERT_THAT(lastRowId.lastRowId, 66); +} + +TEST_F(LastChangedRowIdWithNoTable, TakeLastRowId) +{ + lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 42); + + auto id = lastRowId.takeLastRowId(); + + ASSERT_THAT(id, 42); +} + +TEST_F(LastChangedRowIdWithNoTable, TakeLastRowIdResetsRowIdToMinusOne) +{ + lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 42); + lastRowId.takeLastRowId(); + + auto id = lastRowId.takeLastRowId(); + + ASSERT_THAT(id, -1); +} } // namespace