dbairport.cpp 4.7 KB

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