Added basic support for artist.nfo file parsing, ref #640

This commit is contained in:
emeric
2025-03-22 17:21:08 +01:00
parent 67a6b660be
commit e349e14101
32 changed files with 905 additions and 12 deletions
+1
View File
@@ -1,5 +1,6 @@
add_library(lmsdatabase STATIC
impl/Artist.cpp
impl/ArtistInfo.cpp
impl/AuthToken.cpp
impl/Cluster.cpp
impl/Db.cpp
+12 -1
View File
@@ -265,7 +265,18 @@ namespace lms::db
RangeResults<ArtistId> Artist::findOrphanIds(Session& session, std::optional<Range> range)
{
session.checkReadTransaction();
auto query{ session.getDboSession()->query<ArtistId>("SELECT DISTINCT a.id FROM artist a WHERE NOT EXISTS(SELECT 1 FROM track t INNER JOIN track_artist_link t_a_l ON t_a_l.artist_id = a.id WHERE t.id = t_a_l.track_id)") };
auto query{ session.getDboSession()->query<ArtistId>(R"(SELECT DISTINCT a.id FROM artist a
WHERE NOT EXISTS (
SELECT 1
FROM track t
INNER JOIN track_artist_link t_a_l
ON t_a_l.artist_id = a.id
WHERE t.id = t_a_l.track_id
)
AND NOT EXISTS (
SELECT 1
FROM artist_info ai
WHERE ai.artist_id = a.id))") };
return utils::execRangeQuery<ArtistId>(query, range);
}
+114
View File
@@ -0,0 +1,114 @@
/*
* Copyright (C) 2025 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/>.
*/
#include "database/ArtistInfo.hpp"
#include <Wt/Dbo/WtSqlTraits.h>
#include "database/Artist.hpp"
#include "database/ArtistInfoId.hpp"
#include "database/Directory.hpp"
#include "database/Session.hpp"
#include "Utils.hpp"
#include "traits/IdTypeTraits.hpp"
#include "traits/PathTraits.hpp"
namespace lms::db
{
ArtistInfo::pointer ArtistInfo::create(Session& session)
{
return session.getDboSession()->add(std::unique_ptr<ArtistInfo>{ new ArtistInfo{} });
}
std::size_t ArtistInfo::getCount(Session& session)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->query<int>("SELECT COUNT(*) FROM artist_info"));
}
ArtistInfo::pointer ArtistInfo::find(Session& session, const std::filesystem::path& p)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<ArtistInfo>>("SELECT a_i from artist_info a_i").where("a_i.absolute_file_path = ?").bind(p));
}
ArtistInfo::pointer ArtistInfo::find(Session& session, ArtistInfoId id)
{
session.checkReadTransaction();
return utils::fetchQuerySingleResult(session.getDboSession()->query<Wt::Dbo::ptr<ArtistInfo>>("SELECT a_i from artist_info a_i").where("a_i.id = ?").bind(id));
}
void ArtistInfo::find(Session& session, ArtistId id, std::optional<Range> range, const std::function<void(const pointer&)>& func)
{
session.checkReadTransaction();
auto query{ session.getDboSession()->query<Wt::Dbo::ptr<ArtistInfo>>("SELECT a_i from artist_info a_i").where("a_i.artist_id = ?").bind(id) };
utils::forEachQueryRangeResult(query, range, [&](const ArtistInfo::pointer& entry) {
func(entry);
});
}
void ArtistInfo::find(Session& session, ArtistId id, const std::function<void(const pointer&)>& func)
{
find(session, id, std::nullopt, std::move(func));
}
void ArtistInfo::find(Session& session, ArtistInfoId& lastRetrievedId, std::size_t count, const std::function<void(const pointer&)>& func)
{
session.checkReadTransaction();
auto query{ session.getDboSession()->query<Wt::Dbo::ptr<ArtistInfo>>("SELECT a_i from artist_info a_i").orderBy("a_i.id").where("a_i.id > ?").bind(lastRetrievedId).limit(static_cast<int>(count)) };
utils::forEachQueryResult(query, [&](const ArtistInfo::pointer& entry) {
func(entry);
lastRetrievedId = entry->getId();
});
}
Artist::pointer ArtistInfo::getArtist() const
{
return _artist;
}
Directory::pointer ArtistInfo::getDirectory() const
{
return _directory;
}
void ArtistInfo::setAbsoluteFilePath(const std::filesystem::path& filePath)
{
assert(filePath.is_absolute());
_absoluteFilePath = filePath;
_fileStem = filePath.stem();
}
void ArtistInfo::setDirectory(ObjectPtr<Directory> directory)
{
_directory = getDboPtr(directory);
}
void ArtistInfo::setArtist(ObjectPtr<Artist> artist)
{
_artist = getDboPtr(artist);
}
} // namespace lms::db
+2
View File
@@ -176,11 +176,13 @@ namespace lms::db
query.leftJoin("image i ON d.id = i.directory_id");
query.leftJoin("track_lyrics l_lrc ON d.id = l_lrc.directory_id");
query.leftJoin("playlist_file pl_f ON d.id = pl_f.directory_id");
query.leftJoin("artist_info a_i ON d.id = a_i.directory_id");
query.where("d_child.id IS NULL");
query.where("t.directory_id IS NULL");
query.where("i.directory_id IS NULL");
query.where("l_lrc.directory_id IS NULL");
query.where("pl_f.directory_id IS NULL");
query.where("a_i.directory_id IS NULL");
return utils::execRangeQuery<DirectoryId>(query, range);
}
+24 -1
View File
@@ -35,7 +35,7 @@ namespace lms::db
{
namespace
{
static constexpr Version LMS_DATABASE_VERSION{ 84 };
static constexpr Version LMS_DATABASE_VERSION{ 85 };
}
VersionInfo::VersionInfo()
@@ -1137,6 +1137,28 @@ FROM tracklist)");
utils::executeCommand(*session.getDboSession(), "UPDATE scan_settings SET scan_version = scan_version + 1");
}
void migrateFromV84(Session& session)
{
// New artist info feature
utils::executeCommand(*session.getDboSession(), R"(CREATE TABLE IF NOT EXISTS "artist_info" (
"id" integer primary key autoincrement,
"version" integer not null,
"absolute_file_path" text not null,
"file_last_write" text,
"type" text not null,
"gender" text not null,
"disambiguation" text not null,
"biography" text not null,
"directory_id" bigint,
"artist_id" bigint,
constraint "fk_artist_info_directory" foreign key ("directory_id") references "directory" ("id") on delete cascade deferrable initially deferred,
constraint "fk_artist_info_artist" foreign key ("artist_id") references "artist" ("id") on delete cascade deferrable initially deferred
))");
// Just increment the scan version of the settings to make the next scan rescan everything
utils::executeCommand(*session.getDboSession(), "UPDATE scan_settings SET scan_version = scan_version + 1");
}
bool doDbMigration(Session& session)
{
constexpr std::string_view outdatedMsg{ "Outdated database, please rebuild it (delete the .db file and restart)" };
@@ -1197,6 +1219,7 @@ FROM tracklist)");
{ 81, migrateFromV81 },
{ 82, migrateFromV82 },
{ 83, migrateFromV83 },
{ 84, migrateFromV84 },
};
bool migrationPerformed{};
+7 -2
View File
@@ -19,10 +19,10 @@
#include "database/Session.hpp"
#include "core/Exception.hpp"
#include "core/ILogger.hpp"
#include "core/ITraceLogger.hpp"
#include "database/Artist.hpp"
#include "database/ArtistInfo.hpp"
#include "database/AuthToken.hpp"
#include "database/Cluster.hpp"
#include "database/Db.hpp"
@@ -100,8 +100,8 @@ namespace lms::db
{
_session.setConnectionPool(_db.getConnectionPool());
_session.mapClass<VersionInfo>("version_info");
_session.mapClass<Artist>("artist");
_session.mapClass<ArtistInfo>("artist_info");
_session.mapClass<AuthToken>("auth_token");
_session.mapClass<Cluster>("cluster");
_session.mapClass<ClusterType>("cluster_type");
@@ -133,6 +133,7 @@ namespace lms::db
_session.mapClass<TrackLyrics>("track_lyrics");
_session.mapClass<UIState>("ui_state");
_session.mapClass<User>("user");
_session.mapClass<VersionInfo>("version_info");
}
WriteTransaction Session::createWriteTransaction()
@@ -198,6 +199,10 @@ namespace lms::db
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS artist_sort_name_nocase_idx ON artist(sort_name COLLATE NOCASE)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS artist_mbid_idx ON artist(mbid)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS artist_info_path_idx ON artist_info(absolute_file_path)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS artist_info_directory_id_idx ON artist_info(directory_id)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS artist_info_artist_id_idx ON artist_info(artist_id)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS auth_token_user_domain_idx ON auth_token(user_id, domain)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS auth_token_domain_expiry_idx ON auth_token(domain, expiry)");
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS auth_token_domain_value_idx ON auth_token(domain, value)");
@@ -0,0 +1,108 @@
/*
* Copyright (C) 2025 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 <optional>
#include <string>
#include <string_view>
#include <Wt/Dbo/Dbo.h>
#include <Wt/WDateTime.h>
#include "database/ArtistId.hpp"
#include "database/ArtistInfoId.hpp"
#include "database/DirectoryId.hpp"
#include "database/Object.hpp"
#include "database/Types.hpp"
namespace lms::db
{
class Artist;
class Directory;
class Session;
class ArtistInfo final : public Object<ArtistInfo, ArtistInfoId>
{
public:
ArtistInfo() = default;
// find
static std::size_t getCount(Session& session);
static pointer find(Session& session, ArtistInfoId id);
static void find(Session& session, ArtistId id, std::optional<Range> range, const std::function<void(const pointer&)>& func);
static void find(Session& session, ArtistId id, const std::function<void(const pointer&)>& func);
static pointer find(Session& session, const std::filesystem::path& path);
static void find(Session& session, ArtistInfoId& lastRetrievedId, std::size_t count, const std::function<void(const pointer&)>& func);
// getters
const std::filesystem::path& getAbsoluteFilePath() const { return _absoluteFilePath; }
const Wt::WDateTime& getLastWriteTime() const { return _fileLastWrite; }
ObjectPtr<Directory> getDirectory() const;
ObjectPtr<Artist> getArtist() const;
DirectoryId getDirectoryId() const { return _directory.id(); }
std::string_view getType() const { return _type; }
std::string_view getGender() const { return _gender; }
std::string_view getDisambiguation() const { return _disambiguation; }
std::string_view getBiography() const { return _biography; }
// setters
void setAbsoluteFilePath(const std::filesystem::path& filePath);
void setLastWriteTime(Wt::WDateTime time) { _fileLastWrite = time; }
void setDirectory(ObjectPtr<Directory> directory);
void setArtist(ObjectPtr<Artist> artist);
void setType(std::string_view type) { _type = type; }
void setGender(std::string_view gender) { _gender = gender; }
void setDisambiguation(std::string_view disambiguation) { _disambiguation = disambiguation; }
void setBiography(std::string_view biography) { _biography = biography; };
template<class Action>
void persist(Action& a)
{
Wt::Dbo::field(a, _absoluteFilePath, "absolute_file_path");
Wt::Dbo::field(a, _fileLastWrite, "file_last_write");
Wt::Dbo::field(a, _type, "type");
Wt::Dbo::field(a, _gender, "gender");
Wt::Dbo::field(a, _disambiguation, "disambiguation");
Wt::Dbo::field(a, _biography, "biography");
Wt::Dbo::belongsTo(a, _directory, "directory", Wt::Dbo::OnDeleteCascade);
Wt::Dbo::belongsTo(a, _artist, "artist", Wt::Dbo::OnDeleteCascade);
}
private:
friend class Session;
static pointer create(Session& session);
// Set when coming from artist info file
std::filesystem::path _absoluteFilePath;
std::string _fileStem;
Wt::WDateTime _fileLastWrite;
std::string _type;
std::string _gender;
std::string _disambiguation;
std::string _biography;
Wt::Dbo::ptr<Directory> _directory;
Wt::Dbo::ptr<Artist> _artist;
};
} // namespace lms::db
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2021 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 "database/IdType.hpp"
LMS_DECLARE_IDTYPE(ArtistInfoId)
+112
View File
@@ -0,0 +1,112 @@
/*
* Copyright (C) 2024 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/>.
*/
#include "Common.hpp"
#include "database/Artist.hpp"
#include "database/ArtistInfo.hpp"
#include "database/Directory.hpp"
namespace lms::db::tests
{
using ScopedArtistInfo = ScopedEntity<db::ArtistInfo>;
using ScopedDirectory = ScopedEntity<db::Directory>;
TEST_F(DatabaseFixture, ArtistInfo)
{
{
auto transaction{ session.createReadTransaction() };
EXPECT_EQ(ArtistInfo::getCount(session), 0);
}
ScopedArtistInfo artistInfo{ session };
{
auto transaction{ session.createReadTransaction() };
EXPECT_EQ(ArtistInfo::getCount(session), 1);
}
{
auto transaction{ session.createReadTransaction() };
const ArtistInfo::pointer dbArtistInfo{ ArtistInfo::find(session, artistInfo.getId()) };
EXPECT_EQ(dbArtistInfo->getAbsoluteFilePath(), "");
EXPECT_EQ(dbArtistInfo->getLastWriteTime(), Wt::WDateTime{});
EXPECT_EQ(dbArtistInfo->getArtist(), Artist::pointer{});
EXPECT_EQ(dbArtistInfo->getDirectory(), Directory::pointer{});
EXPECT_EQ(dbArtistInfo->getType(), "");
EXPECT_EQ(dbArtistInfo->getGender(), "");
EXPECT_EQ(dbArtistInfo->getDisambiguation(), "");
EXPECT_EQ(dbArtistInfo->getBiography(), "");
}
ScopedArtist artist{ session, "MyArtist" };
ScopedDirectory directory{ session, "/tmp" };
const Wt::WDateTime dateTime{ Wt::WDate{ 2024, 30, 1 }, Wt::WTime{ 12, 58, 29 } };
// Now change some values
{
auto transaction{ session.createWriteTransaction() };
ArtistInfo::pointer dbArtistInfo{ ArtistInfo::find(session, artistInfo.getId()) };
dbArtistInfo.modify()->setAbsoluteFilePath("/tmp/artist.nfo");
dbArtistInfo.modify()->setLastWriteTime(dateTime);
dbArtistInfo.modify()->setArtist(artist.get());
dbArtistInfo.modify()->setDirectory(directory.get());
dbArtistInfo.modify()->setType("MyType");
dbArtistInfo.modify()->setGender("MyGender");
dbArtistInfo.modify()->setDisambiguation("MyDisambiguation");
dbArtistInfo.modify()->setBiography("MyBiography");
}
// Check values are reflected
{
auto transaction{ session.createReadTransaction() };
const ArtistInfo::pointer dbArtistInfo{ ArtistInfo::find(session, artistInfo.getId()) };
EXPECT_EQ(dbArtistInfo->getAbsoluteFilePath(), "/tmp/artist.nfo");
EXPECT_EQ(dbArtistInfo->getLastWriteTime(), dateTime);
EXPECT_EQ(dbArtistInfo->getArtist(), artist.get());
EXPECT_EQ(dbArtistInfo->getDirectory(), directory.get());
EXPECT_EQ(dbArtistInfo->getDirectoryId(), directory.getId());
EXPECT_EQ(dbArtistInfo->getType(), "MyType");
EXPECT_EQ(dbArtistInfo->getGender(), "MyGender");
EXPECT_EQ(dbArtistInfo->getDisambiguation(), "MyDisambiguation");
EXPECT_EQ(dbArtistInfo->getBiography(), "MyBiography");
}
{
auto transaction{ session.createReadTransaction() };
bool visited{};
ArtistInfo::find(session, artist.getId(), [&](const ArtistInfo::pointer& dbArtistInfo) {
ASSERT_NE(dbArtistInfo, ArtistInfo::pointer{});
EXPECT_EQ(dbArtistInfo->getId(), artistInfo.getId());
visited = true;
});
EXPECT_TRUE(visited);
}
}
} // namespace lms::db::tests
+2 -1
View File
@@ -1,7 +1,8 @@
add_executable(test-database
AuthToken.cpp
Artist.cpp
ArtistInfo.cpp
AuthToken.cpp
Cluster.cpp
Common.cpp
DatabaseTest.cpp
+3
View File
@@ -20,6 +20,8 @@
#include "Common.hpp"
#include "core/String.hpp"
#include "database/Artist.hpp"
#include "database/ArtistInfo.hpp"
#include "database/AuthToken.hpp"
#include "database/Db.hpp"
#include "database/Directory.hpp"
@@ -341,6 +343,7 @@ VALUES
auto transaction{ session.createReadTransaction() };
EXPECT_FALSE(Artist::find(session, ArtistId{}));
EXPECT_FALSE(ArtistInfo::find(session, ArtistInfoId{}));
EXPECT_FALSE(AuthToken::find(session, AuthTokenId{}));
EXPECT_FALSE(Country::find(session, CountryId{}));
EXPECT_FALSE(Cluster::find(session, ClusterId{}));