dbairport.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "dbairport.h"
  2. #include "dbman.cpp"
  3. /*!
  4. * \brief RetreiveAirportNameFromIcaoOrIata Looks up Airport Name
  5. * \param identifier can be ICAO or IATA airport codes.
  6. * \return The name of the airport associated with the above code
  7. */
  8. QString dbAirport::retreiveAirportNameFromIcaoOrIata(QString identifier)
  9. {
  10. QString result = "";
  11. QSqlQuery query;
  12. query.prepare("SELECT name "
  13. "FROM airports WHERE icao LIKE ? OR iata LIKE ?");
  14. identifier.append("%");
  15. identifier.prepend("%");
  16. query.addBindValue(identifier);
  17. query.addBindValue(identifier);
  18. query.exec();
  19. if(query.first())
  20. {
  21. result.append(query.value(0).toString());
  22. return result;
  23. }else
  24. {
  25. result = result.left(result.length()-1);
  26. result.append("No matching airport found.");
  27. return result;
  28. }
  29. }
  30. QString dbAirport::retreiveAirportIdFromIcao(QString identifier)
  31. {
  32. QString result;
  33. QSqlQuery query;
  34. query.prepare("SELECT airport_id FROM airports WHERE icao = ?");
  35. query.addBindValue(identifier);
  36. query.exec();
  37. while(query.next())
  38. {
  39. result.append(query.value(0).toString());
  40. //qDebug() << "db::RetreiveAirportIdFromIcao says Airport found! #" << result;
  41. }
  42. return result;
  43. }
  44. QStringList dbAirport::completeIcaoOrIata(QString icaoStub)
  45. {
  46. QStringList result;
  47. QSqlQuery query;
  48. query.prepare("SELECT icao FROM airports WHERE icao LIKE ? OR iata LIKE ?");
  49. icaoStub.prepend("%"); icaoStub.append("%");
  50. query.addBindValue(icaoStub);
  51. query.addBindValue(icaoStub);
  52. query.exec();
  53. while(query.next())
  54. {
  55. result.append(query.value(0).toString());
  56. qDebug() << "db::CompleteIcaoOrIata says... Result:" << result;
  57. }
  58. return result;
  59. }
  60. /*!
  61. * \brief CheckICAOValid Verifies if a user input airport exists in the database
  62. * \param identifier can be ICAO or IATA airport codes.
  63. * \return bool if airport is in database.
  64. */
  65. bool dbAirport::checkICAOValid(QString identifier)
  66. {
  67. if(identifier.length() == 4)
  68. {
  69. QString check = retreiveAirportIdFromIcao(identifier);
  70. if(check.length() > 0)
  71. {
  72. //qDebug() << "db::CheckICAOValid says: Check passed!";
  73. return 1;
  74. }else
  75. {
  76. //qDebug() << "db::CheckICAOValid says: Check NOT passed! Lookup unsuccessful";
  77. return 0;
  78. }
  79. }else
  80. {
  81. //qDebug() << "db::CheckICAOValid says: Check NOT passed! Empty String NOT epico!";
  82. return 0;
  83. }
  84. }
  85. /*!
  86. * \brief retreiveIcaoCoordinates Looks up coordinates (lat,long) for a given airport
  87. * \param icao 4-letter code for the airport
  88. * \return {lat,lon} in decimal degrees
  89. */
  90. QVector<double> dbAirport::retreiveIcaoCoordinates(QString icao)
  91. {
  92. QSqlQuery query;
  93. query.prepare("SELECT lat, long "
  94. "FROM airports "
  95. "WHERE icao = ?");
  96. query.addBindValue(icao);
  97. query.exec();
  98. QVector<double> result;
  99. while(query.next()) {
  100. result.append(query.value(0).toDouble());
  101. result.append(query.value(1).toDouble());
  102. }
  103. return result;
  104. }