Extracted genre, mood, language and grouping from generic clusters
This commit is contained in:
@@ -35,7 +35,7 @@ namespace lms::db
|
||||
{
|
||||
namespace
|
||||
{
|
||||
static constexpr Version LMS_DATABASE_VERSION{ 107 };
|
||||
static constexpr Version LMS_DATABASE_VERSION{ 108 };
|
||||
}
|
||||
|
||||
VersionInfo::VersionInfo()
|
||||
@@ -1774,6 +1774,127 @@ FROM track)");
|
||||
utils::executeCommand(*session.getDboSession(), R"(ALTER TABLE track RENAME COLUMN recording_mbid_new TO recording_mbid)");
|
||||
}
|
||||
|
||||
void migrateFromV107(Session& session)
|
||||
{
|
||||
// Extract Genre, Mood, Language and Grouping from Cluster (keep it for user tags)
|
||||
|
||||
utils::executeCommand(*session.getDboSession(), R"(CREATE TABLE IF NOT EXISTS "genre" (
|
||||
"id" integer primary key autoincrement,
|
||||
"version" integer not null,
|
||||
"name" text not null,
|
||||
"track_count" integer not null default 0,
|
||||
"release_count" integer not null default 0
|
||||
))");
|
||||
utils::executeCommand(*session.getDboSession(), R"(CREATE TABLE IF NOT EXISTS "track_genre" (
|
||||
"track_id" bigint,
|
||||
"genre_id" bigint,
|
||||
primary key ("track_id", "genre_id"),
|
||||
constraint "fk_track_genre_track" foreign key ("track_id") references "track" ("id") on delete cascade deferrable initially deferred,
|
||||
constraint "fk_track_genre_genre" foreign key ("genre_id") references "genre" ("id") on delete cascade deferrable initially deferred
|
||||
))");
|
||||
|
||||
utils::executeCommand(*session.getDboSession(), R"(INSERT INTO genre (version, name, track_count, release_count)
|
||||
SELECT 0, c.name, c.track_count, c.release_count FROM cluster c
|
||||
INNER JOIN cluster_type ct ON ct.id = c.cluster_type_id
|
||||
WHERE ct.name = 'GENRE')");
|
||||
utils::executeCommand(*session.getDboSession(), R"(INSERT INTO track_genre (track_id, genre_id)
|
||||
SELECT tc.track_id, g.id FROM track_cluster tc
|
||||
INNER JOIN cluster c ON c.id = tc.cluster_id
|
||||
INNER JOIN cluster_type ct ON ct.id = c.cluster_type_id
|
||||
INNER JOIN genre g ON g.name = c.name
|
||||
WHERE ct.name = 'GENRE')");
|
||||
|
||||
utils::executeCommand(*session.getDboSession(), R"(CREATE INDEX "track_genre_genre" ON "track_genre" ("genre_id"))");
|
||||
utils::executeCommand(*session.getDboSession(), R"(CREATE INDEX "track_genre_track" ON "track_genre" ("track_id"))");
|
||||
|
||||
utils::executeCommand(*session.getDboSession(), R"(CREATE TABLE IF NOT EXISTS "mood" (
|
||||
"id" integer primary key autoincrement,
|
||||
"version" integer not null,
|
||||
"name" text not null
|
||||
))");
|
||||
utils::executeCommand(*session.getDboSession(), R"(CREATE TABLE IF NOT EXISTS "track_mood" (
|
||||
"track_id" bigint,
|
||||
"mood_id" bigint,
|
||||
primary key ("track_id", "mood_id"),
|
||||
constraint "fk_track_mood_track" foreign key ("track_id") references "track" ("id") on delete cascade deferrable initially deferred,
|
||||
constraint "fk_track_mood_mood" foreign key ("mood_id") references "mood" ("id") on delete cascade deferrable initially deferred
|
||||
))");
|
||||
|
||||
utils::executeCommand(*session.getDboSession(), R"(INSERT INTO mood (version, name)
|
||||
SELECT 0, c.name FROM cluster c
|
||||
INNER JOIN cluster_type ct ON ct.id = c.cluster_type_id
|
||||
WHERE ct.name = 'MOOD')");
|
||||
utils::executeCommand(*session.getDboSession(), R"(INSERT INTO track_mood (track_id, mood_id)
|
||||
SELECT tc.track_id, m.id FROM track_cluster tc
|
||||
INNER JOIN cluster c ON c.id = tc.cluster_id
|
||||
INNER JOIN cluster_type ct ON ct.id = c.cluster_type_id
|
||||
INNER JOIN mood m ON m.name = c.name
|
||||
WHERE ct.name = 'MOOD')");
|
||||
|
||||
utils::executeCommand(*session.getDboSession(), R"(CREATE INDEX "track_mood_mood" ON "track_mood" ("mood_id"))");
|
||||
utils::executeCommand(*session.getDboSession(), R"(CREATE INDEX "track_mood_track" ON "track_mood" ("track_id"))");
|
||||
|
||||
utils::executeCommand(*session.getDboSession(), R"(CREATE TABLE IF NOT EXISTS "language" (
|
||||
"id" integer primary key autoincrement,
|
||||
"version" integer not null,
|
||||
"name" text not null
|
||||
))");
|
||||
utils::executeCommand(*session.getDboSession(), R"(CREATE TABLE IF NOT EXISTS "track_language" (
|
||||
"track_id" bigint,
|
||||
"language_id" bigint,
|
||||
primary key ("track_id", "language_id"),
|
||||
constraint "fk_track_language_track" foreign key ("track_id") references "track" ("id") on delete cascade deferrable initially deferred,
|
||||
constraint "fk_track_language_language" foreign key ("language_id") references "language" ("id") on delete cascade deferrable initially deferred
|
||||
))");
|
||||
|
||||
utils::executeCommand(*session.getDboSession(), R"(INSERT INTO language (version, name)
|
||||
SELECT 0, c.name FROM cluster c
|
||||
INNER JOIN cluster_type ct ON ct.id = c.cluster_type_id
|
||||
WHERE ct.name = 'LANGUAGE')");
|
||||
utils::executeCommand(*session.getDboSession(), R"(INSERT INTO track_language (track_id, language_id)
|
||||
SELECT tc.track_id, l.id FROM track_cluster tc
|
||||
INNER JOIN cluster c ON c.id = tc.cluster_id
|
||||
INNER JOIN cluster_type ct ON ct.id = c.cluster_type_id
|
||||
INNER JOIN language l ON l.name = c.name
|
||||
WHERE ct.name = 'LANGUAGE')");
|
||||
|
||||
utils::executeCommand(*session.getDboSession(), R"(CREATE INDEX "track_language_language" ON "track_language" ("language_id"))");
|
||||
utils::executeCommand(*session.getDboSession(), R"(CREATE INDEX "track_language_track" ON "track_language" ("track_id"))");
|
||||
|
||||
utils::executeCommand(*session.getDboSession(), R"(CREATE TABLE IF NOT EXISTS "grouping" (
|
||||
"id" integer primary key autoincrement,
|
||||
"version" integer not null,
|
||||
"name" text not null
|
||||
))");
|
||||
utils::executeCommand(*session.getDboSession(), R"(CREATE TABLE IF NOT EXISTS "track_grouping" (
|
||||
"track_id" bigint,
|
||||
"grouping_id" bigint,
|
||||
primary key ("track_id", "grouping_id"),
|
||||
constraint "fk_track_grouping_track" foreign key ("track_id") references "track" ("id") on delete cascade deferrable initially deferred,
|
||||
constraint "fk_track_grouping_grouping" foreign key ("grouping_id") references "grouping" ("id") on delete cascade deferrable initially deferred
|
||||
))");
|
||||
|
||||
utils::executeCommand(*session.getDboSession(), R"(INSERT INTO grouping (version, name)
|
||||
SELECT 0, c.name FROM cluster c
|
||||
INNER JOIN cluster_type ct ON ct.id = c.cluster_type_id
|
||||
WHERE ct.name = 'GROUPING')");
|
||||
utils::executeCommand(*session.getDboSession(), R"(INSERT INTO track_grouping (track_id, grouping_id)
|
||||
SELECT tc.track_id, g.id FROM track_cluster tc
|
||||
INNER JOIN cluster c ON c.id = tc.cluster_id
|
||||
INNER JOIN cluster_type ct ON ct.id = c.cluster_type_id
|
||||
INNER JOIN grouping g ON g.name = c.name
|
||||
WHERE ct.name = 'GROUPING')");
|
||||
|
||||
utils::executeCommand(*session.getDboSession(), R"(CREATE INDEX "track_grouping_grouping" ON "track_grouping" ("grouping_id"))");
|
||||
utils::executeCommand(*session.getDboSession(), R"(CREATE INDEX "track_grouping_track" ON "track_grouping" ("track_id"))");
|
||||
|
||||
utils::executeCommand(*session.getDboSession(), R"(DELETE FROM track_cluster WHERE cluster_id IN (SELECT c.id FROM cluster c INNER JOIN cluster_type ct ON ct.id = c.cluster_type_id WHERE ct.name IN ('GENRE', 'MOOD', 'LANGUAGE', 'GROUPING')))");
|
||||
utils::executeCommand(*session.getDboSession(), R"(ALTER TABLE cluster DROP COLUMN track_count)");
|
||||
utils::executeCommand(*session.getDboSession(), R"(ALTER TABLE cluster DROP COLUMN release_count)");
|
||||
utils::executeCommand(*session.getDboSession(), R"(DELETE FROM cluster WHERE cluster_type_id IN (SELECT id FROM cluster_type WHERE name IN ('GENRE', 'MOOD', 'LANGUAGE', 'GROUPING')))");
|
||||
utils::executeCommand(*session.getDboSession(), R"(DELETE FROM cluster_type WHERE name IN ('GENRE', 'MOOD', 'LANGUAGE', 'GROUPING'))");
|
||||
}
|
||||
|
||||
bool doDbMigration(Session& session)
|
||||
{
|
||||
constexpr std::string_view outdatedMsg{ "Outdated database, please rebuild it (delete the .db file and restart)" };
|
||||
@@ -1857,6 +1978,7 @@ FROM track)");
|
||||
{ 104, migrateFromV104 },
|
||||
{ 105, migrateFromV105 },
|
||||
{ 106, migrateFromV106 },
|
||||
{ 107, migrateFromV107 },
|
||||
};
|
||||
|
||||
bool migrationPerformed{};
|
||||
|
||||
@@ -30,10 +30,14 @@
|
||||
#include "database/objects/AuthToken.hpp"
|
||||
#include "database/objects/Cluster.hpp"
|
||||
#include "database/objects/Directory.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/Image.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/Listen.hpp"
|
||||
#include "database/objects/MediaLibrary.hpp"
|
||||
#include "database/objects/Medium.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/PlayListFile.hpp"
|
||||
#include "database/objects/PlayQueue.hpp"
|
||||
#include "database/objects/Podcast.hpp"
|
||||
@@ -81,6 +85,10 @@ namespace lms::db
|
||||
_session.mapClass<AuthToken>("auth_token");
|
||||
_session.mapClass<Cluster>("cluster");
|
||||
_session.mapClass<ClusterType>("cluster_type");
|
||||
_session.mapClass<Genre>("genre");
|
||||
_session.mapClass<Grouping>("grouping");
|
||||
_session.mapClass<Language>("language");
|
||||
_session.mapClass<Mood>("mood");
|
||||
_session.mapClass<Country>("country");
|
||||
_session.mapClass<Directory>("directory");
|
||||
_session.mapClass<Image>("image");
|
||||
@@ -210,6 +218,11 @@ namespace lms::db
|
||||
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS cluster_cluster_type_idx ON cluster(cluster_type_id)");
|
||||
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS cluster_type_name_idx ON cluster_type(name)");
|
||||
|
||||
utils::executeCommand(_session, "CREATE UNIQUE INDEX IF NOT EXISTS genre_name_idx ON genre(name)");
|
||||
utils::executeCommand(_session, "CREATE UNIQUE INDEX IF NOT EXISTS grouping_name_idx ON grouping(name)");
|
||||
utils::executeCommand(_session, "CREATE UNIQUE INDEX IF NOT EXISTS language_name_idx ON language(name)");
|
||||
utils::executeCommand(_session, "CREATE UNIQUE INDEX IF NOT EXISTS mood_name_idx ON mood(name)");
|
||||
|
||||
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS country_id_idx ON country(id)");
|
||||
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS country_name_idx ON country(name COLLATE NOCASE)");
|
||||
|
||||
|
||||
@@ -26,6 +26,10 @@
|
||||
#include "database/objects/Artwork.hpp"
|
||||
#include "database/objects/Cluster.hpp"
|
||||
#include "database/objects/Directory.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/Release.hpp"
|
||||
#include "database/objects/Track.hpp"
|
||||
#include "database/objects/User.hpp"
|
||||
@@ -55,6 +59,10 @@ namespace lms::db
|
||||
|| params.trackArtistLinkType.has_value()
|
||||
|| params.track.isValid()
|
||||
|| params.filters.clusters.size() == 1
|
||||
|| params.filters.genre.isValid()
|
||||
|| params.filters.grouping.isValid()
|
||||
|| params.filters.language.isValid()
|
||||
|| params.filters.mood.isValid()
|
||||
|| params.filters.codec.has_value()
|
||||
|| params.filters.label.isValid()
|
||||
|| params.filters.releaseType.isValid())
|
||||
@@ -162,6 +170,34 @@ namespace lms::db
|
||||
query.where(oss.str());
|
||||
}
|
||||
|
||||
if (params.filters.genre.isValid())
|
||||
{
|
||||
query.join("track_genre t_g ON t_g.track_id = t_a_l.track_id")
|
||||
.where("t_g.genre_id = ?")
|
||||
.bind(params.filters.genre);
|
||||
}
|
||||
|
||||
if (params.filters.grouping.isValid())
|
||||
{
|
||||
query.join("track_grouping t_gr ON t_gr.track_id = t_a_l.track_id")
|
||||
.where("t_gr.grouping_id = ?")
|
||||
.bind(params.filters.grouping);
|
||||
}
|
||||
|
||||
if (params.filters.language.isValid())
|
||||
{
|
||||
query.join("track_language t_l ON t_l.track_id = t_a_l.track_id")
|
||||
.where("t_l.language_id = ?")
|
||||
.bind(params.filters.language);
|
||||
}
|
||||
|
||||
if (params.filters.mood.isValid())
|
||||
{
|
||||
query.join("track_mood t_m ON t_m.track_id = t_a_l.track_id")
|
||||
.where("t_m.mood_id = ?")
|
||||
.bind(params.filters.mood);
|
||||
}
|
||||
|
||||
if (params.track.isValid())
|
||||
query.where("t_a_l.track_id = ?").bind(params.track);
|
||||
|
||||
|
||||
@@ -25,8 +25,12 @@
|
||||
#include "database/objects/Artist.hpp"
|
||||
#include "database/objects/Artwork.hpp"
|
||||
#include "database/objects/Directory.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/MediaLibrary.hpp"
|
||||
#include "database/objects/Medium.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/Release.hpp"
|
||||
#include "database/objects/Track.hpp"
|
||||
#include "database/objects/TrackArtistLink.hpp"
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
/*
|
||||
* 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/objects/Genre.hpp"
|
||||
|
||||
#include <Wt/Dbo/Impl.h>
|
||||
|
||||
#include "core/ILogger.hpp"
|
||||
|
||||
#include "database/Session.hpp"
|
||||
#include "database/objects/Artist.hpp"
|
||||
#include "database/objects/Artwork.hpp"
|
||||
#include "database/objects/Cluster.hpp"
|
||||
#include "database/objects/Directory.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/MediaLibrary.hpp"
|
||||
#include "database/objects/Medium.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/Release.hpp"
|
||||
#include "database/objects/Track.hpp"
|
||||
#include "database/objects/TrackArtistLink.hpp"
|
||||
#include "database/objects/TrackEmbeddedImage.hpp"
|
||||
#include "database/objects/TrackEmbeddedImageLink.hpp"
|
||||
#include "database/objects/TrackLyrics.hpp"
|
||||
|
||||
#include "Utils.hpp"
|
||||
#include "traits/IdTypeTraits.hpp"
|
||||
#include "traits/StringViewTraits.hpp"
|
||||
|
||||
DBO_INSTANTIATE_TEMPLATES(lms::db::Genre)
|
||||
|
||||
namespace lms::db
|
||||
{
|
||||
namespace
|
||||
{
|
||||
template<typename ResultType>
|
||||
Wt::Dbo::Query<ResultType> createQuery(Session& session, std::string_view itemToSelect, const Genre::FindParameters& params)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
|
||||
auto query{ session.getDboSession()->query<ResultType>("SELECT " + std::string{ itemToSelect } + " FROM genre g") };
|
||||
|
||||
if (params.artist.isValid() || params.track.isValid() || params.release.isValid()
|
||||
|| params.sortMethod == GenreSortMethod::TrackCountDesc)
|
||||
query.join("track_genre t_g ON t_g.genre_id = g.id");
|
||||
|
||||
if (params.track.isValid())
|
||||
query.where("t_g.track_id = ?").bind(params.track);
|
||||
|
||||
if (params.release.isValid() || params.artist.isValid())
|
||||
query.join("track t ON t.id = t_g.track_id");
|
||||
|
||||
if (params.release.isValid())
|
||||
query.where("t.release_id = ?").bind(params.release);
|
||||
|
||||
if (params.artist.isValid())
|
||||
{
|
||||
query.join("track_artist_link t_a_l ON t_a_l.track_id = t.id");
|
||||
query.where("t_a_l.artist_id = ?").bind(params.artist);
|
||||
}
|
||||
|
||||
switch (params.sortMethod)
|
||||
{
|
||||
case GenreSortMethod::None:
|
||||
break;
|
||||
case GenreSortMethod::Name:
|
||||
query.orderBy("g.name COLLATE NOCASE");
|
||||
break;
|
||||
case GenreSortMethod::TrackCountDesc:
|
||||
query.orderBy("COUNT(t_g.track_id) DESC");
|
||||
break;
|
||||
}
|
||||
|
||||
// track_genre has a UNIQUE constraint on (track_id, genre_id), so no duplicates when filtering by track
|
||||
if (!params.track.isValid())
|
||||
query.groupBy("g.id");
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
template<typename ResultType>
|
||||
Wt::Dbo::Query<ResultType> createQuery(Session& session, const Genre::FindParameters& params)
|
||||
{
|
||||
std::string_view itemToSelect;
|
||||
|
||||
if constexpr (std::is_same_v<ResultType, GenreId>)
|
||||
itemToSelect = "g.id";
|
||||
else if constexpr (std::is_same_v<ResultType, Wt::Dbo::ptr<Genre>>)
|
||||
itemToSelect = "g";
|
||||
else
|
||||
static_assert("Unhandled type");
|
||||
|
||||
return createQuery<ResultType>(session, itemToSelect, params);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Genre::Genre(std::string_view name)
|
||||
: _name{ name.substr(0, maxNameLength) }
|
||||
{
|
||||
LMS_LOG_IF(DB, WARNING, name.size() > maxNameLength, "Genre name too long, truncated to '" << _name << "'");
|
||||
}
|
||||
|
||||
Genre::pointer Genre::create(Session& session, std::string_view name)
|
||||
{
|
||||
return session.getDboSession()->add(std::unique_ptr<Genre>{ new Genre{ name } });
|
||||
}
|
||||
|
||||
std::size_t Genre::getCount(Session& session)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
return utils::fetchQuerySingleResult(session.getDboSession()->query<int>("SELECT COUNT(*) FROM genre"));
|
||||
}
|
||||
|
||||
RangeResults<GenreId> Genre::findIds(Session& session, const FindParameters& params)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
auto query{ createQuery<GenreId>(session, params) };
|
||||
return utils::execRangeQuery<GenreId>(query, params.range);
|
||||
}
|
||||
|
||||
RangeResults<Genre::pointer> Genre::find(Session& session, const FindParameters& params)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
auto query{ createQuery<Wt::Dbo::ptr<Genre>>(session, params) };
|
||||
return utils::execRangeQuery<Genre::pointer>(query, params.range);
|
||||
}
|
||||
|
||||
void Genre::find(Session& session, const FindParameters& params, std::function<void(const pointer&)> func)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
auto query{ createQuery<Wt::Dbo::ptr<Genre>>(session, params) };
|
||||
utils::forEachQueryRangeResult(query, params.range, func);
|
||||
}
|
||||
|
||||
Genre::pointer Genre::find(Session& session, GenreId id)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
return utils::fetchQuerySingleResult(session.getDboSession()->find<Genre>().where("id = ?").bind(id));
|
||||
}
|
||||
|
||||
Genre::pointer Genre::find(Session& session, std::string_view name)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
|
||||
if (name.size() > maxNameLength)
|
||||
name = name.substr(0, maxNameLength);
|
||||
|
||||
return utils::fetchQuerySingleResult(session.getDboSession()->find<Genre>().where("name = ?").bind(name));
|
||||
}
|
||||
|
||||
RangeResults<GenreId> Genre::findOrphanIds(Session& session, std::optional<Range> range)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
auto query{ session.getDboSession()->query<GenreId>("SELECT g.id FROM genre g WHERE NOT EXISTS (SELECT 1 FROM track_genre t_g WHERE t_g.genre_id = g.id)") };
|
||||
return utils::execRangeQuery<GenreId>(query, range);
|
||||
}
|
||||
|
||||
std::size_t Genre::computeTrackCount(Session& session, GenreId id)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
return utils::fetchQuerySingleResult(session.getDboSession()->query<int>("SELECT COUNT(t.id) FROM track t INNER JOIN track_genre t_g ON t_g.track_id = t.id").where("t_g.genre_id = ?").bind(id));
|
||||
}
|
||||
|
||||
std::size_t Genre::computeReleaseCount(Session& session, GenreId id)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
return utils::fetchQuerySingleResult(session.getDboSession()->query<int>("SELECT COUNT(DISTINCT t.release_id) FROM track t INNER JOIN track_genre t_g ON t_g.track_id = t.id").where("t_g.genre_id = ?").bind(id));
|
||||
}
|
||||
|
||||
} // namespace lms::db
|
||||
@@ -0,0 +1,175 @@
|
||||
/*
|
||||
* 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/objects/Grouping.hpp"
|
||||
|
||||
#include <Wt/Dbo/Impl.h>
|
||||
|
||||
#include "core/ILogger.hpp"
|
||||
|
||||
#include "database/Session.hpp"
|
||||
#include "database/objects/Artist.hpp"
|
||||
#include "database/objects/Artwork.hpp"
|
||||
#include "database/objects/Cluster.hpp"
|
||||
#include "database/objects/Directory.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/MediaLibrary.hpp"
|
||||
#include "database/objects/Medium.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/Release.hpp"
|
||||
#include "database/objects/Track.hpp"
|
||||
#include "database/objects/TrackArtistLink.hpp"
|
||||
#include "database/objects/TrackEmbeddedImage.hpp"
|
||||
#include "database/objects/TrackEmbeddedImageLink.hpp"
|
||||
#include "database/objects/TrackLyrics.hpp"
|
||||
|
||||
#include "Utils.hpp"
|
||||
#include "traits/IdTypeTraits.hpp"
|
||||
#include "traits/StringViewTraits.hpp"
|
||||
|
||||
DBO_INSTANTIATE_TEMPLATES(lms::db::Grouping)
|
||||
|
||||
namespace lms::db
|
||||
{
|
||||
namespace
|
||||
{
|
||||
template<typename ResultType>
|
||||
Wt::Dbo::Query<ResultType> createQuery(Session& session, std::string_view itemToSelect, const Grouping::FindParameters& params)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
|
||||
auto query{ session.getDboSession()->query<ResultType>("SELECT " + std::string{ itemToSelect } + " FROM grouping g") };
|
||||
|
||||
if (params.artist.isValid() || params.track.isValid() || params.release.isValid()
|
||||
|| params.sortMethod == GroupingSortMethod::TrackCountDesc)
|
||||
query.join("track_grouping t_gr ON t_gr.grouping_id = g.id");
|
||||
|
||||
if (params.track.isValid())
|
||||
query.where("t_gr.track_id = ?").bind(params.track);
|
||||
|
||||
if (params.release.isValid() || params.artist.isValid())
|
||||
query.join("track t ON t.id = t_gr.track_id");
|
||||
|
||||
if (params.release.isValid())
|
||||
query.where("t.release_id = ?").bind(params.release);
|
||||
|
||||
if (params.artist.isValid())
|
||||
{
|
||||
query.join("track_artist_link t_a_l ON t_a_l.track_id = t.id");
|
||||
query.where("t_a_l.artist_id = ?").bind(params.artist);
|
||||
}
|
||||
|
||||
switch (params.sortMethod)
|
||||
{
|
||||
case GroupingSortMethod::None:
|
||||
break;
|
||||
case GroupingSortMethod::Name:
|
||||
query.orderBy("g.name COLLATE NOCASE");
|
||||
break;
|
||||
case GroupingSortMethod::TrackCountDesc:
|
||||
query.orderBy("COUNT(t_gr.track_id) DESC");
|
||||
break;
|
||||
}
|
||||
|
||||
// track_grouping has a UNIQUE constraint on (track_id, grouping_id), so no duplicates when filtering by track
|
||||
if (!params.track.isValid())
|
||||
query.groupBy("g.id");
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
template<typename ResultType>
|
||||
Wt::Dbo::Query<ResultType> createQuery(Session& session, const Grouping::FindParameters& params)
|
||||
{
|
||||
std::string_view itemToSelect;
|
||||
|
||||
if constexpr (std::is_same_v<ResultType, GroupingId>)
|
||||
itemToSelect = "g.id";
|
||||
else if constexpr (std::is_same_v<ResultType, Wt::Dbo::ptr<Grouping>>)
|
||||
itemToSelect = "g";
|
||||
else
|
||||
static_assert("Unhandled type");
|
||||
|
||||
return createQuery<ResultType>(session, itemToSelect, params);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Grouping::Grouping(std::string_view name)
|
||||
: _name{ name.substr(0, maxNameLength) }
|
||||
{
|
||||
LMS_LOG_IF(DB, WARNING, name.size() > maxNameLength, "Grouping name too long, truncated to '" << _name << "'");
|
||||
}
|
||||
|
||||
Grouping::pointer Grouping::create(Session& session, std::string_view name)
|
||||
{
|
||||
return session.getDboSession()->add(std::unique_ptr<Grouping>{ new Grouping{ name } });
|
||||
}
|
||||
|
||||
std::size_t Grouping::getCount(Session& session)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
return utils::fetchQuerySingleResult(session.getDboSession()->query<int>("SELECT COUNT(*) FROM grouping"));
|
||||
}
|
||||
|
||||
RangeResults<GroupingId> Grouping::findIds(Session& session, const FindParameters& params)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
auto query{ createQuery<GroupingId>(session, params) };
|
||||
return utils::execRangeQuery<GroupingId>(query, params.range);
|
||||
}
|
||||
|
||||
RangeResults<Grouping::pointer> Grouping::find(Session& session, const FindParameters& params)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
auto query{ createQuery<Wt::Dbo::ptr<Grouping>>(session, params) };
|
||||
return utils::execRangeQuery<Grouping::pointer>(query, params.range);
|
||||
}
|
||||
|
||||
void Grouping::find(Session& session, const FindParameters& params, std::function<void(const pointer&)> func)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
auto query{ createQuery<Wt::Dbo::ptr<Grouping>>(session, params) };
|
||||
utils::forEachQueryRangeResult(query, params.range, func);
|
||||
}
|
||||
|
||||
Grouping::pointer Grouping::find(Session& session, GroupingId id)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
return utils::fetchQuerySingleResult(session.getDboSession()->find<Grouping>().where("id = ?").bind(id));
|
||||
}
|
||||
|
||||
Grouping::pointer Grouping::find(Session& session, std::string_view name)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
|
||||
if (name.size() > maxNameLength)
|
||||
name = name.substr(0, maxNameLength);
|
||||
|
||||
return utils::fetchQuerySingleResult(session.getDboSession()->find<Grouping>().where("name = ?").bind(name));
|
||||
}
|
||||
|
||||
RangeResults<GroupingId> Grouping::findOrphanIds(Session& session, std::optional<Range> range)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
auto query{ session.getDboSession()->query<GroupingId>("SELECT g.id FROM grouping g WHERE NOT EXISTS (SELECT 1 FROM track_grouping t_gr WHERE t_gr.grouping_id = g.id)") };
|
||||
return utils::execRangeQuery<GroupingId>(query, range);
|
||||
}
|
||||
|
||||
} // namespace lms::db
|
||||
@@ -0,0 +1,175 @@
|
||||
/*
|
||||
* 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/objects/Language.hpp"
|
||||
|
||||
#include <Wt/Dbo/Impl.h>
|
||||
|
||||
#include "core/ILogger.hpp"
|
||||
|
||||
#include "database/Session.hpp"
|
||||
#include "database/objects/Artist.hpp"
|
||||
#include "database/objects/Artwork.hpp"
|
||||
#include "database/objects/Cluster.hpp"
|
||||
#include "database/objects/Directory.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/MediaLibrary.hpp"
|
||||
#include "database/objects/Medium.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/Release.hpp"
|
||||
#include "database/objects/Track.hpp"
|
||||
#include "database/objects/TrackArtistLink.hpp"
|
||||
#include "database/objects/TrackEmbeddedImage.hpp"
|
||||
#include "database/objects/TrackEmbeddedImageLink.hpp"
|
||||
#include "database/objects/TrackLyrics.hpp"
|
||||
|
||||
#include "Utils.hpp"
|
||||
#include "traits/IdTypeTraits.hpp"
|
||||
#include "traits/StringViewTraits.hpp"
|
||||
|
||||
DBO_INSTANTIATE_TEMPLATES(lms::db::Language)
|
||||
|
||||
namespace lms::db
|
||||
{
|
||||
namespace
|
||||
{
|
||||
template<typename ResultType>
|
||||
Wt::Dbo::Query<ResultType> createQuery(Session& session, std::string_view itemToSelect, const Language::FindParameters& params)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
|
||||
auto query{ session.getDboSession()->query<ResultType>("SELECT " + std::string{ itemToSelect } + " FROM language l") };
|
||||
|
||||
if (params.artist.isValid() || params.track.isValid() || params.release.isValid()
|
||||
|| params.sortMethod == LanguageSortMethod::TrackCountDesc)
|
||||
query.join("track_language t_l ON t_l.language_id = l.id");
|
||||
|
||||
if (params.track.isValid())
|
||||
query.where("t_l.track_id = ?").bind(params.track);
|
||||
|
||||
if (params.release.isValid() || params.artist.isValid())
|
||||
query.join("track t ON t.id = t_l.track_id");
|
||||
|
||||
if (params.release.isValid())
|
||||
query.where("t.release_id = ?").bind(params.release);
|
||||
|
||||
if (params.artist.isValid())
|
||||
{
|
||||
query.join("track_artist_link t_a_l ON t_a_l.track_id = t.id");
|
||||
query.where("t_a_l.artist_id = ?").bind(params.artist);
|
||||
}
|
||||
|
||||
switch (params.sortMethod)
|
||||
{
|
||||
case LanguageSortMethod::None:
|
||||
break;
|
||||
case LanguageSortMethod::Name:
|
||||
query.orderBy("l.name COLLATE NOCASE");
|
||||
break;
|
||||
case LanguageSortMethod::TrackCountDesc:
|
||||
query.orderBy("COUNT(t_l.track_id) DESC");
|
||||
break;
|
||||
}
|
||||
|
||||
// track_language has a UNIQUE constraint on (track_id, language_id), so no duplicates when filtering by track
|
||||
if (!params.track.isValid())
|
||||
query.groupBy("l.id");
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
template<typename ResultType>
|
||||
Wt::Dbo::Query<ResultType> createQuery(Session& session, const Language::FindParameters& params)
|
||||
{
|
||||
std::string_view itemToSelect;
|
||||
|
||||
if constexpr (std::is_same_v<ResultType, LanguageId>)
|
||||
itemToSelect = "l.id";
|
||||
else if constexpr (std::is_same_v<ResultType, Wt::Dbo::ptr<Language>>)
|
||||
itemToSelect = "l";
|
||||
else
|
||||
static_assert("Unhandled type");
|
||||
|
||||
return createQuery<ResultType>(session, itemToSelect, params);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Language::Language(std::string_view name)
|
||||
: _name{ name.substr(0, maxNameLength) }
|
||||
{
|
||||
LMS_LOG_IF(DB, WARNING, name.size() > maxNameLength, "Language name too long, truncated to '" << _name << "'");
|
||||
}
|
||||
|
||||
Language::pointer Language::create(Session& session, std::string_view name)
|
||||
{
|
||||
return session.getDboSession()->add(std::unique_ptr<Language>{ new Language{ name } });
|
||||
}
|
||||
|
||||
std::size_t Language::getCount(Session& session)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
return utils::fetchQuerySingleResult(session.getDboSession()->query<int>("SELECT COUNT(*) FROM language"));
|
||||
}
|
||||
|
||||
RangeResults<LanguageId> Language::findIds(Session& session, const FindParameters& params)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
auto query{ createQuery<LanguageId>(session, params) };
|
||||
return utils::execRangeQuery<LanguageId>(query, params.range);
|
||||
}
|
||||
|
||||
RangeResults<Language::pointer> Language::find(Session& session, const FindParameters& params)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
auto query{ createQuery<Wt::Dbo::ptr<Language>>(session, params) };
|
||||
return utils::execRangeQuery<Language::pointer>(query, params.range);
|
||||
}
|
||||
|
||||
void Language::find(Session& session, const FindParameters& params, std::function<void(const pointer&)> func)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
auto query{ createQuery<Wt::Dbo::ptr<Language>>(session, params) };
|
||||
utils::forEachQueryRangeResult(query, params.range, func);
|
||||
}
|
||||
|
||||
Language::pointer Language::find(Session& session, LanguageId id)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
return utils::fetchQuerySingleResult(session.getDboSession()->find<Language>().where("id = ?").bind(id));
|
||||
}
|
||||
|
||||
Language::pointer Language::find(Session& session, std::string_view name)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
|
||||
if (name.size() > maxNameLength)
|
||||
name = name.substr(0, maxNameLength);
|
||||
|
||||
return utils::fetchQuerySingleResult(session.getDboSession()->find<Language>().where("name = ?").bind(name));
|
||||
}
|
||||
|
||||
RangeResults<LanguageId> Language::findOrphanIds(Session& session, std::optional<Range> range)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
auto query{ session.getDboSession()->query<LanguageId>("SELECT l.id FROM language l WHERE NOT EXISTS (SELECT 1 FROM track_language t_l WHERE t_l.language_id = l.id)") };
|
||||
return utils::execRangeQuery<LanguageId>(query, range);
|
||||
}
|
||||
|
||||
} // namespace lms::db
|
||||
@@ -23,6 +23,10 @@
|
||||
#include <Wt/Dbo/WtSqlTraits.h>
|
||||
|
||||
#include "database/Session.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/Track.hpp"
|
||||
#include "database/objects/User.hpp"
|
||||
|
||||
@@ -53,7 +57,11 @@ namespace lms::db
|
||||
|| params.filters.codec.has_value()
|
||||
|| params.filters.label.isValid()
|
||||
|| params.filters.releaseType.isValid()
|
||||
|| params.trackArtistLinkType.has_value())
|
||||
|| params.trackArtistLinkType.has_value()
|
||||
|| params.filters.genre.isValid()
|
||||
|| params.filters.grouping.isValid()
|
||||
|| params.filters.language.isValid()
|
||||
|| params.filters.mood.isValid())
|
||||
{
|
||||
query.join("track t ON t.id = t_a_l.track_id");
|
||||
|
||||
@@ -74,6 +82,30 @@ namespace lms::db
|
||||
query.join("release_release_type r_r_t ON r_r_t.release_id = t.release_id");
|
||||
query.where("r_r_t.release_type_id = ?").bind(params.filters.releaseType);
|
||||
}
|
||||
|
||||
if (params.filters.genre.isValid())
|
||||
{
|
||||
query.join("track_genre t_g ON t_g.track_id = t.id");
|
||||
query.where("t_g.genre_id = ?").bind(params.filters.genre);
|
||||
}
|
||||
|
||||
if (params.filters.grouping.isValid())
|
||||
{
|
||||
query.join("track_grouping t_gr ON t_gr.track_id = t.id");
|
||||
query.where("t_gr.grouping_id = ?").bind(params.filters.grouping);
|
||||
}
|
||||
|
||||
if (params.filters.language.isValid())
|
||||
{
|
||||
query.join("track_language t_l ON t_l.track_id = t.id");
|
||||
query.where("t_l.language_id = ?").bind(params.filters.language);
|
||||
}
|
||||
|
||||
if (params.filters.mood.isValid())
|
||||
{
|
||||
query.join("track_mood t_m ON t_m.track_id = t.id");
|
||||
query.where("t_m.mood_id = ?").bind(params.filters.mood);
|
||||
}
|
||||
}
|
||||
|
||||
if (params.releaseArtistsOnly)
|
||||
@@ -156,6 +188,30 @@ namespace lms::db
|
||||
query.where("r_r_t.release_type_id = ?").bind(params.filters.releaseType);
|
||||
}
|
||||
|
||||
if (params.filters.genre.isValid())
|
||||
{
|
||||
query.join("track_genre t_g ON t_g.track_id = t.id");
|
||||
query.where("t_g.genre_id = ?").bind(params.filters.genre);
|
||||
}
|
||||
|
||||
if (params.filters.grouping.isValid())
|
||||
{
|
||||
query.join("track_grouping t_gr ON t_gr.track_id = t.id");
|
||||
query.where("t_gr.grouping_id = ?").bind(params.filters.grouping);
|
||||
}
|
||||
|
||||
if (params.filters.language.isValid())
|
||||
{
|
||||
query.join("track_language t_l ON t_l.track_id = t.id");
|
||||
query.where("t_l.language_id = ?").bind(params.filters.language);
|
||||
}
|
||||
|
||||
if (params.filters.mood.isValid())
|
||||
{
|
||||
query.join("track_mood t_m ON t_m.track_id = t.id");
|
||||
query.where("t_m.mood_id = ?").bind(params.filters.mood);
|
||||
}
|
||||
|
||||
if (!params.filters.clusters.empty())
|
||||
{
|
||||
std::ostringstream oss;
|
||||
@@ -215,6 +271,30 @@ namespace lms::db
|
||||
query.where("r_r_t.release_type_id = ?").bind(params.filters.releaseType);
|
||||
}
|
||||
|
||||
if (params.filters.genre.isValid())
|
||||
{
|
||||
query.join("track_genre t_g ON t_g.track_id = t.id");
|
||||
query.where("t_g.genre_id = ?").bind(params.filters.genre);
|
||||
}
|
||||
|
||||
if (params.filters.grouping.isValid())
|
||||
{
|
||||
query.join("track_grouping t_gr ON t_gr.track_id = t.id");
|
||||
query.where("t_gr.grouping_id = ?").bind(params.filters.grouping);
|
||||
}
|
||||
|
||||
if (params.filters.language.isValid())
|
||||
{
|
||||
query.join("track_language t_l ON t_l.track_id = t.id");
|
||||
query.where("t_l.language_id = ?").bind(params.filters.language);
|
||||
}
|
||||
|
||||
if (params.filters.mood.isValid())
|
||||
{
|
||||
query.join("track_mood t_m ON t_m.track_id = t.id");
|
||||
query.where("t_m.mood_id = ?").bind(params.filters.mood);
|
||||
}
|
||||
|
||||
if (!params.filters.clusters.empty())
|
||||
{
|
||||
std::ostringstream oss;
|
||||
|
||||
@@ -22,6 +22,10 @@
|
||||
#include <Wt/Dbo/Impl.h>
|
||||
|
||||
#include "database/Session.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/Track.hpp"
|
||||
|
||||
#include "Utils.hpp"
|
||||
|
||||
@@ -26,7 +26,11 @@
|
||||
#include "database/objects/Artwork.hpp"
|
||||
#include "database/objects/Cluster.hpp"
|
||||
#include "database/objects/Directory.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/MediaLibrary.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/Release.hpp"
|
||||
#include "database/objects/Track.hpp"
|
||||
#include "database/objects/TrackArtistLink.hpp"
|
||||
|
||||
@@ -0,0 +1,175 @@
|
||||
/*
|
||||
* 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/objects/Mood.hpp"
|
||||
|
||||
#include <Wt/Dbo/Impl.h>
|
||||
|
||||
#include "core/ILogger.hpp"
|
||||
|
||||
#include "database/Session.hpp"
|
||||
#include "database/objects/Artist.hpp"
|
||||
#include "database/objects/Artwork.hpp"
|
||||
#include "database/objects/Cluster.hpp"
|
||||
#include "database/objects/Directory.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/MediaLibrary.hpp"
|
||||
#include "database/objects/Medium.hpp"
|
||||
#include "database/objects/Release.hpp"
|
||||
#include "database/objects/Track.hpp"
|
||||
#include "database/objects/TrackArtistLink.hpp"
|
||||
#include "database/objects/TrackEmbeddedImage.hpp"
|
||||
#include "database/objects/TrackEmbeddedImageLink.hpp"
|
||||
#include "database/objects/TrackLyrics.hpp"
|
||||
|
||||
#include "Utils.hpp"
|
||||
#include "traits/IdTypeTraits.hpp"
|
||||
#include "traits/StringViewTraits.hpp"
|
||||
|
||||
DBO_INSTANTIATE_TEMPLATES(lms::db::Mood)
|
||||
|
||||
namespace lms::db
|
||||
{
|
||||
namespace
|
||||
{
|
||||
template<typename ResultType>
|
||||
Wt::Dbo::Query<ResultType> createQuery(Session& session, std::string_view itemToSelect, const Mood::FindParameters& params)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
|
||||
auto query{ session.getDboSession()->query<ResultType>("SELECT " + std::string{ itemToSelect } + " FROM mood m") };
|
||||
|
||||
if (params.artist.isValid() || params.track.isValid() || params.release.isValid()
|
||||
|| params.sortMethod == MoodSortMethod::TrackCountDesc)
|
||||
query.join("track_mood t_m ON t_m.mood_id = m.id");
|
||||
|
||||
if (params.track.isValid())
|
||||
query.where("t_m.track_id = ?").bind(params.track);
|
||||
|
||||
if (params.release.isValid() || params.artist.isValid())
|
||||
query.join("track t ON t.id = t_m.track_id");
|
||||
|
||||
if (params.release.isValid())
|
||||
query.where("t.release_id = ?").bind(params.release);
|
||||
|
||||
if (params.artist.isValid())
|
||||
{
|
||||
query.join("track_artist_link t_a_l ON t_a_l.track_id = t.id");
|
||||
query.where("t_a_l.artist_id = ?").bind(params.artist);
|
||||
}
|
||||
|
||||
switch (params.sortMethod)
|
||||
{
|
||||
case MoodSortMethod::None:
|
||||
break;
|
||||
case MoodSortMethod::Name:
|
||||
query.orderBy("m.name COLLATE NOCASE");
|
||||
break;
|
||||
case MoodSortMethod::TrackCountDesc:
|
||||
query.orderBy("COUNT(t_m.track_id) DESC");
|
||||
break;
|
||||
}
|
||||
|
||||
// track_mood has a UNIQUE constraint on (track_id, mood_id), so no duplicates when filtering by track
|
||||
if (!params.track.isValid())
|
||||
query.groupBy("m.id");
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
template<typename ResultType>
|
||||
Wt::Dbo::Query<ResultType> createQuery(Session& session, const Mood::FindParameters& params)
|
||||
{
|
||||
std::string_view itemToSelect;
|
||||
|
||||
if constexpr (std::is_same_v<ResultType, MoodId>)
|
||||
itemToSelect = "m.id";
|
||||
else if constexpr (std::is_same_v<ResultType, Wt::Dbo::ptr<Mood>>)
|
||||
itemToSelect = "m";
|
||||
else
|
||||
static_assert("Unhandled type");
|
||||
|
||||
return createQuery<ResultType>(session, itemToSelect, params);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Mood::Mood(std::string_view name)
|
||||
: _name{ name.substr(0, maxNameLength) }
|
||||
{
|
||||
LMS_LOG_IF(DB, WARNING, name.size() > maxNameLength, "Mood name too long, truncated to '" << _name << "'");
|
||||
}
|
||||
|
||||
Mood::pointer Mood::create(Session& session, std::string_view name)
|
||||
{
|
||||
return session.getDboSession()->add(std::unique_ptr<Mood>{ new Mood{ name } });
|
||||
}
|
||||
|
||||
std::size_t Mood::getCount(Session& session)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
return utils::fetchQuerySingleResult(session.getDboSession()->query<int>("SELECT COUNT(*) FROM mood"));
|
||||
}
|
||||
|
||||
RangeResults<MoodId> Mood::findIds(Session& session, const FindParameters& params)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
auto query{ createQuery<MoodId>(session, params) };
|
||||
return utils::execRangeQuery<MoodId>(query, params.range);
|
||||
}
|
||||
|
||||
RangeResults<Mood::pointer> Mood::find(Session& session, const FindParameters& params)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
auto query{ createQuery<Wt::Dbo::ptr<Mood>>(session, params) };
|
||||
return utils::execRangeQuery<Mood::pointer>(query, params.range);
|
||||
}
|
||||
|
||||
void Mood::find(Session& session, const FindParameters& params, std::function<void(const pointer&)> func)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
auto query{ createQuery<Wt::Dbo::ptr<Mood>>(session, params) };
|
||||
utils::forEachQueryRangeResult(query, params.range, func);
|
||||
}
|
||||
|
||||
Mood::pointer Mood::find(Session& session, MoodId id)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
return utils::fetchQuerySingleResult(session.getDboSession()->find<Mood>().where("id = ?").bind(id));
|
||||
}
|
||||
|
||||
Mood::pointer Mood::find(Session& session, std::string_view name)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
|
||||
if (name.size() > maxNameLength)
|
||||
name = name.substr(0, maxNameLength);
|
||||
|
||||
return utils::fetchQuerySingleResult(session.getDboSession()->find<Mood>().where("name = ?").bind(name));
|
||||
}
|
||||
|
||||
RangeResults<MoodId> Mood::findOrphanIds(Session& session, std::optional<Range> range)
|
||||
{
|
||||
session.checkReadTransaction();
|
||||
auto query{ session.getDboSession()->query<MoodId>("SELECT m.id FROM mood m WHERE NOT EXISTS (SELECT 1 FROM track_mood t_m WHERE t_m.mood_id = m.id)") };
|
||||
return utils::execRangeQuery<MoodId>(query, range);
|
||||
}
|
||||
|
||||
} // namespace lms::db
|
||||
@@ -26,7 +26,11 @@
|
||||
#include "database/Session.hpp"
|
||||
#include "database/objects/Artwork.hpp"
|
||||
#include "database/objects/Directory.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/MediaLibrary.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/Track.hpp"
|
||||
#include "database/objects/TrackList.hpp"
|
||||
#include "database/objects/User.hpp"
|
||||
|
||||
@@ -27,8 +27,12 @@
|
||||
#include "database/objects/Artwork.hpp"
|
||||
#include "database/objects/Cluster.hpp"
|
||||
#include "database/objects/Directory.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/MediaLibrary.hpp"
|
||||
#include "database/objects/Medium.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/Release.hpp"
|
||||
#include "database/objects/Track.hpp"
|
||||
#include "database/objects/TrackArtistLink.hpp"
|
||||
|
||||
@@ -23,6 +23,10 @@
|
||||
#include <Wt/Dbo/WtSqlTraits.h>
|
||||
|
||||
#include "database/Session.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/Track.hpp"
|
||||
#include "database/objects/User.hpp"
|
||||
|
||||
|
||||
@@ -29,8 +29,12 @@
|
||||
#include "database/objects/Artwork.hpp"
|
||||
#include "database/objects/Cluster.hpp"
|
||||
#include "database/objects/Directory.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/MediaLibrary.hpp"
|
||||
#include "database/objects/Medium.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/ReleaseArtistLink.hpp"
|
||||
#include "database/objects/Track.hpp"
|
||||
#include "database/objects/TrackArtistLink.hpp"
|
||||
@@ -77,7 +81,11 @@ namespace lms::db
|
||||
|| params.originalDateRange
|
||||
|| params.trackArtist.isValid()
|
||||
|| params.filters.clusters.size() == 1
|
||||
|| params.filters.genre.isValid()
|
||||
|| params.filters.grouping.isValid()
|
||||
|| params.filters.language.isValid()
|
||||
|| params.filters.mediaLibrary.isValid()
|
||||
|| params.filters.mood.isValid()
|
||||
|| params.filters.codec.has_value()
|
||||
|| params.directory.isValid()
|
||||
|| params.parentDirectory.isValid())
|
||||
@@ -215,6 +223,34 @@ namespace lms::db
|
||||
query.where(oss.str());
|
||||
}
|
||||
|
||||
if (params.filters.genre.isValid())
|
||||
{
|
||||
query.join("track_genre t_g ON t_g.track_id = t.id")
|
||||
.where("t_g.genre_id = ?")
|
||||
.bind(params.filters.genre);
|
||||
}
|
||||
|
||||
if (params.filters.grouping.isValid())
|
||||
{
|
||||
query.join("track_grouping t_gr ON t_gr.track_id = t.id")
|
||||
.where("t_gr.grouping_id = ?")
|
||||
.bind(params.filters.grouping);
|
||||
}
|
||||
|
||||
if (params.filters.language.isValid())
|
||||
{
|
||||
query.join("track_language t_l ON t_l.track_id = t.id")
|
||||
.where("t_l.language_id = ?")
|
||||
.bind(params.filters.language);
|
||||
}
|
||||
|
||||
if (params.filters.mood.isValid())
|
||||
{
|
||||
query.join("track_mood t_m ON t_m.track_id = t.id")
|
||||
.where("t_m.mood_id = ?")
|
||||
.bind(params.filters.mood);
|
||||
}
|
||||
|
||||
if (params.filters.codec.has_value())
|
||||
query.where("t.codec = ?").bind(detail::getDbCodec(params.filters.codec.value()));
|
||||
|
||||
|
||||
@@ -24,6 +24,10 @@
|
||||
#include "core/ILogger.hpp"
|
||||
#include "database/Session.hpp"
|
||||
#include "database/objects/Artist.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/Release.hpp"
|
||||
#include "database/objects/Track.hpp"
|
||||
|
||||
|
||||
@@ -23,6 +23,10 @@
|
||||
#include <Wt/Dbo/WtSqlTraits.h>
|
||||
|
||||
#include "database/Session.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/Track.hpp"
|
||||
#include "database/objects/User.hpp"
|
||||
|
||||
|
||||
@@ -30,8 +30,12 @@
|
||||
#include "database/objects/Artwork.hpp"
|
||||
#include "database/objects/Cluster.hpp"
|
||||
#include "database/objects/Directory.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/MediaLibrary.hpp"
|
||||
#include "database/objects/Medium.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/Release.hpp"
|
||||
#include "database/objects/TrackArtistLink.hpp"
|
||||
#include "database/objects/TrackEmbeddedImage.hpp"
|
||||
@@ -109,6 +113,34 @@ namespace lms::db
|
||||
query.where(oss.str());
|
||||
}
|
||||
|
||||
if (params.filters.genre.isValid())
|
||||
{
|
||||
query.join("track_genre t_g ON t_g.track_id = t.id")
|
||||
.where("t_g.genre_id = ?")
|
||||
.bind(params.filters.genre);
|
||||
}
|
||||
|
||||
if (params.filters.grouping.isValid())
|
||||
{
|
||||
query.join("track_grouping t_gr ON t_gr.track_id = t.id")
|
||||
.where("t_gr.grouping_id = ?")
|
||||
.bind(params.filters.grouping);
|
||||
}
|
||||
|
||||
if (params.filters.language.isValid())
|
||||
{
|
||||
query.join("track_language t_l ON t_l.track_id = t.id")
|
||||
.where("t_l.language_id = ?")
|
||||
.bind(params.filters.language);
|
||||
}
|
||||
|
||||
if (params.filters.mood.isValid())
|
||||
{
|
||||
query.join("track_mood t_m ON t_m.track_id = t.id")
|
||||
.where("t_m.mood_id = ?")
|
||||
.bind(params.filters.mood);
|
||||
}
|
||||
|
||||
if (params.artist.isValid() || !params.artistName.empty())
|
||||
{
|
||||
query.join("artist a ON a.id = t_a_l.artist_id")
|
||||
@@ -446,6 +478,62 @@ namespace lms::db
|
||||
return utils::fetchQueryResults(query);
|
||||
}
|
||||
|
||||
std::vector<Genre::pointer> Track::getGenres() const
|
||||
{
|
||||
return utils::fetchQueryResults<Genre::pointer>(_genres.find());
|
||||
}
|
||||
|
||||
std::vector<GenreId> Track::getGenreIds() const
|
||||
{
|
||||
assert(session());
|
||||
|
||||
const auto query{ session()->query<GenreId>("SELECT t_g.genre_id FROM track_genre t_g").where("t_g.track_id = ?").bind(getId()) };
|
||||
|
||||
return utils::fetchQueryResults(query);
|
||||
}
|
||||
|
||||
std::vector<Grouping::pointer> Track::getGroupings() const
|
||||
{
|
||||
return utils::fetchQueryResults<Grouping::pointer>(_groupings.find());
|
||||
}
|
||||
|
||||
std::vector<GroupingId> Track::getGroupingIds() const
|
||||
{
|
||||
assert(session());
|
||||
|
||||
const auto query{ session()->query<GroupingId>("SELECT t_gr.grouping_id FROM track_grouping t_gr").where("t_gr.track_id = ?").bind(getId()) };
|
||||
|
||||
return utils::fetchQueryResults(query);
|
||||
}
|
||||
|
||||
std::vector<Language::pointer> Track::getLanguages() const
|
||||
{
|
||||
return utils::fetchQueryResults<Language::pointer>(_languages.find());
|
||||
}
|
||||
|
||||
std::vector<LanguageId> Track::getLanguageIds() const
|
||||
{
|
||||
assert(session());
|
||||
|
||||
const auto query{ session()->query<LanguageId>("SELECT t_l.language_id FROM track_language t_l").where("t_l.track_id = ?").bind(getId()) };
|
||||
|
||||
return utils::fetchQueryResults(query);
|
||||
}
|
||||
|
||||
std::vector<Mood::pointer> Track::getMoods() const
|
||||
{
|
||||
return utils::fetchQueryResults<Mood::pointer>(_moods.find());
|
||||
}
|
||||
|
||||
std::vector<MoodId> Track::getMoodIds() const
|
||||
{
|
||||
assert(session());
|
||||
|
||||
const auto query{ session()->query<MoodId>("SELECT t_m.mood_id FROM track_mood t_m").where("t_m.track_id = ?").bind(getId()) };
|
||||
|
||||
return utils::fetchQueryResults(query);
|
||||
}
|
||||
|
||||
ObjectPtr<MediaLibrary> Track::getMediaLibrary() const
|
||||
{
|
||||
return _mediaLibrary;
|
||||
@@ -566,6 +654,34 @@ namespace lms::db
|
||||
_clusters.insert(getDboPtr(cluster));
|
||||
}
|
||||
|
||||
void Track::setGenres(std::span<const ObjectPtr<Genre>> genres)
|
||||
{
|
||||
_genres.clear();
|
||||
for (const ObjectPtr<Genre>& genre : genres)
|
||||
_genres.insert(getDboPtr(genre));
|
||||
}
|
||||
|
||||
void Track::setGroupings(std::span<const ObjectPtr<Grouping>> groupings)
|
||||
{
|
||||
_groupings.clear();
|
||||
for (const ObjectPtr<Grouping>& grouping : groupings)
|
||||
_groupings.insert(getDboPtr(grouping));
|
||||
}
|
||||
|
||||
void Track::setLanguages(std::span<const ObjectPtr<Language>> languages)
|
||||
{
|
||||
_languages.clear();
|
||||
for (const ObjectPtr<Language>& language : languages)
|
||||
_languages.insert(getDboPtr(language));
|
||||
}
|
||||
|
||||
void Track::setMoods(std::span<const ObjectPtr<Mood>> moods)
|
||||
{
|
||||
_moods.clear();
|
||||
for (const ObjectPtr<Mood>& mood : moods)
|
||||
_moods.insert(getDboPtr(mood));
|
||||
}
|
||||
|
||||
void Track::clearLyrics()
|
||||
{
|
||||
_trackLyrics.clear();
|
||||
|
||||
@@ -24,6 +24,10 @@
|
||||
#include "core/ILogger.hpp"
|
||||
#include "database/Session.hpp"
|
||||
#include "database/objects/Artist.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/Track.hpp"
|
||||
|
||||
#include "Utils.hpp"
|
||||
|
||||
@@ -22,6 +22,10 @@
|
||||
#include <Wt/Dbo/Impl.h>
|
||||
|
||||
#include "database/Session.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/Track.hpp"
|
||||
#include "database/objects/User.hpp"
|
||||
|
||||
|
||||
@@ -23,6 +23,10 @@
|
||||
|
||||
#include "database/Session.hpp"
|
||||
#include "database/Types.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/Track.hpp"
|
||||
|
||||
#include "database/objects/TrackEmbeddedImageLink.hpp"
|
||||
|
||||
@@ -23,6 +23,10 @@
|
||||
#include <Wt/Dbo/WtSqlTraits.h>
|
||||
|
||||
#include "database/Session.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/Track.hpp"
|
||||
#include "database/objects/TrackEmbeddedImage.hpp"
|
||||
|
||||
|
||||
@@ -25,6 +25,10 @@
|
||||
|
||||
#include "database/Session.hpp"
|
||||
#include "database/objects/Cluster.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/PlayListFile.hpp"
|
||||
#include "database/objects/Track.hpp"
|
||||
#include "database/objects/User.hpp"
|
||||
|
||||
@@ -25,6 +25,10 @@
|
||||
#include "database/Session.hpp"
|
||||
#include "database/objects/Artist.hpp"
|
||||
#include "database/objects/Directory.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/Release.hpp"
|
||||
#include "database/objects/Track.hpp"
|
||||
|
||||
|
||||
@@ -22,6 +22,10 @@
|
||||
#include <Wt/Dbo/Impl.h>
|
||||
|
||||
#include "database/Session.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/Track.hpp"
|
||||
|
||||
#include "Utils.hpp"
|
||||
|
||||
@@ -27,6 +27,10 @@
|
||||
#include "database/Session.hpp"
|
||||
#include "database/objects/Artist.hpp"
|
||||
#include "database/objects/AuthToken.hpp"
|
||||
#include "database/objects/Genre.hpp"
|
||||
#include "database/objects/Grouping.hpp"
|
||||
#include "database/objects/Language.hpp"
|
||||
#include "database/objects/Mood.hpp"
|
||||
#include "database/objects/Release.hpp"
|
||||
#include "database/objects/Track.hpp"
|
||||
#include "database/objects/UIState.hpp"
|
||||
|
||||
Reference in New Issue
Block a user