Added CategoryTrackListQuery serialization

This commit is contained in:
casey langen 2020-10-04 15:10:25 -07:00
parent e467e28513
commit dd5cd54e33
4 changed files with 92 additions and 25 deletions

View File

@ -3,7 +3,7 @@
[ ] AppendPlaylistQuery
[ ] GetPlaylistQuery
[x] CategoryListQuery
[ ] CategoryTrackListQuery
[x] CategoryTrackListQuery
[ ] DeletePlaylistQuery
[ ] DirectoryTrackListQuery
[x] LyricsQuery

View File

@ -40,7 +40,7 @@
//#include <core/library/query/AppendPlaylistQuery.h>
//#include <core/library/query/GetPlaylistQuery.h>
#include <core/library/query/CategoryListQuery.h>
//#include <core/library/query/CategoryTrackListQuery.h>
#include <core/library/query/CategoryTrackListQuery.h>
//#include <core/library/query/DeletePlaylistQuery.h>
//#include <core/library/query/DirectoryTrackListQuery.h>
#include <core/library/query/LyricsQuery.h>
@ -74,8 +74,9 @@ namespace musik { namespace core { namespace library {
if (name == CategoryListQuery::kQueryName) {
return CategoryListQuery::DeserializeQuery(data);
}
//if (name == CategoryTrackListQuery::kQueryName) {
//}
if (name == CategoryTrackListQuery::kQueryName) {
return CategoryTrackListQuery::DeserializeQuery(library, data);
}
//if (name == DeletePlaylistQuery::kQueryName) {
//}
//if (name == DirectoryTrackListQuery::kQueryName) {

View File

@ -39,10 +39,13 @@
#include <core/library/track/LibraryTrack.h>
#include <core/library/LocalLibraryConstants.h>
#include <core/library/query/util/Serialization.h>
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/algorithm/string.hpp>
#include <json.hpp>
using musik::core::db::Statement;
using musik::core::db::Row;
using musik::core::TrackPtr;
@ -51,9 +54,12 @@ using musik::core::ILibraryPtr;
using namespace musik::core::db;
using namespace musik::core::library::query;
using namespace musik::core::library::query::serialization;
using namespace musik::core::library::constants;
using namespace boost::algorithm;
const std::string CategoryTrackListQuery::kQueryName = "CategoryTrackListQuery";
CategoryTrackListQuery::CategoryTrackListQuery(
musik::core::ILibraryPtr library,
const std::string& filter,
@ -91,18 +97,15 @@ CategoryTrackListQuery::CategoryTrackListQuery(
this->result.reset(new musik::core::TrackList(library));
this->headers.reset(new std::set<size_t>());
this->hash = category::Hash(predicates);
this->sortType = sortType;
category::SplitPredicates(predicates, this->regular, this->extended);
if (filter.size()) {
this->filter = "%" + trim_copy(to_lower_copy(filter)) + "%";
}
if (predicates.size() == 1 && predicates[0].first == Playlists::TABLE_NAME) {
this->type = Playlist;
this->type = Type::Playlist;
}
else {
this->type = Regular;
this->type = Type::Regular;
}
this->orderBy = "ORDER BY " + kTrackListSortOrderBy.find(sortType)->second;
@ -141,20 +144,21 @@ void CategoryTrackListQuery::RegularQuery(musik::core::db::Connection &db) {
std::string query = category::CATEGORY_TRACKLIST_QUERY;
std::string extended = InnerJoinExtended(this->extended, args);
std::string regular = JoinRegular(this->regular, args, " AND ");
std::string trackFilter;
std::string trackFilterClause, trackFilterValue;
std::string limitAndOffset = this->GetLimitAndOffset();
if (this->filter.size()) {
trackFilter = category::CATEGORY_TRACKLIST_FILTER;
args.push_back(category::StringArgument(this->filter));
args.push_back(category::StringArgument(this->filter));
args.push_back(category::StringArgument(this->filter));
args.push_back(category::StringArgument(this->filter));
trackFilterValue = "%" + trim_copy(to_lower_copy(filter)) + "%";
trackFilterClause = category::CATEGORY_TRACKLIST_FILTER;
args.push_back(category::StringArgument(trackFilterValue));
args.push_back(category::StringArgument(trackFilterValue));
args.push_back(category::StringArgument(trackFilterValue));
args.push_back(category::StringArgument(trackFilterValue));
}
category::ReplaceAll(query, "{{extended_predicates}}", extended);
category::ReplaceAll(query, "{{regular_predicates}}", regular);
category::ReplaceAll(query, "{{tracklist_filter}}", trackFilter);
category::ReplaceAll(query, "{{tracklist_filter}}", trackFilterClause);
category::ReplaceAll(query, "{{order_by}}", this->orderBy);
category::ReplaceAll(query, "{{limit_and_offset}}", limitAndOffset);
@ -188,9 +192,56 @@ bool CategoryTrackListQuery::OnRun(Connection& db) {
}
switch (this->type) {
case Playlist: this->PlaylistQuery(db); break;
case Regular: this->RegularQuery(db); break;
case Type::Playlist: this->PlaylistQuery(db); break;
case Type::Regular: this->RegularQuery(db); break;
}
return true;
}
/* ISerializableQuery */
std::string CategoryTrackListQuery::SerializeQuery() {
nlohmann::json output = {
{ "name", kQueryName },
{ "options", {
{ "filter", filter },
{ "regularPredicateList", PredicateListToJson(regular) },
{ "extendedPredicateList", PredicateListToJson(extended) },
{ "sortType", sortType }
}}
};
return output.dump();
}
std::string CategoryTrackListQuery::SerializeResult() {
nlohmann::json output = {
{ "result", {
{ "headers", *this->headers },
{ "trackList", TrackListToJson(*this->result, true) }
}}
};
return output.dump();
}
void CategoryTrackListQuery::DeserializeResult(const std::string& data) {
this->SetStatus(IQuery::Failed);
nlohmann::json result = nlohmann::json::parse(data)["result"];
this->result = std::make_shared<TrackList>(this->library);
TrackListFromJson(result["trackList"], *this->result, this->library);
JsonArrayToSet<std::set<size_t>, size_t>(result["headers"], *this->headers);
this->SetStatus(IQuery::Finished);
}
std::shared_ptr<CategoryTrackListQuery> CategoryTrackListQuery::DeserializeQuery(
musik::core::ILibraryPtr library, const std::string& data)
{
nlohmann::json options = nlohmann::json::parse(data)["options"];
auto result = std::make_shared<CategoryTrackListQuery>(
library,
options["filter"].get<std::string>(),
options["sortType"].get<TrackSortType>());
PredicateListFromJson(options["regularPredicateList"], result->regular);
PredicateListFromJson(options["extendedPredicateList"], result->extended);
return result;
}

View File

@ -47,6 +47,8 @@ namespace musik { namespace core { namespace library { namespace query {
class CategoryTrackListQuery : public TrackListQueryBase {
public:
static const std::string kQueryName;
CategoryTrackListQuery(
musik::core::ILibraryPtr library,
const std::string& filter = "",
@ -73,31 +75,44 @@ namespace musik { namespace core { namespace library { namespace query {
virtual ~CategoryTrackListQuery();
virtual std::string Name() { return "CategoryTrackListQuery"; }
virtual std::string Name() { return kQueryName; }
virtual Result GetResult();
virtual Headers GetHeaders();
virtual size_t GetQueryHash();
/* ISerializableQuery */
virtual std::string SerializeQuery();
virtual std::string SerializeResult();
virtual void DeserializeResult(const std::string& data);
static std::shared_ptr<CategoryTrackListQuery> DeserializeQuery(
musik::core::ILibraryPtr library, const std::string& data);
protected:
virtual bool OnRun(musik::core::db::Connection &db);
private:
enum Type { Playlist, Regular };
enum class Type: int { Playlist = 0, Regular = 1 };
void PlaylistQuery(musik::core::db::Connection &db);
void RegularQuery(musik::core::db::Connection &db);
void ProcessResult(musik::core::db::Statement& stmt);
/* regular instance variables */
musik::core::ILibraryPtr library;
bool parseHeaders;
Result result;
Headers headers;
Type type;
category::PredicateList regular, extended;
size_t hash;
std::string orderBy;
Type type;
/* serialized result fields */
Result result;
Headers headers;
/* serialized query fields */
std::string filter;
category::PredicateList regular, extended;
TrackSortType sortType;
};
} } } }