ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/DecentralizedMedia/Library.cpp
(Generate patch)

Comparing DecentralizedMedia/Library.cpp (file contents):
Revision 549 by douglas, 2005-07-04T21:28:02-07:00 vs.
Revision 555 by douglas, 2005-07-10T21:18:35-07:00

# Line 4 | Line 4
4   //
5   // $Id$
6  
7 < #include <menes/standard.hh>
7 > #include <menes/c++/standard.hh>
8  
9   #include <menes/dbi/resultset.hpp>
10  
11   #include "Library.hpp"
12  
13 < Library::Library(const _R<dbi::Connection>& connection) : connection(connection)
13 > Library::Library(const _R<Connector>& connector) : connector(connector)
14   {
15 +        _R<dbi::Connection> connection(connector->Connect());
16 +
17          connection->Execute(_B("UPDATE files SET live = FALSE"));
18 +
19 +        connector->Release(connection);
20   }
21  
22 < _L<cse::String> Library::GetArtists() const
22 > _L<MediaFolder> Library::GetFolders(unsigned page) const
23   {
24 <        _R<dbi::ResultSet> artists_(connection->Execute(_B("SELECT DISTINCT artist FROM files WHERE live = TRUE")));
25 <        _L<cse::String> artists;
24 >        _R<dbi::Connection> connection(connector->Connect());
25 >        _R<dbi::ResultSet> roots(connection->Parse(_B("SELECT DISTINCT root FROM files WHERE live = TRUE ORDER By root LIMIT 50 OFFSET ?"))->Execute(page * 50));
26 >        _L<MediaFolder> folders;
27  
28 <        while (artists_->MoveNext())
29 <                artists.InsertLast(artists_->GetString(_B("artist")));
28 >        while (roots->MoveNext())
29 >                folders.InsertLast(GetFolder(roots->GetString(_B("root"))));
30  
31 <        return artists;
31 >        connector->Release(connection);
32 >
33 >        return folders;
34   }
35  
36 < _L<cse::String> Library::GetTitles() const
36 > _L<MediaFile> Library::GetFiles(const _L<By>& bys, const _L<cse::String>& items, unsigned page) const
37   {
38 <        _R<dbi::ResultSet> titles_(connection->Execute(_B("SELECT DISTINCT title FROM files WHERE live = TRUE")));
39 <        _L<cse::String> titles;
38 >        if (bys.IsEmpty() || ext::Contains(bys, By::LAST) || bys.GetSize() != items.GetSize())
39 >                return _L<MediaFile>();
40  
41 <        while (titles_->MoveNext())
35 <                titles.InsertLast(titles_->GetString(_B("title")));
41 >        _S<ios::String> statement_;
42  
43 <        return titles;
38 < }
43 >        statement_ << _B("SELECT path FROM files WHERE live = TRUE ");
44  
45 < _L<cse::String> Library::GetAlbums() const
46 < {
42 <        _R<dbi::ResultSet> albums_(connection->Execute(_B("SELECT DISTINCT album FROM files WHERE live = TRUE")));
43 <        _L<cse::String> albums;
45 >        _foreach (const _L<By>, by, bys)
46 >                statement_ << _B("AND ") << *by << _B(" = ? ");
47  
48 <        while (albums_->MoveNext())
46 <                albums.InsertLast(albums_->GetString(_B("album")));
48 >        statement_ << _B("ORDER BY path LIMIT 50 OFFSET ?");
49  
50 <        return albums;
51 < }
50 >        _R<dbi::Connection> connection(connector->Connect());
51 >        _R<dbi::Statement> statement(connection->Parse(statement_));
52  
53 < _L<cse::String> Library::GetGenres() const
54 < {
55 <        _R<dbi::ResultSet> genres_(connection->Execute(_B("SELECT DISTINCT album FROM files WHERE live = TRUE")));
56 <        _L<cse::String> genres;
53 >        _foreach (const _L<cse::String>, item, items)
54 >                statement->Set(_index, *item);
55 >
56 >        statement->Set(items.GetSize(), page * 50);
57 >
58 >        _R<dbi::ResultSet> paths(statement->Execute());
59 >        _L<MediaFile> files;
60 >
61 >        while (paths->MoveNext())
62 >                files.InsertLast(GetFile(paths->GetString(_B("path"))));
63  
64 <        while (genres_->MoveNext())
57 <                genres.InsertLast(genres_->GetString(_B("genre")));
64 >        connector->Release(connection);
65  
66 <        return genres;
66 >        return files;
67   }
68  
69 < _L<MediaFolder> Library::GetFolders() const
69 > _L<cse::String> Library::GetItems(_L<By> bys, _L<cse::String> items, unsigned page) const
70   {
71 <        _R<dbi::ResultSet> roots(connection->Execute(_B("SELECT DISTINCT root FROM files WHERE live = TRUE")));
72 <        _L<MediaFolder> folders;
71 >        if (bys.IsEmpty() || ext::Contains(bys, By::LAST) || bys.GetSize() - 1 != items.GetSize())
72 >                return _L<cse::String>();
73  
74 <        while (roots->MoveNext())
75 <                folders.InsertLast(MediaFolder(connection, roots->GetString(_B("root"))));
74 >        _S<ios::String> statement_;
75 >        By by(bys.Last());
76  
77 <        return folders;
77 >        statement_ << _B("SELECT DISTINCT ") << by << _B(" FROM files WHERE live = TRUE ");
78 >
79 >        bys.RemoveLast();
80 >
81 >        _foreach (const _L<By>, by_, bys)
82 >                statement_ << _B("AND ") << *by_ << _B(" = ? ");
83 >
84 >        statement_ << _B("ORDER BY ") << by << _B(" LIMIT 50 OFFSET ?");
85 >
86 >        _R<dbi::Connection> connection(connector->Connect());
87 >        _R<dbi::Statement> statement(connection->Parse(statement_));
88 >
89 >        _foreach (const _L<cse::String>, item, items)
90 >                statement->Set(_index, *item);
91 >
92 >        statement->Set(items.GetSize(), page * 50);
93 >
94 >        _R<dbi::ResultSet> items_(statement->Execute());
95 >        
96 >        items.Clear();
97 >
98 >        while (items_->MoveNext())
99 >                items.InsertLast(items_->GetString(lexical_cast<cse::String>(by)));
100 >
101 >        connector->Release(connection);
102 >
103 >        return items;
104   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines