dbairport.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. return 1;
  108. }else
  109. {
  110. return 0;
  111. }
  112. }else
  113. {
  114. return 0;
  115. }
  116. }
  117. /*!
  118. * \brief retreiveIcaoCoordinates Looks up coordinates (lat,long) for a given airport
  119. * \param icao 4-letter code for the airport
  120. * \return {lat,lon} in decimal degrees
  121. */
  122. QVector<double> dbAirport::retreiveIcaoCoordinates(QString icao)
  123. {
  124. QSqlQuery query;
  125. query.prepare("SELECT lat, long "
  126. "FROM airports "
  127. "WHERE icao = ?");
  128. query.addBindValue(icao);
  129. query.exec();
  130. QVector<double> result;
  131. while(query.next()) {
  132. result.append(query.value(0).toDouble());
  133. result.append(query.value(1).toDouble());
  134. }
  135. return result;
  136. }
  137. /*!
  138. * \brief dbAirport::retreiveTimezonesIATA look up a list of timezones
  139. * for airports based on their IATA code.
  140. * \return
  141. */
  142. QVector<QPair<QString, QString>> dbAirport::retreiveTimezonesIATA()
  143. {
  144. QSqlQuery query;
  145. query.prepare("SELECT iata, tzolson "
  146. "FROM airports "
  147. "WHERE tzolson IS NOT NULL AND iata IS NOT NULL");
  148. query.exec();
  149. QVector<QPair<QString, QString>> result;
  150. while(query.next()) {
  151. result.append(QPair<QString, QString>(query.value(0).toString(),query.value(1).toString()));
  152. }
  153. return result;
  154. }