2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
#include "sqlitedatabase.h"
|
|
|
|
|
2020-05-27 23:48:03 +02:00
|
|
|
#include "sqlitereadwritestatement.h"
|
|
|
|
#include "sqlitesessions.h"
|
2015-06-01 18:51:55 +02:00
|
|
|
#include "sqlitetable.h"
|
2017-08-17 15:33:25 +02:00
|
|
|
#include "sqlitetransaction.h"
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2018-09-25 15:42:52 +02:00
|
|
|
#include <QFileInfo>
|
|
|
|
|
2018-03-28 17:55:14 +02:00
|
|
|
#include <chrono>
|
|
|
|
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
namespace Sqlite {
|
|
|
|
|
2018-04-04 10:38:04 +02:00
|
|
|
class Database::Statements
|
|
|
|
{
|
|
|
|
public:
|
2025-01-21 19:27:38 +01:00
|
|
|
Statements(Database &database, const source_location &sourceLocation)
|
2018-04-04 10:38:04 +02:00
|
|
|
: database(database)
|
2025-01-21 19:27:38 +01:00
|
|
|
, sourceLocation(sourceLocation)
|
2018-04-04 10:38:04 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
public:
|
|
|
|
Database &database;
|
2025-01-21 19:27:38 +01:00
|
|
|
source_location sourceLocation;
|
|
|
|
ReadWriteStatement<> deferredBegin{"BEGIN", database, sourceLocation};
|
|
|
|
ReadWriteStatement<> immediateBegin{"BEGIN IMMEDIATE", database, sourceLocation};
|
|
|
|
ReadWriteStatement<> exclusiveBegin{"BEGIN EXCLUSIVE", database, sourceLocation};
|
|
|
|
ReadWriteStatement<> commitBegin{"COMMIT", database, sourceLocation};
|
|
|
|
ReadWriteStatement<> rollbackBegin{"ROLLBACK", database, sourceLocation};
|
2020-05-27 23:48:03 +02:00
|
|
|
Sessions sessions{database, "main", "databaseSessions"};
|
2018-04-04 10:38:04 +02:00
|
|
|
};
|
|
|
|
|
2025-01-21 19:27:38 +01:00
|
|
|
Database::Database(const source_location &sourceLocation)
|
|
|
|
: m_databaseBackend(*this, sourceLocation)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2025-01-21 19:27:38 +01:00
|
|
|
Database::Database(Utils::PathString databaseFilePath,
|
|
|
|
JournalMode journalMode,
|
|
|
|
LockingMode lockingMode,
|
|
|
|
const source_location &sourceLocation)
|
|
|
|
: Database{std::move(databaseFilePath), 0ms, journalMode, lockingMode, sourceLocation}
|
2020-05-27 23:48:03 +02:00
|
|
|
{}
|
2018-03-28 17:55:14 +02:00
|
|
|
|
2021-05-12 12:21:58 +02:00
|
|
|
Database::Database(Utils::PathString databaseFilePath,
|
2018-03-28 17:55:14 +02:00
|
|
|
std::chrono::milliseconds busyTimeout,
|
2021-05-12 12:21:58 +02:00
|
|
|
JournalMode journalMode,
|
2025-01-21 19:27:38 +01:00
|
|
|
LockingMode lockingMode,
|
|
|
|
const source_location &sourceLocation)
|
|
|
|
: m_databaseBackend(*this, sourceLocation)
|
2020-05-27 23:48:03 +02:00
|
|
|
, m_busyTimeout(busyTimeout)
|
2017-08-17 15:33:25 +02:00
|
|
|
{
|
2021-04-29 18:02:31 +02:00
|
|
|
std::lock_guard lock{*this};
|
|
|
|
|
2017-10-18 13:30:13 +02:00
|
|
|
setJournalMode(journalMode);
|
2021-05-12 12:21:58 +02:00
|
|
|
open(std::move(databaseFilePath), lockingMode);
|
2020-05-27 23:48:03 +02:00
|
|
|
|
2022-07-28 10:40:18 +02:00
|
|
|
#ifdef SQLITE_REVERSE
|
2021-05-13 00:50:29 +02:00
|
|
|
if (std::rand() % 2)
|
|
|
|
execute("PRAGMA reverse_unordered_selects=1");
|
2020-05-27 23:48:03 +02:00
|
|
|
#endif
|
2017-08-17 15:33:25 +02:00
|
|
|
}
|
|
|
|
|
2018-03-29 18:33:59 +02:00
|
|
|
Database::~Database() = default;
|
|
|
|
|
2025-01-21 19:27:38 +01:00
|
|
|
void Database::activateLogging(const source_location &sourceLocation)
|
2020-05-16 15:24:01 +02:00
|
|
|
{
|
2025-01-21 19:27:38 +01:00
|
|
|
DatabaseBackend::activateLogging(sourceLocation);
|
2020-05-16 15:24:01 +02:00
|
|
|
}
|
|
|
|
|
2025-01-21 19:27:38 +01:00
|
|
|
void Database::open(LockingMode lockingMode, const source_location &sourceLocation)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2025-01-21 19:27:38 +01:00
|
|
|
m_databaseBackend.open(m_databaseFilePath, m_openMode, m_journalMode, sourceLocation);
|
2021-03-16 11:48:12 +01:00
|
|
|
if (m_busyTimeout > 0ms)
|
|
|
|
m_databaseBackend.setBusyTimeout(m_busyTimeout);
|
|
|
|
else
|
2025-01-21 19:27:38 +01:00
|
|
|
m_databaseBackend.registerBusyHandler(sourceLocation);
|
|
|
|
m_databaseBackend.setLockingMode(lockingMode, sourceLocation);
|
|
|
|
m_databaseBackend.setJournalMode(m_journalMode, sourceLocation);
|
|
|
|
registerTransactionStatements(sourceLocation);
|
2017-07-26 16:02:24 +02:00
|
|
|
m_isOpen = true;
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
2025-01-21 19:27:38 +01:00
|
|
|
void Database::open(Utils::PathString &&databaseFilePath,
|
|
|
|
LockingMode lockingMode,
|
|
|
|
const source_location &sourceLocation)
|
2017-07-26 18:43:07 +02:00
|
|
|
{
|
2018-09-25 15:42:52 +02:00
|
|
|
m_isInitialized = QFileInfo::exists(QString(databaseFilePath));
|
2017-07-27 15:59:54 +02:00
|
|
|
setDatabaseFilePath(std::move(databaseFilePath));
|
2025-01-21 19:27:38 +01:00
|
|
|
open(lockingMode, sourceLocation);
|
2017-07-26 18:43:07 +02:00
|
|
|
}
|
|
|
|
|
2025-01-21 19:27:38 +01:00
|
|
|
void Database::close(const source_location &sourceLocation)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
m_isOpen = false;
|
2018-04-04 10:38:04 +02:00
|
|
|
deleteTransactionStatements();
|
2025-01-21 19:27:38 +01:00
|
|
|
m_databaseBackend.close(sourceLocation);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
2018-09-25 15:42:52 +02:00
|
|
|
bool Database::isInitialized() const
|
|
|
|
{
|
|
|
|
return m_isInitialized;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Database::setIsInitialized(bool isInitialized)
|
|
|
|
{
|
|
|
|
m_isInitialized = isInitialized;
|
|
|
|
}
|
|
|
|
|
2017-09-18 10:21:45 +02:00
|
|
|
bool Database::isOpen() const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
return m_isOpen;
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
2021-05-12 12:21:58 +02:00
|
|
|
void Database::setDatabaseFilePath(Utils::PathString databaseFilePath)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
m_databaseFilePath = std::move(databaseFilePath);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
2020-05-27 23:48:03 +02:00
|
|
|
void Database::setAttachedTables(const Utils::SmallStringVector &tables)
|
|
|
|
{
|
|
|
|
m_statements->sessions.setAttachedTables(tables);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Database::applyAndUpdateSessions()
|
|
|
|
{
|
|
|
|
m_statements->sessions.applyAndUpdateSessions();
|
|
|
|
}
|
|
|
|
|
2025-01-21 19:27:38 +01:00
|
|
|
SessionChangeSets Database::changeSets(const source_location &sourceLocation) const
|
2020-11-05 18:12:26 +01:00
|
|
|
{
|
2025-01-21 19:27:38 +01:00
|
|
|
return m_statements->sessions.changeSets(sourceLocation);
|
2020-11-05 18:12:26 +01:00
|
|
|
}
|
|
|
|
|
2017-09-18 10:21:45 +02:00
|
|
|
const Utils::PathString &Database::databaseFilePath() const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
return m_databaseFilePath;
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
2017-09-18 10:21:45 +02:00
|
|
|
void Database::setJournalMode(JournalMode journalMode)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
m_journalMode = journalMode;
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
2017-09-18 10:21:45 +02:00
|
|
|
JournalMode Database::journalMode() const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
return m_journalMode;
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
2025-01-21 19:27:38 +01:00
|
|
|
LockingMode Database::lockingMode(const source_location &sourceLocation) const
|
2021-05-12 12:21:58 +02:00
|
|
|
{
|
2025-01-21 19:27:38 +01:00
|
|
|
return m_databaseBackend.lockingMode(sourceLocation);
|
2021-05-12 12:21:58 +02:00
|
|
|
}
|
|
|
|
|
2017-09-18 10:21:45 +02:00
|
|
|
void Database::setOpenMode(OpenMode openMode)
|
2017-08-01 18:32:29 +02:00
|
|
|
{
|
|
|
|
m_openMode = openMode;
|
|
|
|
}
|
|
|
|
|
2017-09-18 10:21:45 +02:00
|
|
|
OpenMode Database::openMode() const
|
2017-08-01 18:32:29 +02:00
|
|
|
{
|
|
|
|
return m_openMode;
|
|
|
|
}
|
|
|
|
|
2025-01-21 19:27:38 +01:00
|
|
|
void Database::execute(Utils::SmallStringView sqlStatement, const source_location &sourceLocation)
|
2017-07-31 17:44:45 +02:00
|
|
|
{
|
2025-01-21 19:27:38 +01:00
|
|
|
m_databaseBackend.execute(sqlStatement, sourceLocation);
|
2017-07-31 17:44:45 +02:00
|
|
|
}
|
|
|
|
|
2025-01-21 19:27:38 +01:00
|
|
|
void Database::registerTransactionStatements(const source_location &sourceLocation)
|
2018-03-29 18:33:59 +02:00
|
|
|
{
|
2025-01-21 19:27:38 +01:00
|
|
|
m_statements = std::make_unique<Statements>(*this, sourceLocation);
|
2018-04-04 10:38:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Database::deleteTransactionStatements()
|
|
|
|
{
|
|
|
|
m_statements.reset();
|
2018-03-29 18:33:59 +02:00
|
|
|
}
|
2017-08-17 15:33:25 +02:00
|
|
|
|
2025-01-21 19:27:38 +01:00
|
|
|
void Database::deferredBegin(const source_location &sourceLocation)
|
2018-03-29 18:33:59 +02:00
|
|
|
{
|
2025-01-21 19:27:38 +01:00
|
|
|
m_statements->deferredBegin.execute(sourceLocation);
|
2018-03-29 18:33:59 +02:00
|
|
|
}
|
2017-08-17 15:33:25 +02:00
|
|
|
|
2025-01-21 19:27:38 +01:00
|
|
|
void Database::immediateBegin(const source_location &sourceLocation)
|
2018-03-29 18:33:59 +02:00
|
|
|
{
|
2025-01-21 19:27:38 +01:00
|
|
|
m_statements->immediateBegin.execute(sourceLocation);
|
2018-03-29 18:33:59 +02:00
|
|
|
}
|
|
|
|
|
2025-01-21 19:27:38 +01:00
|
|
|
void Database::exclusiveBegin(const source_location &sourceLocation)
|
2018-03-29 18:33:59 +02:00
|
|
|
{
|
2025-01-21 19:27:38 +01:00
|
|
|
m_statements->exclusiveBegin.execute(sourceLocation);
|
2018-03-29 18:33:59 +02:00
|
|
|
}
|
|
|
|
|
2025-01-21 19:27:38 +01:00
|
|
|
void Database::commit(const source_location &sourceLocation)
|
2018-03-29 18:33:59 +02:00
|
|
|
{
|
2025-01-21 19:27:38 +01:00
|
|
|
m_statements->commitBegin.execute(sourceLocation);
|
2018-03-29 18:33:59 +02:00
|
|
|
}
|
|
|
|
|
2025-01-21 19:27:38 +01:00
|
|
|
void Database::rollback(const source_location &sourceLocation)
|
2018-03-29 18:33:59 +02:00
|
|
|
{
|
2025-01-21 19:27:38 +01:00
|
|
|
m_statements->rollbackBegin.execute(sourceLocation);
|
2018-04-04 10:38:04 +02:00
|
|
|
}
|
|
|
|
|
2025-01-21 19:27:38 +01:00
|
|
|
void Database::immediateSessionBegin(const source_location &sourceLocation)
|
2020-05-27 23:48:03 +02:00
|
|
|
{
|
2025-01-21 19:27:38 +01:00
|
|
|
m_statements->immediateBegin.execute(sourceLocation);
|
|
|
|
m_statements->sessions.create(sourceLocation);
|
2020-05-27 23:48:03 +02:00
|
|
|
}
|
2025-01-21 19:27:38 +01:00
|
|
|
|
|
|
|
void Database::sessionCommit(const source_location &sourceLocation)
|
2020-05-27 23:48:03 +02:00
|
|
|
{
|
2025-01-21 19:27:38 +01:00
|
|
|
m_statements->sessions.commit(sourceLocation);
|
|
|
|
m_statements->commitBegin.execute(sourceLocation);
|
2020-05-27 23:48:03 +02:00
|
|
|
}
|
2025-01-21 19:27:38 +01:00
|
|
|
|
|
|
|
void Database::sessionRollback(const source_location &sourceLocation)
|
2020-05-27 23:48:03 +02:00
|
|
|
{
|
|
|
|
m_statements->sessions.rollback();
|
2025-01-21 19:27:38 +01:00
|
|
|
m_statements->rollbackBegin.execute(sourceLocation);
|
2020-05-27 23:48:03 +02:00
|
|
|
}
|
|
|
|
|
2025-01-21 19:27:38 +01:00
|
|
|
void Database::resetDatabaseForTestsOnly(const source_location &sourceLocation)
|
2023-09-21 13:00:40 +02:00
|
|
|
{
|
2025-01-21 19:27:38 +01:00
|
|
|
m_databaseBackend.resetDatabaseForTestsOnly(sourceLocation);
|
2023-09-21 13:00:40 +02:00
|
|
|
setIsInitialized(false);
|
|
|
|
}
|
|
|
|
|
2025-01-21 19:27:38 +01:00
|
|
|
void Database::clearAllTablesForTestsOnly(const source_location &sourceLocation)
|
2023-09-21 13:00:40 +02:00
|
|
|
{
|
2025-01-21 19:27:38 +01:00
|
|
|
m_databaseBackend.disableForeignKeys(sourceLocation);
|
2023-09-21 13:00:40 +02:00
|
|
|
{
|
2025-01-21 19:27:38 +01:00
|
|
|
Sqlite::ImmediateTransaction transaction{*this, sourceLocation};
|
2023-09-21 13:00:40 +02:00
|
|
|
|
|
|
|
ReadStatement<1> tablesStatement{"SELECT name FROM sqlite_schema WHERE type='table'", *this};
|
2025-01-21 19:27:38 +01:00
|
|
|
auto tables = tablesStatement.template values<Utils::SmallString>(sourceLocation);
|
2023-09-21 13:00:40 +02:00
|
|
|
for (const auto &table : tables)
|
2025-01-21 19:27:38 +01:00
|
|
|
execute("DELETE FROM " + table, sourceLocation);
|
2023-09-21 13:00:40 +02:00
|
|
|
|
2025-01-21 19:27:38 +01:00
|
|
|
transaction.commit(sourceLocation);
|
2023-09-21 13:00:40 +02:00
|
|
|
}
|
|
|
|
|
2025-01-21 19:27:38 +01:00
|
|
|
m_databaseBackend.enableForeignKeys(sourceLocation);
|
2023-09-21 13:00:40 +02:00
|
|
|
}
|
|
|
|
|
2017-09-18 10:21:45 +02:00
|
|
|
DatabaseBackend &Database::backend()
|
2017-07-26 18:43:07 +02:00
|
|
|
{
|
|
|
|
return m_databaseBackend;
|
|
|
|
}
|
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
} // namespace Sqlite
|