FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
libfossdbQueryResult.hpp
Go to the documentation of this file.
1 /*
2 Copyright (C) 2014-2015, Siemens AG
3 Author: Daniele Fognini
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2
7 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 See the GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
19 #ifndef LIBFOSSDBQUERYRESULT_HPP_
20 #define LIBFOSSDBQUERYRESULT_HPP_
21 
22 extern "C" {
23 #include "libfossdbmanager.h"
24 }
25 
26 #include "uniquePtr.hpp"
27 
28 #include <vector>
29 
35 namespace fo {
41  public:
46  void operator()(PGresult* p) {
47  PQclear(p);
48  }
49  };
50 
55  class QueryResult {
56  friend class DbManager;
57 
58  friend class AgentDatabaseHandler;
59 
60  private:
61  QueryResult(PGresult* ptr);
62 
63  public:
64  QueryResult(QueryResult&& o): ptr(std::move(o.ptr)) {};
65 
66  bool isFailed() const;
67 
73  operator bool() const {
74  return !isFailed();
75  };
76 
77  int getRowCount() const;
78 
79  std::vector<std::string> getRow(int i) const;
80 
81  template<typename T>
82  std::vector<T> getSimpleResults(int columnN, T (functionP)(const char*)) const;
83 
84  private:
85  unptr::unique_ptr <PGresult, PGresultDeleter> ptr;
86  };
87 
97  template<typename T>
98  std::vector<T> QueryResult::getSimpleResults(int columnN, T (functionP)(const char*)) const {
99  std::vector<T> result;
100 
101  if (ptr) {
102  PGresult* r = ptr.get();
103 
104  if (columnN < PQnfields(r)) {
105  for (int i = 0; i < getRowCount(); i++) {
106  result.push_back(functionP(PQgetvalue(r, i, columnN)));
107  }
108  }
109  }
110 
111  return result;
112  }
113 }
114 #endif
DB wrapper for agents.
void operator()(PGresult *p)
PGresult deleter (for shared pointer)
Database handler for agents.
Wrapper for DB result.
Defined which unique to be used by creating new unptr namespace.
fo namespace holds the FOSSology library functions.
unptr::unique_ptr< PGresult, PGresultDeleter > ptr
Unique pointer to the actual PGresult.
std::vector< T > getSimpleResults(int columnN, T(functionP)(const char *)) const
Get vector of a single column from query result.