dbflight.cpp 8.4 KB

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