Commit 400da9de authored by Marco Bubke's avatar Marco Bubke

Sqlite: Remove static database handle

Change-Id: Ic31f61a477ad681652eeccdaa39f82970c57eb5a
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
parent e562cc3f
......@@ -41,16 +41,22 @@ SqliteDatabase::~SqliteDatabase()
void SqliteDatabase::open()
{
m_sqliteDatabaseBackEnd.open(m_databaseFilePath);
m_sqliteDatabaseBackEnd.setJournalMode(journalMode());
m_databaseBackend.open(m_databaseFilePath);
m_databaseBackend.setJournalMode(journalMode());
initializeTables();
m_isOpen = true;
}
void SqliteDatabase::open(const QString &databaseFilePath)
{
setDatabaseFilePath(databaseFilePath);
open();
}
void SqliteDatabase::close()
{
m_isOpen = false;
m_sqliteDatabaseBackEnd.close();
m_databaseBackend.close();
}
bool SqliteDatabase::isOpen() const
......@@ -89,10 +95,25 @@ JournalMode SqliteDatabase::journalMode() const
return m_journalMode;
}
int SqliteDatabase::changesCount()
{
return m_databaseBackend.changesCount();
}
int SqliteDatabase::totalChangesCount()
{
return m_databaseBackend.totalChangesCount();
}
void SqliteDatabase::initializeTables()
{
for (SqliteTable *table: tables())
table->initialize();
}
SqliteDatabaseBackend &SqliteDatabase::backend()
{
return m_databaseBackend;
}
} // namespace Sqlite
......@@ -34,14 +34,19 @@
namespace Sqlite {
class SqliteTable;
class SqliteDatabaseBackend;
class SQLITE_EXPORT SqliteDatabase
{
friend class SqliteAbstractTransaction;
friend class SqliteStatement;
public:
SqliteDatabase();
~SqliteDatabase();
void open();
void open(const QString &databaseFilePath);
void close();
bool isOpen() const;
......@@ -55,12 +60,16 @@ public:
void setJournalMode(JournalMode journalMode);
JournalMode journalMode() const;
int changesCount();
int totalChangesCount();
private:
void initializeTables();
SqliteDatabaseBackend &backend();
private:
SqliteDatabaseBackend m_sqliteDatabaseBackEnd;
SqliteDatabaseBackend m_databaseBackend;
QVector<SqliteTable*> m_sqliteTables;
QString m_databaseFilePath;
JournalMode m_journalMode;
......
......@@ -48,19 +48,15 @@
namespace Sqlite {
QTC_THREAD_LOCAL SqliteDatabaseBackend *sqliteDatabaseBackend = nullptr;
SqliteDatabaseBackend::SqliteDatabaseBackend()
: m_databaseHandle(nullptr),
m_cachedTextEncoding(Utf8)
{
sqliteDatabaseBackend = this;
}
SqliteDatabaseBackend::~SqliteDatabaseBackend()
{
closeWithoutException();
sqliteDatabaseBackend = nullptr;
}
void SqliteDatabaseBackend::setMmapSize(qint64 defaultSize, qint64 maximumSize)
......@@ -123,22 +119,22 @@ void SqliteDatabaseBackend::open(const QString &databaseFilePath)
sqlite3 *SqliteDatabaseBackend::sqliteDatabaseHandle()
{
checkDatabaseBackendIsNotNull();
checkDatabaseHandleIsNotNull();
return threadLocalInstance()->m_databaseHandle;
return m_databaseHandle;
}
void SqliteDatabaseBackend::setPragmaValue(const Utf8String &pragmaKey, const Utf8String &newPragmaValue)
{
SqliteReadWriteStatement::execute(Utf8StringLiteral("PRAGMA ") + pragmaKey + Utf8StringLiteral("='") + newPragmaValue + Utf8StringLiteral("'"));
Utf8String pragmeValueInDatabase = SqliteReadWriteStatement::toValue<Utf8String>(Utf8StringLiteral("PRAGMA ") + pragmaKey);
SqliteReadWriteStatement statement(Utf8StringLiteral("PRAGMA ") + pragmaKey + Utf8StringLiteral("='") + newPragmaValue + Utf8StringLiteral("'"), *this);
statement.step();
Utf8String pragmeValueInDatabase = toValue<Utf8String>(Utf8StringLiteral("PRAGMA ") + pragmaKey);
checkPragmaValue(pragmeValueInDatabase, newPragmaValue);
}
Utf8String SqliteDatabaseBackend::pragmaValue(const Utf8String &pragma) const
Utf8String SqliteDatabaseBackend::pragmaValue(const Utf8String &pragma)
{
return SqliteReadWriteStatement::toValue<Utf8String>(Utf8StringLiteral("PRAGMA ") + pragma);
return toValue<Utf8String>(Utf8StringLiteral("PRAGMA ") + pragma);
}
void SqliteDatabaseBackend::setJournalMode(JournalMode journalMode)
......@@ -146,7 +142,7 @@ void SqliteDatabaseBackend::setJournalMode(JournalMode journalMode)
setPragmaValue(Utf8StringLiteral("journal_mode"), journalModeToPragma(journalMode));
}
JournalMode SqliteDatabaseBackend::journalMode() const
JournalMode SqliteDatabaseBackend::journalMode()
{
return pragmaToJournalMode(pragmaValue(Utf8StringLiteral("journal_mode")));
}
......@@ -165,7 +161,7 @@ TextEncoding SqliteDatabaseBackend::textEncoding()
Utf8StringVector SqliteDatabaseBackend::columnNames(const Utf8String &tableName)
{
SqliteReadStatement statement(Utf8StringLiteral("SELECT * FROM ") + tableName);
SqliteReadWriteStatement statement(Utf8StringLiteral("SELECT * FROM ") + tableName, *this);
return statement.columnNames();
}
......@@ -179,6 +175,12 @@ int SqliteDatabaseBackend::totalChangesCount()
return sqlite3_total_changes(sqliteDatabaseHandle());
}
void SqliteDatabaseBackend::execute(const Utf8String &sqlStatementUtf8)
{
SqliteReadWriteStatement statement(sqlStatementUtf8, *this);
statement.step();
}
void SqliteDatabaseBackend::close()
{
checkForOpenDatabaseWhichCanBeClosed();
......@@ -191,12 +193,6 @@ void SqliteDatabaseBackend::close()
}
SqliteDatabaseBackend *SqliteDatabaseBackend::threadLocalInstance()
{
checkDatabaseBackendIsNotNull();
return sqliteDatabaseBackend;
}
bool SqliteDatabaseBackend::databaseIsOpen() const
{
return m_databaseHandle != nullptr;
......@@ -282,16 +278,10 @@ void SqliteDatabaseBackend::checkPragmaValue(const Utf8String &databaseValue, co
void SqliteDatabaseBackend::checkDatabaseHandleIsNotNull()
{
if (sqliteDatabaseBackend->m_databaseHandle == nullptr)
if (m_databaseHandle == nullptr)
throwException("SqliteDatabaseBackend: database is not open!");
}
void SqliteDatabaseBackend::checkDatabaseBackendIsNotNull()
{
if (sqliteDatabaseBackend == nullptr)
throwException("SqliteDatabaseBackend: database backend is not initialized!");
}
void SqliteDatabaseBackend::checkIfMultithreadingIsActivated(int resultCode)
{
if (resultCode != SQLITE_OK)
......@@ -357,7 +347,7 @@ JournalMode SqliteDatabaseBackend::pragmaToJournalMode(const Utf8String &pragma)
int index = indexOfPragma(pragma, journalModeStrings, SIZE_OF_BYTEARRAY_ARRAY(journalModeStrings));
if (index < 0)
throwException("SqliteDatabaseBackend::pragmaToJournalMode: pragma can't be transformed in a journal mode enumeration!");
throwExceptionStatic("SqliteDatabaseBackend::pragmaToJournalMode: pragma can't be transformed in a journal mode enumeration!");
return static_cast<JournalMode>(index);
}
......@@ -378,17 +368,32 @@ TextEncoding SqliteDatabaseBackend::pragmaToTextEncoding(const Utf8String &pragm
int index = indexOfPragma(pragma, textEncodingStrings, SIZE_OF_BYTEARRAY_ARRAY(textEncodingStrings));
if (index < 0)
throwException("SqliteDatabaseBackend::pragmaToTextEncoding: pragma can't be transformed in a text encoding enumeration!");
throwExceptionStatic("SqliteDatabaseBackend::pragmaToTextEncoding: pragma can't be transformed in a text encoding enumeration!");
return static_cast<TextEncoding>(index);
}
void SqliteDatabaseBackend::throwException(const char *whatHasHappens)
void SqliteDatabaseBackend::throwExceptionStatic(const char *whatHasHappens)
{
if (sqliteDatabaseBackend && sqliteDatabaseBackend->m_databaseHandle)
throw SqliteException(whatHasHappens, sqlite3_errmsg(sqliteDatabaseBackend->m_databaseHandle));
throw SqliteException(whatHasHappens);
}
void SqliteDatabaseBackend::throwException(const char *whatHasHappens) const
{
if (m_databaseHandle)
throw SqliteException(whatHasHappens, sqlite3_errmsg(m_databaseHandle));
else
throw SqliteException(whatHasHappens);
}
template <typename Type>
Type SqliteDatabaseBackend::toValue(const Utf8String &sqlStatementUtf8)
{
SqliteReadWriteStatement statement(sqlStatementUtf8, *this);
statement.next();
return statement.value<Type>(0);
}
} // namespace Sqlite
......@@ -42,38 +42,42 @@ public:
SqliteDatabaseBackend();
~SqliteDatabaseBackend();
static void setMmapSize(qint64 defaultSize, qint64 maximumSize);
static void activateMultiThreading();
static void activateLogging();
static void initializeSqliteLibrary();
static void shutdownSqliteLibrary();
static void checkpointFullWalLog();
void setMmapSize(qint64 defaultSize, qint64 maximumSize);
void activateMultiThreading();
void activateLogging();
void initializeSqliteLibrary();
void shutdownSqliteLibrary();
void checkpointFullWalLog();
void open(const QString &databaseFilePath);
void close();
void closeWithoutException();
static SqliteDatabaseBackend *threadLocalInstance();
static sqlite3* sqliteDatabaseHandle();
sqlite3* sqliteDatabaseHandle();
void setJournalMode(JournalMode journalMode);
JournalMode journalMode() const;
JournalMode journalMode();
void setTextEncoding(TextEncoding textEncoding);
TextEncoding textEncoding();
static Utf8StringVector columnNames(const Utf8String &tableName);
Utf8StringVector columnNames(const Utf8String &tableName);
static int changesCount();
static int totalChangesCount();
int changesCount();
int totalChangesCount();
void execute(const Utf8String &sqlStatementUtf8);
template <typename Type>
Type toValue(const Utf8String &sqlStatementUtf8);
protected:
bool databaseIsOpen() const;
void setPragmaValue(const Utf8String &pragma, const Utf8String &value);
Utf8String pragmaValue(const Utf8String &pragma) const;
Utf8String pragmaValue(const Utf8String &pragma);
void registerBusyHandler();
void registerRankingFunction();
......@@ -86,22 +90,22 @@ protected:
void checkCanOpenDatabase(const QString &databaseFilePath);
void checkDatabaseCouldBeOpened(int resultCode);
void checkPragmaValue(const Utf8String &databaseValue, const Utf8String &expectedValue);
static void checkDatabaseHandleIsNotNull();
static void checkDatabaseBackendIsNotNull();
static void checkIfMultithreadingIsActivated(int resultCode);
static void checkIfLoogingIsActivated(int resultCode);
static void checkMmapSizeIsSet(int resultCode);
static void checkInitializeSqliteLibraryWasSuccesful(int resultCode);
static void checkShutdownSqliteLibraryWasSuccesful(int resultCode);
static void checkIfLogCouldBeCheckpointed(int resultCode);
void checkDatabaseHandleIsNotNull();
void checkIfMultithreadingIsActivated(int resultCode);
void checkIfLoogingIsActivated(int resultCode);
void checkMmapSizeIsSet(int resultCode);
void checkInitializeSqliteLibraryWasSuccesful(int resultCode);
void checkShutdownSqliteLibraryWasSuccesful(int resultCode);
void checkIfLogCouldBeCheckpointed(int resultCode);
static int indexOfPragma(const Utf8String pragma, const Utf8String pragmas[], size_t pragmaCount);
static const Utf8String &journalModeToPragma(JournalMode journalMode);
static JournalMode pragmaToJournalMode(const Utf8String &pragma);
static const Utf8String &textEncodingToPragma(TextEncoding textEncoding);
const Utf8String &textEncodingToPragma(TextEncoding textEncoding);
static TextEncoding pragmaToTextEncoding(const Utf8String &pragma);
Q_NORETURN static void throwException(const char *whatHasHappens);
Q_NORETURN static void throwExceptionStatic(const char *whatHasHappens);
Q_NORETURN void throwException(const char *whatHasHappens) const;
private:
sqlite3 *m_databaseHandle;
......
......@@ -29,8 +29,9 @@
namespace Sqlite {
SqliteReadStatement::SqliteReadStatement(const Utf8String &sqlStatementUtf8)
: SqliteStatement(sqlStatementUtf8)
SqliteReadStatement::SqliteReadStatement(const Utf8String &sqlStatementUtf8,
SqliteDatabase &database)
: SqliteStatement(sqlStatementUtf8, database)
{
checkIsReadOnlyStatement();
}
......
......@@ -32,7 +32,7 @@ namespace Sqlite {
class SQLITE_EXPORT SqliteReadStatement final : private SqliteStatement
{
public:
explicit SqliteReadStatement(const Utf8String &sqlStatementUtf8);
explicit SqliteReadStatement(const Utf8String &sqlStatementUtf8, SqliteDatabase &database);
using SqliteStatement::next;
using SqliteStatement::reset;
......
......@@ -27,8 +27,15 @@
namespace Sqlite {
SqliteReadWriteStatement::SqliteReadWriteStatement(const Utf8String &sqlStatementUft8)
: SqliteStatement(sqlStatementUft8)
SqliteReadWriteStatement::SqliteReadWriteStatement(const Utf8String &sqlStatementUft8,
SqliteDatabase &database)
: SqliteStatement(sqlStatementUft8, database)
{
}
SqliteReadWriteStatement::SqliteReadWriteStatement(const Utf8String &sqlStatementUft8,
SqliteDatabaseBackend &backend)
: SqliteStatement(sqlStatementUft8, backend)
{
}
......
......@@ -31,8 +31,10 @@ namespace Sqlite {
class SQLITE_EXPORT SqliteReadWriteStatement final : private SqliteStatement
{
friend class SqliteDatabaseBackend;
public:
explicit SqliteReadWriteStatement(const Utf8String &sqlStatementUft8);
explicit SqliteReadWriteStatement(const Utf8String &sqlStatementUft8, SqliteDatabase &database);
using SqliteStatement::next;
using SqliteStatement::step;
......@@ -48,7 +50,10 @@ public:
using SqliteStatement::columnCount;
using SqliteStatement::columnNames;
using SqliteStatement::toValue;
using SqliteStatement::execute;
private:
explicit SqliteReadWriteStatement(const Utf8String &sqlStatementUft8,
SqliteDatabaseBackend &backend);
};
} // namespace Sqlite
......@@ -25,6 +25,7 @@
#include "sqlitestatement.h"
#include "sqlitedatabase.h"
#include "sqlitedatabasebackend.h"
#include "sqliteexception.h"
......@@ -41,8 +42,15 @@
namespace Sqlite {
SqliteStatement::SqliteStatement(const Utf8String &sqlStatementUtf8)
SqliteStatement::SqliteStatement(const Utf8String &sqlStatementUtf8, SqliteDatabase &database)
: SqliteStatement(sqlStatementUtf8, database.backend())
{
}
SqliteStatement::SqliteStatement(const Utf8String &sqlStatementUtf8, SqliteDatabaseBackend &databaseBackend)
: m_compiledStatement(nullptr, deleteCompiledStatement),
m_databaseBackend(databaseBackend),
m_bindingParameterCount(0),
m_columnCount(0),
m_isReadyToFetchValues(false)
......@@ -295,12 +303,6 @@ const Utf8StringVector &SqliteStatement::bindingColumnNames() const
return m_bindingColumnNames;
}
void SqliteStatement::execute(const Utf8String &sqlStatementUtf8)
{
SqliteStatement statement(sqlStatementUtf8);
statement.step();
}
void SqliteStatement::prepare(const Utf8String &sqlStatementUtf8)
{
int resultCode;
......@@ -318,19 +320,14 @@ void SqliteStatement::prepare(const Utf8String &sqlStatementUtf8)
checkForPrepareError(resultCode);
}
sqlite3 *SqliteStatement::sqliteDatabaseHandle()
sqlite3 *SqliteStatement::sqliteDatabaseHandle() const
{
return SqliteDatabaseBackend::sqliteDatabaseHandle();
return m_databaseBackend.sqliteDatabaseHandle();
}
TextEncoding SqliteStatement::databaseTextEncoding()
{
if (SqliteDatabaseBackend::threadLocalInstance())
return SqliteDatabaseBackend::threadLocalInstance()->textEncoding();
throwException("SqliteStatement::databaseTextEncoding: database backend instance is null!");
Q_UNREACHABLE();
return m_databaseBackend.textEncoding();
}
bool SqliteStatement::checkForStepError(int resultCode) const
......@@ -445,7 +442,7 @@ bool SqliteStatement::isReadOnlyStatement() const
return sqlite3_stmt_readonly(m_compiledStatement.get());
}
void SqliteStatement::throwException(const char *whatHasHappened)
void SqliteStatement::throwException(const char *whatHasHappened) const
{
throw SqliteException(whatHasHappened, sqlite3_errmsg(sqliteDatabaseHandle()));
}
......@@ -654,21 +651,21 @@ template SQLITE_EXPORT Utf8StringVector SqliteStatement::values<Utf8StringVector
template SQLITE_EXPORT QVector<QString> SqliteStatement::values<QVector<QString>>(int column) const;
template <typename Type>
Type SqliteStatement::toValue(const Utf8String &sqlStatementUtf8)
Type SqliteStatement::toValue(const Utf8String &sqlStatementUtf8, SqliteDatabase &database)
{
SqliteStatement statement(sqlStatementUtf8);
SqliteStatement statement(sqlStatementUtf8, database);
statement.next();
return statement.value<Type>(0);
}
template SQLITE_EXPORT int SqliteStatement::toValue<int>(const Utf8String &sqlStatementUtf8);
template SQLITE_EXPORT qint64 SqliteStatement::toValue<qint64>(const Utf8String &sqlStatementUtf8);
template SQLITE_EXPORT double SqliteStatement::toValue<double>(const Utf8String &sqlStatementUtf8);
template SQLITE_EXPORT QString SqliteStatement::toValue<QString>(const Utf8String &sqlStatementUtf8);
template SQLITE_EXPORT QByteArray SqliteStatement::toValue<QByteArray>(const Utf8String &sqlStatementUtf8);
template SQLITE_EXPORT Utf8String SqliteStatement::toValue<Utf8String>(const Utf8String &sqlStatementUtf8);
template SQLITE_EXPORT QVariant SqliteStatement::toValue<QVariant>(const Utf8String &sqlStatementUtf8);
template SQLITE_EXPORT int SqliteStatement::toValue<int>(const Utf8String &sqlStatementUtf8, SqliteDatabase &database);
template SQLITE_EXPORT qint64 SqliteStatement::toValue<qint64>(const Utf8String &sqlStatementUtf8, SqliteDatabase &database);
template SQLITE_EXPORT double SqliteStatement::toValue<double>(const Utf8String &sqlStatementUtf8, SqliteDatabase &database);
template SQLITE_EXPORT QString SqliteStatement::toValue<QString>(const Utf8String &sqlStatementUtf8, SqliteDatabase &database);
template SQLITE_EXPORT QByteArray SqliteStatement::toValue<QByteArray>(const Utf8String &sqlStatementUtf8, SqliteDatabase &database);
template SQLITE_EXPORT Utf8String SqliteStatement::toValue<Utf8String>(const Utf8String &sqlStatementUtf8, SqliteDatabase &database);
template SQLITE_EXPORT QVariant SqliteStatement::toValue<QVariant>(const Utf8String &sqlStatementUtf8, SqliteDatabase &database);
} // namespace Sqlite
......@@ -42,10 +42,13 @@ struct sqlite3;
namespace Sqlite {
class SqliteDatabase;
class SqliteDatabaseBackend;
class SQLITE_EXPORT SqliteStatement
{
protected:
explicit SqliteStatement(const Utf8String &sqlStatementUtf8);
explicit SqliteStatement(const Utf8String &sqlStatementUtf8, SqliteDatabase &database);
static void deleteCompiledStatement(sqlite3_stmt *m_compiledStatement);
......@@ -85,10 +88,8 @@ protected:
QMap<QString, QVariant> rowColumnValueMap() const;
QMap<QString, QVariant> twoColumnValueMap() const;
static void execute(const Utf8String &sqlStatementUtf8);
template <typename Type>
static Type toValue(const Utf8String &sqlStatementUtf8);
static Type toValue(const Utf8String &sqlStatementUtf8, SqliteDatabase &database);
void prepare(const Utf8String &sqlStatementUtf8);
void waitForUnlockNotify() const;
......@@ -96,8 +97,8 @@ protected:
void write(const RowDictionary &rowDictionary);
void writeUnchecked(const RowDictionary &rowDictionary);
static sqlite3 *sqliteDatabaseHandle();
static TextEncoding databaseTextEncoding();
sqlite3 *sqliteDatabaseHandle() const;
TextEncoding databaseTextEncoding();
bool checkForStepError(int resultCode) const;
......@@ -114,16 +115,21 @@ protected:
void setColumnCount();
void checkBindingValueMapIsEmpty(const RowDictionary &rowDictionary) const;
bool isReadOnlyStatement() const;
Q_NORETURN static void throwException(const char *whatHasHappened);
Q_NORETURN void throwException(const char *whatHasHappened) const;
template <typename ContainerType>
ContainerType columnValues(const QVector<int> &columnIndices) const;
QString columnName(int column) const;
protected:
explicit SqliteStatement(const Utf8String &sqlStatementUtf8,
SqliteDatabaseBackend &databaseBackend);
private:
std::unique_ptr<sqlite3_stmt, void (*)(sqlite3_stmt*)> m_compiledStatement;
Utf8StringVector m_bindingColumnNames;
SqliteDatabaseBackend &m_databaseBackend;
int m_bindingParameterCount;
int m_columnCount;
mutable bool m_isReadyToFetchValues;
......
......@@ -88,8 +88,8 @@ void SqliteTable::initialize()
createTableSqlStatementBuilder.setUseWithoutRowId(m_withoutRowId);
createTableSqlStatementBuilder.setColumnDefinitions(createColumnDefintions());
SqliteImmediateTransaction transaction;
SqliteWriteStatement::execute(createTableSqlStatementBuilder.sqlStatement());
SqliteImmediateTransaction transaction(*m_sqliteDatabase);
SqliteWriteStatement(createTableSqlStatementBuilder.sqlStatement(), *m_sqliteDatabase).step();
transaction.commit();
m_isReady = true;
......
......@@ -25,6 +25,8 @@
#include "sqlitetransaction.h"
#include "sqlitedatabase.h"
#include "sqlitedatabasebackend.h"
#include "sqlitewritestatement.h"
namespace Sqlite {
......@@ -32,28 +34,41 @@ namespace Sqlite {
SqliteAbstractTransaction::~SqliteAbstractTransaction()
{
if (!m_isAlreadyCommited)
SqliteWriteStatement::execute(Utf8StringLiteral("ROLLBACK"));
m_databaseBackend.execute(Utf8StringLiteral("ROLLBACK"));
}
void SqliteAbstractTransaction::commit()
{
SqliteWriteStatement::execute(Utf8StringLiteral("COMMIT"));
m_databaseBackend.execute(Utf8StringLiteral("COMMIT"));
m_isAlreadyCommited = true;
}
SqliteTransaction::SqliteTransaction()
SqliteAbstractTransaction::SqliteAbstractTransaction(SqliteDatabaseBackend &backend)
: m_databaseBackend(backend)
{
SqliteWriteStatement::execute(Utf8StringLiteral("BEGIN"));
}
SqliteImmediateTransaction::SqliteImmediateTransaction()
SqliteAbstractTransaction::SqliteAbstractTransaction(SqliteDatabase &database)
: SqliteAbstractTransaction(database.backend())
{
SqliteWriteStatement::execute(Utf8StringLiteral("BEGIN IMMEDIATE"));
}
SqliteExclusiveTransaction::SqliteExclusiveTransaction()
SqliteTransaction::SqliteTransaction(SqliteDatabase &database)
: SqliteAbstractTransaction(database)
{
SqliteWriteStatement::execute(Utf8StringLiteral("BEGIN EXCLUSIVE"));
m_databaseBackend.execute(Utf8StringLiteral("BEGIN"));
}
SqliteImmediateTransaction::SqliteImmediateTransaction(SqliteDatabase &database)
: SqliteAbstractTransaction(database)
{
m_databaseBackend.execute(Utf8StringLiteral("BEGIN IMMEDIATE"));
}
SqliteExclusiveTransaction::SqliteExclusiveTransaction(SqliteDatabase &database)
: SqliteAbstractTransaction(database)
{
m_databaseBackend.execute(Utf8StringLiteral("BEGIN EXCLUSIVE"));
}
} // namespace Sqlite
......@@ -29,6 +29,9 @@
namespace Sqlite {
class SqliteDatabaseBackend;
class SqliteDatabase;
class SQLITE_EXPORT SqliteAbstractTransaction
{
public:
......@@ -36,6 +39,13 @@ public:
void commit();
protected:
SqliteAbstractTransaction(SqliteDatabaseBackend &backend);
SqliteAbstractTransaction(SqliteDatabase &database);
protected:
SqliteDatabaseBackend &m_databaseBackend;
private:
bool m_isAlreadyCommited = false;
};
......@@ -44,21 +54,21 @@ private:
class SQLITE_EXPORT SqliteTransaction final : public SqliteAbstractTransaction
{
public:
SqliteTransaction();
SqliteTransaction(SqliteDatabase &database);
};
class SQLITE_EXPORT SqliteImmediateTransaction final : public SqliteAbstractTransaction
{
public:
SqliteImmediateTransaction();
SqliteImmediateTransaction(SqliteDatabase &database);
};
class SQLITE_EXPORT SqliteExclusiveTransaction final : public SqliteAbstractTransaction
{
public:
SqliteExclusiveTransaction();
SqliteExclusiveTransaction(SqliteDatabase &database);
};
......
......@@ -27,8 +27,9 @@
namespace Sqlite {
SqliteWriteStatement::SqliteWriteStatement(const Utf8String &sqlStatementUtf8)
: SqliteStatement(sqlStatementUtf8)
SqliteWriteStatement::SqliteWriteStatement(const Utf8String &sqlStatementUtf8,
SqliteDatabase &database)
: SqliteStatement(sqlStatementUtf8, database)
{
checkIsWritableStatement();
}
......
......@@ -32,7 +32,7 @@ namespace Sqlite {
class SQLITE_EXPORT SqliteWriteStatement : private SqliteStatement
{
public:
explicit SqliteWriteStatement(const Utf8String &sqlStatementUtf8);
explicit SqliteWriteStatement(const Utf8String &sqlStatementUtf8, SqliteDatabase &database);
using SqliteStatement::step;
using SqliteStatement::reset;
......@@ -43,7 +43,6 @@ public:
using SqliteStatement::bindingColumnNames;
using SqliteStatement::write;
using SqliteStatement::writeUnchecked;
using SqliteStatement::execute;
protected:
void checkIsWritableStatement();
......
......@@ -135,7 +135,7 @@ TEST_F(SqliteDatabaseBackend, TextEncodingCannotBeChangedAfterTouchingDatabase)
{
databaseBackend.setJournalMode(JournalMode::Memory);
SqliteWriteStatement::execute(Utf8StringLiteral("CREATE TABLE text(name, number)"));
databaseBackend.execute(Utf8StringLiteral("CREATE TABLE text(name, number)"));
ASSERT_THROW(databaseBackend.setTextEncoding(Utf16), SqliteException);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please