FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
libfossdbQueryResult.cc
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 it
6 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 #include "libfossdbQueryResult.hpp"
20 
21 #include <string>
22 
23 using namespace fo;
24 
34 QueryResult::QueryResult(PGresult* pgResult) : ptr(unptr::unique_ptr<PGresult, PGresultDeleter>(pgResult)) {
35 };
36 
43 bool QueryResult::isFailed() const {
44  return ptr.get() == NULL;
45 }
46 
52  if (ptr) {
53  return PQntuples(ptr.get());
54  }
55 
56  return -1;
57 }
58 
64 std::vector<std::string> QueryResult::getRow(int i) const {
65  std::vector<std::string> result;
66 
67  if (ptr) {
68  PGresult* r = ptr.get();
69 
70  if (i >= 0 && i < getRowCount()) {
71  for (int j = 0; j < PQnfields(r); j++) {
72  result.push_back(std::string(PQgetvalue(r, i, j)));
73  }
74  }
75  }
76 
77  return result;
78 }
bool isFailed() const
Check if the query failed.
Wrapper for DB result.
PGresult deleter (for shared pointer)
std::vector< std::string > getRow(int i) const
int getRowCount() const
fo namespace holds the FOSSology library functions.
QueryResult(PGresult *ptr)
unptr::unique_ptr< PGresult, PGresultDeleter > ptr
Unique pointer to the actual PGresult.