Created an interface for the Db class
This commit is contained in:
@@ -17,8 +17,6 @@
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "database/Db.hpp"
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
@@ -31,6 +29,8 @@
|
||||
#include "database/Session.hpp"
|
||||
#include "database/User.hpp"
|
||||
|
||||
#include "Db.hpp"
|
||||
|
||||
namespace lms::db
|
||||
{
|
||||
namespace
|
||||
@@ -134,6 +134,11 @@ namespace lms::db
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::unique_ptr<IDb> createDb(const std::filesystem::path& dbPath, std::size_t connectionCount)
|
||||
{
|
||||
return std::make_unique<Db>(dbPath, connectionCount);
|
||||
}
|
||||
|
||||
// Session living class handling the database and the login
|
||||
Db::Db(const std::filesystem::path& dbPath, std::size_t connectionCount)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Emeric Poupon
|
||||
*
|
||||
* This file is part of LMS.
|
||||
*
|
||||
* LMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* LMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
#include <Wt/Dbo/SqlConnectionPool.h>
|
||||
|
||||
#include "core/RecursiveSharedMutex.hpp"
|
||||
|
||||
#include "database/IDb.hpp"
|
||||
|
||||
namespace lms::db
|
||||
{
|
||||
class Db : public IDb
|
||||
{
|
||||
public:
|
||||
Db(const std::filesystem::path& dbPath, std::size_t connectionCount);
|
||||
|
||||
Session& getTLSSession() override;
|
||||
|
||||
void executeSql(const std::string& sql) override;
|
||||
|
||||
private:
|
||||
Db(const Db&) = delete;
|
||||
Db& operator=(const Db&) = delete;
|
||||
|
||||
friend class Session;
|
||||
|
||||
core::RecursiveSharedMutex& getMutex() { return _sharedMutex; }
|
||||
Wt::Dbo::SqlConnectionPool& getConnectionPool() { return *_connectionPool; }
|
||||
|
||||
void performQuickCheck();
|
||||
void performIntegrityCheck();
|
||||
void performForeignKeyConstraintsCheck();
|
||||
|
||||
class ScopedConnection
|
||||
{
|
||||
public:
|
||||
ScopedConnection(Wt::Dbo::SqlConnectionPool& pool);
|
||||
~ScopedConnection();
|
||||
|
||||
Wt::Dbo::SqlConnection* operator->() const;
|
||||
Wt::Dbo::SqlConnection& operator*() const { return *_connection; }
|
||||
|
||||
private:
|
||||
ScopedConnection(const ScopedConnection&) = delete;
|
||||
ScopedConnection& operator=(const ScopedConnection&) = delete;
|
||||
|
||||
Wt::Dbo::SqlConnectionPool& _connectionPool;
|
||||
std::unique_ptr<Wt::Dbo::SqlConnection> _connection;
|
||||
};
|
||||
|
||||
core::RecursiveSharedMutex _sharedMutex;
|
||||
std::unique_ptr<Wt::Dbo::SqlConnectionPool> _connectionPool;
|
||||
|
||||
std::mutex _tlsSessionsMutex;
|
||||
std::vector<std::unique_ptr<Session>> _tlsSessions;
|
||||
};
|
||||
|
||||
} // namespace lms::db
|
||||
@@ -24,11 +24,11 @@
|
||||
#include "core/Exception.hpp"
|
||||
#include "core/ILogger.hpp"
|
||||
#include "core/ITraceLogger.hpp"
|
||||
#include "database/Db.hpp"
|
||||
#include "database/ScanSettings.hpp"
|
||||
#include "database/Session.hpp"
|
||||
#include "database/User.hpp"
|
||||
|
||||
#include "Db.hpp"
|
||||
#include "Utils.hpp"
|
||||
|
||||
namespace lms::db
|
||||
@@ -67,7 +67,7 @@ namespace lms::db::Migration
|
||||
class ScopedNoForeignKeys
|
||||
{
|
||||
public:
|
||||
ScopedNoForeignKeys(Db& db)
|
||||
ScopedNoForeignKeys(IDb& db)
|
||||
: _db{ db }
|
||||
{
|
||||
_db.executeSql("PRAGMA foreign_keys=OFF");
|
||||
@@ -83,7 +83,7 @@ namespace lms::db::Migration
|
||||
ScopedNoForeignKeys& operator=(const ScopedNoForeignKeys&) = delete;
|
||||
ScopedNoForeignKeys& operator=(ScopedNoForeignKeys&&) = delete;
|
||||
|
||||
Db& _db;
|
||||
IDb& _db;
|
||||
};
|
||||
|
||||
namespace
|
||||
|
||||
@@ -26,7 +26,6 @@
|
||||
#include "database/Artwork.hpp"
|
||||
#include "database/AuthToken.hpp"
|
||||
#include "database/Cluster.hpp"
|
||||
#include "database/Db.hpp"
|
||||
#include "database/Directory.hpp"
|
||||
#include "database/Image.hpp"
|
||||
#include "database/Listen.hpp"
|
||||
@@ -53,6 +52,7 @@
|
||||
#include "database/UIState.hpp"
|
||||
#include "database/User.hpp"
|
||||
|
||||
#include "Db.hpp"
|
||||
#include "Migration.hpp"
|
||||
#include "Utils.hpp"
|
||||
#include "traits/EnumSetTraits.hpp"
|
||||
@@ -96,10 +96,10 @@ namespace lms::db
|
||||
#endif
|
||||
}
|
||||
|
||||
Session::Session(Db& db)
|
||||
Session::Session(IDb& db)
|
||||
: _db{ db }
|
||||
{
|
||||
_session.setConnectionPool(_db.getConnectionPool());
|
||||
_session.setConnectionPool(static_cast<Db&>(_db).getConnectionPool());
|
||||
|
||||
_session.mapClass<Artist>("artist");
|
||||
_session.mapClass<ArtistInfo>("artist_info");
|
||||
@@ -140,7 +140,7 @@ namespace lms::db
|
||||
|
||||
WriteTransaction Session::createWriteTransaction()
|
||||
{
|
||||
return WriteTransaction{ _db.getMutex(), _session };
|
||||
return WriteTransaction{ static_cast<Db&>(_db).getMutex(), _session };
|
||||
}
|
||||
|
||||
ReadTransaction Session::createReadTransaction()
|
||||
@@ -359,7 +359,7 @@ namespace lms::db
|
||||
|
||||
// We manually take a lock here since vacuum cannot be inside a transaction
|
||||
{
|
||||
std::unique_lock lock{ _db.getMutex() };
|
||||
std::unique_lock lock{ static_cast<Db&>(_db).getMutex() };
|
||||
_db.executeSql("VACUUM");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user