StdAir Logo  1.00.0
C++ Standard Airline IT Object Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DBManagerForAirlines.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.h>
12 #include <mysql/soci-mysql.h>
13 #endif // SOCI_HEADERS_BURIED
14 // StdAir
21 
22 namespace stdair {
23 
24  // //////////////////////////////////////////////////////////////////////
27  DBRequestStatement_T& ioSelectStatement,
28  AirlineStruct& ioAirline) {
29 
30  try {
31 
32  // Instanciate a SQL statement (no request is performed at that stage)
38  ioSelectStatement = (ioSociSession.prepare
39  << "select iata_code, name "
40  << "from airlines ", soci::into (ioAirline));
41 
42  // Execute the SQL query
43  ioSelectStatement.execute();
44 
45  } catch (std::exception const& lException) {
46  throw SQLDatabaseException (lException.what());
47  }
48  }
49 
50  // //////////////////////////////////////////////////////////////////////
51  void DBManagerForAirlines::
52  prepareSelectOnAirlineCodeStatement (DBSession_T& ioSociSession,
53  DBRequestStatement_T& ioSelectStatement,
54  const AirlineCode_T& iAirlineCode,
55  AirlineStruct& ioAirline) {
56 
57  try {
58 
59  // Instanciate a SQL statement (no request is performed at that stage)
66  ioSelectStatement = (ioSociSession.prepare
67  << "select iata_code, name "
68  << "from airlines "
69  << "where iata_code = :airline_code ",
70  soci::into (ioAirline), soci::use (iAirlineCode));
71 
72  // Execute the SQL query
73  ioSelectStatement.execute();
74 
75  } catch (std::exception const& lException) {
76  throw SQLDatabaseException (lException.what());
77  }
78  }
79 
80  // //////////////////////////////////////////////////////////////////////
83  AirlineStruct& ioAirline) {
84  bool hasStillData = false;
85 
86  try {
87 
88  // Retrieve the next row of Airline object
89  hasStillData = ioStatement.fetch();
90 
91  } catch (std::exception const& lException) {
92  throw SQLDatabaseException (lException.what());
93  }
94 
95  return hasStillData;
96  }
97 
98  // //////////////////////////////////////////////////////////////////////
100  const AirlineStruct& iAirline) {
101  try {
102  // Begin a transaction on the database
103  ioSociSession.begin();
104 
105  // Retrieve the airline code
106  const std::string& lAirlineCode = iAirline.getAirlineCode();
107 
108  // Retrieve the airline name
109  const std::string& lAirlineName = iAirline.getAirlineName();
110 
111  // Instanciate a SQL statement (no request is performed at that stage)
112  DBRequestStatement_T lUpdateStatement =
113  (ioSociSession.prepare
114  << "update airlines "
115  << "set name = :name "
116  << "where iata_code = :iata_code",
117  soci::use (lAirlineName), soci::use (lAirlineCode));
118 
119  // Execute the SQL query
120  lUpdateStatement.execute (true);
121 
122  // Commit the transaction on the database
123  ioSociSession.commit();
124 
125  // Debug
126  // STDAIR_LOG_DEBUG ("[" << lAirlineCode << "] " << iAirline);
127 
128  } catch (std::exception const& lException) {
129  throw SQLDatabaseException (lException.what());
130  }
131  }
132 
133  // //////////////////////////////////////////////////////////////////////
135  const AirlineCode_T& iAirlineCode,
136  AirlineStruct& ioAirline) {
137  bool oHasRetrievedAirline = false;
138 
139  try {
140  // Prepare the SQL request corresponding to the select statement
141  DBRequestStatement_T lSelectStatement (ioSociSession);
142  prepareSelectOnAirlineCodeStatement (ioSociSession, lSelectStatement,
143  iAirlineCode, ioAirline);
144 
145  // const bool shouldDoReset = true;
146  bool hasStillData = iterateOnStatement (lSelectStatement, ioAirline);
147  if (hasStillData == true) {
148  oHasRetrievedAirline = true;
149  }
150 
151  // Sanity check
152  // const bool shouldNotDoReset = false;
153  hasStillData = iterateOnStatement (lSelectStatement, ioAirline);
154 
155  // Debug
156  // STDAIR_LOG_DEBUG ("[" << iDocID << "] " << ioAirline);
157 
158  } catch (std::exception const& lException) {
159  throw SQLDatabaseException (lException.what());
160  }
161 
162  return oHasRetrievedAirline;
163  }
164 
165 }