Commit 98035954 authored by Marco Bubke's avatar Marco Bubke

Sqlite: Add Read and ReadWrite mode to database

We want open databases in read only mode too.

Change-Id: I3b20602d4bd7841c9fca3260edce082d21022052
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
parent 52a911b9
......@@ -30,15 +30,14 @@
namespace Sqlite {
SqliteDatabase::SqliteDatabase()
: m_databaseBackend(*this),
m_journalMode(JournalMode::Wal)
: m_databaseBackend(*this)
{
}
void SqliteDatabase::open()
{
m_databaseBackend.open(m_databaseFilePath);
m_databaseBackend.setJournalMode(journalMode());
m_databaseBackend.open(m_databaseFilePath, m_openMode);
m_databaseBackend.setJournalMode(m_journalMode);
initializeTables();
m_isOpen = true;
}
......@@ -92,6 +91,16 @@ JournalMode SqliteDatabase::journalMode() const
return m_journalMode;
}
void SqliteDatabase::setOpenMode(OpenMode openMode)
{
m_openMode = openMode;
}
OpenMode SqliteDatabase::openMode() const
{
return m_openMode;
}
int SqliteDatabase::changesCount()
{
return m_databaseBackend.changesCount();
......
......@@ -65,6 +65,9 @@ public:
void setJournalMode(JournalMode journalMode);
JournalMode journalMode() const;
void setOpenMode(OpenMode openMode);
OpenMode openMode() const;
int changesCount();
int totalChangesCount();
......@@ -80,7 +83,8 @@ private:
SqliteDatabaseBackend m_databaseBackend;
std::vector<SqliteTable> m_sqliteTables;
Utils::PathString m_databaseFilePath;
JournalMode m_journalMode;
JournalMode m_journalMode = JournalMode::Wal;
OpenMode m_openMode = OpenMode::ReadWrite;
bool m_isOpen = false;
};
......
......@@ -101,13 +101,13 @@ void SqliteDatabaseBackend::checkpointFullWalLog()
checkIfLogCouldBeCheckpointed(resultCode);
}
void SqliteDatabaseBackend::open(Utils::SmallStringView databaseFilePath)
void SqliteDatabaseBackend::open(Utils::SmallStringView databaseFilePath, OpenMode mode)
{
checkCanOpenDatabase(databaseFilePath);
int resultCode = sqlite3_open_v2(databaseFilePath.data(),
&m_databaseHandle,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
openMode(mode),
NULL);
checkDatabaseCouldBeOpened(resultCode);
......@@ -375,6 +375,18 @@ TextEncoding SqliteDatabaseBackend::pragmaToTextEncoding(Utils::SmallStringView
return static_cast<TextEncoding>(index);
}
int SqliteDatabaseBackend::openMode(OpenMode mode)
{
int sqliteMode = SQLITE_OPEN_CREATE;
switch (mode) {
case OpenMode::ReadOnly: sqliteMode |= SQLITE_OPEN_READONLY; break;
case OpenMode::ReadWrite: sqliteMode |= SQLITE_OPEN_READWRITE; break;
}
return sqliteMode;
}
void SqliteDatabaseBackend::throwExceptionStatic(const char *whatHasHappens)
{
throw SqliteException(whatHasHappens);
......
......@@ -54,7 +54,7 @@ public:
void shutdownSqliteLibrary();
void checkpointFullWalLog();
void open(Utils::SmallStringView databaseFilePath);
void open(Utils::SmallStringView databaseFilePath, OpenMode openMode);
void close();
void closeWithoutException();
......@@ -76,6 +76,8 @@ public:
template <typename Type>
Type toValue(Utils::SmallStringView sqlStatement);
static int openMode(OpenMode);
protected:
bool databaseIsOpen() const;
......@@ -106,9 +108,11 @@ protected:
Utils::SmallStringView textEncodingToPragma(TextEncoding textEncoding);
static TextEncoding pragmaToTextEncoding(Utils::SmallStringView pragma);
Q_NORETURN static void throwExceptionStatic(const char *whatHasHappens);
Q_NORETURN void throwException(const char *whatHasHappens) const;
private:
SqliteDatabase &m_database;
sqlite3 *m_databaseHandle;
......
......@@ -66,6 +66,12 @@ enum class JournalMode : char
Wal
};
enum class OpenMode : char
{
ReadOnly,
ReadWrite
};
enum TextEncoding : char
{
Utf8,
......
......@@ -63,6 +63,13 @@ TEST_F(SqliteDatabase, SetJournalMode)
ASSERT_THAT(database.journalMode(), JournalMode::Memory);
}
TEST_F(SqliteDatabase, SetOpenlMode)
{
database.setOpenMode(OpenMode::ReadOnly);
ASSERT_THAT(database.openMode(), OpenMode::ReadOnly);
}
TEST_F(SqliteDatabase, OpenDatabase)
{
database.close();
......
......@@ -30,10 +30,14 @@
#include <sqliteexception.h>
#include <sqlitewritestatement.h>
#include <sqlite3.h>
#include <QDir>
namespace {
using Backend = Sqlite::SqliteDatabaseBackend;
using Sqlite::SqliteException;
using Sqlite::SqliteWriteStatement;
......@@ -52,7 +56,7 @@ using SqliteDatabaseBackendSlowTest = SqliteDatabaseBackend;
TEST_F(SqliteDatabaseBackend, OpenAlreadyOpenDatabase)
{
ASSERT_THROW(databaseBackend.open(databaseFilePath), SqliteException);
ASSERT_THROW(databaseBackend.open(databaseFilePath, OpenMode::ReadWrite), SqliteException);
}
TEST_F(SqliteDatabaseBackend, CloseAlreadyClosedDatabase)
......@@ -64,7 +68,7 @@ TEST_F(SqliteDatabaseBackend, CloseAlreadyClosedDatabase)
TEST_F(SqliteDatabaseBackend, OpenWithWrongPath)
{
ASSERT_THROW(databaseBackend.open("/xxx/SqliteDatabaseBackendTest.db"), SqliteException);
ASSERT_THROW(databaseBackend.open("/xxx/SqliteDatabaseBackendTest.db", OpenMode::ReadWrite), SqliteException);
}
TEST_F(SqliteDatabaseBackend, DefaultJournalMode)
......@@ -142,10 +146,24 @@ TEST_F(SqliteDatabaseBackend, TextEncodingCannotBeChangedAfterTouchingDatabase)
ASSERT_THROW(databaseBackend.setTextEncoding(Utf16), SqliteException);
}
TEST_F(SqliteDatabaseBackend, OpenModeReadOnly)
{
auto mode = Backend::openMode(OpenMode::ReadOnly);
ASSERT_THAT(mode, SQLITE_OPEN_CREATE | SQLITE_OPEN_READONLY);
}
TEST_F(SqliteDatabaseBackend, OpenModeReadWrite)
{
auto mode = Backend::openMode(OpenMode::ReadWrite);
ASSERT_THAT(mode, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE);
}
void SqliteDatabaseBackend::SetUp()
{
QDir::temp().remove(QStringLiteral("SqliteDatabaseBackendTest.db"));
databaseBackend.open(databaseFilePath);
databaseBackend.open(databaseFilePath, OpenMode::ReadWrite);
}
void SqliteDatabaseBackend::TearDown()
......
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