Commit 5e5bb34e authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Fix some remaining GROUP BY count requests

parent bfe8fef5
......@@ -566,9 +566,10 @@ Query<IAlbum> Album::fromGenre( MediaLibraryPtr ml, int64_t genreId, const Query
{
std::string req = "FROM " + Table::Name + " alb "
"INNER JOIN " + AlbumTrack::Table::Name + " att ON att.album_id = alb.id_album "
"WHERE att.genre_id = ? GROUP BY att.album_id";
"WHERE att.genre_id = ?";
std::string groupAndOrderBy = "GROUP BY att.album_id" + orderBy( params );
return make_query<Album, IAlbum>( ml, "alb.*", std::move( req ),
orderBy( params ), genreId );
std::move( groupAndOrderBy ), genreId );
}
Query<IAlbum> Album::searchFromGenre( MediaLibraryPtr ml, const std::string& pattern,
......@@ -579,9 +580,11 @@ Query<IAlbum> Album::searchFromGenre( MediaLibraryPtr ml, const std::string& pat
"WHERE id_album IN "
"(SELECT rowid FROM " + Table::Name + "Fts WHERE " +
Table::Name + "Fts MATCH '*' || ? || '*')"
"AND att.genre_id = ? GROUP BY att.album_id";
"AND att.genre_id = ?";
std::string groupAndOrderBy = "GROUP BY att.album_id" + orderBy( params );
return make_query<Album, IAlbum>( ml, "alb.*", std::move( req ),
orderBy( params ), pattern, genreId );
std::move( groupAndOrderBy ), pattern,
genreId );
}
Query<IAlbum> Album::listAll( MediaLibraryPtr ml, const QueryParameters* params )
......
......@@ -79,12 +79,12 @@ Query<IArtist> Genre::artists( const QueryParameters* params ) const
{
std::string req = "FROM " + Artist::Table::Name + " a "
"INNER JOIN " + AlbumTrack::Table::Name + " att ON att.artist_id = a.id_artist "
"WHERE att.genre_id = ? GROUP BY att.artist_id";
std::string orderBy = "ORDER BY a.name";
"WHERE att.genre_id = ?";
std::string groupAndOrderBy = "GROUP BY att.artist_id ORDER BY a.name";
if ( params != nullptr && params->desc == true )
orderBy += " DESC";
groupAndOrderBy += " DESC";
return make_query<Artist, IArtist>( m_ml, "a.*", std::move( req ),
std::move( orderBy ), m_id );
std::move( groupAndOrderBy ), m_id );
}
Query<IArtist> Genre::searchArtists( const std::string& pattern,
......
......@@ -99,7 +99,9 @@ TEST_F( Genres, ListArtists )
auto track = album2->addTrack( m, i, 1, a2->id(), nullptr );
track->setGenre( g );
}
artists = g->artists( nullptr )->all();
auto query = g->artists( nullptr );
ASSERT_EQ( 2u, query->count() );
artists = query->all();
ASSERT_EQ( 2u, artists.size() );
}
......@@ -128,15 +130,18 @@ TEST_F( Genres, ListAlbums )
auto genres = ml->genres( nullptr )->all();
for ( auto& genre : genres )
{
auto albums = genre->albums( nullptr )->all();
auto query = genre->albums( nullptr );
auto albums = query->all();
if ( genre->id() == g->id() )
{
// Initial genre with 2 albums:
ASSERT_EQ( 2u, query->count() );
ASSERT_EQ( 2u, albums.size() );
}
else
{
ASSERT_EQ( 1u, query->count() );
ASSERT_EQ( 1u, albums.size() );
ASSERT_EQ( album->id(), albums[0]->id() );
}
......@@ -310,19 +315,20 @@ TEST_F( Genres, SearchAlbums )
auto t2 = a2->addTrack( m2, 1, 1, 0, nullptr );
m2->save();
auto a3 = ml->createAlbum( "another album" );
auto m3 = std::static_pointer_cast<Media>( ml->addMedia( "track3.mp3" ) );
m3->setType( IMedia::Type::Audio );
auto t3 = a1->addTrack( m3, 2, 1, 0, g.get() );
auto t3 = a3->addTrack( m3, 1, 1, 0, g.get() );
m3->save();
auto query = ml->searchAlbums( "album", nullptr);
ASSERT_EQ( 2u, query->count() );
ASSERT_EQ( 3u, query->count() );
auto albums = query->all();
ASSERT_EQ( 2u, albums.size() );
ASSERT_EQ( 3u, albums.size() );
query = g->searchAlbums( "album", nullptr );
ASSERT_EQ( 1u, query->count() );
ASSERT_EQ( 2u, query->count() );
albums = query->all();
ASSERT_EQ( 1u, albums.size() );
ASSERT_EQ( 2u, albums.size() );
ASSERT_EQ( a1->id(), albums[0]->id() );
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment