dbflight.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 "dbpilots.h"
  20. #include "dbaircraft.h"
  21. /*!
  22. * \brief dbFlight::verifyInput Checks the validity of the data in a
  23. * flight object against the database and sets its verified flag
  24. * accordingly. Information about partial validity can be obtained
  25. * by querying the flight objects invalidItems member.
  26. */
  27. void dbFlight::verifyInput(flight object)
  28. {
  29. object.print();
  30. //to do
  31. }
  32. flight dbFlight::retreiveFlight(QString flight_id)
  33. {
  34. //To Do
  35. QSqlQuery query;
  36. query.prepare("SELECT * FROM flights WHERE id = ?");
  37. query.addBindValue(flight_id);
  38. query.exec();
  39. if(query.first());
  40. else
  41. {
  42. qWarning() << __func__ << "No Flight with this ID found";
  43. return flight(); //return empty
  44. }
  45. flight object;
  46. object.id = query.value(0).toInt();
  47. object.doft = QDate::fromString(query.value(1).toString(),Qt::ISODate);
  48. object.dept = query.value(2).toString();
  49. object.dest = query.value(3).toString();
  50. object.tofb = QTime::fromString(
  51. calc::minutes_to_string(
  52. query.value(4).toString()),"hh:mm");
  53. object.tonb = QTime::fromString(
  54. calc::minutes_to_string(
  55. query.value(5).toString()),"hh:mm");
  56. object.pic = dbPilots::retreivePilotNameFromID(
  57. query.value(6).toString());
  58. object.acft = dbAircraft::retreiveRegistration(
  59. query.value(7).toString());
  60. object.tblk = QTime::fromString(
  61. calc::minutes_to_string(
  62. query.value(8).toString()),"hh:mm");
  63. object.tSPSE = QTime::fromString(
  64. calc::minutes_to_string(
  65. query.value(9).toString()),"hh:mm");
  66. object.tSPME = QTime::fromString(
  67. calc::minutes_to_string(
  68. query.value(10).toString()),"hh:mm");
  69. object.tMP = QTime::fromString(
  70. calc::minutes_to_string(
  71. query.value(11).toString()),"hh:mm");
  72. object.tNIGHT = QTime::fromString(
  73. calc::minutes_to_string(
  74. query.value(12).toString()),"hh:mm");
  75. object.tIFR = QTime::fromString(
  76. calc::minutes_to_string(
  77. query.value(13).toString()),"hh:mm");
  78. object.tPIC = QTime::fromString(
  79. calc::minutes_to_string(
  80. query.value(14).toString()),"hh:mm");
  81. object.tPICUS = QTime::fromString(
  82. calc::minutes_to_string(
  83. query.value(15).toString()),"hh:mm");
  84. object.tSIC = QTime::fromString(
  85. calc::minutes_to_string(
  86. query.value(16).toString()),"hh:mm");
  87. object.tDUAL = QTime::fromString(
  88. calc::minutes_to_string(
  89. query.value(17).toString()),"hh:mm");
  90. object.tFI = QTime::fromString(
  91. calc::minutes_to_string(
  92. query.value(18).toString()),"hh:mm");
  93. object.tSIM = QTime::fromString(
  94. calc::minutes_to_string(
  95. query.value(19).toString()),"hh:mm");
  96. object.pilotFlying = query.value(20).toInt();
  97. object.toDay = query.value(21).toInt();
  98. object.toNight = query.value(22).toInt();
  99. object.ldgDay = query.value(23).toInt();
  100. object.ldgNight = query.value(24).toInt();
  101. object.autoland = query.value(25).toInt();
  102. object.secondPilot = query.value(26).toInt();
  103. object.thirdPilot = query.value(27).toInt();
  104. object.approachType = query.value(28).toString();
  105. object.flightNumber = query.value(29).toString();
  106. object.remarks = query.value(30).toString();
  107. //Database entries are assumed to be valid
  108. object.isValid = true;
  109. object.invalidItems.clear();
  110. return object;
  111. }
  112. bool dbFlight::commitFlight(flight object)
  113. {
  114. //To Do
  115. qDebug() << object;
  116. return false;
  117. }
  118. /*!
  119. * \brief SelectFlightById Retreives a single flight from the database.
  120. * \param flight_id Primary Key of flights database
  121. * \return Flight details of selected flight.
  122. */
  123. QVector<QString> dbFlight::selectFlightById(QString flight_id)
  124. {
  125. QSqlQuery query;
  126. query.prepare("SELECT * FROM flights WHERE id = ?");
  127. query.addBindValue(flight_id);
  128. query.exec();
  129. if(query.first());
  130. else
  131. {
  132. qDebug() << "db::SelectFlightById - No Flight with this ID found";
  133. QVector<QString> flight; //return empty
  134. return flight;
  135. }
  136. QVector<QString> flight;
  137. flight.append(query.value(0).toString());
  138. flight.append(query.value(1).toString());
  139. flight.append(query.value(2).toString());
  140. flight.append(query.value(3).toString());
  141. flight.append(query.value(4).toString());
  142. flight.append(query.value(5).toString());
  143. flight.append(query.value(6).toString());
  144. flight.append(query.value(7).toString());
  145. flight.append(query.value(8).toString());
  146. qDebug() << "db::SelectFlightById - retreived flight: " << flight;
  147. return flight;
  148. }
  149. /*!
  150. * \brief deleteFlightById Deletes a Flight from the database.
  151. * Entries in the basic flights table as well as in the extras table are deleted.
  152. * \param flight_id The primary key of the entry in the database
  153. * \return True if no errors, otherwise false
  154. */
  155. bool dbFlight::deleteFlightById(QString flight_id)
  156. {
  157. QSqlQuery query;
  158. query.prepare("DELETE FROM flights WHERE id = ?");
  159. query.addBindValue(flight_id);
  160. query.exec();
  161. QString error = query.lastError().text();
  162. QSqlQuery query2;
  163. query2.prepare("DELETE FROM extras WHERE extras_id = ?");
  164. query2.addBindValue(flight_id);
  165. query2.exec();
  166. QString error2 = query2.lastError().text();
  167. qDebug() << "db::deleteFlightById: Removing flight with ID#: " << flight_id;
  168. if(error.length() > 0 || error2.length() > 0)
  169. {
  170. qWarning() << "db::deleteFlightsById: Errors have occured: " << error << " " << error2;
  171. return false;
  172. }else
  173. {
  174. return true;
  175. }
  176. }
  177. /*!
  178. * \brief CreateFlightVectorFromInput Converts input from NewFlight Window into database format
  179. * \param doft Date of flight
  180. * \param dept Place of Departure
  181. * \param tofb Time Off Blocks (UTC)
  182. * \param dest Place of Destination
  183. * \param tonb Time On Blocks (UTC)
  184. * \param tblk Total Block Time
  185. * \param pic Pilot in command
  186. * \param acft Aircraft
  187. * \return Vector of values ready for committing
  188. */
  189. QVector<QString> dbFlight::createFlightVectorFromInput(QString doft, QString dept, QTime tofb, QString dest,
  190. QTime tonb, QTime tblk, QString pic, QString acft)
  191. {
  192. QVector<QString> flight;
  193. flight.insert(0, ""); // ID, created as primary key during commit
  194. flight.insert(1, doft);
  195. flight.insert(2, dept);
  196. flight.insert(3, QString::number(calc::time_to_minutes(tofb)));
  197. flight.insert(4, dest);
  198. flight.insert(5, QString::number(calc::time_to_minutes(tonb)));
  199. flight.insert(6, QString::number(calc::time_to_minutes(tblk)));
  200. flight.insert(7, pic); // lookup and matching tbd
  201. flight.insert(8, acft);// lookup and matching tbd
  202. //qDebug() << flight;
  203. return flight;
  204. }
  205. /*!
  206. * \brief CommitToScratchpad Commits the inputs of the NewFlight window to a scratchpad
  207. * to make them available for restoring entries when the input fields are being reloaded.
  208. * \param flight The input data, which was not accepted for commiting to the flights table.
  209. */
  210. void dbFlight::commitToScratchpad(QVector<QString> flight)// to store input mask
  211. {
  212. //qDebug() << "Saving invalid flight to scratchpad";
  213. QSqlQuery query;
  214. query.prepare("INSERT INTO scratchpad (doft, dept, tofb, dest, tonb, tblk, pic, acft) "
  215. "VALUES (:doft, :dept, :tofb, :dest, :tonb, :tblk, :pic, :acft)");
  216. //flight[0] is primary key, not required for commit
  217. query.bindValue(":doft", flight[1]); //string
  218. query.bindValue(":dept", flight[2]);
  219. query.bindValue(":tofb", flight[3].toInt()); //int
  220. query.bindValue(":dest", flight[4]);
  221. query.bindValue(":tonb", flight[5].toInt());
  222. query.bindValue(":tblk", flight[6].toInt());
  223. query.bindValue(":pic", flight[7].toInt());
  224. query.bindValue(":acft", flight[8].toInt());
  225. query.exec();
  226. qDebug() << query.lastError().text();
  227. }
  228. /*!
  229. * \brief RetreiveScratchpad Selects data from scratchpad
  230. * \return Vector of data contained in scratchpad
  231. */
  232. QVector<QString> dbFlight::retreiveScratchpad()
  233. {
  234. //qDebug() << "Retreiving invalid flight from scratchpad";
  235. QSqlQuery query;
  236. query.prepare("SELECT * FROM scratchpad");
  237. query.exec();
  238. if(query.first());
  239. else
  240. {
  241. //qDebug() << ("scratchpad empty");
  242. QVector<QString> flight; //return empty
  243. return flight;
  244. }
  245. query.previous();
  246. QVector<QString> flight;
  247. while (query.next()) {
  248. flight.append(query.value(0).toString());
  249. flight.append(query.value(1).toString());
  250. flight.append(query.value(2).toString());
  251. flight.append(calc::minutes_to_string((query.value(3).toString())));
  252. flight.append(query.value(4).toString());
  253. flight.append(calc::minutes_to_string((query.value(5).toString())));
  254. flight.append(calc::minutes_to_string((query.value(6).toString())));
  255. flight.append(query.value(7).toString());
  256. flight.append(query.value(8).toString());
  257. }
  258. clearScratchpad();
  259. return flight;
  260. }
  261. /*!
  262. * \brief CheckScratchpad Verifies if the scratchpad contains data
  263. * \return true if scratchpad contains data
  264. */
  265. bool dbFlight::checkScratchpad() // see if scratchpad is empty
  266. {
  267. //qDebug() << "Checking if scratchpad contains data";
  268. QSqlQuery query;
  269. query.prepare("SELECT * FROM scratchpad");
  270. query.exec();
  271. if(query.first())
  272. {
  273. //qDebug() << "Scratchpad contains data";
  274. return 1;
  275. }
  276. else
  277. {
  278. //qDebug() << ("Scratchpad contains NO data");
  279. return 0;
  280. }
  281. }
  282. /*!
  283. * \brief ClearScratchpad Deletes data contained in the scratchpad
  284. */
  285. void dbFlight::clearScratchpad()
  286. {
  287. qDebug() << "Deleting scratchpad";
  288. QSqlQuery query;
  289. query.prepare("DELETE FROM scratchpad;");
  290. query.exec();
  291. }