Sqlite: Add session support

Session are captured by hooking in the sqlite changes. They are saved in
blobs and containing inserts, update and deletes. Because the are
semantically coupled to translactions we add a

Change-Id: Ie095558ebc50601fcaae32958ebeeb98b72d73b9
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Marco Bubke
2020-05-27 23:48:03 +02:00
committed by Tim Jenssen
parent d6b1281a65
commit a61eff0079
29 changed files with 1253 additions and 48 deletions

View File

@@ -38,9 +38,14 @@ class Unique
friend bool operator==(Unique, Unique) { return true; }
};
enum class AutoIncrement { No, Yes };
class PrimaryKey
{
friend bool operator==(PrimaryKey, PrimaryKey) { return true; }
public:
AutoIncrement autoincrement = AutoIncrement::No;
};
class NotNull

View File

@@ -125,7 +125,12 @@ public:
void operator()(const Unique &) { columnDefinitionString.append(" UNIQUE"); }
void operator()(const PrimaryKey &) { columnDefinitionString.append(" PRIMARY KEY"); }
void operator()(const PrimaryKey &primaryKey)
{
columnDefinitionString.append(" PRIMARY KEY");
if (primaryKey.autoincrement == AutoIncrement::Yes)
columnDefinitionString.append(" AUTOINCREMENT");
}
void operator()(const ForeignKey &foreignKey)
{

View File

@@ -17,6 +17,8 @@ SOURCES += \
$$PWD/sqliteglobal.cpp \
$$PWD/sqlitereadstatement.cpp \
$$PWD/sqlitereadwritestatement.cpp \
$$PWD/sqlitesessionchangeset.cpp \
$$PWD/sqlitesessions.cpp \
$$PWD/sqlitewritestatement.cpp \
$$PWD/sqlstatementbuilder.cpp \
$$PWD/utf8string.cpp \
@@ -33,6 +35,8 @@ HEADERS += \
$$PWD/sqliteglobal.h \
$$PWD/sqlitereadstatement.h \
$$PWD/sqlitereadwritestatement.h \
$$PWD/sqlitesessionchangeset.h \
$$PWD/sqlitesessions.h \
$$PWD/sqlitetransaction.h \
$$PWD/sqlitevalue.h \
$$PWD/sqlitewritestatement.h \
@@ -46,16 +50,16 @@ HEADERS += \
$$PWD/sqliteindex.h \
$$PWD/sqlitebasestatement.h
DEFINES += SQLITE_THREADSAFE=2 SQLITE_ENABLE_FTS5 \
SQLITE_ENABLE_UNLOCK_NOTIFY SQLITE_ENABLE_JSON1 \
SQLITE_DEFAULT_FOREIGN_KEYS=1 SQLITE_TEMP_STORE=2 SQLITE_DEFAULT_PAGE_SIZE=32768 \
DEFINES += SQLITE_THREADSAFE=2 SQLITE_ENABLE_FTS5 SQLITE_ENABLE_UNLOCK_NOTIFY \
SQLITE_ENABLE_JSON1 SQLITE_DEFAULT_FOREIGN_KEYS=1 SQLITE_TEMP_STORE=2 \
SQLITE_DEFAULT_WAL_SYNCHRONOUS=1 SQLITE_MAX_WORKER_THREADS SQLITE_DEFAULT_MEMSTATUS=0 \
SQLITE_OMIT_DEPRECATED SQLITE_OMIT_DECLTYPE \
SQLITE_MAX_EXPR_DEPTH=0 SQLITE_OMIT_SHARED_CACHE SQLITE_USE_ALLOCA \
SQLITE_ENABLE_MEMORY_MANAGEMENT SQLITE_ENABLE_NULL_TRIM SQLITE_OMIT_EXPLAIN \
SQLITE_OMIT_LOAD_EXTENSION SQLITE_OMIT_UTF16 SQLITE_DQS=0 \
SQLITE_ENABLE_STAT4 HAVE_ISNAN HAVE_FDATASYNC HAVE_MALLOC_USABLE_SIZE \
SQLITE_DEFAULT_MMAP_SIZE=268435456 SQLITE_CORE
SQLITE_DEFAULT_MMAP_SIZE=268435456 SQLITE_CORE SQLITE_ENABLE_SESSION SQLITE_ENABLE_PREUPDATE_HOOK \
SQLITE_LIKE_DOESNT_MATCH_BLOBS
CONFIG(debug, debug|release): DEFINES += SQLITE_ENABLE_API_ARMOR

View File

@@ -3,7 +3,5 @@ win32:QMAKE_CXXFLAGS_DEBUG += -O2
include(../../qtcreatorlibrary.pri)
win32:DEFINES += SQLITE_API=__declspec(dllexport)
unix:DEFINES += SQLITE_API=\"__attribute__((visibility(\\\"default\\\")))\"
include(sqlite-lib.pri)

View File

@@ -67,6 +67,8 @@ public:
return "REAL";
case ColumnType::Text:
return "TEXT";
case ColumnType::Blob:
return "BLOB";
}
Q_UNREACHABLE();

View File

@@ -25,9 +25,10 @@
#include "sqlitedatabase.h"
#include "sqlitereadwritestatement.h"
#include "sqlitesessions.h"
#include "sqlitetable.h"
#include "sqlitetransaction.h"
#include "sqlitereadwritestatement.h"
#include <QFileInfo>
@@ -51,6 +52,7 @@ public:
ReadWriteStatement exclusiveBegin{"BEGIN EXCLUSIVE", database};
ReadWriteStatement commitBegin{"COMMIT", database};
ReadWriteStatement rollbackBegin{"ROLLBACK", database};
Sessions sessions{database, "main", "databaseSessions"};
};
Database::Database()
@@ -60,17 +62,20 @@ Database::Database()
Database::Database(Utils::PathString &&databaseFilePath, JournalMode journalMode)
: Database(std::move(databaseFilePath), 1000ms, journalMode)
{
}
{}
Database::Database(Utils::PathString &&databaseFilePath,
std::chrono::milliseconds busyTimeout,
JournalMode journalMode)
: m_databaseBackend(*this),
m_busyTimeout(busyTimeout)
: m_databaseBackend(*this)
, m_busyTimeout(busyTimeout)
{
setJournalMode(journalMode);
open(std::move(databaseFilePath));
#ifndef QT_NO_DEBUG
execute("PRAGMA reverse_unordered_selects=1");
#endif
}
Database::~Database() = default;
@@ -136,6 +141,16 @@ void Database::setDatabaseFilePath(Utils::PathString &&databaseFilePath)
m_databaseFilePath = std::move(databaseFilePath);
}
void Database::setAttachedTables(const Utils::SmallStringVector &tables)
{
m_statements->sessions.setAttachedTables(tables);
}
void Database::applyAndUpdateSessions()
{
m_statements->sessions.applyAndUpdateSessions();
}
const Utils::PathString &Database::databaseFilePath() const
{
return m_databaseFilePath;
@@ -215,6 +230,22 @@ void Database::rollback()
m_statements->rollbackBegin.execute();
}
void Database::immediateSessionBegin()
{
m_statements->immediateBegin.execute();
m_statements->sessions.create();
}
void Database::sessionCommit()
{
m_statements->sessions.commit();
m_statements->commitBegin.execute();
}
void Database::sessionRollback()
{
m_statements->sessions.rollback();
m_statements->rollbackBegin.execute();
}
void Database::lock()
{
m_databaseMutex.lock();

View File

@@ -123,6 +123,9 @@ public:
void resetUpdateHook() { m_databaseBackend.resetUpdateHook(); }
void setAttachedTables(const Utils::SmallStringVector &tables);
void applyAndUpdateSessions();
private:
void deferredBegin() override;
void immediateBegin() override;
@@ -131,6 +134,9 @@ private:
void rollback() override;
void lock() override;
void unlock() override;
void immediateSessionBegin() override;
void sessionCommit() override;
void sessionRollback() override;
void initializeTables();
void registerTransactionStatements();

View File

@@ -25,7 +25,7 @@
#pragma once
#include <utils/smallstringview.h>
#include <utils/smallstringvector.h>
#include "sqliteglobal.h"
@@ -42,6 +42,8 @@ public:
virtual void execute(Utils::SmallStringView sqlStatement) = 0;
virtual void setUpdateHook(UpdateCallback &callback) = 0;
virtual void resetUpdateHook() = 0;
virtual void applyAndUpdateSessions() = 0;
virtual void setAttachedTables(const Utils::SmallStringVector &tables) = 0;
protected:
~DatabaseInterface() = default;

View File

@@ -43,7 +43,7 @@ public:
, m_sqliteErrorMessage(std::move(sqliteErrorMessage))
{}
const char *what() const noexcept override { return m_sqliteErrorMessage.data(); }
const char *what() const noexcept override { return m_whatErrorHasHappen; }
void printWarning() const;
@@ -283,4 +283,20 @@ public:
{}
};
class CannotApplyChangeSet : public Exception
{
public:
CannotApplyChangeSet(const char *whatErrorHasHappen)
: Exception(whatErrorHasHappen)
{}
};
class ChangeSetIsMisused : public Exception
{
public:
ChangeSetIsMisused(const char *whatErrorHasHappen)
: Exception(whatErrorHasHappen)
{}
};
} // namespace Sqlite

View File

@@ -39,14 +39,7 @@
namespace Sqlite {
enum class ColumnType : char
{
Numeric,
Integer,
Real,
Text,
None
};
enum class ColumnType : char { Numeric, Integer, Real, Text, Blob, None };
enum class ConstraintType : char { NoConstraint, PrimaryKey, Unique, ForeignKey };

View File

@@ -0,0 +1,72 @@
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "sqlitesessionchangeset.h"
#include "sqlitesessions.h"
#include <utils/smallstringio.h>
#include <sqlite3ext.h>
namespace Sqlite {
namespace {
void checkResultCode(int resultCode)
{
switch (resultCode) {
case SQLITE_NOMEM:
throw std::bad_alloc();
}
if (resultCode != SQLITE_OK)
throw UnknowError("Unknow exception");
}
} // namespace
SessionChangeSet::SessionChangeSet(Utils::span<const byte> blob)
: data(sqlite3_malloc64(blob.size()))
, size(int(blob.size()))
{
std::memcpy(data, blob.data(), blob.size());
}
SessionChangeSet::SessionChangeSet(Sessions &session)
{
int resultCode = sqlite3session_changeset(session.session.get(), &size, &data);
checkResultCode(resultCode);
}
SessionChangeSet::~SessionChangeSet()
{
sqlite3_free(data);
}
Utils::span<const byte> SessionChangeSet::asSpan() const
{
return {static_cast<const byte *>(data), static_cast<std::size_t>(size)};
}
} // namespace Sqlite

View File

@@ -0,0 +1,80 @@
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "sqliteglobal.h"
#include <utils/span.h>
#include <memory>
#include <vector>
#include <iosfwd>
namespace Sqlite {
class Sessions;
class SessionChangeSet
{
public:
SessionChangeSet(Utils::span<const byte> blob);
SessionChangeSet(Sessions &session);
~SessionChangeSet();
SessionChangeSet(const SessionChangeSet &) = delete;
void operator=(const SessionChangeSet &) = delete;
SessionChangeSet(SessionChangeSet &&other) noexcept
{
SessionChangeSet temp;
swap(temp, other);
swap(temp, *this);
}
void operator=(SessionChangeSet &);
Utils::span<const byte> asSpan() const;
friend void swap(SessionChangeSet &first, SessionChangeSet &second) noexcept
{
SessionChangeSet temp;
std::swap(temp.data, first.data);
std::swap(temp.size, first.size);
std::swap(first.data, second.data);
std::swap(first.size, second.size);
std::swap(temp.data, second.data);
std::swap(temp.size, second.size);
}
private:
SessionChangeSet() = default;
public:
void *data = nullptr;
int size = {};
};
using SessionChangeSets = std::vector<SessionChangeSet>;
} // namespace Sqlite

View File

@@ -0,0 +1,188 @@
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "sqlitesessions.h"
#include "sqlitereadstatement.h"
#include "sqlitesessionchangeset.h"
#include "sqlitetable.h"
#include <sqlite3ext.h>
#include <memory>
namespace Sqlite {
namespace {
void checkResultCode(int resultCode)
{
switch (resultCode) {
case SQLITE_NOMEM:
throw std::bad_alloc();
case SQLITE_SCHEMA:
throw CannotApplyChangeSet("Cannot apply change set!");
case SQLITE_MISUSE:
throw ChangeSetIsMisused("Change set is misused!");
}
if (resultCode != SQLITE_OK)
throw UnknowError("Unknow exception");
}
int xConflict(void *, int conflict, sqlite3_changeset_iter *)
{
switch (conflict) {
case SQLITE_CHANGESET_DATA:
return SQLITE_CHANGESET_REPLACE;
case SQLITE_CHANGESET_NOTFOUND:
return SQLITE_CHANGESET_OMIT;
case SQLITE_CHANGESET_CONFLICT:
return SQLITE_CHANGESET_REPLACE;
case SQLITE_CHANGESET_CONSTRAINT:
return SQLITE_CHANGESET_OMIT;
case SQLITE_CHANGESET_FOREIGN_KEY:
return SQLITE_CHANGESET_OMIT;
}
return SQLITE_CHANGESET_ABORT;
}
} // namespace
void Sessions::attachTables(const Utils::SmallStringVector &tableNames)
{
for (Utils::SmallStringView tableName : tableNames) {
int resultCode = sqlite3session_attach(session.get(), tableName.data());
checkResultCode(resultCode);
}
}
Sessions::~Sessions() = default;
void Sessions::setAttachedTables(Utils::SmallStringVector tables)
{
tableNames = std::move(tables);
}
void Sessions::create()
{
sqlite3_session *newSession = nullptr;
int resultCode = sqlite3session_create(database.backend().sqliteDatabaseHandle(),
databaseName.data(),
&newSession);
session.reset(newSession);
checkResultCode(resultCode);
attachTables(tableNames);
}
void Sessions::commit()
{
if (session && !sqlite3session_isempty(session.get())) {
SessionChangeSet changeSet{*this};
insertSession.write(changeSet.asSpan());
}
session.reset();
}
void Sessions::rollback()
{
session.reset();
}
void Internal::SessionsBase::createSessionTable(Database &database)
{
Sqlite::Table table;
table.setUseIfNotExists(true);
table.setName(sessionsTableName);
table.addColumn("id", Sqlite::ColumnType::Integer, {Sqlite::PrimaryKey{AutoIncrement::Yes}});
table.addColumn("changeset", Sqlite::ColumnType::Blob);
table.initialize(database);
}
void Sessions::revert()
{
ReadStatement selectChangeSets{Utils::PathString{"SELECT changeset FROM ",
sessionsTableName,
" ORDER BY id DESC"},
database};
auto changeSets = selectChangeSets.values<SessionChangeSet>(1024);
for (auto &changeSet : changeSets) {
int resultCode = sqlite3changeset_apply_v2(database.backend().sqliteDatabaseHandle(),
changeSet.size,
changeSet.data,
nullptr,
xConflict,
nullptr,
nullptr,
nullptr,
SQLITE_CHANGESETAPPLY_INVERT
| SQLITE_CHANGESETAPPLY_NOSAVEPOINT);
checkResultCode(resultCode);
}
}
void Sessions::apply()
{
ReadStatement selectChangeSets{Utils::PathString{"SELECT changeset FROM ",
sessionsTableName,
" ORDER BY id"},
database};
auto changeSets = selectChangeSets.values<SessionChangeSet>(1024);
for (auto &changeSet : changeSets) {
int resultCode = sqlite3changeset_apply_v2(database.backend().sqliteDatabaseHandle(),
changeSet.size,
changeSet.data,
nullptr,
xConflict,
nullptr,
nullptr,
nullptr,
SQLITE_CHANGESETAPPLY_NOSAVEPOINT);
checkResultCode(resultCode);
}
}
void Sessions::applyAndUpdateSessions()
{
create();
apply();
deleteAll();
commit();
}
void Sessions::deleteAll()
{
WriteStatement{Utils::SmallString{"DELETE FROM ", sessionsTableName}, database}.execute();
}
} // namespace Sqlite

View File

@@ -0,0 +1,95 @@
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "sqlitedatabase.h"
#include "sqlitewritestatement.h"
extern "C" {
typedef struct sqlite3_session sqlite3_session;
void sqlite3session_delete(sqlite3_session *pSession);
};
namespace Sqlite {
namespace Internal {
class SQLITE_EXPORT SessionsBase
{
public:
SessionsBase(Database &database, Utils::SmallStringView sessionsTableName)
: sessionsTableName(sessionsTableName)
{
createSessionTable(database);
}
void createSessionTable(Database &database);
public:
Utils::SmallString sessionsTableName;
};
} // namespace Internal
class SQLITE_EXPORT Sessions : public Internal::SessionsBase
{
public:
Sessions(Database &database,
Utils::SmallStringView databaseName,
Utils::SmallStringView sessionsTableName)
: SessionsBase(database, sessionsTableName)
, database(database)
, insertSession{Utils::PathString{"INSERT INTO ",
sessionsTableName,
"(changeset) VALUES(?)"},
database}
, databaseName(databaseName)
, session{nullptr, sqlite3session_delete}
{}
~Sessions();
void setAttachedTables(Utils::SmallStringVector tables);
void create();
void commit();
void rollback();
void revert();
void apply();
void applyAndUpdateSessions();
void deleteAll();
private:
void attachTables(const Utils::SmallStringVector &tables);
public:
Database &database;
WriteStatement insertSession;
Utils::SmallString databaseName;
Utils::SmallStringVector tableNames;
std::unique_ptr<sqlite3_session, decltype(&sqlite3session_delete)> session;
};
} // namespace Sqlite

View File

@@ -27,6 +27,8 @@
#include "sqliteglobal.h"
#include <utils/smallstringview.h>
#include <exception>
#include <mutex>
@@ -49,6 +51,9 @@ public:
virtual void rollback() = 0;
virtual void lock() = 0;
virtual void unlock() = 0;
virtual void immediateSessionBegin() = 0;
virtual void sessionCommit() = 0;
virtual void sessionRollback() = 0;
protected:
~TransactionInterface() = default;
@@ -82,6 +87,42 @@ protected:
bool m_rollback = false;
};
class AbstractThrowingSessionTransaction
{
public:
AbstractThrowingSessionTransaction(const AbstractTransaction &) = delete;
AbstractThrowingSessionTransaction &operator=(const AbstractTransaction &) = delete;
void commit()
{
m_interface.sessionCommit();
m_isAlreadyCommited = true;
m_locker.unlock();
}
~AbstractThrowingSessionTransaction() noexcept(false)
{
try {
if (m_rollback)
m_interface.sessionRollback();
} catch (...) {
if (!std::uncaught_exception())
throw;
}
}
protected:
AbstractThrowingSessionTransaction(TransactionInterface &interface)
: m_interface(interface)
{}
protected:
TransactionInterface &m_interface;
std::unique_lock<TransactionInterface> m_locker{m_interface};
bool m_isAlreadyCommited = false;
bool m_rollback = false;
};
class AbstractThrowingTransaction : public AbstractTransaction
{
public:
@@ -181,6 +222,23 @@ public:
};
using ExclusiveTransaction = BasicExclusiveTransaction<AbstractThrowingTransaction>;
using ExclusiveNonThrowingDestructorTransaction = BasicExclusiveTransaction<AbstractNonThrowingDestructorTransaction>;
using ExclusiveNonThrowingDestructorTransaction
= BasicExclusiveTransaction<AbstractNonThrowingDestructorTransaction>;
class ImmediateSessionTransaction final : public AbstractThrowingSessionTransaction
{
public:
ImmediateSessionTransaction(TransactionInterface &interface)
: AbstractThrowingSessionTransaction(interface)
{
interface.immediateSessionBegin();
}
~ImmediateSessionTransaction()
{
AbstractThrowingSessionTransaction::m_rollback
= !AbstractThrowingSessionTransaction::m_isAlreadyCommited;
}
};
} // namespace Sqlite

View File

@@ -30,6 +30,8 @@
#include <QVariant>
#include <cstddef>
#pragma once
namespace Sqlite {

View File

@@ -398,7 +398,7 @@ public:
"sourceId",
database};
mutable ReadStatement fetchIndexingTimeStampsStatement{
"SELECT sourceId, indexingTimeStamp FROM fileStatuses", database};
"SELECT sourceId, indexingTimeStamp FROM fileStatuses ORDER BY sourceId", database};
mutable ReadStatement fetchDependentSourceIdsStatement{
"WITH RECURSIVE collectedDependencies(sourceId) AS (VALUES(?) UNION SELECT "
"sourceDependencies.sourceId FROM sourceDependencies, collectedDependencies WHERE "

View File

@@ -510,4 +510,24 @@ TEST_F(CreateTableSqlStatementBuilder, GeneratedAlwaysVirtual)
"VIRTUAL)");
}
TEST_F(CreateTableSqlStatementBuilder, PrimaryKeyAutoincrement)
{
builder.clear();
builder.setTableName("test");
builder.addColumn("id", ColumnType::Integer, {Sqlite::PrimaryKey{Sqlite::AutoIncrement::Yes}});
ASSERT_THAT(builder.sqlStatement(), "CREATE TABLE test(id INTEGER PRIMARY KEY AUTOINCREMENT)");
}
TEST_F(CreateTableSqlStatementBuilder, BlobType)
{
builder.clear();
builder.setTableName("test");
builder.addColumn("data", ColumnType::Blob);
ASSERT_THAT(builder.sqlStatement(), "CREATE TABLE test(data BLOB)");
}
} // namespace

View File

@@ -58,6 +58,7 @@
#include <sourcedependency.h>
#include <sourcelocationentry.h>
#include <sourcelocationscontainer.h>
#include <sqlitesessionchangeset.h>
#include <sqlitevalue.h>
#include <symbol.h>
#include <symbolentry.h>
@@ -67,6 +68,8 @@
#include <usedmacro.h>
#include <utils/link.h>
#include <sqlite3ext.h>
namespace {
ClangBackEnd::FilePathCaching *filePathCache = nullptr;
}
@@ -321,6 +324,90 @@ std::ostream &operator<<(std::ostream &out, const Value &value)
return out << ")";
}
namespace {
Utils::SmallStringView operationText(int operation)
{
switch (operation) {
case SQLITE_INSERT:
return "INSERT";
case SQLITE_UPDATE:
return "UPDATE";
case SQLITE_DELETE:
return "DELETE";
}
return {};
}
std::ostream &operator<<(std::ostream &out, sqlite3_changeset_iter *iter)
{
out << "(";
const char *tableName = nullptr;
int columns = 0;
int operation = 0;
sqlite3_value *value = nullptr;
sqlite3changeset_op(iter, &tableName, &columns, &operation, 0);
out << operationText(operation) << " " << tableName << " {";
if (operation == SQLITE_UPDATE || operation == SQLITE_DELETE) {
out << "Old: [";
for (int i = 0; i < columns; i++) {
sqlite3changeset_old(iter, i, &value);
if (value)
out << " " << sqlite3_value_text(value);
else
out << " -";
}
out << "]";
}
if (operation == SQLITE_UPDATE)
out << ", ";
if (operation == SQLITE_UPDATE || operation == SQLITE_INSERT) {
out << "New: [";
for (int i = 0; i < columns; i++) {
sqlite3changeset_new(iter, i, &value);
if (value)
out << " " << sqlite3_value_text(value);
else
out << " -";
}
out << "]";
}
out << "})";
return out;
}
} // namespace
std::ostream &operator<<(std::ostream &out, const SessionChangeSet &changeset)
{
sqlite3_changeset_iter *iter = nullptr;
sqlite3changeset_start(&iter, changeset.size, const_cast<void *>(changeset.data));
out << "ChangeSets([";
if (SQLITE_ROW == sqlite3changeset_next(iter)) {
out << iter;
while (SQLITE_ROW == sqlite3changeset_next(iter))
out << ", " << iter;
}
sqlite3changeset_finalize(iter);
out << "])";
return out;
}
} // namespace Sqlite
namespace ClangBackEnd {

View File

@@ -66,8 +66,10 @@ void PrintTo(const TextRange &range, ::std::ostream *os);
namespace Sqlite {
class Value;
class SessionChangeSet;
std::ostream &operator<<(std::ostream &out, const Value &value);
std::ostream &operator<<(std::ostream &out, const SessionChangeSet &changeset);
} // namespace Sqlite
namespace ProjectExplorer {

View File

@@ -63,5 +63,9 @@ public:
MOCK_METHOD1(setUpdateHook, void(Sqlite::DatabaseInterface::UpdateCallback &));
MOCK_METHOD0(resetUpdateHook, void());
MOCK_METHOD0(applyAndUpdateSessions, void());
MOCK_METHOD1(setAttachedTables, void(const Utils::SmallStringVector &tables));
};

View File

@@ -40,4 +40,7 @@ public:
MOCK_METHOD0(rollback, void ());
MOCK_METHOD0(lock, void ());
MOCK_METHOD0(unlock, void ());
MOCK_METHOD0(immediateSessionBegin, void());
MOCK_METHOD0(sessionCommit, void());
MOCK_METHOD0(sessionRollback, void());
};

View File

@@ -28,6 +28,7 @@
#include "spydummy.h"
#include <sqlitedatabase.h>
#include <sqlitereadstatement.h>
#include <sqlitetable.h>
#include <sqlitewritestatement.h>
#include <utf8string.h>
@@ -50,26 +51,33 @@ using Sqlite::Table;
class SqliteDatabase : public ::testing::Test
{
protected:
void SetUp() override
SqliteDatabase()
{
database.setJournalMode(JournalMode::Memory);
database.setDatabaseFilePath(databaseFilePath);
auto &table = database.addTable();
table.setName("test");
table.addColumn("id", Sqlite::ColumnType::Integer, {Sqlite::PrimaryKey{}});
table.addColumn("name");
database.open();
}
void TearDown() override
~SqliteDatabase()
{
if (database.isOpen())
database.close();
}
std::vector<Utils::SmallString> names() const
{
return Sqlite::ReadStatement("SELECT name FROM test", database).values<Utils::SmallString>(8);
}
protected:
SpyDummy spyDummy;
QString databaseFilePath{":memory:"};
Sqlite::Database database;
mutable Sqlite::Database database;
Sqlite::TransactionInterface &transactionInterface = database;
MockFunction<void(Sqlite::ChangeType tupe, char const *, char const *, long long)> callbackMock;
Sqlite::Database::UpdateCallback callback = callbackMock.AsStdFunction();
@@ -307,4 +315,33 @@ TEST_F(SqliteDatabase, TableUpdateHookCall)
Sqlite::WriteStatement("INSERT INTO test(name) VALUES (?)", database).write(42);
}
TEST_F(SqliteDatabase, SessionsCommit)
{
database.setAttachedTables({"test"});
Sqlite::WriteStatement("INSERT INTO test(id, name) VALUES (?,?)", database).write(1, "foo");
Sqlite::ImmediateSessionTransaction transaction{database};
Sqlite::WriteStatement("INSERT INTO test(id, name) VALUES (?,?)", database).write(2, "bar");
transaction.commit();
Sqlite::WriteStatement("INSERT OR REPLACE INTO test(id, name) VALUES (?,?)", database).write(2, "hoo");
database.applyAndUpdateSessions();
ASSERT_THAT(names(), ElementsAre("foo", "bar"));
}
TEST_F(SqliteDatabase, SessionsRollback)
{
database.setAttachedTables({"test"});
Sqlite::WriteStatement("INSERT INTO test(id, name) VALUES (?,?)", database).write(1, "foo");
{
Sqlite::ImmediateSessionTransaction transaction{database};
Sqlite::WriteStatement("INSERT INTO test(id, name) VALUES (?,?)", database).write(2, "bar");
}
Sqlite::WriteStatement("INSERT OR REPLACE INTO test(id, name) VALUES (?,?)", database).write(2, "hoo");
database.applyAndUpdateSessions();
ASSERT_THAT(names(), ElementsAre("foo", "hoo"));
}
} // namespace

View File

@@ -0,0 +1,443 @@
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "googletest.h"
#include <sqlitedatabase.h>
#include <sqlitereadstatement.h>
#include <sqlitesessionchangeset.h>
#include <sqlitesessions.h>
#include <sqlitetransaction.h>
#include <sqlitewritestatement.h>
#include <ostream>
namespace {
using Sqlite::SessionChangeSet;
using Sqlite::SessionChangeSets;
class DatabaseExecute
{
public:
DatabaseExecute(Utils::SmallStringView sqlStatement, Sqlite::Database &database)
{
database.execute(sqlStatement);
}
};
class Data
{
public:
Data(Sqlite::ValueView name, Sqlite::ValueView number, Sqlite::ValueView value)
: name(name)
, number(number)
, value(value)
{}
Sqlite::Value name;
Sqlite::Value number;
Sqlite::Value value;
};
std::ostream &operator<<(std::ostream &out, const Data &data)
{
return out << "(" << data.name << ", " << data.number << " " << data.value << ")";
}
MATCHER_P3(HasData,
name,
number,
value,
std::string(negation ? "hasn't " : "has ") + PrintToString(name) + ", "
+ PrintToString(number) + ", " + PrintToString(value))
{
const Data &data = arg;
return data.name == name && data.number == number && data.value == value;
}
class Tag
{
public:
Tag(Sqlite::ValueView name, Sqlite::ValueView tag)
: name(name)
, tag(tag)
{}
Sqlite::Value name;
Sqlite::Value tag;
};
std::ostream &operator<<(std::ostream &out, const Tag &tag)
{
return out << "(" << tag.name << ", " << tag.tag << ")";
}
MATCHER_P2(HasTag,
name,
tag,
std::string(negation ? "hasn't " : "has ") + PrintToString(name) + ", " + PrintToString(tag))
{
const Tag &t = arg;
return t.name == name && t.tag == tag;
}
class Sessions : public testing::Test
{
protected:
Sessions() { sessions.setAttachedTables({"data", "tags"}); }
std::vector<Data> fetchData() { return selectData.values<Data, 3>(8); }
std::vector<Tag> fetchTags() { return selectTags.values<Tag, 2>(8); }
SessionChangeSets fetchChangeSets() { return selectChangeSets.values<SessionChangeSet>(8); }
protected:
Sqlite::Database database{":memory:", Sqlite::JournalMode::Memory};
DatabaseExecute createTable{"CREATE TABLE data(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT "
"UNIQUE, number NUMERIC, value NUMERIC)",
database};
DatabaseExecute createTable2{"CREATE TABLE tags(id INTEGER PRIMARY KEY AUTOINCREMENT, dataId "
"INTEGER NOT NULL REFERENCES data ON DELETE CASCADE DEFERRABLE "
"INITIALLY DEFERRED, tag NUMERIC)",
database};
Sqlite::Sessions sessions{database, "main", "testsessions"};
Sqlite::WriteStatement insertData{"INSERT INTO data(name, number, value) VALUES (?1, ?2, ?3) "
"ON CONFLICT (name) DO UPDATE SET (number, value) = (?2, ?3)",
database};
Sqlite::WriteStatement updateNumber{"UPDATE data SET number = ?002 WHERE name=?001", database};
Sqlite::WriteStatement updateValue{"UPDATE data SET value = ?002 WHERE name=?001", database};
Sqlite::WriteStatement deleteData{"DELETE FROM data WHERE name=?", database};
Sqlite::WriteStatement deleteTag{
"DELETE FROM tags WHERE dataId=(SELECT id FROM data WHERE name=?)", database};
Sqlite::WriteStatement insertTag{
"INSERT INTO tags(dataId, tag) VALUES ((SELECT id FROM data WHERE name=?1), ?2) ", database};
Sqlite::ReadStatement selectData{"SELECT name, number, value FROM data", database};
Sqlite::ReadStatement selectTags{"SELECT name, tag FROM tags JOIN data ON data.id=tags.dataId",
database};
Sqlite::ReadStatement selectChangeSets{"SELECT changeset FROM testsessions", database};
};
TEST_F(Sessions, DontThrowForCommittingWithoutSessionStart)
{
ASSERT_NO_THROW(sessions.commit());
}
TEST_F(Sessions, CreateEmptySession)
{
sessions.create();
sessions.commit();
ASSERT_THAT(fetchChangeSets(), IsEmpty());
}
TEST_F(Sessions, CreateSessionWithInsert)
{
sessions.create();
insertData.write("foo", 22, 3.14);
sessions.commit();
ASSERT_THAT(fetchChangeSets(), SizeIs(1));
}
TEST_F(Sessions, CreateSessionWithUpdate)
{
insertData.write("foo", 22, 3.14);
sessions.create();
updateNumber.write("foo", "bar");
sessions.commit();
ASSERT_THAT(fetchChangeSets(), SizeIs(1));
}
TEST_F(Sessions, CreateSessionWithDelete)
{
insertData.write("foo", 22, 3.14);
sessions.create();
deleteData.write("foo");
sessions.commit();
ASSERT_THAT(fetchChangeSets(), SizeIs(1));
}
TEST_F(Sessions, CreateSessionWithInsertAndUpdate)
{
sessions.create();
insertData.write("foo", 22, 3.14);
sessions.commit();
sessions.create();
updateNumber.write("foo", "bar");
sessions.commit();
ASSERT_THAT(fetchChangeSets(), SizeIs(2));
}
TEST_F(Sessions, CreateSession)
{
sessions.create();
insertData.write("foo", 22, 3.14);
sessions.commit();
ASSERT_THAT(fetchChangeSets(), SizeIs(1));
}
TEST_F(Sessions, RevertSession)
{
sessions.create();
insertData.write("foo", 22, 3.14);
sessions.commit();
sessions.revert();
ASSERT_THAT(fetchData(), IsEmpty());
}
TEST_F(Sessions, RevertSessionToBase)
{
insertData.write("bar", "foo", 99);
sessions.create();
insertData.write("foo", 22, 3.14);
sessions.commit();
sessions.revert();
ASSERT_THAT(fetchData(), ElementsAre(HasData("bar", "foo", 99)));
}
TEST_F(Sessions, RevertMultipleSession)
{
sessions.create();
insertData.write("foo", 22, 3.14);
sessions.commit();
sessions.create();
updateNumber.write("foo", "bar");
sessions.commit();
sessions.revert();
ASSERT_THAT(fetchData(), IsEmpty());
}
TEST_F(Sessions, ApplySession)
{
sessions.create();
insertData.write("foo", 22, 3.14);
sessions.commit();
sessions.apply();
ASSERT_THAT(fetchData(), ElementsAre(HasData("foo", 22, 3.14)));
}
TEST_F(Sessions, ApplySessionAfterAddingNewEntries)
{
sessions.create();
insertData.write("foo", 22, 3.14);
sessions.commit();
insertData.write("bar", "foo", 99);
sessions.apply();
ASSERT_THAT(fetchData(),
UnorderedElementsAre(HasData("foo", 22, 3.14), HasData("bar", "foo", 99)));
}
TEST_F(Sessions, ApplyOverridesEntriesWithUniqueConstraint)
{
sessions.create();
insertData.write("foo", 22, 3.14);
sessions.commit();
insertData.write("foo", "bar", 3.14);
sessions.apply();
ASSERT_THAT(fetchData(), ElementsAre(HasData("foo", 22, 3.14)));
}
TEST_F(Sessions, ApplyDoesNotOverrideDeletedEntries)
{
insertData.write("foo", "bar", 3.14);
sessions.create();
insertData.write("foo", 22, 3.14);
sessions.commit();
deleteData.write("foo");
sessions.apply();
ASSERT_THAT(fetchData(), IsEmpty());
}
TEST_F(Sessions, ApplyDoesOnlyOverwriteUpdatedValues)
{
insertData.write("foo", "bar", 3.14);
sessions.create();
updateValue.write("foo", 1234);
sessions.commit();
insertData.write("foo", "poo", 891);
sessions.apply();
ASSERT_THAT(fetchData(), ElementsAre(HasData("foo", "poo", 1234)));
}
TEST_F(Sessions, ApplyDoesDoesNotOverrideForeignKeyIfReferenceIsDeleted)
{
insertData.write("foo2", "bar", 3.14);
insertData.write("foo", "bar", 3.14);
sessions.create();
insertTag.write("foo2", 4321);
insertTag.write("foo", 1234);
sessions.commit();
deleteData.write("foo");
sessions.apply();
ASSERT_THAT(fetchTags(), ElementsAre(HasTag("foo2", 4321)));
}
TEST_F(Sessions, ApplyDoesDoesNotOverrideIfConstraintsIsApplied)
{
insertData.write("foo", "bar", 3.14);
sessions.create();
deleteData.write("foo");
sessions.commit();
sessions.revert();
insertTag.write("foo", 1234);
sessions.apply();
ASSERT_THAT(fetchTags(), IsEmpty());
}
TEST_F(Sessions, ApplyDoesDoesNotOverrideForeignKeyIfReferenceIsDeletedDeferred)
{
Sqlite::DeferredTransaction transaction{database};
insertData.write("foo2", "bar", 3.14);
insertData.write("foo", "bar", 3.14);
sessions.create();
insertTag.write("foo2", 4321);
insertTag.write("foo", 1234);
sessions.commit();
deleteData.write("foo");
sessions.apply();
transaction.commit();
ASSERT_THAT(fetchTags(), ElementsAre(HasTag("foo2", 4321)));
}
TEST_F(Sessions, EndSessionOnRollback)
{
insertData.write("foo", "bar", 3.14);
sessions.create();
updateValue.write("foo", 99);
sessions.rollback();
sessions.commit();
sessions.create();
updateNumber.write("foo", 333);
sessions.commit();
updateValue.write("foo", 666);
sessions.apply();
ASSERT_THAT(fetchData(), ElementsAre(HasData("foo", 333, 666)));
}
TEST_F(Sessions, EndSessionOnCommit)
{
insertData.write("foo", "bar", 3.14);
sessions.create();
updateValue.write("foo", 99);
sessions.commit();
updateValue.write("foo", 666);
sessions.commit();
sessions.apply();
ASSERT_THAT(fetchData(), ElementsAre(HasData("foo", "bar", 99)));
}
TEST_F(Sessions, DeleteSessions)
{
insertData.write("foo", "bar", 3.14);
sessions.create();
updateValue.write("foo", 99);
sessions.commit();
sessions.revert();
sessions.deleteAll();
sessions.apply();
ASSERT_THAT(fetchData(), ElementsAre(HasData("foo", "bar", 3.14)));
}
TEST_F(Sessions, DeleteAllSessions)
{
insertData.write("foo", "bar", 3.14);
sessions.create();
updateValue.write("foo", 99);
sessions.commit();
sessions.revert();
sessions.deleteAll();
sessions.apply();
ASSERT_THAT(fetchData(), ElementsAre(HasData("foo", "bar", 3.14)));
}
TEST_F(Sessions, ApplyAndUpdateSessions)
{
insertData.write("foo", "bar", 3.14);
sessions.create();
updateValue.write("foo", 99);
sessions.commit();
updateValue.write("foo", 99);
sessions.applyAndUpdateSessions();
updateValue.write("foo", 22);
sessions.apply();
ASSERT_THAT(fetchData(), ElementsAre(HasData("foo", "bar", 22)));
}
TEST_F(Sessions, ApplyAndUpdateSessionsHasOnlyOneChangeSet)
{
insertData.write("foo", "bar", 3.14);
sessions.create();
updateValue.write("foo", 99);
sessions.commit();
updateValue.write("foo", 99);
sessions.applyAndUpdateSessions();
ASSERT_THAT(fetchChangeSets(), SizeIs(1));
}
} // namespace

View File

@@ -460,9 +460,8 @@ TEST_F(SqliteStatement, GetTupleValuesWithoutArguments)
auto values = statement.values<Tuple, 3>(3);
ASSERT_THAT(values, ElementsAre(Tuple{"bar", 0, 1},
Tuple{"foo", 23.3, 2},
Tuple{"poo", 40.0, 3}));
ASSERT_THAT(values,
UnorderedElementsAre(Tuple{"bar", 0, 1}, Tuple{"foo", 23.3, 2}, Tuple{"poo", 40.0, 3}));
}
TEST_F(SqliteStatement, GetSingleValuesWithoutArguments)
@@ -471,7 +470,7 @@ TEST_F(SqliteStatement, GetSingleValuesWithoutArguments)
std::vector<Utils::SmallString> values = statement.values<Utils::SmallString>(3);
ASSERT_THAT(values, ElementsAre("bar", "foo", "poo"));
ASSERT_THAT(values, UnorderedElementsAre("bar", "foo", "poo"));
}
class FooValue
@@ -497,7 +496,7 @@ TEST_F(SqliteStatement, GetSingleSqliteValuesWithoutArguments)
std::vector<FooValue> values = statement.values<FooValue>(3);
ASSERT_THAT(values, ElementsAre(Eq("blah"), Eq(23.3), Eq(40), IsNull()));
ASSERT_THAT(values, UnorderedElementsAre(Eq("blah"), Eq(23.3), Eq(40), IsNull()));
}
TEST_F(SqliteStatement, GetStructValuesWithoutArguments)
@@ -506,7 +505,8 @@ TEST_F(SqliteStatement, GetStructValuesWithoutArguments)
auto values = statement.values<Output, 3>(3);
ASSERT_THAT(values, ElementsAre(Output{"bar", "blah", 1},
ASSERT_THAT(values,
UnorderedElementsAre(Output{"bar", "blah", 1},
Output{"foo", "23.3", 2},
Output{"poo", "40", 3}));
}
@@ -529,8 +529,7 @@ TEST_F(SqliteStatement, GetValuesForMultipleOutputValuesAndContainerQueryValues)
auto values = statement.values<Tuple, 3>(3, queryValues);
ASSERT_THAT(values, ElementsAre(Tuple{"poo", 40, 3.},
Tuple{"foo", 23.3, 2.}));
ASSERT_THAT(values, UnorderedElementsAre(Tuple{"poo", 40, 3.}, Tuple{"foo", 23.3, 2.}));
}
TEST_F(SqliteStatement, GetValuesForSingleOutputValuesAndContainerQueryValues)
@@ -540,7 +539,7 @@ TEST_F(SqliteStatement, GetValuesForSingleOutputValuesAndContainerQueryValues)
std::vector<Utils::SmallString> values = statement.values<Utils::SmallString>(3, queryValues);
ASSERT_THAT(values, ElementsAre("poo", "foo"));
ASSERT_THAT(values, UnorderedElementsAre("poo", "foo"));
}
TEST_F(SqliteStatement, GetValuesForMultipleOutputValuesAndContainerQueryTupleValues)
@@ -552,8 +551,7 @@ TEST_F(SqliteStatement, GetValuesForMultipleOutputValuesAndContainerQueryTupleVa
auto values = statement.values<ResultTuple, 3>(3, queryValues);
ASSERT_THAT(values, ElementsAre(ResultTuple{"poo", 40, 3},
ResultTuple{"bar", 0, 1}));
ASSERT_THAT(values, UnorderedElementsAre(ResultTuple{"poo", 40, 3}, ResultTuple{"bar", 0, 1}));
}
TEST_F(SqliteStatement, GetValuesForSingleOutputValuesAndContainerQueryTupleValues)
@@ -564,7 +562,7 @@ TEST_F(SqliteStatement, GetValuesForSingleOutputValuesAndContainerQueryTupleValu
std::vector<Utils::SmallString> values = statement.values<Utils::SmallString>(3, queryValues);
ASSERT_THAT(values, ElementsAre("poo", "bar"));
ASSERT_THAT(values, UnorderedElementsAre("poo", "bar"));
}
TEST_F(SqliteStatement, GetValuesForMultipleOutputValuesAndMultipleQueryValue)
@@ -616,8 +614,7 @@ TEST_F(SqliteStatement, GetStructOutputValuesAndContainerQueryTupleValues)
auto values = statement.values<Output, 3>(3, queryValues);
ASSERT_THAT(values, ElementsAre(Output{"poo", "40", 3},
Output{"bar", "blah", 1}));
ASSERT_THAT(values, UnorderedElementsAre(Output{"poo", "40", 3}, Output{"bar", "blah", 1}));
}
TEST_F(SqliteStatement, GetBlobValues)

View File

@@ -33,12 +33,13 @@
namespace {
using Sqlite::DeferredTransaction;
using Sqlite::ImmediateTransaction;
using Sqlite::ExclusiveTransaction;
using Sqlite::DeferredNonThrowingDestructorTransaction;
using Sqlite::ImmediateNonThrowingDestructorTransaction;
using Sqlite::DeferredTransaction;
using Sqlite::ExclusiveNonThrowingDestructorTransaction;
using Sqlite::ExclusiveTransaction;
using Sqlite::ImmediateNonThrowingDestructorTransaction;
using Sqlite::ImmediateSessionTransaction;
using Sqlite::ImmediateTransaction;
class SqliteTransaction : public testing::Test
{
@@ -316,4 +317,56 @@ TEST_F(SqliteTransaction, TransactionRollbackInDestructorDontThrows)
ASSERT_NO_THROW(ExclusiveNonThrowingDestructorTransaction{mockTransactionBackend});
}
TEST_F(SqliteTransaction, ImmediateSessionTransactionCommit)
{
InSequence s;
EXPECT_CALL(mockTransactionBackend, lock());
EXPECT_CALL(mockTransactionBackend, immediateSessionBegin());
EXPECT_CALL(mockTransactionBackend, sessionCommit());
EXPECT_CALL(mockTransactionBackend, unlock());
ImmediateSessionTransaction transaction{mockTransactionBackend};
transaction.commit();
}
TEST_F(SqliteTransaction, ImmediateSessionTransactionRollBack)
{
InSequence s;
EXPECT_CALL(mockTransactionBackend, lock());
EXPECT_CALL(mockTransactionBackend, immediateSessionBegin());
EXPECT_CALL(mockTransactionBackend, sessionRollback());
EXPECT_CALL(mockTransactionBackend, unlock());
ImmediateSessionTransaction transaction{mockTransactionBackend};
}
TEST_F(SqliteTransaction, SessionTransactionRollbackInDestructorThrows)
{
ON_CALL(mockTransactionBackend, sessionRollback()).WillByDefault(Throw(Sqlite::Exception("foo")));
ASSERT_THROW(ImmediateSessionTransaction{mockTransactionBackend}, Sqlite::Exception);
}
TEST_F(SqliteTransaction, ImmidiateSessionTransactionBeginThrows)
{
ON_CALL(mockTransactionBackend, immediateSessionBegin())
.WillByDefault(Throw(Sqlite::Exception("foo")));
ASSERT_THROW(ImmediateSessionTransaction{mockTransactionBackend}, Sqlite::Exception);
}
TEST_F(SqliteTransaction, ImmediateSessionTransactionBeginThrowsAndNotRollback)
{
InSequence s;
EXPECT_CALL(mockTransactionBackend, lock());
EXPECT_CALL(mockTransactionBackend, immediateSessionBegin()).WillOnce(Throw(Sqlite::Exception("foo")));
EXPECT_CALL(mockTransactionBackend, sessionRollback()).Times(0);
EXPECT_CALL(mockTransactionBackend, unlock());
ASSERT_ANY_THROW(ImmediateSessionTransaction{mockTransactionBackend});
}
} // namespace

View File

@@ -81,6 +81,7 @@ SOURCES += \
smallstring-test.cpp \
sourcerangefilter-test.cpp \
spydummy.cpp \
sqlitesessions-test.cpp \
sqlitevalue-test.cpp \
symbolindexer-test.cpp \
symbolsfindfilter-test.cpp \

View File

@@ -25,8 +25,8 @@
#include "googletest.h"
#include <sqlitedatabase.h>
#include <sqliteglobal.h>
#include <utils/temporarydirectory.h>
#include <QCoreApplication>
@@ -38,6 +38,7 @@
int main(int argc, char *argv[])
{
Sqlite::Database::activateLogging();
const QString temporayDirectoryPath = QDir::tempPath() +"/QtCreator-UnitTests-XXXXXX";
Utils::TemporaryDirectory::setMasterTemporaryDirectory(temporayDirectoryPath);
qputenv("TMPDIR", Utils::TemporaryDirectory::masterDirectoryPath().toUtf8());