FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
libfossdbmanagerclass.cc
Go to the documentation of this file.
1 /*
2  Author: Johannes Najjar, Cedric Bodet, Andreas Wuerl, Daniele Fognini
3  Copyright (C) 2014, Siemens AG
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 
20 
21 extern "C" {
22 #include "libfossscheduler.h"
23 #include "libfossagent.h"
24 }
25 
31 using namespace fo;
32 
39 static fo_dbManager* doConnect(int* argc, char** argv) {
40  fo_dbManager* _dbManager;
41  fo_scheduler_connect_dbMan(argc, argv, &_dbManager);
42  return _dbManager;
43 }
44 
50 static inline unptr::shared_ptr<fo_dbManager> makeShared(fo_dbManager * p)
51 {
52  return unptr::shared_ptr<fo_dbManager>(p, DbManagerStructDeleter());
53 }
54 
60 DbManager::DbManager(int* argc, char** argv) :
61  dbManager(makeShared(doConnect(argc, argv)))
62 {
63 }
64 
69  dbManager(makeShared(_dbManager))
70 {
71 }
72 
77 PGconn* DbManager::getConnection() const
78 {
79  return fo_dbManager_getWrappedConnection(getStruct_dbManager());
80 }
81 
88 {
89  return DbManager(fo_dbManager_fork(getStruct_dbManager()));
90 }
91 
97 {
98  return dbManager.get();
99 }
100 
107 bool DbManager::tableExists(const char* tableName) const
108 {
109  return fo_dbManager_tableExists(getStruct_dbManager(), tableName) != 0;
110 }
111 
118 bool DbManager::sequenceExists(const char* name) const
119 {
120  return fo_dbManager_exists(getStruct_dbManager(), "sequence", name) != 0;
121 }
122 
128 bool DbManager::begin() const
129 {
130  return fo_dbManager_begin(getStruct_dbManager()) != 0;
131 }
132 
138 bool DbManager::commit() const
139 {
140  return fo_dbManager_commit(getStruct_dbManager()) != 0;
141 }
142 
149 {
150  return fo_dbManager_rollback(getStruct_dbManager()) != 0;
151 }
152 
162 QueryResult DbManager::queryPrintf(const char* queryFormat, ...) const
163 {
164  va_list args;
165  va_start(args, queryFormat);
166  char* queryString = g_strdup_vprintf(queryFormat, args);
167  va_end(args);
168 
169  QueryResult result(fo_dbManager_Exec_printf(getStruct_dbManager(), queryString));
170 
171  g_free(queryString);
172  return result;
173 }
174 
182 {
183  va_list args;
184  va_start(args, stmt);
185  PGresult* pgResult = fo_dbManager_ExecPreparedv(stmt, args);
186  va_end(args);
187 
188  return QueryResult(pgResult);
189 }
190 
196 void DbManager::ignoreWarnings(bool b) const
197 {
198  fo_dbManager_ignoreWarnings(getStruct_dbManager(), b);
199 }
200 
201 
DB wrapper for agents.
bool tableExists(const char *tableName) const
QueryResult execPrepared(fo_dbManager_PreparedStatement *stmt,...) const
Execute a prepared statement with new parameters.
DB wrapper for agents.
DB manager deleter (for shared pointer)
void ignoreWarnings(bool) const
fo_dbManager * getStruct_dbManager() const
PGconn * getConnection() const
Wrapper for DB result.
bool rollback() const
unptr::shared_ptr< fo_dbManager > dbManager
Shared DB manager.
DbManager(int *argc, char **argv)
Constructor for DbManager.
QueryResult queryPrintf(const char *queryFormat,...) const
Execute a query in printf format.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
void fo_scheduler_connect_dbMan(int *argc, char **argv, fo_dbManager **dbManager)
Make a connection from an agent to the scheduler and create a DB manager as well. ...
static fo_dbManager * doConnect(int *argc, char **argv)
Get the C wrapper for DB manager.
bool sequenceExists(const char *name) const
fo namespace holds the FOSSology library functions.
static unptr::shared_ptr< fo_dbManager > makeShared(fo_dbManager *p)
Get a shared pointer for DB manager.
DbManager spawn() const