StdAir Logo  1.00.3
C++ Standard Airline IT Object Library
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 #if defined(SOCI_HEADERS_BURIED)
10 #include <soci/core/soci.h>
11 #include <soci/backends/mysql/soci-mysql.h>
12 #else // SOCI_HEADERS_BURIED
13 #include <soci.h>
14 #include <mysql/soci-mysql.h>
15 #endif // SOCI_HEADERS_BURIED
16 // StdAir
21 
22 namespace stdair {
23 
24  // //////////////////////////////////////////////////////////////////////
25  DBSessionManager::DBSessionManager () : _dbSession (NULL) {
26  }
27 
28  // //////////////////////////////////////////////////////////////////////
29  DBSessionManager::DBSessionManager (const DBSessionManager&)
30  : _dbSession (NULL) {
31  assert (false);
32  }
33 
34  // //////////////////////////////////////////////////////////////////////
35  DBSessionManager::~DBSessionManager () {
36  // std::cout << "In DBSessionManager destructor" << std::endl;
37  dbFinalise();
38  }
39 
40  // //////////////////////////////////////////////////////////////////////
41  void DBSessionManager::dbInit (const BasDBParams& iDBParams) {
42 
43  // Database parameters
44  std::ostringstream oStr;
45  oStr << "db=" << iDBParams.getDBName() << " user=" << iDBParams.getUser()
46  << " password=" << iDBParams.getPassword()
47  << " port=" << iDBParams.getPort() << " host=" << iDBParams.getHost();
48  const std::string lDBSessionConnectionString (oStr.str());
49 
50  // Instanciate the database session: nothing else is performed at that stage
51  _dbSession = new DBSession_T;
52 
53  try {
54  // Open the connection to the database
55  _dbSession->open (soci::mysql, lDBSessionConnectionString);
56 
57  } catch (std::exception const& lException) {
58  std::ostringstream oMessage;
59  oMessage <<"Error while opening a connection to database: "
60  << lException.what() << std::endl
61  << "Database parameters used:"
62  << " db=" << iDBParams.getDBName()
63  << " user=" << iDBParams.getUser()
64  << " port=" << iDBParams.getPort()
65  << " host=" << iDBParams.getHost();
66  throw SQLDatabaseConnectionImpossibleException (oMessage.str());
67  }
68  }
69 
70  // //////////////////////////////////////////////////////////////////////
71  void DBSessionManager::dbFinalise () {
72  delete _dbSession; _dbSession = NULL;
73  }
74 
75  // //////////////////////////////////////////////////////////////////////
76  void DBSessionManager::init (const BasDBParams& iDBParams) {
77  DBSessionManager& lInstance = instance();
78  lInstance.dbInit (iDBParams);
79  }
80 
81  // //////////////////////////////////////////////////////////////////////
83  static DBSessionManager _instance;
84  return _instance;
85  }
86 
87  // //////////////////////////////////////////////////////////////////////
88  void DBSessionManager::clean() {
89  }
90 
91  // //////////////////////////////////////////////////////////////////////
93  if (_dbSession == NULL) {
95  }
96  assert (_dbSession != NULL);
97  return *_dbSession;
98  }
99 
100 }
Handle on the StdAir library context.
DBSession_T & getDBSession() const
soci::session DBSession_T
Definition: stdair_db.hpp:20
static DBSessionManager & instance()