1 |
// Library |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include <menes/c++/standard.hh> |
8 |
|
9 |
#include <menes/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 |
_L<MediaFile> Library::GetFiles(const _L<By>& bys, const _L<cse::String>& items, unsigned page) const |
37 |
{ |
38 |
if (bys.IsEmpty() || ext::Contains(bys, By::LAST) || bys.GetSize() != items.GetSize()) |
39 |
return _L<MediaFile>(); |
40 |
|
41 |
_S<ios::String> statement_; |
42 |
|
43 |
statement_ << _B("SELECT path FROM files WHERE live = TRUE "); |
44 |
|
45 |
_foreach (const _L<By>, by, bys) |
46 |
statement_ << _B("AND ") << *by << _B(" = ? "); |
47 |
|
48 |
statement_ << _B("ORDER BY path LIMIT 50 OFFSET ?"); |
49 |
|
50 |
_R<dbi::Connection> connection(connector->Connect()); |
51 |
_R<dbi::Statement> statement(connection->Parse(statement_)); |
52 |
|
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 |
connector->Release(connection); |
65 |
|
66 |
return files; |
67 |
} |
68 |
|
69 |
_L<cse::String> Library::GetItems(_L<By> bys, _L<cse::String> items, unsigned page) const |
70 |
{ |
71 |
if (bys.IsEmpty() || ext::Contains(bys, By::LAST) || bys.GetSize() - 1 != items.GetSize()) |
72 |
return _L<cse::String>(); |
73 |
|
74 |
_S<ios::String> statement_; |
75 |
By by(bys.Last()); |
76 |
|
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 |
} |