forked from qt-creator/qt-creator
Sqlite: Improve LastChangedRowId
Sometimes we want not only the row id from one table but two or three. Change-Id: I6d5444a71ecbfe6c1af8073be80b04932ea9268d Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
@@ -40,12 +40,43 @@ public:
|
||||
Utils::SmallStringView databaseName,
|
||||
Utils::SmallStringView tableName)
|
||||
: database(database)
|
||||
, databaseName(databaseName)
|
||||
, tableName(tableName)
|
||||
|
||||
{
|
||||
callback = [this](ChangeType, char const *database, char const *table, long long rowId) {
|
||||
if (this->databaseName == database && this->tableName == table)
|
||||
lastRowId = rowId;
|
||||
callback = [=](ChangeType, char const *database, char const *table, long long rowId) {
|
||||
if (databaseName == database && tableName == table)
|
||||
this->lastRowId = rowId;
|
||||
};
|
||||
|
||||
database.setUpdateHook(callback);
|
||||
}
|
||||
|
||||
LastChangedRowId(DatabaseInterface &database,
|
||||
Utils::SmallStringView databaseName,
|
||||
Utils::SmallStringView tableName,
|
||||
Utils::SmallStringView tableName2)
|
||||
: database(database)
|
||||
|
||||
{
|
||||
callback = [=](ChangeType, char const *database, char const *table, long long rowId) {
|
||||
if (databaseName == database && (tableName == table || tableName2 == table))
|
||||
this->lastRowId = rowId;
|
||||
};
|
||||
|
||||
database.setUpdateHook(callback);
|
||||
}
|
||||
|
||||
LastChangedRowId(DatabaseInterface &database,
|
||||
Utils::SmallStringView databaseName,
|
||||
Utils::SmallStringView tableName,
|
||||
Utils::SmallStringView tableName2,
|
||||
Utils::SmallStringView tableName3)
|
||||
: database(database)
|
||||
|
||||
{
|
||||
callback = [=](ChangeType, char const *database, char const *table, long long rowId) {
|
||||
if (databaseName == database
|
||||
&& (tableName == table || tableName2 == table || tableName3 == table))
|
||||
this->lastRowId = rowId;
|
||||
};
|
||||
|
||||
database.setUpdateHook(callback);
|
||||
@@ -63,8 +94,6 @@ public:
|
||||
public:
|
||||
DatabaseInterface &database;
|
||||
DatabaseInterface::UpdateCallback callback;
|
||||
Utils::SmallStringView databaseName;
|
||||
Utils::SmallStringView tableName;
|
||||
long long lastRowId = -1;
|
||||
};
|
||||
|
||||
|
||||
@@ -64,16 +64,20 @@ TEST_F(LastChangedRowId, CallbackSetsLastRowId)
|
||||
|
||||
TEST_F(LastChangedRowId, CallbackChecksDatabaseName)
|
||||
{
|
||||
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 33);
|
||||
|
||||
lastRowId.callback(Sqlite::ChangeType::Update, "temp", "foo", 42);
|
||||
|
||||
ASSERT_THAT(lastRowId.lastRowId, -1);
|
||||
ASSERT_THAT(lastRowId.lastRowId, 33);
|
||||
}
|
||||
|
||||
TEST_F(LastChangedRowId, CallbackChecksTableName)
|
||||
{
|
||||
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 33);
|
||||
|
||||
lastRowId.callback(Sqlite::ChangeType::Update, "main", "bar", 42);
|
||||
|
||||
ASSERT_THAT(lastRowId.lastRowId, -1);
|
||||
ASSERT_THAT(lastRowId.lastRowId, 33);
|
||||
}
|
||||
|
||||
TEST_F(LastChangedRowId, LastCallSetsRowId)
|
||||
@@ -105,4 +109,182 @@ TEST_F(LastChangedRowId, TakeLastRowIdResetsRowIdToMinusOne)
|
||||
ASSERT_THAT(id, -1);
|
||||
}
|
||||
|
||||
class LastChangedRowIdWithTwoTables : public testing::Test
|
||||
{
|
||||
protected:
|
||||
NiceMock<MockSqliteDatabase> mockSqliteDatabase;
|
||||
|
||||
Sqlite::LastChangedRowId lastRowId{mockSqliteDatabase, "main", "foo", "bar"};
|
||||
};
|
||||
|
||||
TEST_F(LastChangedRowIdWithTwoTables, SetUpdateHookInContructor)
|
||||
{
|
||||
EXPECT_CALL(mockSqliteDatabase, setUpdateHook(_));
|
||||
|
||||
Sqlite::LastChangedRowId lastRowId{mockSqliteDatabase, "main", "foo"};
|
||||
}
|
||||
|
||||
TEST_F(LastChangedRowIdWithTwoTables, ResetUpdateHookInDestructor)
|
||||
{
|
||||
EXPECT_CALL(mockSqliteDatabase, resetUpdateHook());
|
||||
}
|
||||
|
||||
TEST_F(LastChangedRowIdWithTwoTables, GetMinusOneAsRowIdIfNoCallbackWasCalled)
|
||||
{
|
||||
ASSERT_THAT(lastRowId.lastRowId, -1);
|
||||
}
|
||||
|
||||
TEST_F(LastChangedRowIdWithTwoTables, CallbackSetsLastRowIdFirstTable)
|
||||
{
|
||||
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 42);
|
||||
|
||||
ASSERT_THAT(lastRowId.lastRowId, 42);
|
||||
}
|
||||
|
||||
TEST_F(LastChangedRowIdWithTwoTables, CallbackSetsLastRowIdSecondTable)
|
||||
{
|
||||
lastRowId.callback(Sqlite::ChangeType::Update, "main", "bar", 66);
|
||||
|
||||
ASSERT_THAT(lastRowId.lastRowId, 66);
|
||||
}
|
||||
|
||||
TEST_F(LastChangedRowIdWithTwoTables, CallbackChecksDatabaseName)
|
||||
{
|
||||
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 33);
|
||||
|
||||
lastRowId.callback(Sqlite::ChangeType::Update, "temp", "foo", 42);
|
||||
|
||||
ASSERT_THAT(lastRowId.lastRowId, 33);
|
||||
}
|
||||
|
||||
TEST_F(LastChangedRowIdWithTwoTables, CallbackChecksTableName)
|
||||
{
|
||||
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 33);
|
||||
|
||||
lastRowId.callback(Sqlite::ChangeType::Update, "main", "zoo", 42);
|
||||
|
||||
ASSERT_THAT(lastRowId.lastRowId, 33);
|
||||
}
|
||||
|
||||
TEST_F(LastChangedRowIdWithTwoTables, LastCallSetsRowId)
|
||||
{
|
||||
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 42);
|
||||
|
||||
lastRowId.callback(Sqlite::ChangeType::Delete, "main", "bar", 66);
|
||||
|
||||
ASSERT_THAT(lastRowId.lastRowId, 66);
|
||||
}
|
||||
|
||||
TEST_F(LastChangedRowIdWithTwoTables, TakeLastRowId)
|
||||
{
|
||||
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 42);
|
||||
|
||||
auto id = lastRowId.takeLastRowId();
|
||||
|
||||
ASSERT_THAT(id, 42);
|
||||
}
|
||||
|
||||
TEST_F(LastChangedRowIdWithTwoTables, TakeLastRowIdResetsRowIdToMinusOne)
|
||||
{
|
||||
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 42);
|
||||
lastRowId.takeLastRowId();
|
||||
|
||||
auto id = lastRowId.takeLastRowId();
|
||||
|
||||
ASSERT_THAT(id, -1);
|
||||
}
|
||||
|
||||
class LastChangedRowIdWithThreeTables : public testing::Test
|
||||
{
|
||||
protected:
|
||||
NiceMock<MockSqliteDatabase> mockSqliteDatabase;
|
||||
|
||||
Sqlite::LastChangedRowId lastRowId{mockSqliteDatabase, "main", "foo", "bar", "too"};
|
||||
};
|
||||
|
||||
TEST_F(LastChangedRowIdWithThreeTables, SetUpdateHookInContructor)
|
||||
{
|
||||
EXPECT_CALL(mockSqliteDatabase, setUpdateHook(_));
|
||||
|
||||
Sqlite::LastChangedRowId lastRowId{mockSqliteDatabase, "main", "foo"};
|
||||
}
|
||||
|
||||
TEST_F(LastChangedRowIdWithThreeTables, ResetUpdateHookInDestructor)
|
||||
{
|
||||
EXPECT_CALL(mockSqliteDatabase, resetUpdateHook());
|
||||
}
|
||||
|
||||
TEST_F(LastChangedRowIdWithThreeTables, GetMinusOneAsRowIdIfNoCallbackWasCalled)
|
||||
{
|
||||
ASSERT_THAT(lastRowId.lastRowId, -1);
|
||||
}
|
||||
|
||||
TEST_F(LastChangedRowIdWithThreeTables, CallbackSetsLastRowIdFirstTable)
|
||||
{
|
||||
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 42);
|
||||
|
||||
ASSERT_THAT(lastRowId.lastRowId, 42);
|
||||
}
|
||||
|
||||
TEST_F(LastChangedRowIdWithThreeTables, CallbackSetsLastRowIdSecondTable)
|
||||
{
|
||||
lastRowId.callback(Sqlite::ChangeType::Update, "main", "bar", 42);
|
||||
|
||||
ASSERT_THAT(lastRowId.lastRowId, 42);
|
||||
}
|
||||
|
||||
TEST_F(LastChangedRowIdWithThreeTables, CallbackSetsLastRowIdThirdTable)
|
||||
{
|
||||
lastRowId.callback(Sqlite::ChangeType::Update, "main", "too", 42);
|
||||
|
||||
ASSERT_THAT(lastRowId.lastRowId, 42);
|
||||
}
|
||||
|
||||
TEST_F(LastChangedRowIdWithThreeTables, CallbackChecksDatabaseName)
|
||||
{
|
||||
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 33);
|
||||
|
||||
lastRowId.callback(Sqlite::ChangeType::Update, "temp", "foo", 42);
|
||||
|
||||
ASSERT_THAT(lastRowId.lastRowId, 33);
|
||||
}
|
||||
|
||||
TEST_F(LastChangedRowIdWithThreeTables, CallbackChecksTableName)
|
||||
{
|
||||
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 33);
|
||||
|
||||
lastRowId.callback(Sqlite::ChangeType::Update, "main", "zoo", 42);
|
||||
|
||||
ASSERT_THAT(lastRowId.lastRowId, 33);
|
||||
}
|
||||
|
||||
TEST_F(LastChangedRowIdWithThreeTables, LastCallSetsRowId)
|
||||
{
|
||||
lastRowId.callback(Sqlite::ChangeType::Update, "main", "bar", 42);
|
||||
lastRowId.callback(Sqlite::ChangeType::Insert, "main", "too", 33);
|
||||
|
||||
lastRowId.callback(Sqlite::ChangeType::Delete, "main", "too", 66);
|
||||
|
||||
ASSERT_THAT(lastRowId.lastRowId, 66);
|
||||
}
|
||||
|
||||
TEST_F(LastChangedRowIdWithThreeTables, TakeLastRowId)
|
||||
{
|
||||
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 42);
|
||||
|
||||
auto id = lastRowId.takeLastRowId();
|
||||
|
||||
ASSERT_THAT(id, 42);
|
||||
}
|
||||
|
||||
TEST_F(LastChangedRowIdWithThreeTables, TakeLastRowIdResetsRowIdToMinusOne)
|
||||
{
|
||||
lastRowId.callback(Sqlite::ChangeType::Update, "main", "foo", 42);
|
||||
lastRowId.takeLastRowId();
|
||||
|
||||
auto id = lastRowId.takeLastRowId();
|
||||
|
||||
ASSERT_THAT(id, -1);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user