Adding support for partial dates (only year or month), fixes #477
This commit is contained in:
@@ -35,7 +35,7 @@ namespace lms::db
|
||||
{
|
||||
namespace
|
||||
{
|
||||
static constexpr Version LMS_DATABASE_VERSION{ 79 };
|
||||
static constexpr Version LMS_DATABASE_VERSION{ 80 };
|
||||
}
|
||||
|
||||
VersionInfo::VersionInfo()
|
||||
@@ -88,6 +88,14 @@ namespace lms::db::Migration
|
||||
|
||||
namespace
|
||||
{
|
||||
void dropIndexes(Session& session)
|
||||
{
|
||||
// Make sure we remove all the previoulsy created index, the createIndexesIfNeeded will recreate them all
|
||||
std::vector<std::string> indexeNames{ utils::fetchQueryResults(session.getDboSession()->query<std::string>(R"(SELECT name FROM sqlite_master WHERE type = 'index' AND name LIKE '%_idx')")) };
|
||||
for (const auto& indexName : indexeNames)
|
||||
utils::executeCommand(*session.getDboSession(), "DROP INDEX " + indexName);
|
||||
}
|
||||
|
||||
void migrateFromV33(Session& session)
|
||||
{
|
||||
// remove name from track_artist_link
|
||||
@@ -461,9 +469,7 @@ SELECT
|
||||
void migrateFromV56(Session& session)
|
||||
{
|
||||
// Make sure we remove all the previoulsy created index, the createIndexesIfNeeded will recreate them all
|
||||
std::vector<std::string> indexeNames{ utils::fetchQueryResults(session.getDboSession()->query<std::string>(R"(SELECT name FROM sqlite_master WHERE type = 'index' AND name LIKE '%_idx')")) };
|
||||
for (const auto& indexName : indexeNames)
|
||||
utils::executeCommand(*session.getDboSession(), "DROP INDEX " + indexName);
|
||||
dropIndexes(session);
|
||||
}
|
||||
|
||||
void migrateFromV57(Session& session)
|
||||
@@ -1044,6 +1050,19 @@ FROM tracklist)");
|
||||
utils::executeCommand(*session.getDboSession(), "UPDATE scan_settings SET scan_version = scan_version + 1");
|
||||
}
|
||||
|
||||
void migrateFromV79(Session& session)
|
||||
{
|
||||
// Make sure we remove all the previoulsy created index, the createIndexesIfNeeded will recreate them all
|
||||
dropIndexes(session);
|
||||
|
||||
// New partial date/time support
|
||||
utils::executeCommand(*session.getDboSession(), "ALTER TABLE track DROP COLUMN year");
|
||||
utils::executeCommand(*session.getDboSession(), "ALTER TABLE track DROP COLUMN original_year");
|
||||
|
||||
// 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)" };
|
||||
@@ -1099,6 +1118,7 @@ FROM tracklist)");
|
||||
{ 76, migrateFromV76 },
|
||||
{ 77, migrateFromV77 },
|
||||
{ 78, migrateFromV78 },
|
||||
{ 79, migrateFromV79 },
|
||||
};
|
||||
|
||||
bool migrationPerformed{};
|
||||
@@ -1144,4 +1164,4 @@ FROM tracklist)");
|
||||
|
||||
return migrationPerformed;
|
||||
}
|
||||
} // namespace lms::db::Migration
|
||||
} // namespace lms::db::Migration
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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 "core/PartialDateTime.hpp"
|
||||
|
||||
#include <Wt/Dbo/SqlTraits.h>
|
||||
|
||||
namespace Wt::Dbo
|
||||
{
|
||||
template<>
|
||||
struct sql_value_traits<lms::core::PartialDateTime>
|
||||
{
|
||||
static std::string type(SqlConnection* conn, int /*size*/)
|
||||
{
|
||||
return conn->dateTimeType(SqlDateTimeType::DateTime);
|
||||
}
|
||||
|
||||
static void bind(const lms::core::PartialDateTime& dateTime, SqlStatement* statement, int column, int /* size */)
|
||||
{
|
||||
if (!dateTime.isValid())
|
||||
statement->bindNull(column);
|
||||
else
|
||||
statement->bind(column, dateTime.toISO8601String());
|
||||
}
|
||||
|
||||
static bool read(lms::core::PartialDateTime& dateTime, SqlStatement* statement, int column, int size)
|
||||
{
|
||||
std::string str;
|
||||
if (!statement->getResult(column, &str, size))
|
||||
return false;
|
||||
|
||||
dateTime = lms::core::PartialDateTime::fromString(str);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
} // namespace Wt::Dbo
|
||||
@@ -21,7 +21,7 @@
|
||||
|
||||
#include <Wt/Dbo/WtSqlTraits.h>
|
||||
|
||||
#include "core/ILogger.hpp"
|
||||
#include "core/PartialDateTime.hpp"
|
||||
#include "database/Artist.hpp"
|
||||
#include "database/Cluster.hpp"
|
||||
#include "database/Directory.hpp"
|
||||
@@ -32,6 +32,7 @@
|
||||
|
||||
#include "EnumSetTraits.hpp"
|
||||
#include "IdTypeTraits.hpp"
|
||||
#include "PartialDateTimeTraits.hpp"
|
||||
#include "SqlQuery.hpp"
|
||||
#include "StringViewTraits.hpp"
|
||||
#include "Utils.hpp"
|
||||
@@ -90,8 +91,8 @@ namespace lms::db
|
||||
|
||||
if (params.dateRange)
|
||||
{
|
||||
query.where("COALESCE(CAST(SUBSTR(t.date, 1, 4) AS INTEGER), t.year) >= ?").bind(params.dateRange->begin);
|
||||
query.where("COALESCE(CAST(SUBSTR(t.date, 1, 4) AS INTEGER), t.year) <= ?").bind(params.dateRange->end);
|
||||
query.where("CAST(SUBSTR(t.date, 1, 4) AS INTEGER) >= ?").bind(params.dateRange->begin);
|
||||
query.where("CAST(SUBSTR(t.date, 1, 4) AS INTEGER) <= ?").bind(params.dateRange->end);
|
||||
}
|
||||
|
||||
if (!params.name.empty())
|
||||
@@ -210,16 +211,16 @@ namespace lms::db
|
||||
query.orderBy("t.file_last_write DESC");
|
||||
break;
|
||||
case ReleaseSortMethod::DateAsc:
|
||||
query.orderBy("COALESCE(t.date, CAST(t.year AS TEXT)) ASC, r.name COLLATE NOCASE");
|
||||
query.orderBy("t.date ASC, r.name COLLATE NOCASE");
|
||||
break;
|
||||
case ReleaseSortMethod::DateDesc:
|
||||
query.orderBy("COALESCE(t.date, CAST(t.year AS TEXT)) DESC, r.name COLLATE NOCASE");
|
||||
query.orderBy("t.date DESC, r.name COLLATE NOCASE");
|
||||
break;
|
||||
case ReleaseSortMethod::OriginalDate:
|
||||
query.orderBy("COALESCE(original_date, CAST(original_year AS TEXT), date, CAST(year AS TEXT)), r.name COLLATE NOCASE");
|
||||
query.orderBy("COALESCE(t.original_date, t.date), r.name COLLATE NOCASE");
|
||||
break;
|
||||
case ReleaseSortMethod::OriginalDateDesc:
|
||||
query.orderBy("COALESCE(original_date, CAST(original_year AS TEXT), date, CAST(year AS TEXT)) DESC, r.name COLLATE NOCASE");
|
||||
query.orderBy("COALESCE(t.original_date, t.date) DESC, r.name COLLATE NOCASE");
|
||||
break;
|
||||
case ReleaseSortMethod::StarredDateDesc:
|
||||
assert(params.starringUser.isValid());
|
||||
@@ -453,22 +454,22 @@ namespace lms::db
|
||||
return discs;
|
||||
}
|
||||
|
||||
Wt::WDate Release::getDate() const
|
||||
core::PartialDateTime Release::getDate() const
|
||||
{
|
||||
return getDate(false);
|
||||
}
|
||||
|
||||
Wt::WDate Release::getOriginalDate() const
|
||||
core::PartialDateTime Release::getOriginalDate() const
|
||||
{
|
||||
return getDate(true);
|
||||
}
|
||||
|
||||
Wt::WDate Release::getDate(bool original) const
|
||||
core::PartialDateTime Release::getDate(bool original) const
|
||||
{
|
||||
assert(session());
|
||||
|
||||
const char* field{ original ? "original_date" : "date" };
|
||||
auto query{ (session()->query<Wt::WDate>(std::string{ "SELECT " } + "t." + field + " FROM track t").where("t.release_id = ?").groupBy(field).bind(getId())) };
|
||||
auto query{ (session()->query<core::PartialDateTime>(std::string{ "SELECT " } + "t." + field + " FROM track t").where("t.release_id = ?").groupBy(field).bind(getId())) };
|
||||
|
||||
const auto dates{ utils::fetchQueryResults(query) };
|
||||
|
||||
@@ -493,17 +494,25 @@ namespace lms::db
|
||||
{
|
||||
assert(session());
|
||||
|
||||
const char* field{ original ? "original_year" : "year" };
|
||||
auto query{ session()->query<std::optional<int>>(std::string{ "SELECT " } + "t." + field + " FROM track t").where("t.release_id = ?").bind(getId()).groupBy(field) };
|
||||
const char* field{ original ? "original_date" : "date" };
|
||||
auto query{ session()->query<core::PartialDateTime>(std::string{ "SELECT " } + "t." + field + " FROM track t").where("t.release_id = ?").bind(getId()).groupBy(field) };
|
||||
|
||||
const auto years{ utils::fetchQueryResults(query) };
|
||||
bool multiYears{};
|
||||
std::optional<int> year{};
|
||||
utils::forEachQueryResult(query, [&](core::PartialDateTime dateTime) {
|
||||
assert(dateTime.isValid());
|
||||
|
||||
// various years => invalid years
|
||||
const std::size_t count{ years.size() };
|
||||
if (count == 0 || count > 1)
|
||||
if (!year)
|
||||
year = dateTime.getYear().value();
|
||||
else if (*year != dateTime.getYear().value())
|
||||
multiYears = true;
|
||||
});
|
||||
|
||||
if (multiYears)
|
||||
return std::nullopt;
|
||||
|
||||
return years.front();
|
||||
assert(year);
|
||||
return *year;
|
||||
}
|
||||
|
||||
std::optional<std::string> Release::getCopyright() const
|
||||
|
||||
@@ -52,6 +52,7 @@
|
||||
|
||||
#include "EnumSetTraits.hpp"
|
||||
#include "Migration.hpp"
|
||||
#include "PartialDateTimeTraits.hpp"
|
||||
#include "PathTraits.hpp"
|
||||
#include "Utils.hpp"
|
||||
|
||||
@@ -247,12 +248,10 @@ namespace lms::db
|
||||
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS track_name_idx ON track(name)");
|
||||
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS track_name_nocase_idx ON track(name COLLATE NOCASE)");
|
||||
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS track_original_date_idx ON track(original_date)");
|
||||
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS track_original_year_idx ON track(original_year)");
|
||||
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS track_recording_mbid_idx ON track(recording_mbid)");
|
||||
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS track_release_idx ON track(release_id)");
|
||||
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS track_release_file_last_write_idx ON track(release_id, file_last_write)");
|
||||
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS track_release_year_idx ON track(release_id, year)");
|
||||
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS track_year_idx ON track(year)");
|
||||
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS track_release_date_idx ON track(release_id, date)");
|
||||
|
||||
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS tracklist_name_idx ON tracklist(name)");
|
||||
utils::executeCommand(_session, "CREATE INDEX IF NOT EXISTS tracklist_user_type_idx ON tracklist(user_id, type)");
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
#include "database/User.hpp"
|
||||
|
||||
#include "IdTypeTraits.hpp"
|
||||
#include "PartialDateTimeTraits.hpp"
|
||||
#include "PathTraits.hpp"
|
||||
#include "SqlQuery.hpp"
|
||||
#include "StringViewTraits.hpp"
|
||||
@@ -450,6 +451,16 @@ namespace lms::db
|
||||
_trackLyrics.insert(getDboPtr(lyrics));
|
||||
}
|
||||
|
||||
std::optional<int> Track::getYear() const
|
||||
{
|
||||
return _date.getYear();
|
||||
}
|
||||
|
||||
std::optional<int> Track::getOriginalYear() const
|
||||
{
|
||||
return _originalDate.getYear();
|
||||
}
|
||||
|
||||
bool Track::hasLyrics() const
|
||||
{
|
||||
return !_trackLyrics.empty();
|
||||
|
||||
@@ -41,9 +41,4 @@ namespace lms::db
|
||||
{
|
||||
return allowedAudioBitrates.find(bitrate) != std::cend(allowedAudioBitrates);
|
||||
}
|
||||
|
||||
DateRange DateRange::fromYearRange(int from, int to)
|
||||
{
|
||||
return DateRange{ from, to };
|
||||
}
|
||||
} // namespace lms::db
|
||||
|
||||
Reference in New Issue
Block a user