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 550 by douglas, 2005-07-04T23:37:40-07:00 vs.
Revision 558 by douglas, 2005-07-17T00:50:52-07:00

# Line 4 | Line 4
4   //
5   // $Id$
6  
7 < #include <menes/standard.hh>
7 > #include <menes/cxx/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 ORDER BY artist")));
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 > unsigned Library::GetFoldersPages() const
37   {
38 <        _R<dbi::ResultSet> titles_(connection->Execute(_B("SELECT DISTINCT title FROM files WHERE live = TRUE ORDER BY title")));
39 <        _L<cse::String> titles;
38 >        _R<dbi::Connection> connection(connector->Connect());
39 >        _R<dbi::ResultSet> count(connection->Execute(_B("SELECT count(DISTINCT root) FROM files WHERE live = TRUE")));
40 >        unsigned pages(0);
41 >
42 >        if (count->MoveNext())
43 >                pages = (count->Get<unsigned>(_B("count")) + 49) / 50;
44  
45 <        while (titles_->MoveNext())
35 <                titles.InsertLast(titles_->GetString(_B("title")));
45 >        connector->Release(connection);
46  
47 <        return titles;
47 >        return pages;
48   }
49  
50 < _L<cse::String> Library::GetAlbums() const
50 > _L<MediaFile> Library::GetFiles(const _L<By>& bys, const _L<cse::String>& items, unsigned page) const
51   {
52 <        _R<dbi::ResultSet> albums_(connection->Execute(_B("SELECT DISTINCT album FROM files WHERE live = TRUE ORDER BY album")));
53 <        _L<cse::String> albums;
52 >        if (bys.IsEmpty() || ext::Contains(bys, By::LAST) || bys.GetSize() != items.GetSize())
53 >                return _L<MediaFile>();
54 >
55 >        _S<ios::String> statement_;
56 >
57 >        statement_ << _B("SELECT path FROM files WHERE live = TRUE ");
58 >
59 >        _foreach (const _L<By>, by, bys)
60 >                statement_ << _B("AND ") << *by << _B(" = ? ");
61 >
62 >        statement_ << _B("ORDER BY path LIMIT 50 OFFSET ?");
63 >
64 >        _R<dbi::Connection> connection(connector->Connect());
65 >        _R<dbi::Statement> statement(connection->Parse(statement_));
66 >
67 >        _foreach (const _L<cse::String>, item, items)
68 >                statement->Set(_index, *item);
69  
70 <        while (albums_->MoveNext())
46 <                albums.InsertLast(albums_->GetString(_B("album")));
70 >        statement->Set(items.GetSize(), page * 50);
71  
72 <        return albums;
72 >        _R<dbi::ResultSet> paths(statement->Execute());
73 >        _L<MediaFile> files;
74 >
75 >        while (paths->MoveNext())
76 >                files.InsertLast(GetFile(paths->GetString(_B("path"))));
77 >
78 >        connector->Release(connection);
79 >
80 >        return files;
81   }
82  
83 < _L<cse::String> Library::GetGenres() const
83 > unsigned Library::GetFilesPages(const _L<By>& bys, const _L<cse::String>& items) const
84   {
85 <        _R<dbi::ResultSet> genres_(connection->Execute(_B("SELECT DISTINCT genre FROM files WHERE live = TRUE ORDER BY genre")));
86 <        _L<cse::String> genres;
85 >        if (bys.IsEmpty() || ext::Contains(bys, By::LAST) || bys.GetSize() != items.GetSize())
86 >                return 0;
87 >
88 >        _S<ios::String> statement_;
89 >
90 >        statement_ << _B("SELECT count(path) FROM files WHERE live = TRUE ");
91 >
92 >        _foreach (const _L<By>, by, bys)
93 >                statement_ << _B("AND ") << *by << _B(" = ?");
94  
95 <        while (genres_->MoveNext())
96 <                genres.InsertLast(genres_->GetString(_B("genre")));
95 >        _R<dbi::Connection> connection(connector->Connect());
96 >        _R<dbi::Statement> statement(connection->Parse(statement_));
97  
98 <        return genres;
98 >        _foreach (const _L<cse::String>, item, items)
99 >                statement->Set(_index, *item);
100 >
101 >        _R<dbi::ResultSet> count(statement->Execute());
102 >        unsigned pages(0);
103 >
104 >        if (count->MoveNext())
105 >                pages = (count->Get<unsigned>(_B("count")) + 49) / 50;
106 >
107 >        connector->Release(connection);
108 >
109 >        return pages;
110   }
111  
112 < _L<MediaFolder> Library::GetFolders() const
112 > _L<cse::String> Library::GetItems(_L<By> bys, _L<cse::String> items, unsigned page) const
113   {
114 <        _R<dbi::ResultSet> roots(connection->Execute(_B("SELECT DISTINCT root FROM files WHERE live = TRUE ORDER By root")));
115 <        _L<MediaFolder> folders;
114 >        if (bys.IsEmpty() || ext::Contains(bys, By::LAST) || bys.GetSize() - 1 != items.GetSize())
115 >                return _L<cse::String>();
116  
117 <        while (roots->MoveNext())
118 <                folders.InsertLast(MediaFolder(connection, roots->GetString(_B("root"))));
117 >        _S<ios::String> statement_;
118 >        By by(bys.Last());
119  
120 <        return folders;
120 >        statement_ << _B("SELECT DISTINCT ") << by << _B(" FROM files WHERE live = TRUE ");
121 >
122 >        bys.RemoveLast();
123 >
124 >        _foreach (const _L<By>, by_, bys)
125 >                statement_ << _B("AND ") << *by_ << _B(" = ? ");
126 >
127 >        statement_ << _B("ORDER BY ") << by << _B(" LIMIT 50 OFFSET ?");
128 >
129 >        _R<dbi::Connection> connection(connector->Connect());
130 >        _R<dbi::Statement> statement(connection->Parse(statement_));
131 >
132 >        _foreach (const _L<cse::String>, item, items)
133 >                statement->Set(_index, *item);
134 >
135 >        statement->Set(items.GetSize(), page * 50);
136 >
137 >        _R<dbi::ResultSet> items_(statement->Execute());
138 >        
139 >        items.Clear();
140 >
141 >        while (items_->MoveNext())
142 >                items.InsertLast(items_->GetString(lexical_cast<cse::String>(by)));
143 >
144 >        connector->Release(connection);
145 >
146 >        return items;
147 > }
148 >
149 > unsigned Library::GetItemsPages(_L<By> bys, const _L<cse::String>& items) const
150 > {
151 >        if (bys.IsEmpty() || ext::Contains(bys, By::LAST) || bys.GetSize() - 1 != items.GetSize())
152 >                return 0;
153 >
154 >        _S<ios::String> statement_;
155 >        By by(bys.Last());
156 >
157 >        statement_ << _B("SELECT count(DISTINCT ") << by << _B(") FROM files WHERE live = TRUE");
158 >
159 >        bys.RemoveLast();
160 >
161 >        _foreach (const _L<By>, by_, bys)
162 >                statement_ << _B(" AND ") << *by_ << _B(" = ?");
163 >
164 >        _R<dbi::Connection> connection(connector->Connect());
165 >        _R<dbi::Statement> statement(connection->Parse(statement_));
166 >
167 >        _foreach (const _L<cse::String>, item, items)
168 >                statement->Set(_index, *item);
169 >
170 >        _R<dbi::ResultSet> count(statement->Execute());
171 >        unsigned pages(0);
172 >
173 >        if (count->MoveNext())
174 >                pages = (count->Get<unsigned>(_B("count")) + 49) / 50;
175 >
176 >        connector->Release(connection);
177 >
178 >        return pages;
179   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines