2
0

databasecache.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #ifndef DATABASECACHE_H
  2. #define DATABASECACHE_H
  3. #include "QtWidgets/qcompleter.h"
  4. #include "src/opl.h"
  5. #include <QtCore>
  6. namespace OPL{
  7. using IdMap = QHash<int, QString>;
  8. #define DBCache DatabaseCache::getInstance()
  9. /*!
  10. * \brief Caches certain often accessed database content in memory
  11. * \details Access to the database is rather slow and memory these days is cheap enough to cache some contents for ease
  12. * and speed of access. This class provides lists of database entries that are used as inputs for QCompleter instances
  13. * as well as maps which contain database entries and their associated row ids, which are used for user input verification.
  14. *
  15. * The Cache can be accessed by using the DBCACHE macro and needs to be updated whenever the database contents are modified.
  16. */
  17. class DatabaseCache : public QObject
  18. {
  19. public:
  20. static DatabaseCache& getInstance() {
  21. static DatabaseCache instance;
  22. return instance;
  23. }
  24. DatabaseCache(DatabaseCache const&) = delete;
  25. void operator=(DatabaseCache const&) = delete;
  26. enum CompleterTarget {PilotNames, Tails, AircraftTypes, Airports, AirportsICAO, AirportsIATA, Companies};
  27. void init();
  28. const IdMap &getAirportsMapICAO() const;
  29. const IdMap &getAirportsMapIATA() const;
  30. const IdMap &getPilotNamesMap() const;
  31. const QStringList &getPilotNamesList() const;
  32. const QStringList &getTailsList() const;
  33. const QStringList &getAirportList() const;
  34. const QStringList &getCompaniesList() const;
  35. private:
  36. Q_OBJECT
  37. DatabaseCache() {};
  38. // Id Maps
  39. IdMap airportsMapICAO;
  40. IdMap airportsMapIATA;
  41. IdMap pilotNamesMap;
  42. IdMap tailsMap;
  43. // Lists
  44. QStringList pilotNamesList;
  45. QStringList tailsList;
  46. QStringList airportList;
  47. QStringList companiesList;
  48. const IdMap fetchMap(CompleterTarget target);
  49. const QStringList fetchList(CompleterTarget target);
  50. void updateTails();
  51. void updateAirports();
  52. void updateSimulators();
  53. void updatePilots();
  54. public slots:
  55. void update(const OPL::DbTable table);
  56. };
  57. }// namespace OPL
  58. #endif // DATABASECACHE_H