OpenTREP Logo  0.6.0
C++ Open Travel Request Parsing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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/sqlite3/soci-sqlite3.h>
11 // OpenTrep
12 #include <opentrep/DBParams.hpp>
15 
16 namespace OPENTREP {
17 
18  // //////////////////////////////////////////////////////////////////////
19  DBSessionManager::DBSessionManager() : _dbSession (NULL) {
20  }
21 
22  // //////////////////////////////////////////////////////////////////////
23  DBSessionManager::DBSessionManager (const DBParams& iDBParams)
24  : _dbSession (NULL) {
25  init (iDBParams);
26  }
27 
28  // //////////////////////////////////////////////////////////////////////
29  DBSessionManager::~DBSessionManager() {
30  delete _dbSession; _dbSession = NULL;
31  }
32 
33  // //////////////////////////////////////////////////////////////////////
34  soci::session& DBSessionManager::getDBSessionRef() const {
35  assert (_dbSession != NULL);
36  return *_dbSession;
37  }
38 
39  // //////////////////////////////////////////////////////////////////////
40  void DBSessionManager::init (const DBParams& iDBParams) {
41 
42  // Check that the parameters for the SQL database are not empty
43  if (iDBParams.checkSQLite() == false) {
44  std::ostringstream errorStr;
45  errorStr << "At least one of the parameters for the SQL "
46  << "database is empty: " << iDBParams;
47  OPENTREP_LOG_ERROR (errorStr.str());
48  throw XapianTravelDatabaseEmptyException (errorStr.str());
49  }
50 
51  // Instanciate a (SOCI) database session: nothing is performed at
52  // that stage
53  _dbSession = new soci::session();
54 
55  try {
56 
57  // Open the connection to the database
58  _dbSession->open (soci::sqlite3, iDBParams.toSQLiteConnectionString());
59 
60  } catch (std::exception const& lException) {
61  std::ostringstream errorStr;
62  errorStr << "Error while opening a connection to database: "
63  << lException.what() << std::endl;
64  errorStr << "Database parameters used: " << iDBParams.toString();
65  OPENTREP_LOG_ERROR (errorStr.str());
66  throw SQLDatabaseConnectionImpossibleException (errorStr.str());
67  }
68  }
69 
70 }
#define OPENTREP_LOG_ERROR(iToBeLogged)
Definition: Logger.hpp:23