Adding support for partial dates (only year or month), fixes #477

This commit is contained in:
emeric
2025-01-19 23:50:39 +01:00
parent c73ff7dc03
commit 33759def78
27 changed files with 594 additions and 170 deletions
+1
View File
@@ -12,6 +12,7 @@ add_library(lmscore SHARED
impl/IOContextRunner.cpp
impl/Logger.cpp
impl/NetAddress.cpp
impl/PartialDateTime.cpp
impl/Path.cpp
impl/Random.cpp
impl/RecursiveSharedMutex.cpp
+155
View File
@@ -0,0 +1,155 @@
/*
* 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 "core/PartialDateTime.hpp"
#include <iomanip>
#include <limits>
#include <sstream>
namespace lms::core
{
PartialDateTime::PartialDateTime(int year)
: _year{ static_cast<std::int16_t>(year) }
, _precision{ Precision::Year }
{
}
PartialDateTime::PartialDateTime(int year, unsigned month)
: _year{ static_cast<std::int16_t>(year) }
, _month{ static_cast<std::uint8_t>(month) }
, _precision{ Precision::Month }
{
}
PartialDateTime::PartialDateTime(int year, unsigned month, unsigned day)
: _year{ static_cast<std::int16_t>(year) }
, _month{ static_cast<std::uint8_t>(month) }
, _day{ static_cast<std::uint8_t>(day) }
, _precision{ Precision::Day }
{
}
PartialDateTime::PartialDateTime(int year, unsigned month, unsigned day, unsigned hour, unsigned min, unsigned sec)
: _year{ static_cast<std::int16_t>(year) }
, _month{ static_cast<std::uint8_t>(month) }
, _day{ static_cast<std::uint8_t>(day) }
, _hour{ static_cast<std::uint8_t>(hour) }
, _min{ static_cast<std::uint8_t>(min) }
, _sec{ static_cast<std::uint8_t>(sec) }
, _precision{ Precision::Sec }
{
}
PartialDateTime PartialDateTime::fromString(std::string_view str)
{
PartialDateTime res;
const std::string dateTimeStr{ str };
static constexpr const char* formats[]{
"%Y-%m-%dT%H:%M:%S",
"%Y-%m-%d %H:%M:%S",
"%Y/%m/%d %H:%M:%S",
};
for (const char* format : formats)
{
PartialDateTime candidate;
std::tm tm{};
tm.tm_year = std::numeric_limits<decltype(tm.tm_year)>::min();
tm.tm_mon = std::numeric_limits<decltype(tm.tm_mon)>::min();
tm.tm_mday = std::numeric_limits<decltype(tm.tm_mday)>::min();
tm.tm_hour = std::numeric_limits<decltype(tm.tm_hour)>::min();
tm.tm_min = std::numeric_limits<decltype(tm.tm_min)>::min();
tm.tm_sec = std::numeric_limits<decltype(tm.tm_sec)>::min();
std::istringstream ss{ dateTimeStr };
ss >> std::get_time(&tm, format);
if (ss.fail())
continue;
if (tm.tm_sec != std::numeric_limits<decltype(tm.tm_sec)>::min())
{
candidate._sec = tm.tm_sec;
candidate._precision = Precision::Sec;
}
if (tm.tm_min != std::numeric_limits<decltype(tm.tm_min)>::min())
{
candidate._min = tm.tm_min;
if (candidate._precision == Precision::Invalid)
candidate._precision = Precision::Min;
}
if (tm.tm_hour != std::numeric_limits<decltype(tm.tm_hour)>::min())
{
candidate._hour = tm.tm_hour;
if (candidate._precision == Precision::Invalid)
candidate._precision = Precision::Hour;
}
if (tm.tm_mday != std::numeric_limits<decltype(tm.tm_mday)>::min())
{
candidate._day = tm.tm_mday;
if (candidate._precision == Precision::Invalid)
candidate._precision = Precision::Day;
}
if (tm.tm_mon != std::numeric_limits<decltype(tm.tm_mon)>::min())
{
candidate._month = tm.tm_mon + 1; // tm.tm_mon is [0, 11]
if (candidate._precision == Precision::Invalid)
candidate._precision = Precision::Month;
}
if (tm.tm_year != std::numeric_limits<decltype(tm.tm_year)>::min())
{
candidate._year = tm.tm_year + 1900; // tm.tm_year is years since 1900
if (candidate._precision == Precision::Invalid)
candidate._precision = Precision::Year;
}
if (candidate > res)
res = candidate;
if (res._precision == Precision::Sec)
break;
}
return res;
}
std::string PartialDateTime::toISO8601String() const
{
if (_precision == Precision::Invalid)
return "";
std::ostringstream ss;
ss << std::setfill('0') << std::setw(4) << _year;
if (_precision >= Precision::Month)
ss << "-" << std::setw(2) << static_cast<int>(_month);
if (_precision >= Precision::Day)
ss << "-" << std::setw(2) << static_cast<int>(_day);
if (_precision >= Precision::Hour)
ss << 'T' << std::setw(2) << static_cast<int>(_hour);
if (_precision >= Precision::Min)
ss << ':' << std::setw(2) << static_cast<int>(_min);
if (_precision >= Precision::Sec)
ss << ':' << std::setw(2) << static_cast<int>(_sec);
return ss.str();
}
} // namespace lms::core
@@ -0,0 +1,67 @@
/*
* 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 <cstdint>
#include <optional>
#include <string>
#include <string_view>
namespace lms::core
{
class PartialDateTime
{
public:
constexpr PartialDateTime() = default;
PartialDateTime(int year);
PartialDateTime(int year, unsigned month);
PartialDateTime(int year, unsigned month, unsigned day);
PartialDateTime(int year, unsigned month, unsigned day, unsigned hour, unsigned min, unsigned sec);
static PartialDateTime fromString(std::string_view str);
std::string toISO8601String() const;
bool isValid() const { return _precision != Precision::Invalid; }
constexpr std::optional<int> getYear() const { return (_precision >= Precision::Year ? std::make_optional(_year) : std::nullopt); }
constexpr std::optional<int> getMonth() const { return (_precision >= Precision::Month ? std::make_optional(_month) : std::nullopt); }
constexpr std::optional<int> getDay() const { return (_precision >= Precision::Day ? std::make_optional(_day) : std::nullopt); }
constexpr auto operator<=>(const PartialDateTime& other) const = default;
private:
std::int16_t _year{};
std::uint8_t _month{}; // 1 to 12
std::uint8_t _day{}; // 1 to 31
std::uint8_t _hour{}; // 0 to 23
std::uint8_t _min{}; // 0 to 59
std::uint8_t _sec{}; // 0 to 59
enum class Precision : std::uint8_t
{
Invalid,
Year,
Month,
Day,
Hour,
Min,
Sec,
};
Precision _precision{ Precision::Invalid };
};
} // namespace lms::core
+1
View File
@@ -3,6 +3,7 @@ include(GoogleTest)
add_executable(test-core
EnumSet.cpp
LiteralString.cpp
PartialDateTime.cpp
Path.cpp
RecursiveSharedMutex.cpp
Service.cpp
+115
View File
@@ -0,0 +1,115 @@
/*
* 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 <gtest/gtest.h>
#include "core/PartialDateTime.hpp"
namespace lms::core::stringUtils::tests
{
TEST(PartialDateTime, year)
{
EXPECT_EQ(PartialDateTime{}.getYear(), std::nullopt);
EXPECT_EQ(PartialDateTime{ 1992 }.getYear(), 1992);
}
TEST(PartialDateTime, month)
{
EXPECT_EQ(PartialDateTime{}.getMonth(), std::nullopt);
EXPECT_EQ((PartialDateTime{ 1992, 3 }.getMonth()), std::optional<int>{ 3 });
}
TEST(PartialDateTime, day)
{
EXPECT_EQ(PartialDateTime{}.getDay(), std::nullopt);
EXPECT_EQ((PartialDateTime{ 1992, 3, 27 }.getDay()), std::optional<int>{ 27 });
}
TEST(PartialDateTime, comparison)
{
EXPECT_EQ((PartialDateTime{ 1992, 3, 27 }), (PartialDateTime{ 1992, 3, 27 }));
EXPECT_EQ((PartialDateTime{ 1992, 3 }), (PartialDateTime{ 1992, 3 }));
EXPECT_EQ(PartialDateTime{ 1992 }, PartialDateTime{ 1992 });
EXPECT_NE((PartialDateTime{ 1992, 3, 27 }), (PartialDateTime{ 1992, 3 }));
EXPECT_NE((PartialDateTime{ 1992, 3, 27 }), (PartialDateTime{ 1992 }));
EXPECT_NE((PartialDateTime{ 1992, 3 }), (PartialDateTime{ 1992 }));
EXPECT_NE((PartialDateTime{ 1992, 3, 27 }), (PartialDateTime{ 1992, 3, 28 }));
EXPECT_NE((PartialDateTime{ 1992, 3, 27 }), (PartialDateTime{ 1992, 4, 27 }));
EXPECT_NE((PartialDateTime{ 1992, 3, 27 }), (PartialDateTime{ 1993, 3, 27 }));
EXPECT_GT((PartialDateTime{ 1993, 3, 28 }), (PartialDateTime{ 1993, 3, 27 }));
EXPECT_GT((PartialDateTime{ 1993, 4 }), (PartialDateTime{ 1993, 3, 27 }));
EXPECT_GT((PartialDateTime{ 1994 }), (PartialDateTime{ 1993, 3, 27 }));
EXPECT_LT((PartialDateTime{ 1993, 3, 27 }), (PartialDateTime{ 1993, 3, 28 }));
EXPECT_LT((PartialDateTime{ 1993, 3, 27 }), (PartialDateTime{ 1993, 4 }));
EXPECT_LT((PartialDateTime{ 1993, 3, 27 }), (PartialDateTime{ 1994 }));
}
TEST(PartialDateTime, stringComparison)
{
EXPECT_EQ((PartialDateTime{ 1992, 3, 27 }.toISO8601String()), (PartialDateTime{ 1992, 3, 27 }.toISO8601String()));
EXPECT_EQ((PartialDateTime{ 1992, 3 }.toISO8601String()), (PartialDateTime{ 1992, 3 }.toISO8601String()));
EXPECT_EQ(PartialDateTime{ 1992 }.toISO8601String(), PartialDateTime{ 1992 }.toISO8601String());
EXPECT_NE((PartialDateTime{ 1992, 3, 27 }.toISO8601String()), (PartialDateTime{ 1992, 3 }.toISO8601String()));
EXPECT_NE((PartialDateTime{ 1992, 3, 27 }.toISO8601String()), (PartialDateTime{ 1992 }.toISO8601String()));
EXPECT_NE((PartialDateTime{ 1992, 3 }.toISO8601String()), (PartialDateTime{ 1992 }.toISO8601String()));
EXPECT_NE((PartialDateTime{ 1992, 3, 27 }.toISO8601String()), (PartialDateTime{ 1992, 3, 28 }.toISO8601String()));
EXPECT_NE((PartialDateTime{ 1992, 3, 27 }.toISO8601String()), (PartialDateTime{ 1992, 4, 27 }.toISO8601String()));
EXPECT_NE((PartialDateTime{ 1992, 3, 27 }.toISO8601String()), (PartialDateTime{ 1993, 3, 27 }.toISO8601String()));
EXPECT_GT((PartialDateTime{ 1993, 3, 28 }.toISO8601String()), (PartialDateTime{ 1993, 3, 27 }.toISO8601String()));
EXPECT_GT((PartialDateTime{ 1993, 4 }.toISO8601String()), (PartialDateTime{ 1993, 3, 27 }.toISO8601String()));
EXPECT_GT((PartialDateTime{ 1994 }.toISO8601String()), (PartialDateTime{ 1993, 3, 27 }.toISO8601String()));
EXPECT_LT((PartialDateTime{ 1993, 3, 27 }.toISO8601String()), (PartialDateTime{ 1993, 3, 28 }.toISO8601String()));
EXPECT_LT((PartialDateTime{ 1993, 3, 27 }.toISO8601String()), (PartialDateTime{ 1993, 4 }.toISO8601String()));
EXPECT_LT((PartialDateTime{ 1993, 3, 27 }.toISO8601String()), (PartialDateTime{ 1994 }.toISO8601String()));
}
TEST(PartialDateTime, stringConversions)
{
struct TestCase
{
std::string_view input;
std::string_view expectedOutput;
};
constexpr TestCase tests[]{
{ "1992", "1992" },
{ "1992-03", "1992-03" },
{ "1992-03-27", "1992-03-27" },
{ "1992-03-27T15", "1992-03-27T15" },
{ "1992-03-27T15:08", "1992-03-27T15:08" },
{ "1992-03-27T15:08:57", "1992-03-27T15:08:57" },
{ "1992-03-27 15", "1992-03-27T15" },
{ "1992-03-27 15:08", "1992-03-27T15:08" },
{ "1992-03-27 15:08:57", "1992-03-27T15:08:57" },
{ "1992", "1992" },
{ "1992/03", "1992-03" },
{ "1992/03/27", "1992-03-27" },
{ "1992/03/27 15", "1992-03-27T15" },
{ "1992/03/27 15:08", "1992-03-27T15:08" },
{ "1992/03/27 15:08:57", "1992-03-27T15:08:57" },
};
for (const TestCase& test : tests)
{
const PartialDateTime dateTime{ PartialDateTime::fromString(test.input) };
EXPECT_EQ(dateTime.toISO8601String(), test.expectedOutput) << "Input = '" << test.input;
}
}
} // namespace lms::core::stringUtils::tests
+24 -4
View File
@@ -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{};
@@ -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
+27 -18
View File
@@ -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
+2 -3
View File
@@ -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)");
+11
View File
@@ -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();
-5
View File
@@ -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
@@ -19,7 +19,6 @@
#pragma once
#include <filesystem>
#include <optional>
#include <span>
#include <string>
@@ -30,6 +29,7 @@
#include <Wt/WDateTime.h>
#include "core/EnumSet.hpp"
#include "core/PartialDateTime.hpp"
#include "core/UUID.hpp"
#include "database/ArtistId.hpp"
#include "database/ClusterId.hpp"
@@ -126,7 +126,7 @@ namespace lms::db
ReleaseSortMethod sortMethod{ ReleaseSortMethod::None };
std::optional<Range> range;
Wt::WDateTime writtenAfter;
std::optional<DateRange> dateRange;
std::optional<YearRange> dateRange;
UserId starringUser; // only releases starred by this user
std::optional<FeedbackBackend> feedbackBackend; // and for this backend
ArtistId artist; // only releases that involved this user
@@ -167,7 +167,7 @@ namespace lms::db
writtenAfter = _after;
return *this;
}
FindParameters& setDateRange(const std::optional<DateRange>& _dateRange)
FindParameters& setDateRange(const std::optional<YearRange>& _dateRange)
{
dateRange = _dateRange;
return *this;
@@ -227,9 +227,9 @@ namespace lms::db
std::vector<std::vector<ObjectPtr<Cluster>>> getClusterGroups(const std::vector<ClusterTypeId>& clusterTypeIds, std::size_t size) const;
// Utility functions (if all tracks have the same values, which is legit to not be the case)
Wt::WDate getDate() const;
core::PartialDateTime getDate() const;
std::optional<int> getYear() const;
Wt::WDate getOriginalDate() const;
core::PartialDateTime getOriginalDate() const;
std::optional<int> getOriginalYear() const;
std::optional<std::string> getCopyright() const;
std::optional<std::string> getCopyrightURL() const;
@@ -302,7 +302,7 @@ namespace lms::db
Release(const std::string& name, const std::optional<core::UUID>& MBID = {});
static pointer create(Session& session, const std::string& name, const std::optional<core::UUID>& MBID = {});
Wt::WDate getDate(bool original) const;
core::PartialDateTime getDate(bool original) const;
std::optional<int> getYear(bool original) const;
static constexpr std::size_t _maxNameLength{ 512 };
+12 -17
View File
@@ -33,6 +33,7 @@
#include <Wt/WDateTime.h>
#include "core/EnumSet.hpp"
#include "core/PartialDateTime.hpp"
#include "core/UUID.hpp"
#include "database/ArtistId.hpp"
#include "database/ClusterId.hpp"
@@ -223,16 +224,14 @@ namespace lms::db
void setRelativeFilePath(const std::filesystem::path& filePath);
void setFileSize(std::size_t fileSize) { _fileSize = fileSize; }
void setLastWriteTime(Wt::WDateTime time) { _fileLastWrite = time; }
void setAddedTime(Wt::WDateTime time) { _fileAdded = time; }
void setAddedTime(core::PartialDateTime time) { _fileAdded = time; }
void setBitrate(std::size_t bitrate) { _bitrate = bitrate; }
void setBitsPerSample(std::size_t bitsPerSample) { _bitsPerSample = bitsPerSample; }
void setDuration(std::chrono::milliseconds duration) { _duration = duration; }
void setChannelCount(std::size_t channelCount) { _channelCount = channelCount; }
void setSampleRate(std::size_t channelCount) { _sampleRate = channelCount; }
void setDate(const Wt::WDate& date) { _date = date; }
void setYear(std::optional<int> year) { _year = year; }
void setOriginalDate(const Wt::WDate& date) { _originalDate = date; }
void setOriginalYear(std::optional<int> year) { _originalYear = year; }
void setDate(const core::PartialDateTime& date) { _date = date; }
void setOriginalDate(const core::PartialDateTime& date) { _originalDate = date; }
void setHasCover(bool hasCover) { _hasCover = hasCover; }
void setTrackMBID(const std::optional<core::UUID>& MBID) { _trackMBID = MBID ? MBID->getAsString() : ""; }
void setRecordingMBID(const std::optional<core::UUID>& MBID) { _recordingMBID = MBID ? MBID->getAsString() : ""; }
@@ -268,12 +267,12 @@ namespace lms::db
std::chrono::milliseconds getDuration() const { return _duration; }
std::size_t getSampleRate() const { return _sampleRate; }
const Wt::WDateTime& getLastWritten() const { return _fileLastWrite; }
const Wt::WDate& getDate() const { return _date; }
std::optional<int> getYear() const { return _year; }
const Wt::WDate& getOriginalDate() const { return _originalDate; }
std::optional<int> getOriginalYear() const { return _originalYear; };
const core::PartialDateTime& getDate() const { return _date; }
std::optional<int> getYear() const;
const core::PartialDateTime& getOriginalDate() const { return _originalDate; }
std::optional<int> getOriginalYear() const;
const Wt::WDateTime& getLastWriteTime() const { return _fileLastWrite; }
const Wt::WDateTime& getAddedTime() const { return _fileAdded; }
const core::PartialDateTime& getAddedTime() const { return _fileAdded; }
bool hasCover() const { return _hasCover; }
bool hasLyrics() const;
std::optional<core::UUID> getTrackMBID() const { return core::UUID::fromString(_trackMBID); }
@@ -314,9 +313,7 @@ namespace lms::db
Wt::Dbo::field(a, _channelCount, "channel_count");
Wt::Dbo::field(a, _sampleRate, "sample_rate");
Wt::Dbo::field(a, _date, "date");
Wt::Dbo::field(a, _year, "year");
Wt::Dbo::field(a, _originalDate, "original_date");
Wt::Dbo::field(a, _originalYear, "original_year");
Wt::Dbo::field(a, _absoluteFilePath, "absolute_file_path");
Wt::Dbo::field(a, _relativeFilePath, "relative_file_path");
Wt::Dbo::field(a, _fileStem, "file_stem");
@@ -362,17 +359,15 @@ namespace lms::db
int _channelCount{};
std::chrono::duration<int, std::milli> _duration{};
int _sampleRate{};
Wt::WDate _date;
std::optional<int> _year;
Wt::WDate _originalDate;
std::optional<int> _originalYear;
core::PartialDateTime _date;
core::PartialDateTime _originalDate;
std::filesystem::path _absoluteFilePath; // full path
std::filesystem::path _relativeFilePath; // relative to root (that may be deleted)
std::filesystem::path _fileStem;
std::filesystem::path _fileName;
long long _fileSize{};
Wt::WDateTime _fileLastWrite;
Wt::WDateTime _fileAdded;
core::PartialDateTime _fileAdded;
bool _hasCover{};
std::string _trackMBID;
std::string _recordingMBID;
+3 -5
View File
@@ -100,12 +100,10 @@ namespace lms::db
}
};
struct DateRange
struct YearRange
{
int begin;
int end;
static DateRange fromYearRange(int from, int to);
int begin{};
int end{};
};
struct DiscInfo
+21 -17
View File
@@ -19,6 +19,7 @@
#include "Common.hpp"
#include "core/PartialDateTime.hpp"
#include "database/Image.hpp"
namespace lms::db::tests
@@ -462,8 +463,8 @@ namespace lms::db::tests
{
ScopedRelease release1{ session, "MyRelease1" };
ScopedRelease release2{ session, "MyRelease2" };
const Wt::WDate release1Date{ Wt::WDate{ 1994, 2, 3 } };
const Wt::WDate release1OriginalDate{ Wt::WDate{ 1993, 4, 5 } };
const core::PartialDateTime release1Date{ 1994, 2, 3 };
const core::PartialDateTime release1OriginalDate{ 1993, 4, 5 };
ScopedTrack track1A{ session };
ScopedTrack track1B{ session };
@@ -473,7 +474,7 @@ namespace lms::db::tests
{
auto transaction{ session.createReadTransaction() };
const auto releases{ Release::findIds(session, Release::FindParameters{}.setDateRange(DateRange::fromYearRange(0, 3000))) };
const auto releases{ Release::findIds(session, Release::FindParameters{}.setDateRange(YearRange{ -3000, 3000 })) };
EXPECT_EQ(releases.results.size(), 0);
}
@@ -492,20 +493,23 @@ namespace lms::db::tests
EXPECT_EQ(release1.get()->getDate(), release1Date);
EXPECT_EQ(release1.get()->getOriginalDate(), release1OriginalDate);
EXPECT_EQ(release1.get()->getYear(), release1Date.getYear());
EXPECT_EQ(release1.get()->getOriginalYear(), release1OriginalDate.getYear());
}
{
auto transaction{ session.createReadTransaction() };
auto releases{ Release::findIds(session, Release::FindParameters{}.setDateRange(DateRange::fromYearRange(1950, 2000))) };
auto releases{ Release::findIds(session, Release::FindParameters{}.setDateRange(YearRange{ 1950, 2000 })) };
ASSERT_EQ(releases.results.size(), 1);
EXPECT_EQ(releases.results.front(), release1.getId());
releases = Release::findIds(session, Release::FindParameters{}.setDateRange(DateRange::fromYearRange(1994, 1994)));
releases = Release::findIds(session, Release::FindParameters{}.setDateRange(YearRange{ 1994, 1994 }));
ASSERT_EQ(releases.results.size(), 1);
EXPECT_EQ(releases.results.front(), release1.getId());
releases = Release::findIds(session, Release::FindParameters{}.setDateRange(DateRange::fromYearRange(1993, 1993)));
releases = Release::findIds(session, Release::FindParameters{}.setDateRange(YearRange{ 1993, 1993 }));
ASSERT_EQ(releases.results.size(), 0);
}
}
@@ -525,7 +529,7 @@ namespace lms::db::tests
{
auto transaction{ session.createReadTransaction() };
const auto releases{ Release::findIds(session, Release::FindParameters{}.setDateRange(DateRange::fromYearRange(0, 3000))) };
const auto releases{ Release::findIds(session, Release::FindParameters{}.setDateRange(YearRange{ 0, 3000 })) };
EXPECT_EQ(releases.results.size(), 0);
}
@@ -537,10 +541,10 @@ namespace lms::db::tests
track2A.get().modify()->setRelease(release2.get());
track2B.get().modify()->setRelease(release2.get());
track1A.get().modify()->setYear(release1Year);
track1B.get().modify()->setYear(release1Year);
track1A.get().modify()->setOriginalYear(release1OriginalYear);
track1B.get().modify()->setOriginalYear(release1OriginalYear);
track1A.get().modify()->setDate(core::PartialDateTime{ release1Year });
track1B.get().modify()->setDate(core::PartialDateTime{ release1Year });
track1A.get().modify()->setOriginalDate(core::PartialDateTime{ release1OriginalYear });
track1B.get().modify()->setOriginalDate(core::PartialDateTime{ release1OriginalYear });
EXPECT_EQ(release1.get()->getYear(), release1Year);
EXPECT_EQ(release1.get()->getOriginalYear(), release1OriginalYear);
@@ -549,15 +553,15 @@ namespace lms::db::tests
{
auto transaction{ session.createReadTransaction() };
auto releases{ Release::findIds(session, Release::FindParameters{}.setDateRange(DateRange::fromYearRange(1950, 2000))) };
auto releases{ Release::findIds(session, Release::FindParameters{}.setDateRange(YearRange{ 1950, 2000 })) };
ASSERT_EQ(releases.results.size(), 1);
EXPECT_EQ(releases.results.front(), release1.getId());
releases = Release::findIds(session, Release::FindParameters{}.setDateRange(DateRange::fromYearRange(1994, 1994)));
releases = Release::findIds(session, Release::FindParameters{}.setDateRange(YearRange{ 1994, 1994 }));
ASSERT_EQ(releases.results.size(), 1);
EXPECT_EQ(releases.results.front(), release1.getId());
releases = Release::findIds(session, Release::FindParameters{}.setDateRange(DateRange::fromYearRange(1993, 1993)));
releases = Release::findIds(session, Release::FindParameters{}.setDateRange(YearRange{ 1993, 1993 }));
ASSERT_EQ(releases.results.size(), 0);
}
}
@@ -957,11 +961,11 @@ namespace lms::db::tests
TEST_F(DatabaseFixture, Release_sortMethod)
{
ScopedRelease release1{ session, "MyRelease1" };
const Wt::WDate release1Date{ Wt::WDate{ 2000, 2, 3 } };
const Wt::WDate release1OriginalDate{ Wt::WDate{ 1993, 4, 5 } };
const core::PartialDateTime release1Date{ 2000, 2, 3 };
const core::PartialDateTime release1OriginalDate{ 1993, 4, 5 };
ScopedRelease release2{ session, "MyRelease2" };
const Wt::WDate release2Date{ Wt::WDate{ 1994, 2, 3 } };
const core::PartialDateTime release2Date{ 1994, 2, 3 };
ScopedTrack track1{ session };
ScopedTrack track2{ session };
+6 -12
View File
@@ -259,8 +259,8 @@ namespace lms::db::tests
TEST_F(DatabaseFixture, Track_date)
{
ScopedTrack track{ session };
const Wt::WDate date{ 1995, 5, 5 };
const Wt::WDate originalDate{ 1994, 2, 2 };
const core::PartialDateTime date{ 1995, 5, 5 };
const core::PartialDateTime originalDate{ 1994, 2, 2 };
{
auto transaction{ session.createReadTransaction() };
EXPECT_EQ(track->getYear(), std::nullopt);
@@ -275,22 +275,16 @@ namespace lms::db::tests
{
auto transaction{ session.createReadTransaction() };
EXPECT_EQ(track->getYear(), std::nullopt);
EXPECT_EQ(track->getOriginalYear(), std::nullopt);
EXPECT_EQ(track->getYear(), 1995);
EXPECT_EQ(track->getOriginalYear(), 1994);
EXPECT_EQ(track->getDate(), date);
EXPECT_EQ(track->getOriginalDate(), originalDate);
}
{
auto transaction{ session.createWriteTransaction() };
track.get().modify()->setYear(date.year());
track.get().modify()->setOriginalYear(originalDate.year());
}
{
auto transaction{ session.createReadTransaction() };
EXPECT_EQ(track->getYear(), date.year());
EXPECT_EQ(track->getOriginalYear(), originalDate.year());
EXPECT_EQ(track->getYear(), date.getYear());
EXPECT_EQ(track->getOriginalYear(), originalDate.getYear());
}
}
+13 -33
View File
@@ -22,6 +22,7 @@
#include <span>
#include "core/ILogger.hpp"
#include "core/PartialDateTime.hpp"
#include "core/String.hpp"
#include "metadata/Exception.hpp"
@@ -360,44 +361,27 @@ namespace lms::metadata
track.recordingMBID = getTagValueAs<core::UUID>(tagReader, TagType::MusicBrainzRecordingID);
track.acoustID = getTagValueAs<core::UUID>(tagReader, TagType::AcoustID);
track.position = getTagValueAs<std::size_t>(tagReader, TagType::TrackNumber); // May parse 'Number/Total', that's fine
if (auto dateStr = getTagValueAs<std::string>(tagReader, TagType::Date))
if (const auto dateStr{ getTagValueAs<std::string>(tagReader, TagType::Date) })
{
if (const Wt::WDate date{ utils::parseDate(*dateStr) }; date.isValid())
{
if (const core::PartialDateTime date{ core::PartialDateTime::fromString(*dateStr) }; date.isValid())
track.date = date;
track.year = date.year();
}
else
{
track.year = utils::parseYear(*dateStr);
}
}
if (auto dateStr = getTagValueAs<std::string>(tagReader, TagType::OriginalReleaseDate))
if (const auto dateStr = getTagValueAs<std::string>(tagReader, TagType::OriginalReleaseDate))
{
if (const Wt::WDate date{ utils::parseDate(*dateStr) }; date.isValid())
{
if (const core::PartialDateTime date{ core::PartialDateTime::fromString(*dateStr) }; date.isValid())
track.originalDate = date;
track.originalYear = date.year();
}
else
{
track.originalYear = utils::parseYear(*dateStr);
}
}
if (auto dateStr = getTagValueAs<std::string>(tagReader, TagType::OriginalReleaseYear))
{
if (const auto dateStr{ getTagValueAs<std::string>(tagReader, TagType::OriginalReleaseYear) })
track.originalYear = utils::parseYear(*dateStr);
if (const auto encodingTimeStr{ getTagValueAs<std::string>(tagReader, TagType::EncodingTime) })
{
if (const core::PartialDateTime date{ core::PartialDateTime::fromString(*encodingTimeStr) }; date.isValid())
track.encodingTime = date;
}
track.advisory = getAdvisory(tagReader);
if (const auto encodingTime{ getTagValueAs<std::string>(tagReader, TagType::EncodingTime) })
{
if (auto dateTime{ core::stringUtils::fromISO8601String(*encodingTime) }; dateTime.isValid())
track.encodingTime = dateTime;
else if (const Wt::WDate date{ utils::parseDate(*encodingTime) }; date.isValid())
track.encodingTime = Wt::WDateTime{ date };
}
track.lyrics = getLyrics(tagReader); // no custom delimiter on lyrics
track.comments = getTagValuesAs<std::string>(tagReader, TagType::Comment, {} /* no custom delimiter on comments */);
track.copyright = getTagValueAs<std::string>(tagReader, TagType::Copyright).value_or("");
@@ -432,13 +416,9 @@ namespace lms::metadata
track.remixerArtists = getArtists(tagReader, { TagType::Remixers, TagType::Remixer }, { TagType::RemixersSortOrder, TagType::RemixerSortOrder }, {}, _artistTagDelimiters, _defaultTagDelimiters);
track.performerArtists = getPerformerArtists(tagReader); // artistDelimiters not supported
// If a file has date but no year, set it
if (!track.year && track.date.isValid())
track.year = track.date.year();
// If a file has originalDate but no originalYear, set it
if (!track.originalYear && track.originalDate.isValid())
track.originalYear = track.originalDate.year();
if (!track.originalYear)
track.originalYear = track.originalDate.getYear();
}
std::optional<Medium> Parser::getMedium(const ITagReader& tagReader)
+1
View File
@@ -18,6 +18,7 @@
*/
#include "Utils.hpp"
#include <ctime>
#include <iomanip>
#include <sstream>
+5 -8
View File
@@ -26,9 +26,7 @@
#include <string_view>
#include <vector>
#include <Wt/WDate.h>
#include <Wt/WDateTime.h>
#include "core/PartialDateTime.hpp"
#include "core/UUID.hpp"
#include "Lyrics.hpp"
@@ -120,12 +118,11 @@ namespace lms::metadata
std::vector<std::string> moods;
std::vector<std::string> languages;
Tags userExtraTags;
std::optional<int> year{};
Wt::WDate date;
std::optional<int> originalYear{};
Wt::WDate originalDate;
core::PartialDateTime date;
std::optional<int> originalYear;
core::PartialDateTime originalDate;
std::optional<Advisory> advisory;
Wt::WDateTime encodingTime;
core::PartialDateTime encodingTime;
bool hasCover{};
std::optional<core::UUID> acoustID;
std::string copyright;
+38 -14
View File
@@ -125,9 +125,9 @@ namespace lms::metadata
EXPECT_EQ(track->copyright, "MyCopyright");
EXPECT_EQ(track->copyrightURL, "MyCopyrightURL");
ASSERT_TRUE(track->date.isValid());
EXPECT_EQ(track->date.year(), 2020);
EXPECT_EQ(track->date.month(), 3);
EXPECT_EQ(track->date.day(), 4);
EXPECT_EQ(track->date.getYear(), 2020);
EXPECT_EQ(track->date.getMonth(), 3);
EXPECT_EQ(track->date.getDay(), 4);
EXPECT_FALSE(track->hasCover);
ASSERT_EQ(track->genres.size(), 2);
EXPECT_EQ(track->genres[0], "Genre1");
@@ -157,9 +157,9 @@ namespace lms::metadata
EXPECT_EQ(track->moods[0], "Mood1");
EXPECT_EQ(track->moods[1], "Mood2");
ASSERT_TRUE(track->originalDate.isValid());
EXPECT_EQ(track->originalDate.year(), 2019);
EXPECT_EQ(track->originalDate.month(), 2);
EXPECT_EQ(track->originalDate.day(), 3);
EXPECT_EQ(track->originalDate.getYear(), 2019);
EXPECT_EQ(track->originalDate.getMonth(), 2);
EXPECT_EQ(track->originalDate.getDay(), 3);
ASSERT_TRUE(track->originalYear.has_value());
EXPECT_EQ(track->originalYear.value(), 2019);
ASSERT_TRUE(track->performerArtists.contains("Rolea"));
@@ -188,8 +188,6 @@ namespace lms::metadata
ASSERT_EQ(track->userExtraTags["MY_AWESOME_TAG_B"].size(), 2);
EXPECT_EQ(track->userExtraTags["MY_AWESOME_TAG_B"][0], "MyTagValue1ForTagB");
EXPECT_EQ(track->userExtraTags["MY_AWESOME_TAG_B"][1], "MyTagValue2ForTagB");
ASSERT_TRUE(track->year.has_value());
EXPECT_EQ(track->year.value(), 2020);
// Medium
ASSERT_TRUE(track->medium.has_value());
@@ -615,7 +613,7 @@ namespace lms::metadata
TEST(Parser, encodingTime)
{
auto doTest = [](std::string_view value, Wt::WDateTime expectedValue) {
auto doTest = [](std::string_view value, core::PartialDateTime expectedValue) {
const TestTagReader testTags{
{
{ TagType::EncodingTime, { value } },
@@ -628,10 +626,36 @@ namespace lms::metadata
ASSERT_EQ(track->encodingTime, expectedValue) << "Value = '" << value << "'";
};
doTest("", Wt::WDateTime{});
doTest("foo", Wt::WDateTime{});
doTest("2020-01-03T09:08:11.075", Wt::WDateTime{ Wt::WDate{ 2020, 01, 03 }, Wt::WTime{ 9, 8, 11, 75 } });
doTest("2020-01-03", Wt::WDateTime{ Wt::WDate{ 2020, 01, 03 } });
doTest("2020/01/03", Wt::WDateTime{ Wt::WDate{ 2020, 01, 03 } });
doTest("", core::PartialDateTime{});
doTest("foo", core::PartialDateTime{});
doTest("2020-01-03T09:08:11.075", core::PartialDateTime{ 2020, 01, 03, 9, 8, 11 });
doTest("2020-01-03", core::PartialDateTime{ 2020, 01, 03 });
doTest("2020/01/03", core::PartialDateTime{ 2020, 01, 03 });
}
TEST(Parser, date)
{
auto doTest = [](std::string_view value, core::PartialDateTime expectedValue) {
const TestTagReader testTags{
{
{ TagType::Date, { value } },
}
};
Parser parser;
std::unique_ptr<Track> track{ Parser{}.parse(testTags) };
ASSERT_EQ(track->date, expectedValue) << "Value = '" << value << "'";
};
doTest("", core::PartialDateTime{});
doTest("foo", core::PartialDateTime{});
doTest("2020-01-03", core::PartialDateTime{ 2020, 01, 03 });
doTest("2020-01", core::PartialDateTime{ 2020, 1 });
doTest("2020", core::PartialDateTime{ 2020 });
doTest("2020/01/03", core::PartialDateTime{ 2020, 01, 03 });
doTest("2020/01", core::PartialDateTime{ 2020, 1 });
doTest("2020", core::PartialDateTime{ 2020 });
}
} // namespace lms::metadata
@@ -22,6 +22,7 @@
#include "core/IConfig.hpp"
#include "core/ILogger.hpp"
#include "core/ITraceLogger.hpp"
#include "core/PartialDateTime.hpp"
#include "core/Path.hpp"
#include "core/Service.hpp"
#include "database/Artist.hpp"
@@ -479,7 +480,17 @@ namespace lms::scanner
{
track = dbSession.create<db::Track>();
track.modify()->setAbsoluteFilePath(_file);
track.modify()->setAddedTime(fileInfo->lastWriteTime); // may be erased by encodingTime
const core::PartialDateTime addedTime{
fileInfo->lastWriteTime.date().year(),
static_cast<unsigned>(fileInfo->lastWriteTime.date().month()),
static_cast<unsigned>(fileInfo->lastWriteTime.date().day()),
static_cast<unsigned>(fileInfo->lastWriteTime.time().hour()),
static_cast<unsigned>(fileInfo->lastWriteTime.time().minute()),
static_cast<unsigned>(fileInfo->lastWriteTime.time().second())
};
track.modify()->setAddedTime(addedTime); // may be erased by encodingTime
added = true;
}
@@ -555,18 +566,14 @@ namespace lms::scanner
track.modify()->setTrackNumber(_parsedTrack->position);
track.modify()->setDiscNumber(_parsedTrack->medium ? _parsedTrack->medium->position : std::nullopt);
track.modify()->setDate(_parsedTrack->date);
track.modify()->setYear(_parsedTrack->year);
track.modify()->setOriginalDate(_parsedTrack->originalDate);
track.modify()->setOriginalYear(_parsedTrack->originalYear);
if (!track->getOriginalDate().isValid() && _parsedTrack->originalYear)
track.modify()->setOriginalDate(core::PartialDateTime{ *_parsedTrack->originalYear });
// If a file has an OriginalDate but no date, set it to ease filtering
if (!_parsedTrack->date.isValid() && _parsedTrack->originalDate.isValid())
track.modify()->setDate(_parsedTrack->originalDate);
// If a file has an OriginalYear but no Year, set it to ease filtering
if (!_parsedTrack->year && _parsedTrack->originalYear)
track.modify()->setYear(_parsedTrack->originalYear);
track.modify()->setRecordingMBID(_parsedTrack->recordingMBID);
track.modify()->setTrackMBID(_parsedTrack->mbid);
if (auto trackFeatures{ db::TrackFeatures::find(dbSession, track->getId()) })
@@ -25,6 +25,7 @@
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/Types.hpp"
#include "database/User.hpp"
#include "services/feedback/IFeedbackService.hpp"
#include "services/scrobbling/IScrobblingService.hpp"
@@ -106,7 +107,7 @@ namespace lms::api::subsonic
Release::FindParameters params;
params.setSortMethod(fromYear > toYear ? ReleaseSortMethod::DateDesc : ReleaseSortMethod::DateAsc);
params.setRange(range);
params.setDateRange(DateRange::fromYearRange(std::min(fromYear, toYear), std::max(fromYear, toYear)));
params.setDateRange(YearRange{ std::min(fromYear, toYear), std::max(fromYear, toYear) });
params.setMediaLibrary(mediaLibraryId);
releases = Release::findIds(context.dbSession, params);
+1 -1
View File
@@ -204,7 +204,7 @@ namespace lms::api::subsonic
albumNode.createEmptyArrayChild("artists");
albumNode.setAttribute("displayArtist", "");
}
albumNode.addChild("originalReleaseDate", createItemDateNode(release->getOriginalDate(), release->getOriginalYear()));
albumNode.addChild("originalReleaseDate", createItemDateNode(release->getOriginalDate()));
albumNode.setAttribute("isCompilation", release->isCompilation());
+6 -10
View File
@@ -23,20 +23,16 @@
namespace lms::api::subsonic
{
Response::Node createItemDateNode(const Wt::WDate& date, std::optional<int> year)
Response::Node createItemDateNode(const core::PartialDateTime& date)
{
Response::Node itemDateNode;
if (date.isValid())
{
itemDateNode.setAttribute("year", date.year());
itemDateNode.setAttribute("month", date.month());
itemDateNode.setAttribute("day", date.day());
}
else if (year)
{
if (auto year{ date.getYear() })
itemDateNode.setAttribute("year", *year);
}
if (auto month{ date.getMonth() })
itemDateNode.setAttribute("month", *month);
if (auto day{ date.getDay() })
itemDateNode.setAttribute("day", *day);
return itemDateNode;
}
@@ -19,11 +19,11 @@
#pragma once
#include <Wt/WDate.h>
#include "core/PartialDateTime.hpp"
#include "SubsonicResponse.hpp"
namespace lms::api::subsonic
{
Response::Node createItemDateNode(const Wt::WDate& date, std::optional<int> year);
Response::Node createItemDateNode(const core::PartialDateTime& date);
}
+3 -1
View File
@@ -174,7 +174,9 @@ namespace lms::ui::releaseHelpers
{
Wt::WString res;
// Year can be here, but originalYear can't be here without year (enforced by scanner)
// Year could be here, but originalYear can't be here without year (enforced by scanner)
assert(year || !originalYear);
if (!year)
return res;
+3 -5
View File
@@ -235,12 +235,10 @@ namespace lms::metadata
std::cout << "Position: " << *track->position << std::endl;
if (track->date.isValid())
std::cout << "Date: " << track->date.toString("yyyy-MM-dd") << std::endl;
if (track->year)
std::cout << "Year: " << *track->year << std::endl;
std::cout << "Date: " << track->date.toISO8601String() << std::endl;
if (track->originalDate.isValid())
std::cout << "Original date: " << track->originalDate.toString("yyyy-MM-dd") << std::endl;
std::cout << "Original date: " << track->originalDate.toISO8601String() << std::endl;
if (track->originalYear)
std::cout << "Original year: " << *track->originalYear << std::endl;
@@ -269,7 +267,7 @@ namespace lms::metadata
std::cout << "Advisory: " << *track->advisory << std::endl;
if (track->encodingTime.isValid())
std::cout << "Encoding time: " << core::stringUtils::toISO8601String(track->encodingTime) << std::endl;
std::cout << "Encoding time: " << track->encodingTime.toISO8601String() << std::endl;
if (track->medium)
std::cout << "Medium: " << *track->medium;