2
0

dbaircraft.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 "dbaircraft.h"
  19. /*!
  20. * \brief RetreiveRegistration Looks up tail_id from Database
  21. * \param tail_ID Primary Key of tails database
  22. * \return Registration
  23. */
  24. QString dbAircraft::retreiveRegistration(QString tail_ID)
  25. {
  26. QString acftRegistration("");
  27. QSqlQuery query;
  28. query.prepare("SELECT registration FROM tails WHERE tail_id == ?");
  29. query.addBindValue(tail_ID.toInt());
  30. query.exec();
  31. if(query.first());
  32. else
  33. qDebug() << ("No Aircraft with this ID found");
  34. query.previous();//To go back to index 0
  35. while (query.next()) {
  36. acftRegistration.append(query.value(0).toString());
  37. }
  38. return acftRegistration;
  39. }
  40. QStringList dbAircraft::retreiveRegistrationList()
  41. {
  42. QSqlQuery query;
  43. query.prepare("SELECT registration FROM tails");
  44. query.exec();
  45. QStringList result;
  46. while (query.next()) {
  47. result.append(query.value(0).toString());
  48. }
  49. return result;
  50. }
  51. /*!
  52. * \brief newAcftGetString Looks up an aircraft Registration in the database
  53. * \param searchstring
  54. * \return Registration, make, model and variant
  55. */
  56. QStringList dbAircraft::newAcftGetString(QString searchstring)
  57. {
  58. QStringList result;
  59. if(searchstring.length()<2){return result;}
  60. QSqlQuery query;
  61. query.prepare("SELECT registration, make, model, variant "
  62. "FROM aircraft "
  63. "INNER JOIN tails on tails.aircraft_ID = aircraft.aircraft_id "
  64. "WHERE tails.registration LIKE ?");
  65. searchstring.append("%"); searchstring.prepend("%");
  66. query.addBindValue(searchstring);
  67. query.exec();
  68. while(query.next())
  69. {
  70. result.append(query.value(0).toString() + " (" + query.value(1).toString() + "-" + query.value(2).toString() + "-" + query.value(3).toString() + ")");
  71. }
  72. qDebug() << "newAcftGetString: " << result.length() << result;
  73. return result;
  74. }
  75. /*!
  76. * \brief dbAircraft::newAcftGetId Looks up a registration in the databse
  77. * \param registration Aircraft Registration
  78. * \return tail_id or empty string
  79. */
  80. QString dbAircraft::retreiveTailId(QString registration)
  81. {
  82. QString result;
  83. QSqlQuery query;
  84. query.prepare("SELECT tail_id "
  85. "FROM tails "
  86. "WHERE registration LIKE ?");
  87. registration.prepend("%"); registration.append("%");
  88. query.addBindValue(registration);
  89. query.exec();
  90. while(query.next())
  91. {
  92. result.append(query.value(0).toString());
  93. }
  94. qDebug() << "retreiveTailId: " << result;
  95. return result;
  96. }
  97. /*!
  98. * \brief dbAircraft::retreiveAircraftId Looks up aircraft_id in tails table
  99. * \param tail_id
  100. * \return aircraft_id
  101. */
  102. QString dbAircraft::retreiveAircraftId(QString tail_id)
  103. {
  104. QString result;
  105. QSqlQuery query;
  106. query.prepare("SELECT aircraft_id "
  107. "FROM tails "
  108. "WHERE tail_id = ?");
  109. query.addBindValue(tail_id);
  110. query.exec();
  111. while(query.next())
  112. {
  113. result.append(query.value(0).toString());
  114. }
  115. qDebug() << "retreiveAircraftId: " << result;
  116. return result;
  117. }
  118. /*!
  119. * \brief dbAircraft::retreiveAircraftTypeFromReg Searches the tails Database
  120. * \param searchstring
  121. * \return {registration, type, iata Code, tail_id} or {}
  122. */
  123. QVector<QString> dbAircraft::retreiveAircraftTypeFromReg(QString searchstring)
  124. {
  125. QSqlQuery query;
  126. query.prepare("SELECT Name, iata, registration, tail_id " //"SELECT Registration, Name, icao, iata "
  127. "FROM aircraft "
  128. "INNER JOIN tails on tails.aircraft_ID = aircraft.aircraft_id "
  129. "WHERE tails.registration LIKE ?");
  130. // Returns Registration/Name/icao/iata
  131. searchstring.prepend("%");
  132. searchstring.append("%");
  133. query.addBindValue(searchstring);
  134. query.exec();
  135. QVector<QString> result;
  136. if(query.first())
  137. {
  138. QString acType = query.value(0).toString();
  139. QString iataCode = query.value(1).toString();
  140. QString registration = query.value(2).toString();
  141. QString tail_id = query.value(3).toString();
  142. result.append(registration); result.append(acType);
  143. result.append(iataCode); result.append(tail_id);
  144. return result;
  145. }else
  146. {
  147. return result; // return empty
  148. }
  149. }
  150. /*!
  151. * \brief dbAircraft::retreiveAircraftMake Search function to provide a
  152. * QStringList to the QCompleter
  153. * \param searchstring A possible aircraft manufacturer
  154. * \return Possible values according to the aircraft database
  155. */
  156. QStringList dbAircraft::retreiveAircraftMake(QString searchstring)
  157. {
  158. QStringList result;
  159. QSqlQuery query;
  160. query.prepare("SELECT make from aircraft WHERE make LIKE ?");
  161. searchstring.prepend("%"); searchstring.append("%");
  162. query.addBindValue(searchstring);
  163. query.exec();
  164. while(query.next())
  165. {
  166. result.append(query.value(0).toString());
  167. }
  168. qDebug() << "db::RetreiveAircraftMake... Result:" << result;
  169. return result;
  170. }
  171. /*!
  172. * \brief dbAircraft::retreiveAircraftModel Search function to provide a
  173. * QStringList to the QCompleter
  174. * \param make A possible aircraft family (A320, 737,...)
  175. * \param searchstring
  176. * \return
  177. */
  178. QStringList dbAircraft::retreiveAircraftModel(QString make, QString searchstring)
  179. {
  180. QStringList result;
  181. QSqlQuery query;
  182. query.prepare("SELECT model FROM aircraft WHERE make = ? AND model LIKE ?");
  183. query.addBindValue(make);
  184. searchstring.prepend("%"); searchstring.append("%");
  185. query.addBindValue(searchstring);
  186. query.exec();
  187. while(query.next())
  188. {
  189. result.append(query.value(0).toString());
  190. qDebug() << "db::RetreiveAircraftModel... Result:" << result;
  191. }
  192. return result;
  193. }
  194. /*!
  195. * \brief dbAircraft::retreiveAircraftVariant Search function to provide a
  196. * QStringList to the QCompleter
  197. * \param make Aircraft manufacturer
  198. * \param model Aircraft family
  199. * \param searchstring
  200. * \return Aircraft Variant
  201. */
  202. QStringList dbAircraft::retreiveAircraftVariant(QString make, QString model, QString searchstring)
  203. {
  204. QStringList result;
  205. QSqlQuery query;
  206. query.prepare("SELECT variant from aircraft WHERE make = ? AND model = ? AND variant LIKE ?");
  207. query.addBindValue(make);
  208. query.addBindValue(model);
  209. searchstring.prepend("%"); searchstring.append("%");
  210. query.addBindValue(searchstring);
  211. query.exec();
  212. while(query.next())
  213. {
  214. result.append(query.value(0).toString());
  215. qDebug() << "db::RetreiveAircraftVariant... Result:" << result;
  216. }
  217. return result;
  218. }
  219. /*!
  220. * \brief dbAircraft::retreiveAircraftIdFromMakeModelVariant Looks up the unique
  221. * aircraft id for a given specification of make, model and variant
  222. * \param make Aircraft manufacturer (e.g. Boeing)
  223. * \param model Aircraft family (e.g. 737)
  224. * \param variant Aircraft variant (e.g. -800)
  225. * \return arcraft_id primary key of aircraft database
  226. */
  227. QString dbAircraft::retreiveAircraftIdFromMakeModelVariant(QString make, QString model, QString variant)
  228. {
  229. QString result;
  230. QSqlQuery query;
  231. query.prepare("SELECT aircraft_id FROM aircraft WHERE make = ? AND model = ? AND variant = ?");
  232. query.addBindValue(make);
  233. query.addBindValue(model);
  234. query.addBindValue(variant);
  235. query.exec();
  236. if(query.first())
  237. {
  238. result.append(query.value(0).toString());
  239. qDebug() << "db::RetreiveAircraftIdFromMakeModelVariant: Aircraft found! ID# " << result;
  240. return result;
  241. }else
  242. {
  243. result = result.left(result.length()-1);
  244. result.append("0");
  245. qDebug() << "db::RetreiveAircraftIdFromMakeModelVariant: ERROR - no AircraftId found.";
  246. return result;
  247. }
  248. }
  249. /*!
  250. * \brief dbAircraft::commitTailToDb Creates a new entry in the tails database
  251. * \param registration
  252. * \param aircraft_id Primary key of aircraft database
  253. * \param company optional entry if a/c is associated with a certain company
  254. * \return true on success
  255. */
  256. bool dbAircraft::commitTailToDb(QString registration, QString aircraft_id, QString company)
  257. {
  258. QSqlQuery commit;
  259. commit.prepare("INSERT INTO tails (registration, aircraft_id, company) VALUES (?,?,?)");
  260. commit.addBindValue(registration);
  261. commit.addBindValue(aircraft_id);
  262. commit.addBindValue(company);
  263. commit.exec();
  264. if(commit.lastError().text().length() < 0){
  265. qWarning() << "db::CommitAircraftToDb:: SQL error:" << commit.lastError().text();
  266. return false;
  267. }else{
  268. return true;
  269. }
  270. }
  271. QVector<QString> dbAircraft::retreiveAircraftDetails(QString aircraft_id)
  272. {
  273. QSqlQuery query;
  274. query.prepare("SELECT singlepilot, multipilot, singleengine, "
  275. "multiengine, turboprop, jet, heavy "
  276. "FROM aircraft "
  277. "WHERE aircraft_id = ?");
  278. query.addBindValue(aircraft_id);
  279. query.exec();
  280. QVector<QString> result;
  281. while(query.next())
  282. {
  283. result.append(query.value(0).toString()); // Singlepilot
  284. result.append(query.value(1).toString()); // Multipilot
  285. result.append(query.value(2).toString()); // Singlengine
  286. result.append(query.value(3).toString()); // Multiengine
  287. result.append(query.value(4).toString()); // turboprop
  288. result.append(query.value(5).toString()); // jet
  289. result.append(query.value(6).toString()); // heavy
  290. qDebug() << "dbaircraft::retreiveAircraftDetails... Result:" << result;
  291. }
  292. return result;
  293. }