$treeview $search $mathjax
StdAir Logo  1.00.1
$projectbrief
$projectbrief
$searchbox

stdair/command/DBManagerForAirlines.cpp

Go to the documentation of this file.
00001 // //////////////////////////////////////////////////////////////////////
00002 // Import section
00003 // //////////////////////////////////////////////////////////////////////
00004 // STL
00005 #include <cassert>
00006 // SOCI
00007 #if defined(SOCI_HEADERS_BURIED)
00008 #include <soci/core/soci.h>
00009 #include <soci/backends/mysql/soci-mysql.h>
00010 #else // SOCI_HEADERS_BURIED
00011 #include <soci.h>
00012 #include <mysql/soci-mysql.h>
00013 #endif // SOCI_HEADERS_BURIED
00014 // StdAir
00015 #include <stdair/stdair_basic_types.hpp>
00016 #include <stdair/stdair_exceptions.hpp>
00017 #include <stdair/bom/AirlineStruct.hpp>
00018 #include <stdair/dbadaptor/DbaAirline.hpp>
00019 #include <stdair/command/DBManagerForAirlines.hpp>
00020 #include <stdair/service/Logger.hpp>
00021 
00022 namespace stdair {
00023 
00024   // //////////////////////////////////////////////////////////////////////
00025   void DBManagerForAirlines::
00026   prepareSelectStatement (DBSession_T& ioSociSession,
00027                           DBRequestStatement_T& ioSelectStatement,
00028                           AirlineStruct& ioAirline) {
00029   
00030     try {
00031     
00032       // Instanciate a SQL statement (no request is performed at that stage)
00038       ioSelectStatement = (ioSociSession.prepare
00039                            << "select iata_code, name "
00040                            << "from airlines ", soci::into (ioAirline));
00041 
00042       // Execute the SQL query
00043       ioSelectStatement.execute();
00044 
00045     } catch (std::exception const& lException) {
00046       throw SQLDatabaseException (lException.what());
00047     }
00048   }
00049 
00050   // //////////////////////////////////////////////////////////////////////
00051   void DBManagerForAirlines::
00052   prepareSelectOnAirlineCodeStatement (DBSession_T& ioSociSession,
00053                                        DBRequestStatement_T& ioSelectStatement,
00054                                        const AirlineCode_T& iAirlineCode,
00055                                        AirlineStruct& ioAirline) {
00056   
00057     try {
00058     
00059       // Instanciate a SQL statement (no request is performed at that stage)
00066       ioSelectStatement = (ioSociSession.prepare
00067                            << "select iata_code, name "
00068                            << "from airlines "
00069                            << "where iata_code = :airline_code ",
00070                            soci::into (ioAirline), soci::use (iAirlineCode));
00071 
00072       // Execute the SQL query
00073       ioSelectStatement.execute();
00074 
00075     } catch (std::exception const& lException) {
00076       throw SQLDatabaseException (lException.what());
00077     }
00078   }
00079 
00080   // //////////////////////////////////////////////////////////////////////
00081   bool DBManagerForAirlines::
00082   iterateOnStatement (DBRequestStatement_T& ioStatement,
00083                       AirlineStruct& ioAirline) {
00084     bool hasStillData = false;
00085   
00086     try {
00087 
00088       // Retrieve the next row of Airline object
00089       hasStillData = ioStatement.fetch();
00090       
00091     } catch (std::exception const& lException) {
00092       throw SQLDatabaseException (lException.what());
00093     }
00094 
00095     return hasStillData;
00096   }
00097 
00098   // //////////////////////////////////////////////////////////////////////
00099   void DBManagerForAirlines::updateAirlineInDB (DBSession_T& ioSociSession,
00100                                                 const AirlineStruct& iAirline) {
00101     try {
00102       // Begin a transaction on the database
00103       ioSociSession.begin();
00104 
00105       // Retrieve the airline code
00106       const std::string& lAirlineCode = iAirline.getAirlineCode();
00107       
00108       // Retrieve the airline name
00109       const std::string& lAirlineName = iAirline.getAirlineName();
00110       
00111       // Instanciate a SQL statement (no request is performed at that stage)
00112       DBRequestStatement_T lUpdateStatement =
00113         (ioSociSession.prepare
00114          << "update airlines "
00115          << "set name = :name "
00116          << "where iata_code = :iata_code",
00117          soci::use (lAirlineName), soci::use (lAirlineCode));
00118       
00119       // Execute the SQL query
00120       lUpdateStatement.execute (true);
00121       
00122       // Commit the transaction on the database
00123       ioSociSession.commit();
00124         
00125       // Debug
00126       // STDAIR_LOG_DEBUG ("[" << lAirlineCode << "] " << iAirline);
00127       
00128     } catch (std::exception const& lException) {
00129       throw SQLDatabaseException (lException.what());
00130     }
00131   }
00132 
00133   // //////////////////////////////////////////////////////////////////////
00134   bool DBManagerForAirlines::retrieveAirline (DBSession_T& ioSociSession,
00135                                    const AirlineCode_T& iAirlineCode,
00136                                    AirlineStruct& ioAirline) {
00137     bool oHasRetrievedAirline = false;
00138       
00139     try {
00140       // Prepare the SQL request corresponding to the select statement
00141       DBRequestStatement_T lSelectStatement (ioSociSession);
00142       prepareSelectOnAirlineCodeStatement (ioSociSession, lSelectStatement,
00143                                            iAirlineCode, ioAirline);
00144       
00145       // const bool shouldDoReset = true;
00146       bool hasStillData = iterateOnStatement (lSelectStatement, ioAirline);
00147       if (hasStillData == true) {
00148         oHasRetrievedAirline = true;
00149       }
00150 
00151       // Sanity check
00152       // const bool shouldNotDoReset = false;
00153       hasStillData = iterateOnStatement (lSelectStatement, ioAirline);
00154       
00155       // Debug
00156       // STDAIR_LOG_DEBUG ("[" << iDocID << "] " << ioAirline);
00157       
00158     } catch (std::exception const& lException) {
00159       throw SQLDatabaseException (lException.what());
00160     }
00161 
00162     return oHasRetrievedAirline;
00163   }
00164 
00165 }