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