dbairport.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "dbairport.h"
  19. /*!
  20. * \brief RetreiveAirportNameFromIcaoOrIata Looks up Airport Name
  21. * \param identifier can be ICAO or IATA airport codes.
  22. * \return The name of the airport associated with the above code
  23. */
  24. QString dbAirport::retreiveAirportNameFromIcaoOrIata(QString identifier)
  25. {
  26. QString result = "";
  27. QSqlQuery query;
  28. query.prepare("SELECT name "
  29. "FROM airports WHERE icao LIKE ? OR iata LIKE ?");
  30. identifier.append("%");
  31. identifier.prepend("%");
  32. query.addBindValue(identifier);
  33. query.addBindValue(identifier);
  34. query.exec();
  35. if(query.first())
  36. {
  37. result.append(query.value(0).toString());
  38. return result;
  39. }else
  40. {
  41. result = result.left(result.length()-1);
  42. result.append("No matching airport found.");
  43. return result;
  44. }
  45. }
  46. QString dbAirport::retreiveAirportIdFromIcao(QString identifier)
  47. {
  48. QString result;
  49. QSqlQuery query;
  50. query.prepare("SELECT airport_id FROM airports WHERE icao = ?");
  51. query.addBindValue(identifier);
  52. query.exec();
  53. while(query.next())
  54. {
  55. result.append(query.value(0).toString());
  56. //qDebug() << "db::RetreiveAirportIdFromIcao says Airport found! #" << result;
  57. }
  58. return result;
  59. }
  60. QStringList dbAirport::completeIcaoOrIata(QString icaoStub)
  61. {
  62. QStringList result;
  63. QSqlQuery query;
  64. query.prepare("SELECT icao FROM airports WHERE icao LIKE ? OR iata LIKE ?");
  65. icaoStub.prepend("%"); icaoStub.append("%");
  66. query.addBindValue(icaoStub);
  67. query.addBindValue(icaoStub);
  68. query.exec();
  69. while(query.next())
  70. {
  71. result.append(query.value(0).toString());
  72. qDebug() << "db::CompleteIcaoOrIata says... Result:" << result;
  73. }
  74. return result;
  75. }
  76. /*!
  77. * \brief dbAirport::retreiveIataIcaoList Provides a QStringList of airport codes
  78. * in the database
  79. * \return ICAO and IATA codes in the database
  80. */
  81. QStringList dbAirport::retreiveIataIcaoList()
  82. {
  83. QSqlQuery query;
  84. query.prepare("SELECT icao, iata from airports");
  85. query.exec();
  86. QStringList result;
  87. while(query.next())
  88. {
  89. result.append(query.value(0).toString());
  90. result.append(query.value(1).toString());
  91. }
  92. result.removeAll(QString(""));
  93. return result;
  94. }
  95. /*!
  96. * \brief CheckICAOValid Verifies if a user input airport exists in the database
  97. * \param identifier can be ICAO or IATA airport codes.
  98. * \return bool if airport is in database.
  99. */
  100. bool dbAirport::checkICAOValid(QString identifier)
  101. {
  102. if(identifier.length() == 4)
  103. {
  104. QString check = retreiveAirportIdFromIcao(identifier);
  105. if(check.length() > 0)
  106. {
  107. //qDebug() << "db::CheckICAOValid says: Check passed!";
  108. return 1;
  109. }else
  110. {
  111. //qDebug() << "db::CheckICAOValid says: Check NOT passed! Lookup unsuccessful";
  112. return 0;
  113. }
  114. }else
  115. {
  116. //qDebug() << "db::CheckICAOValid says: Check NOT passed! Empty String NOT epico!";
  117. return 0;
  118. }
  119. }
  120. /*!
  121. * \brief retreiveIcaoCoordinates Looks up coordinates (lat,long) for a given airport
  122. * \param icao 4-letter code for the airport
  123. * \return {lat,lon} in decimal degrees
  124. */
  125. QVector<double> dbAirport::retreiveIcaoCoordinates(QString icao)
  126. {
  127. QSqlQuery query;
  128. query.prepare("SELECT lat, long "
  129. "FROM airports "
  130. "WHERE icao = ?");
  131. query.addBindValue(icao);
  132. query.exec();
  133. QVector<double> result;
  134. while(query.next()) {
  135. result.append(query.value(0).toDouble());
  136. result.append(query.value(1).toDouble());
  137. }
  138. return result;
  139. }
  140. /*!
  141. * \brief dbAirport::retreiveTimezonesIATA look up a list of timezones
  142. * for airports based on their IATA code.
  143. * \return
  144. */
  145. QVector<QPair<QString, QString>> dbAirport::retreiveTimezonesIATA()
  146. {
  147. QSqlQuery query;
  148. query.prepare("SELECT iata, tzolson "
  149. "FROM airports "
  150. "WHERE tzolson IS NOT NULL AND iata IS NOT NULL");
  151. query.exec();
  152. QVector<QPair<QString, QString>> result;
  153. while(query.next()) {
  154. result.append(QPair<QString, QString>(query.value(0).toString(),query.value(1).toString()));
  155. }
  156. return result;
  157. }