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)
|
||||
{
|
||||
|
||||
@@ -19,23 +19,25 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
#include <Wt/Dbo/SqlConnectionPool.h>
|
||||
|
||||
#include "core/RecursiveSharedMutex.hpp"
|
||||
|
||||
#include "database/IDb.hpp"
|
||||
|
||||
namespace lms::db
|
||||
{
|
||||
class Session;
|
||||
class Db
|
||||
class Db : public IDb
|
||||
{
|
||||
public:
|
||||
Db(const std::filesystem::path& dbPath, std::size_t connectionCount = 10);
|
||||
Db(const std::filesystem::path& dbPath, std::size_t connectionCount);
|
||||
|
||||
Session& getTLSSession();
|
||||
Session& getTLSSession() override;
|
||||
|
||||
void executeSql(const std::string& sql);
|
||||
void executeSql(const std::string& sql) override;
|
||||
|
||||
private:
|
||||
Db(const Db&) = delete;
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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 <filesystem>
|
||||
#include <memory>
|
||||
|
||||
namespace lms::db
|
||||
{
|
||||
class Session;
|
||||
class IDb
|
||||
{
|
||||
public:
|
||||
virtual ~IDb() = default;
|
||||
|
||||
virtual Session& getTLSSession() = 0;
|
||||
|
||||
virtual void executeSql(const std::string& sql) = 0; // TODO make this private
|
||||
};
|
||||
|
||||
std::unique_ptr<IDb> createDb(const std::filesystem::path& dbPath, std::size_t connectionCount = 10);
|
||||
} // namespace lms::db
|
||||
@@ -65,11 +65,11 @@ namespace lms::db
|
||||
Wt::Dbo::Transaction _transaction;
|
||||
};
|
||||
|
||||
class Db;
|
||||
class IDb;
|
||||
class Session
|
||||
{
|
||||
public:
|
||||
Session(Db& db);
|
||||
Session(IDb& db);
|
||||
~Session() = default;
|
||||
Session(const Session&) = delete;
|
||||
Session& operator=(const Session&) = delete;
|
||||
@@ -111,7 +111,7 @@ namespace lms::db
|
||||
{
|
||||
return &_session;
|
||||
}
|
||||
Db& getDb()
|
||||
IDb& getDb()
|
||||
{
|
||||
return _db;
|
||||
}
|
||||
@@ -146,7 +146,7 @@ namespace lms::db
|
||||
private:
|
||||
void execute(std::string_view query, long long id);
|
||||
|
||||
Db& _db;
|
||||
IDb& _db;
|
||||
Wt::Dbo::Session _session;
|
||||
};
|
||||
} // namespace lms::db
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
|
||||
#include "Common.hpp"
|
||||
|
||||
#include "database/Db.hpp"
|
||||
#include "database/Release.hpp"
|
||||
#include "database/Session.hpp"
|
||||
#include "database/Types.hpp"
|
||||
@@ -29,13 +28,13 @@ namespace lms::db::tests
|
||||
TmpDatabase::TmpDatabase()
|
||||
: _tmpFile{ std::tmpnam(nullptr) }
|
||||
, _fileDeleter{ _tmpFile }
|
||||
, _db{ _tmpFile }
|
||||
, _db{ createDb(_tmpFile) }
|
||||
{
|
||||
}
|
||||
|
||||
db::Db& TmpDatabase::getDb()
|
||||
db::IDb& TmpDatabase::getDb()
|
||||
{
|
||||
return _db;
|
||||
return *_db;
|
||||
}
|
||||
|
||||
DatabaseFixture::~DatabaseFixture()
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
|
||||
#include "database/Artist.hpp"
|
||||
#include "database/Cluster.hpp"
|
||||
#include "database/Db.hpp"
|
||||
#include "database/IDb.hpp"
|
||||
#include "database/Listen.hpp"
|
||||
#include "database/MediaLibrary.hpp"
|
||||
#include "database/Release.hpp"
|
||||
@@ -131,12 +131,12 @@ namespace lms::db::tests
|
||||
public:
|
||||
TmpDatabase();
|
||||
|
||||
db::Db& getDb();
|
||||
IDb& getDb();
|
||||
|
||||
private:
|
||||
const std::filesystem::path _tmpFile;
|
||||
ScopedFileDeleter _fileDeleter;
|
||||
db::Db _db;
|
||||
std::unique_ptr<IDb> _db;
|
||||
};
|
||||
|
||||
class DatabaseFixture : public ::testing::Test
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
#include "database/Artist.hpp"
|
||||
#include "database/ArtistInfo.hpp"
|
||||
#include "database/AuthToken.hpp"
|
||||
#include "database/Db.hpp"
|
||||
#include "database/Directory.hpp"
|
||||
#include "database/Image.hpp"
|
||||
#include "database/PlayListFile.hpp"
|
||||
|
||||
Reference in New Issue
Block a user