StdAir Logo  1.00.10
C++ Standard Airline IT Object Library
DBManagerForAirlines.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 // SOCI
7 #include <soci/soci.h>
8 #include <soci/mysql/soci-mysql.h>
9 // StdAir
16 
17 namespace stdair {
18 
19  // //////////////////////////////////////////////////////////////////////
21  prepareSelectStatement (DBSession_T& ioSociSession,
22  DBRequestStatement_T& ioSelectStatement,
23  AirlineStruct& ioAirline) {
24 
25  try {
26 
27  // Instanciate a SQL statement (no request is performed at that stage)
33  ioSelectStatement = (ioSociSession.prepare
34  << "select iata_code, name "
35  << "from airlines ", soci::into (ioAirline));
36 
37  // Execute the SQL query
38  ioSelectStatement.execute();
39 
40  } catch (std::exception const& lException) {
41  throw SQLDatabaseException (lException.what());
42  }
43  }
44 
45  // //////////////////////////////////////////////////////////////////////
46  void DBManagerForAirlines::
47  prepareSelectOnAirlineCodeStatement (DBSession_T& ioSociSession,
48  DBRequestStatement_T& ioSelectStatement,
49  const AirlineCode_T& iAirlineCode,
50  AirlineStruct& ioAirline) {
51 
52  try {
53 
54  // Instanciate a SQL statement (no request is performed at that stage)
61  ioSelectStatement = (ioSociSession.prepare
62  << "select iata_code, name "
63  << "from airlines "
64  << "where iata_code = :airline_code ",
65  soci::into (ioAirline), soci::use (iAirlineCode));
66 
67  // Execute the SQL query
68  ioSelectStatement.execute();
69 
70  } catch (std::exception const& lException) {
71  throw SQLDatabaseException (lException.what());
72  }
73  }
74 
75  // //////////////////////////////////////////////////////////////////////
78  AirlineStruct& ioAirline) {
79  bool hasStillData = false;
80 
81  try {
82 
83  // Retrieve the next row of Airline object
84  hasStillData = ioStatement.fetch();
85 
86  } catch (std::exception const& lException) {
87  throw SQLDatabaseException (lException.what());
88  }
89 
90  return hasStillData;
91  }
92 
93  // //////////////////////////////////////////////////////////////////////
95  const AirlineStruct& iAirline) {
96  try {
97  // Begin a transaction on the database
98  ioSociSession.begin();
99 
100  // Retrieve the airline code
101  const std::string& lAirlineCode = iAirline.getAirlineCode();
102 
103  // Retrieve the airline name
104  const std::string& lAirlineName = iAirline.getAirlineName();
105 
106  // Instanciate a SQL statement (no request is performed at that stage)
107  DBRequestStatement_T lUpdateStatement =
108  (ioSociSession.prepare
109  << "update airlines "
110  << "set name = :name "
111  << "where iata_code = :iata_code",
112  soci::use (lAirlineName), soci::use (lAirlineCode));
113 
114  // Execute the SQL query
115  lUpdateStatement.execute (true);
116 
117  // Commit the transaction on the database
118  ioSociSession.commit();
119 
120  // Debug
121  // STDAIR_LOG_DEBUG ("[" << lAirlineCode << "] " << iAirline);
122 
123  } catch (std::exception const& lException) {
124  throw SQLDatabaseException (lException.what());
125  }
126  }
127 
128  // //////////////////////////////////////////////////////////////////////
130  const AirlineCode_T& iAirlineCode,
131  AirlineStruct& ioAirline) {
132  bool oHasRetrievedAirline = false;
133 
134  try {
135  // Prepare the SQL request corresponding to the select statement
136  DBRequestStatement_T lSelectStatement (ioSociSession);
137  prepareSelectOnAirlineCodeStatement (ioSociSession, lSelectStatement,
138  iAirlineCode, ioAirline);
139 
140  // const bool shouldDoReset = true;
141  bool hasStillData = iterateOnStatement (lSelectStatement, ioAirline);
142  if (hasStillData == true) {
143  oHasRetrievedAirline = true;
144  }
145 
146  // Sanity check
147  // const bool shouldNotDoReset = false;
148  hasStillData = iterateOnStatement (lSelectStatement, ioAirline);
149 
150  // Debug
151  // STDAIR_LOG_DEBUG ("[" << iDocID << "] " << ioAirline);
152 
153  } catch (std::exception const& lException) {
154  throw SQLDatabaseException (lException.what());
155  }
156 
157  return oHasRetrievedAirline;
158  }
159 
160 }
Handle on the StdAir library context.
soci::statement DBRequestStatement_T
Definition: stdair_db.hpp:23
std::string AirlineCode_T
soci::session DBSession_T
Definition: stdair_db.hpp:20
const AirlineCode_T & getAirlineCode() const
const std::string & getAirlineName() const
static void updateAirlineInDB(DBSession_T &, const AirlineStruct &)
static void prepareSelectStatement(DBSession_T &, DBRequestStatement_T &, AirlineStruct &)
static bool iterateOnStatement(DBRequestStatement_T &, AirlineStruct &)
static bool retrieveAirline(DBSession_T &, const AirlineCode_T &, AirlineStruct &)