FOSSology  4.4.0
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  SPDX-FileCopyrightText: © 2014 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 
10 extern "C" {
11 #include "libfossscheduler.h"
12 #include "libfossagent.h"
13 }
14 
20 using namespace fo;
21 
28 static fo_dbManager* doConnect(int* argc, char** argv) {
29  fo_dbManager* _dbManager;
30  fo_scheduler_connect_dbMan(argc, argv, &_dbManager);
31  return _dbManager;
32 }
33 
39 static inline unptr::shared_ptr<fo_dbManager> makeShared(fo_dbManager * p)
40 {
41  return unptr::shared_ptr<fo_dbManager>(p, DbManagerStructDeleter());
42 }
43 
49 DbManager::DbManager(int* argc, char** argv) :
50  dbManager(makeShared(doConnect(argc, argv)))
51 {
52 }
53 
58  dbManager(makeShared(_dbManager))
59 {
60 }
61 
66 PGconn* DbManager::getConnection() const
67 {
68  return fo_dbManager_getWrappedConnection(getStruct_dbManager());
69 }
70 
77 {
78  return DbManager(fo_dbManager_fork(getStruct_dbManager()));
79 }
80 
86 {
87  return dbManager.get();
88 }
89 
96 bool DbManager::tableExists(const char* tableName) const
97 {
98  return fo_dbManager_tableExists(getStruct_dbManager(), tableName) != 0;
99 }
100 
107 bool DbManager::sequenceExists(const char* name) const
108 {
109  return fo_dbManager_exists(getStruct_dbManager(), "sequence", name) != 0;
110 }
111 
117 bool DbManager::begin() const
118 {
119  return fo_dbManager_begin(getStruct_dbManager()) != 0;
120 }
121 
127 bool DbManager::commit() const
128 {
129  return fo_dbManager_commit(getStruct_dbManager()) != 0;
130 }
131 
138 {
139  return fo_dbManager_rollback(getStruct_dbManager()) != 0;
140 }
141 
151 QueryResult DbManager::queryPrintf(const char* queryFormat, ...) const
152 {
153  va_list args;
154  va_start(args, queryFormat);
155  char* queryString = g_strdup_vprintf(queryFormat, args);
156  va_end(args);
157 
158  QueryResult result(fo_dbManager_Exec_printf(getStruct_dbManager(), queryString));
159 
160  g_free(queryString);
161  return result;
162 }
163 
171 {
172  va_list args;
173  va_start(args, stmt);
174  PGresult* pgResult = fo_dbManager_ExecPreparedv(stmt, args);
175  va_end(args);
176 
177  return QueryResult(pgResult);
178 }
179 
185 void DbManager::ignoreWarnings(bool b) const
186 {
187  fo_dbManager_ignoreWarnings(getStruct_dbManager(), b);
188 }
189 
190 
DB manager deleter (for shared pointer)
DB wrapper for agents.
QueryResult execPrepared(fo_dbManager_PreparedStatement *stmt,...) const
Execute a prepared statement with new parameters.
bool tableExists(const char *tableName) const
void ignoreWarnings(bool) const
QueryResult queryPrintf(const char *queryFormat,...) const
Execute a query in printf format.
unptr::shared_ptr< fo_dbManager > dbManager
Shared DB manager.
PGconn * getConnection() const
bool sequenceExists(const char *name) const
fo_dbManager * getStruct_dbManager() const
DbManager(int *argc, char **argv)
Constructor for DbManager.
DbManager spawn() const
Wrapper for DB result.
static fo_dbManager * doConnect(int *argc, char **argv)
Get the C wrapper for DB manager.
static unptr::shared_ptr< fo_dbManager > makeShared(fo_dbManager *p)
Get a shared pointer for DB manager.
DB wrapper for agents.
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.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
fo namespace holds the FOSSology library functions.