dbflight.cpp 7.4 KB

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