dbairport.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 "dbman.cpp"
  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 CheckICAOValid Verifies if a user input airport exists in the database
  79. * \param identifier can be ICAO or IATA airport codes.
  80. * \return bool if airport is in database.
  81. */
  82. bool dbAirport::checkICAOValid(QString identifier)
  83. {
  84. if(identifier.length() == 4)
  85. {
  86. QString check = retreiveAirportIdFromIcao(identifier);
  87. if(check.length() > 0)
  88. {
  89. //qDebug() << "db::CheckICAOValid says: Check passed!";
  90. return 1;
  91. }else
  92. {
  93. //qDebug() << "db::CheckICAOValid says: Check NOT passed! Lookup unsuccessful";
  94. return 0;
  95. }
  96. }else
  97. {
  98. //qDebug() << "db::CheckICAOValid says: Check NOT passed! Empty String NOT epico!";
  99. return 0;
  100. }
  101. }
  102. /*!
  103. * \brief retreiveIcaoCoordinates Looks up coordinates (lat,long) for a given airport
  104. * \param icao 4-letter code for the airport
  105. * \return {lat,lon} in decimal degrees
  106. */
  107. QVector<double> dbAirport::retreiveIcaoCoordinates(QString icao)
  108. {
  109. QSqlQuery query;
  110. query.prepare("SELECT lat, long "
  111. "FROM airports "
  112. "WHERE icao = ?");
  113. query.addBindValue(icao);
  114. query.exec();
  115. QVector<double> result;
  116. while(query.next()) {
  117. result.append(query.value(0).toDouble());
  118. result.append(query.value(1).toDouble());
  119. }
  120. return result;
  121. }