1.00.26
C++ Standard Airline IT Object Library
Toggle main menu visibility
Loading...
Searching...
No Matches
DBSessionManager.cpp
Go to the documentation of this file.
1
// //////////////////////////////////////////////////////////////////////
2
// Import section
3
// //////////////////////////////////////////////////////////////////////
4
// STL
5
#include <cassert>
6
#include <string>
7
#include <sstream>
8
// SOCI
9
#include <soci/soci.h>
10
#include <soci/mysql/soci-mysql.h>
11
// StdAir
12
#include <
stdair/stdair_exceptions.hpp
>
13
#include <
stdair/basic/BasDBParams.hpp
>
14
#include <
stdair/service/DBSessionManager.hpp
>
15
#include <
stdair/service/Logger.hpp
>
16
17
namespace
stdair
{
18
19
// //////////////////////////////////////////////////////////////////////
20
DBSessionManager::DBSessionManager () : _dbSession (NULL) {
21
}
22
23
// //////////////////////////////////////////////////////////////////////
24
DBSessionManager::DBSessionManager (
const
DBSessionManager&)
25
: _dbSession (NULL) {
26
assert (
false
);
27
}
28
29
// //////////////////////////////////////////////////////////////////////
30
DBSessionManager::~DBSessionManager () {
31
// std::cout << "In DBSessionManager destructor" << std::endl;
32
dbFinalise();
33
}
34
35
// //////////////////////////////////////////////////////////////////////
36
void
DBSessionManager::dbInit (
const
BasDBParams& iDBParams) {
37
38
// Database parameters
39
std::ostringstream oStr;
40
oStr <<
"db="
<< iDBParams.getDBName() <<
" user="
<< iDBParams.getUser()
41
<<
" password="
<< iDBParams.getPassword()
42
<<
" port="
<< iDBParams.getPort() <<
" host="
<< iDBParams.getHost();
43
const
std::string lDBSessionConnectionString (oStr.str());
44
45
// Instanciate the database session: nothing else is performed at that stage
46
_dbSession =
new
DBSession_T
;
47
48
try
{
49
// Open the connection to the database
50
_dbSession->open (soci::mysql, lDBSessionConnectionString);
51
52
}
catch
(std::exception
const
& lException) {
53
std::ostringstream oMessage;
54
oMessage <<
"Error while opening a connection to database: "
55
<< lException.what() << std::endl
56
<<
"Database parameters used:"
57
<<
" db="
<< iDBParams.getDBName()
58
<<
" user="
<< iDBParams.getUser()
59
<<
" port="
<< iDBParams.getPort()
60
<<
" host="
<< iDBParams.getHost();
61
throw
SQLDatabaseConnectionImpossibleException (oMessage.str());
62
}
63
}
64
65
// //////////////////////////////////////////////////////////////////////
66
void
DBSessionManager::dbFinalise () {
67
delete
_dbSession; _dbSession = NULL;
68
}
69
70
// //////////////////////////////////////////////////////////////////////
71
void
DBSessionManager::init (
const
BasDBParams& iDBParams) {
72
DBSessionManager& lInstance = instance();
73
lInstance.dbInit (iDBParams);
74
}
75
76
// //////////////////////////////////////////////////////////////////////
77
DBSessionManager&
DBSessionManager::instance
() {
78
static
DBSessionManager
_instance
;
79
return
_instance
;
80
}
81
82
// //////////////////////////////////////////////////////////////////////
83
void
DBSessionManager::clean() {
84
}
85
86
// //////////////////////////////////////////////////////////////////////
87
DBSession_T
&
DBSessionManager::getDBSession
()
const
{
88
if
(_dbSession == NULL) {
89
throw
NonInitialisedDBSessionManagerException
(
""
);
90
}
91
assert (_dbSession != NULL);
92
return
*_dbSession;
93
}
94
95
}
BasDBParams.hpp
DBSessionManager.hpp
Logger.hpp
stdair_exceptions.hpp
stdair
Handle on the StdAir library context.
Definition
BasChronometer.cpp:9
stdair::_instance
FacBom< BOM > * FacBom< BOM >::_instance
Definition
FacBom.hpp:81
stdair::DBSession_T
soci::session DBSession_T
Definition
stdair_db.hpp:20
stdair::DBSessionManager::instance
static DBSessionManager & instance()
Definition
DBSessionManager.cpp:77
stdair::DBSessionManager::getDBSession
DBSession_T & getDBSession() const
Definition
DBSessionManager.cpp:87
stdair::NonInitialisedDBSessionManagerException
Definition
stdair_exceptions.hpp:188
Generated for StdAir by
1.17.0