FOSSology  4.4.0
Open Source License Compliance by Open Source Software
libfossdbQueryResult.hpp
Go to the documentation of this file.
1 /*
2  Author: Daniele Fognini
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 #ifndef LIBFOSSDBQUERYRESULT_HPP_
9 #define LIBFOSSDBQUERYRESULT_HPP_
10 
11 #include "libfossdbmanager.h"
12 
13 #include "uniquePtr.hpp"
14 
15 #include <vector>
16 
22 namespace fo {
28  public:
33  void operator()(PGresult* p) {
34  PQclear(p);
35  }
36  };
37 
42  class QueryResult {
43  friend class DbManager;
44 
45  friend class AgentDatabaseHandler;
46 
47  private:
48  QueryResult(PGresult* ptr);
49 
50  public:
51  QueryResult(QueryResult&& o): ptr(std::move(o.ptr)) {};
52 
53  bool isFailed() const;
54 
60  operator bool() const {
61  return !isFailed();
62  };
63 
64  int getRowCount() const;
65 
66  std::vector<std::string> getRow(int i) const;
67 
68  template<typename T>
69  std::vector<T> getSimpleResults(int columnN, T (functionP)(const char*)) const;
70 
71  private:
72  unptr::unique_ptr <PGresult, PGresultDeleter> ptr;
73  };
74 
84  template<typename T>
85  std::vector<T> QueryResult::getSimpleResults(int columnN, T (functionP)(const char*)) const {
86  std::vector<T> result;
87 
88  if (ptr) {
89  PGresult* r = ptr.get();
90 
91  if (columnN < PQnfields(r)) {
92  for (int i = 0; i < getRowCount(); i++) {
93  result.push_back(functionP(PQgetvalue(r, i, columnN)));
94  }
95  }
96  }
97 
98  return result;
99  }
100 }
101 #endif
Database handler for agents.
DB wrapper for agents.
PGresult deleter (for shared pointer)
void operator()(PGresult *p)
Wrapper for DB result.
std::vector< T > getSimpleResults(int columnN, T(functionP)(const char *)) const
Get vector of a single column from query result.
bool isFailed() const
Check if the query failed.
unptr::unique_ptr< PGresult, PGresultDeleter > ptr
Unique pointer to the actual PGresult.
std::vector< std::string > getRow(int i) const
QueryResult(PGresult *ptr)
fo namespace holds the FOSSology library functions.
Defined which unique to be used by creating new unptr namespace.