dbflight.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include "dbflight.h"
  2. #include "dbman.cpp"
  3. dbFlight::dbFlight()
  4. {
  5. }
  6. /*!
  7. * \brief SelectFlightById Retreives a single flight from the database.
  8. * \param flight_id Primary Key of flights database
  9. * \return Flight details of selected flight.
  10. */
  11. QVector<QString> dbFlight::selectFlightById(QString flight_id)
  12. {
  13. QSqlQuery query;
  14. query.prepare("SELECT * FROM flights WHERE id = ?");
  15. query.addBindValue(flight_id);
  16. query.exec();
  17. if(query.first());
  18. else
  19. {
  20. qDebug() << "db::SelectFlightById - No Flight with this ID found";
  21. QVector<QString> flight; //return empty
  22. return flight;
  23. }
  24. QVector<QString> flight;
  25. flight.append(query.value(0).toString());
  26. flight.append(query.value(1).toString());
  27. flight.append(query.value(2).toString());
  28. flight.append(query.value(3).toString());
  29. flight.append(query.value(4).toString());
  30. flight.append(query.value(5).toString());
  31. flight.append(query.value(6).toString());
  32. flight.append(query.value(7).toString());
  33. flight.append(query.value(8).toString());
  34. qDebug() << "db::SelectFlightById - retreived flight: " << flight;
  35. return flight;
  36. }
  37. /*!
  38. * \brief deleteFlightById Deletes a Flight from the database.
  39. * Entries in the basic flights table as well as in the extras table are deleted.
  40. * \param flight_id The primary key of the entry in the database
  41. * \return True if no errors, otherwise false
  42. */
  43. bool dbFlight::deleteFlightById(QString flight_id)
  44. {
  45. QSqlQuery query;
  46. query.prepare("DELETE FROM flights WHERE id = ?");
  47. query.addBindValue(flight_id);
  48. query.exec();
  49. QString error = query.lastError().text();
  50. QSqlQuery query2;
  51. query2.prepare("DELETE FROM extras WHERE extras_id = ?");
  52. query2.addBindValue(flight_id);
  53. query2.exec();
  54. QString error2 = query2.lastError().text();
  55. qDebug() << "db::deleteFlightById: Removing flight with ID#: " << flight_id;
  56. if(error.length() > 0 || error2.length() > 0)
  57. {
  58. qWarning() << "db::deleteFlightsById: Errors have occured: " << error << " " << error2;
  59. return false;
  60. }else
  61. {
  62. return true;
  63. }
  64. }
  65. /*!
  66. * \brief CreateFlightVectorFromInput Converts input from NewFlight Window into database format
  67. * \param doft Date of flight
  68. * \param dept Place of Departure
  69. * \param tofb Time Off Blocks (UTC)
  70. * \param dest Place of Destination
  71. * \param tonb Time On Blocks (UTC)
  72. * \param tblk Total Block Time
  73. * \param pic Pilot in command
  74. * \param acft Aircraft
  75. * \return Vector of values ready for committing
  76. */
  77. QVector<QString> dbFlight::createFlightVectorFromInput(QString doft, QString dept, QTime tofb, QString dest,
  78. QTime tonb, QTime tblk, QString pic, QString acft)
  79. {
  80. QVector<QString> flight;
  81. flight.insert(0, ""); // ID, created as primary key during commit
  82. flight.insert(1, doft);
  83. flight.insert(2, dept);
  84. flight.insert(3, QString::number(calc::time_to_minutes(tofb)));
  85. flight.insert(4, dest);
  86. flight.insert(5, QString::number(calc::time_to_minutes(tonb)));
  87. flight.insert(6, QString::number(calc::time_to_minutes(tblk)));
  88. flight.insert(7, pic); // lookup and matching tbd
  89. flight.insert(8, acft);// lookup and matching tbd
  90. //qDebug() << flight;
  91. return flight;
  92. }
  93. /*!
  94. * \brief CommitFlight Inserts prepared flight vector into database. Also creates
  95. * a corresponding entry in the extras database to ensure matching IDs.
  96. * \param flight a Vector of values in database format
  97. */
  98. void dbFlight::commitFlight(QVector<QString> flight)// flight vector shall always have length 9
  99. {
  100. QSqlQuery query;
  101. query.prepare("INSERT INTO flights (doft, dept, tofb, dest, tonb, tblk, pic, acft) "
  102. "VALUES (:doft, :dept, :tofb, :dest, :tonb, :tblk, :pic, :acft)");
  103. //flight[0] is primary key, not required for commit
  104. query.bindValue(":doft", flight[1]); //string
  105. query.bindValue(":dept", flight[2]);
  106. query.bindValue(":tofb", flight[3].toInt()); //int
  107. query.bindValue(":dest", flight[4]);
  108. query.bindValue(":tonb", flight[5].toInt());
  109. query.bindValue(":tblk", flight[6].toInt());
  110. query.bindValue(":pic", flight[7].toInt());
  111. query.bindValue(":acft", flight[8].toInt());
  112. query.exec();
  113. qDebug() << "Error message for commiting flight: " << query.lastError().text();
  114. QSqlQuery query2;
  115. query2.prepare("INSERT INTO extras DEFAULT VALUES");
  116. query2.exec();
  117. qDebug() << "Creating extras entry" << query2.lastError().text();
  118. }
  119. /*!
  120. * \brief CommitToScratchpad Commits the inputs of the NewFlight window to a scratchpad
  121. * to make them available for restoring entries when the input fields are being reloaded.
  122. * \param flight The input data, which was not accepted for commiting to the flights table.
  123. */
  124. void dbFlight::commitToScratchpad(QVector<QString> flight)// to store input mask
  125. {
  126. //qDebug() << "Saving invalid flight to scratchpad";
  127. QSqlQuery query;
  128. query.prepare("INSERT INTO scratchpad (doft, dept, tofb, dest, tonb, tblk, pic, acft) "
  129. "VALUES (:doft, :dept, :tofb, :dest, :tonb, :tblk, :pic, :acft)");
  130. //flight[0] is primary key, not required for commit
  131. query.bindValue(":doft", flight[1]); //string
  132. query.bindValue(":dept", flight[2]);
  133. query.bindValue(":tofb", flight[3].toInt()); //int
  134. query.bindValue(":dest", flight[4]);
  135. query.bindValue(":tonb", flight[5].toInt());
  136. query.bindValue(":tblk", flight[6].toInt());
  137. query.bindValue(":pic", flight[7].toInt());
  138. query.bindValue(":acft", flight[8].toInt());
  139. query.exec();
  140. qDebug() << query.lastError().text();
  141. }
  142. /*!
  143. * \brief RetreiveScratchpad Selects data from scratchpad
  144. * \return Vector of data contained in scratchpad
  145. */
  146. QVector<QString> dbFlight::retreiveScratchpad()
  147. {
  148. //qDebug() << "Retreiving invalid flight from scratchpad";
  149. QSqlQuery query;
  150. query.prepare("SELECT * FROM scratchpad");
  151. query.exec();
  152. if(query.first());
  153. else
  154. {
  155. //qDebug() << ("scratchpad empty");
  156. QVector<QString> flight; //return empty
  157. return flight;
  158. }
  159. query.previous();
  160. QVector<QString> flight;
  161. while (query.next()) {
  162. flight.append(query.value(0).toString());
  163. flight.append(query.value(1).toString());
  164. flight.append(query.value(2).toString());
  165. flight.append(calc::minutes_to_string((query.value(3).toString())));
  166. flight.append(query.value(4).toString());
  167. flight.append(calc::minutes_to_string((query.value(5).toString())));
  168. flight.append(calc::minutes_to_string((query.value(6).toString())));
  169. flight.append(query.value(7).toString());
  170. flight.append(query.value(8).toString());
  171. }
  172. clearScratchpad();
  173. return flight;
  174. }
  175. /*!
  176. * \brief CheckScratchpad Verifies if the scratchpad contains data
  177. * \return true if scratchpad contains data
  178. */
  179. bool dbFlight::checkScratchpad() // see if scratchpad is empty
  180. {
  181. //qDebug() << "Checking if scratchpad contains data";
  182. QSqlQuery query;
  183. query.prepare("SELECT * FROM scratchpad");
  184. query.exec();
  185. if(query.first())
  186. {
  187. //qDebug() << "Scratchpad contains data";
  188. return 1;
  189. }
  190. else
  191. {
  192. //qDebug() << ("Scratchpad contains NO data");
  193. return 0;
  194. }
  195. }
  196. /*!
  197. * \brief ClearScratchpad Deletes data contained in the scratchpad
  198. */
  199. void dbFlight::clearScratchpad()
  200. {
  201. qDebug() << "Deleting scratchpad";
  202. QSqlQuery query;
  203. query.prepare("DELETE FROM scratchpad;");
  204. query.exec();
  205. }