Commit 816a9175 authored by Marco Bubke's avatar Marco Bubke

Sqlite: Extend database

lastInsertedRowId() function was added and more test are now under test.

Change-Id: I02bf11dbab29654dbff9f2cad8c13c0c4d15e3be
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
parent fc231309
......@@ -108,16 +108,6 @@ OpenMode Database::openMode() const
return m_openMode;
}
int Database::changesCount()
{
return m_databaseBackend.changesCount();
}
int Database::totalChangesCount()
{
return m_databaseBackend.totalChangesCount();
}
void Database::execute(Utils::SmallStringView sqlStatement)
{
m_databaseBackend.execute(sqlStatement);
......
......@@ -73,13 +73,25 @@ public:
void setOpenMode(OpenMode openMode);
OpenMode openMode() const;
int changesCount();
int totalChangesCount();
void execute(Utils::SmallStringView sqlStatement);
DatabaseBackend &backend();
int64_t lastInsertedRowId() const
{
return m_databaseBackend.lastInsertedRowId();
}
int changesCount()
{
return m_databaseBackend.changesCount();
}
int totalChangesCount()
{
return m_databaseBackend.totalChangesCount();
}
private:
void initializeTables();
std::mutex &databaseMutex() { return m_databaseMutex; }
......
......@@ -110,7 +110,7 @@ void DatabaseBackend::open(Utils::SmallStringView databaseFilePath, OpenMode mod
cacheTextEncoding();
}
sqlite3 *DatabaseBackend::sqliteDatabaseHandle()
sqlite3 *DatabaseBackend::sqliteDatabaseHandle() const
{
checkDatabaseHandleIsNotNull();
return m_databaseHandle;
......@@ -157,16 +157,21 @@ Utils::SmallStringVector DatabaseBackend::columnNames(Utils::SmallStringView tab
return statement.columnNames();
}
int DatabaseBackend::changesCount()
int DatabaseBackend::changesCount() const
{
return sqlite3_changes(sqliteDatabaseHandle());
}
int DatabaseBackend::totalChangesCount()
int DatabaseBackend::totalChangesCount() const
{
return sqlite3_total_changes(sqliteDatabaseHandle());
}
int64_t DatabaseBackend::lastInsertedRowId() const
{
return sqlite3_last_insert_rowid(sqliteDatabaseHandle());
}
void DatabaseBackend::execute(Utils::SmallStringView sqlStatement)
{
ReadWriteStatement statement(sqlStatement, m_database);
......@@ -274,7 +279,7 @@ void DatabaseBackend::checkPragmaValue(Utils::SmallStringView databaseValue,
throw PragmaValueNotSet("SqliteDatabaseBackend::setPragmaValue: pragma value is not set!");
}
void DatabaseBackend::checkDatabaseHandleIsNotNull()
void DatabaseBackend::checkDatabaseHandleIsNotNull() const
{
if (m_databaseHandle == nullptr)
throwDatabaseIsNotOpen("SqliteDatabaseBackend: database is not open!");
......
......@@ -58,7 +58,7 @@ public:
void close();
void closeWithoutException();
sqlite3* sqliteDatabaseHandle();
sqlite3* sqliteDatabaseHandle() const;
void setJournalMode(JournalMode journalMode);
JournalMode journalMode();
......@@ -68,8 +68,10 @@ public:
Utils::SmallStringVector columnNames(Utils::SmallStringView tableName);
int changesCount();
int totalChangesCount();
int changesCount() const;
int totalChangesCount() const;
int64_t lastInsertedRowId() const;
void execute(Utils::SmallStringView sqlStatement);
......@@ -95,7 +97,7 @@ protected:
void checkCanOpenDatabase(Utils::SmallStringView databaseFilePath);
void checkDatabaseCouldBeOpened(int resultCode);
void checkPragmaValue(Utils::SmallStringView databaseValue, Utils::SmallStringView expectedValue);
void checkDatabaseHandleIsNotNull();
void checkDatabaseHandleIsNotNull() const;
void checkIfMultithreadingIsActivated(int resultCode);
void checkIfLoogingIsActivated(int resultCode);
void checkMmapSizeIsSet(int resultCode);
......
......@@ -29,6 +29,7 @@
#include <sqlitedatabase.h>
#include <sqlitetable.h>
#include <sqlitewritestatement.h>
#include <utf8string.h>
#include <QSignalSpy>
......@@ -96,6 +97,30 @@ TEST_F(SqliteDatabase, AddTable)
ASSERT_THAT(database.tables(), Contains(sqliteTable));
}
TEST_F(SqliteDatabase, GetChangesCount)
{
Sqlite::WriteStatement statement("INSERT INTO test(name) VALUES (?)", database);
statement.write(42);
ASSERT_THAT(database.changesCount(), 1);
}
TEST_F(SqliteDatabase, GetTotalChangesCount)
{
Sqlite::WriteStatement statement("INSERT INTO test(name) VALUES (?)", database);
statement.write(42);
ASSERT_THAT(database.lastInsertedRowId(), 1);
}
TEST_F(SqliteDatabase, GetLastInsertedRowId)
{
Sqlite::WriteStatement statement("INSERT INTO test(name) VALUES (?)", database);
statement.write(42);
ASSERT_THAT(database.lastInsertedRowId(), 1);
}
TEST_F(SqliteDatabase, TableIsReadyAfterOpenDatabase)
{
database.close();
......@@ -112,6 +137,10 @@ void SqliteDatabase::SetUp()
{
database.setJournalMode(JournalMode::Memory);
database.setDatabaseFilePath(databaseFilePath);
auto &table = database.addTable();
table.setName("test");
table.addColumn("name");
database.open();
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment