1 |
// Library |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include <cxx/standard.hh> |
8 |
|
9 |
#include <dbi/resultset.hpp> |
10 |
|
11 |
#include "Library.hpp" |
12 |
|
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<MediaFolder> Library::GetFolders(unsigned page) const |
23 |
{ |
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 (roots->MoveNext()) |
29 |
folders.InsertLast(GetFolder(roots->GetString(_B("root")))); |
30 |
|
31 |
connector->Release(connection); |
32 |
|
33 |
return folders; |
34 |
} |
35 |
|
36 |
unsigned Library::GetFoldersPages() const |
37 |
{ |
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 |
connector->Release(connection); |
46 |
|
47 |
return pages; |
48 |
} |
49 |
|
50 |
_L<MediaFile> Library::GetFiles(const _L<By>& bys, const _L<cse::String>& items, unsigned page) const |
51 |
{ |
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 |
statement->Set(items.GetSize(), page * 50); |
71 |
|
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 |
unsigned Library::GetFilesPages(const _L<By>& bys, const _L<cse::String>& items) const |
84 |
{ |
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 |
_R<dbi::Connection> connection(connector->Connect()); |
96 |
_R<dbi::Statement> statement(connection->Parse(statement_)); |
97 |
|
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<cse::String> Library::GetItems(_L<By> bys, _L<cse::String> items, unsigned page) const |
113 |
{ |
114 |
if (bys.IsEmpty() || ext::Contains(bys, By::LAST) || bys.GetSize() - 1 != items.GetSize()) |
115 |
return _L<cse::String>(); |
116 |
|
117 |
_S<ios::String> statement_; |
118 |
By by(bys.Last()); |
119 |
|
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 |
} |