--- DecentralizedMedia/Library.cpp 2005/07/04 05:13:07 546 +++ DecentralizedMedia/Library.cpp 2005/07/11 10:24:59 556 @@ -4,68 +4,176 @@ // // $Id$ -#include +#include -#include +#include #include "Library.hpp" -Library::Library(const _R& connection) : connection(connection) +Library::Library(const _R& connector) : connector(connector) { + _R connection(connector->Connect()); + connection->Execute(_B("UPDATE files SET live = FALSE")); + + connector->Release(connection); } -_L Library::GetArtists() const +_L Library::GetFolders(unsigned page) const { - _R artists_(connection->Execute(_B("SELECT DISTINCT artist FROM files WHERE live = TRUE"))); - _L artists; + _R connection(connector->Connect()); + _R roots(connection->Parse(_B("SELECT DISTINCT root FROM files WHERE live = TRUE ORDER By root LIMIT 50 OFFSET ?"))->Execute(page * 50)); + _L folders; - while (artists_->MoveNext()) - artists.InsertLast(artists_->GetString(_B("artist"))); + while (roots->MoveNext()) + folders.InsertLast(GetFolder(roots->GetString(_B("root")))); - return artists; + connector->Release(connection); + + return folders; } -_L Library::GetTitles() const +unsigned Library::GetFoldersPages() const { - _R titles_(connection->Execute(_B("SELECT DISTINCT title FROM files WHERE live = TRUE"))); - _L titles; + _R connection(connector->Connect()); + _R count(connection->Execute(_B("SELECT count(DISTINCT root) FROM files WHERE live = TRUE"))); + unsigned pages(0); + + if (count->MoveNext()) + pages = (count->Get(_B("count")) + 49) / 50; - while (titles_->MoveNext()) - titles.InsertLast(titles_->GetString(_B("title"))); + connector->Release(connection); - return titles; + return pages; } -_L Library::GetAlbums() const +_L Library::GetFiles(const _L& bys, const _L& items, unsigned page) const { - _R albums_(connection->Execute(_B("SELECT DISTINCT album FROM files WHERE live = TRUE"))); - _L albums; + if (bys.IsEmpty() || ext::Contains(bys, By::LAST) || bys.GetSize() != items.GetSize()) + return _L(); + + _S statement_; + + statement_ << _B("SELECT path FROM files WHERE live = TRUE "); + + _foreach (const _L, by, bys) + statement_ << _B("AND ") << *by << _B(" = ? "); + + statement_ << _B("ORDER BY path LIMIT 50 OFFSET ?"); + + _R connection(connector->Connect()); + _R statement(connection->Parse(statement_)); + + _foreach (const _L, item, items) + statement->Set(_index, *item); - while (albums_->MoveNext()) - albums.InsertLast(albums_->GetString(_B("album"))); + statement->Set(items.GetSize(), page * 50); - return albums; + _R paths(statement->Execute()); + _L files; + + while (paths->MoveNext()) + files.InsertLast(GetFile(paths->GetString(_B("path")))); + + connector->Release(connection); + + return files; } -_L Library::GetGenres() const +unsigned Library::GetFilesPages(const _L& bys, const _L& items) const { - _R genres_(connection->Execute(_B("SELECT DISTINCT album FROM files WHERE live = TRUE"))); - _L genres; + if (bys.IsEmpty() || ext::Contains(bys, By::LAST) || bys.GetSize() != items.GetSize()) + return 0; + + _S statement_; + + statement_ << _B("SELECT count(path) FROM files WHERE live = TRUE "); + + _foreach (const _L, by, bys) + statement_ << _B("AND ") << *by << _B(" = ?"); - while (genres_->MoveNext()) - genres.InsertLast(genres_->GetString(_B("genre"))); + _R connection(connector->Connect()); + _R statement(connection->Parse(statement_)); - return genres; + _foreach (const _L, item, items) + statement->Set(_index, *item); + + _R count(statement->Execute()); + unsigned pages(0); + + if (count->MoveNext()) + pages = (count->Get(_B("count")) + 49) / 50; + + connector->Release(connection); + + return pages; } -_L Library::GetFolders() const +_L Library::GetItems(_L bys, _L items, unsigned page) const { - _R roots(connection->Execute(_B("SELECT DISTINCT root FROM files WHERE live = TRUE"))); - _L folders; + if (bys.IsEmpty() || ext::Contains(bys, By::LAST) || bys.GetSize() - 1 != items.GetSize()) + return _L(); - while (roots->MoveNext()) - folders.InsertLast(MediaFolder(connection, roots->GetString(_B("root")))); + _S statement_; + By by(bys.Last()); - return folders; + statement_ << _B("SELECT DISTINCT ") << by << _B(" FROM files WHERE live = TRUE "); + + bys.RemoveLast(); + + _foreach (const _L, by_, bys) + statement_ << _B("AND ") << *by_ << _B(" = ? "); + + statement_ << _B("ORDER BY ") << by << _B(" LIMIT 50 OFFSET ?"); + + _R connection(connector->Connect()); + _R statement(connection->Parse(statement_)); + + _foreach (const _L, item, items) + statement->Set(_index, *item); + + statement->Set(items.GetSize(), page * 50); + + _R items_(statement->Execute()); + + items.Clear(); + + while (items_->MoveNext()) + items.InsertLast(items_->GetString(lexical_cast(by))); + + connector->Release(connection); + + return items; +} + +unsigned Library::GetItemsPages(_L bys, const _L& items) const +{ + if (bys.IsEmpty() || ext::Contains(bys, By::LAST) || bys.GetSize() - 1 != items.GetSize()) + return 0; + + _S statement_; + By by(bys.Last()); + + statement_ << _B("SELECT count(DISTINCT ") << by << _B(") FROM files WHERE live = TRUE"); + + bys.RemoveLast(); + + _foreach (const _L, by_, bys) + statement_ << _B(" AND ") << *by_ << _B(" = ?"); + + _R connection(connector->Connect()); + _R statement(connection->Parse(statement_)); + + _foreach (const _L, item, items) + statement->Set(_index, *item); + + _R count(statement->Execute()); + unsigned pages(0); + + if (count->MoveNext()) + pages = (count->Get(_B("count")) + 49) / 50; + + connector->Release(connection); + + return pages; }