StdAir Logo  1.00.12
C++ Standard Airline IT Object Library
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
16
17namespace 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) {
90 }
91 assert (_dbSession != NULL);
92 return *_dbSession;
93 }
94
95}
Handle on the StdAir library context.
soci::session DBSession_T
Definition: stdair_db.hpp:20