TraDemGen Logo  1.00.0
C++ Simulated Travel Demand Generation Library
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
DBManager.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 // SOCI
7 #if defined(SOCI_HEADERS_BURIED)
8 #include <soci/core/soci.h>
9 #include <soci/backends/mysql/soci-mysql.h>
10 #else // SOCI_HEADERS_BURIED
11 #include <soci/soci.h>
12 #include <soci/mysql/soci-mysql.h>
13 #endif // SOCI_HEADERS_BURIED
14 // StdAir
15 #include <stdair/bom/AirlineStruct.hpp>
16 #include <stdair/service/Logger.hpp>
17 // TraDemGen
19 
20 namespace TRADEMGEN {
21 
22  // //////////////////////////////////////////////////////////////////////
23  void DBManager::
24  prepareSelectStatement (stdair::DBSession_T& ioSociSession,
25  stdair::DBRequestStatement_T& ioSelectStatement,
26  stdair::AirlineStruct& ioAirline) {
27 
28  try {
29 
30  // Instanciate a SQL statement (no request is performed at that stage)
44  } catch (std::exception const& lException) {
45  STDAIR_LOG_ERROR ("Error: " << lException.what());
46  throw stdair::SQLDatabaseException (lException.what());
47  }
48  }
49 
50  // //////////////////////////////////////////////////////////////////////
51  void DBManager::
52  prepareSelectOnAirlineCodeStatement (stdair::DBSession_T& ioSociSession,
53  stdair::DBRequestStatement_T& ioSelectStatement,
54  const stdair::AirlineCode_T& iAirlineCode,
55  stdair::AirlineStruct& ioAirline) {
56 
57  try {
58 
59  // Instanciate a SQL statement (no request is performed at that stage)
90  } catch (std::exception const& lException) {
91  STDAIR_LOG_ERROR ("Error: " << lException.what());
92  throw stdair::SQLDatabaseException (lException.what());
93  }
94  }
95 
96  // //////////////////////////////////////////////////////////////////////
97  bool DBManager::iterateOnStatement (stdair::DBRequestStatement_T& ioStatement,
98  stdair::AirlineStruct& ioAirline,
99  const bool iShouldDoReset) {
100  bool hasStillData = false;
101 
102  try {
103 
104  // Reset the list of names of the given Airline object
105  if (iShouldDoReset == true) {
106  // ioAirline.resetMatrix();
107  }
108 
109  // Retrieve the next row of Airline object
110  hasStillData = ioStatement.fetch();
111 
112  } catch (std::exception const& lException) {
113  STDAIR_LOG_ERROR ("Error: " << lException.what());
114  throw stdair::SQLDatabaseException (lException.what());
115  }
116 
117  return hasStillData;
118  }
119 
120  // //////////////////////////////////////////////////////////////////////
121  void DBManager::updateAirlineInDB (stdair::DBSession_T& ioSociSession,
122  const stdair::AirlineStruct& iAirline) {
123 
124  try {
125 
126  // Begin a transaction on the database
127  ioSociSession.begin();
128 
129  // Instanciate a SQL statement (no request is performed at that stage)
130  std::string lAirlineCode;
131  /*
132  stdair::DBRequestStatement_T lUpdateStatement =
133  (ioSociSession.prepare
134  << "update ref_airline_details "
135  << "set xapian_docid = :xapian_docid "
136  << "where code = :code", soci::use (lDocID), soci::use (lAirlineCode));
137 
138  // Execute the SQL query
139  lDocID = iAirline.getDocID();
140  lAirlineCode = iAirline.getAirlineCode();
141  lUpdateStatement.execute (true);
142  */
143 
144  // Commit the transaction on the database
145  ioSociSession.commit();
146 
147  // Debug
148  // TRADEMGEN_LOG_DEBUG ("[" << lDocID << "] " << iAirline);
149 
150  } catch (std::exception const& lException) {
151  STDAIR_LOG_ERROR ("Error: " << lException.what());
152  throw stdair::SQLDatabaseException (lException.what());
153  }
154  }
155 
156  // //////////////////////////////////////////////////////////////////////
157  bool DBManager::retrieveAirline (stdair::DBSession_T& ioSociSession,
158  const stdair::AirlineCode_T& iAirlineCode,
159  stdair::AirlineStruct& ioAirline) {
160  bool oHasRetrievedAirline = false;
161 
162  try {
163 
164  // Prepare the SQL request corresponding to the select statement
165  stdair::DBRequestStatement_T lSelectStatement (ioSociSession);
166  DBManager::prepareSelectOnAirlineCodeStatement (ioSociSession,
167  lSelectStatement,
168  iAirlineCode, ioAirline);
169  const bool shouldDoReset = true;
170  bool hasStillData = iterateOnStatement (lSelectStatement, ioAirline,
171  shouldDoReset);
172  if (hasStillData == true) {
173  oHasRetrievedAirline = true;
174  }
175 
176  // Sanity check
177  const bool shouldNotDoReset = false;
178  hasStillData = iterateOnStatement (lSelectStatement, ioAirline,
179  shouldNotDoReset);
180  // Debug
181  // STDAIR_LOG_DEBUG ("[" << iDocID << "] " << ioAirline);
182 
183  } catch (std::exception const& lException) {
184  STDAIR_LOG_ERROR ("Error: " << lException.what());
185  throw stdair::SQLDatabaseException (lException.what());
186  }
187 
188  return oHasRetrievedAirline;
189  }
190 
191 }