dbflight.cpp 8.1 KB

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