Leosac  0.8.0
Open Source Access Control
LogEntry.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "LogEntry.hpp"
22 #include "tools/LogEntry_odb.h"
23 #include "tools/LogEntry_odb_pgsql.h"
24 #include "tools/LogEntry_odb_sqlite.h"
25 #include "tools/db/database.hpp"
26 #include <odb/pgsql/database.hxx>
27 #include <odb/sqlite/database.hxx>
28 
29 using namespace Leosac;
30 using namespace Leosac::Tools;
31 
33  : version_(0)
34 {
35 }
36 using Query = odb::query<Tools::LogEntry>;
37 using Result = odb::result<Tools::LogEntry>;
38 
39 static Result fetch_sqlite(DBPtr database, const std::string &order_by,
40  int page_size, int offset)
41 {
42  using SQLiteQuery = odb::sqlite::query<Tools::LogEntry>;
43  auto sl_db = std::static_pointer_cast<odb::sqlite::database>(database);
44  odb::sqlite::query<Tools::LogEntry> sl_q("ORDER BY" + Query::id + order_by +
45  "LIMIT" + SQLiteQuery::_val(page_size) +
46  "OFFSET" + SQLiteQuery::_val(offset));
47  return sl_db->query<Tools::LogEntry>(sl_q);
48 }
49 
50 static Result fetch_pgsql(DBPtr database, const std::string &order_by, int page_size,
51  int offset)
52 {
53  using PGSQLQuery = odb::pgsql::query<Tools::LogEntry>;
54  auto pg_db = std::static_pointer_cast<odb::pgsql::database>(database);
55  odb::pgsql::query<Tools::LogEntry> pg_q("ORDER BY" + Query::id + order_by +
56  "LIMIT" + PGSQLQuery::_val(page_size) +
57  "OFFSET" + PGSQLQuery::_val(offset));
58  return pg_db->query<Tools::LogEntry>(pg_q);
59 }
60 
62  int page_size, bool order_asc)
63 {
64  std::vector<LogEntry> entries;
65 
66  if (database)
67  {
68  odb::transaction t(database->begin());
69 
70  int offset = page_number * page_size;
71  std::string order_by = order_asc ? "ASC" : "DESC";
72  Result res;
73 
74  // LIMIT needs to be database specific.
75  if (database->id() == odb::database_id::id_sqlite)
76  {
77  res = fetch_sqlite(database, order_by, page_size, offset);
78  }
79  else if (database->id() == odb::database_id::id_pgsql)
80  {
81  res = fetch_pgsql(database, order_by, page_size, offset);
82  }
83  Tools::LogView view(database->query_value<Tools::LogView>());
84  for (Tools::LogEntry &entry : res)
85  {
86  entries.push_back(entry);
87  }
88 
89  return {.entries = entries,
90  .total = view.count,
91  .last = view.count / page_size,
92  .first = 0};
93  }
94  return {};
95 }
Leosac::Tools::LogEntry::retrieve
static QueryResult retrieve(DBPtr database, int page_number, int page_size, bool order_asc)
Definition: LogEntry.cpp:61
Leosac::Tools::LogView::count
size_t count
Definition: LogEntry.hpp:98
database.hpp
Leosac::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
Query
odb::query< Tools::LogEntry > Query
Definition: LogEntry.cpp:36
Leosac::Tools::LogView
SQL view over the LogEntry table.
Definition: LogEntry.hpp:95
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Tools::LogEntry::LogEntry
LogEntry()
Definition: LogEntry.cpp:32
leosacexception.hpp
Exception class for LEOSAC Project related errors.
Leosac::Tools::LogEntry::QueryResult
Structure holding the result for a retrieve() call.
Definition: LogEntry.hpp:71
LogEntry.hpp
Leosac::Tools::LogEntry
A log entry.
Definition: LogEntry.hpp:40
Leosac::Tools
Definition: DatabaseLogSink.hpp:27
Result
odb::result< Tools::LogEntry > Result
Definition: LogEntry.cpp:37