row.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. *openPilotLog - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020-2023 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. #ifndef ROW_H
  19. #define ROW_H
  20. #include "src/opl.h"
  21. namespace OPL {
  22. /*!
  23. * \brief The Row class provides an interface for retreiving and submitting entries from the database.
  24. *
  25. * \details The Row class is a base class and when instantiated, the appropriate subclass should be used.
  26. *
  27. * The database holds all the data related to the logbook in different tables. Each of these tables is composed of
  28. * rows. Each row has different columns and each column contains the data. As such, an entry can be thought of
  29. * as a row in the database. The row class encapsulates the data contained in each row.
  30. *
  31. * A row is uniquely identified by its position in the database, consisting of the table name (QString) and the row id (int).
  32. * A new entry, which is not yet in the database has the row id 0. If a new row object is created, the hasData
  33. * bool is set to false. Before submitting the entry to the database, setData() has to be called to fill the row
  34. * with data and toggle the verification bit.
  35. *
  36. * The Row Object holds all the necessary information the Database class needs to commit (create or update) it.
  37. * The Identifying information can be accessed with getRowId and getTable() / getTableName().
  38. *
  39. * For convenience and readabilty, subclasses exist that have the table property pre-set. These rows are then
  40. * referred to as entries. See AircraftEntry, FlightEntry etc. These subclasses have public static members which
  41. * hold the column names used in the sql database. These can be used to access the data held in the row by column.
  42. */
  43. class Row
  44. {
  45. public:
  46. /*!
  47. * \brief Create a new empty row entry
  48. */
  49. Row();
  50. /*!
  51. * \brief Create a row entry specifying its table, row id and row data.
  52. */
  53. Row(OPL::DbTable table_name, int row_id, const RowData_T &row_data);
  54. /*!
  55. * \brief Create a row entry specifying its table and row id.
  56. */
  57. Row(OPL::DbTable table_name, int row_id);
  58. /*!
  59. * \brief Create a row entry specifying its table name.
  60. * \param table_name
  61. */
  62. Row(OPL::DbTable table_name);
  63. Row(const Row&) = default;
  64. Row& operator=(const Row&) = default;
  65. /*!
  66. * \brief get the Row Data contained in the Row
  67. * \details The row data is a Map where the sql column name is the key and its value is the value.
  68. */
  69. const RowData_T& getData() const;
  70. void setData(const RowData_T &value);
  71. /*!
  72. * \brief Get the entries row id in the database
  73. */
  74. int getRowId() const;
  75. /*!
  76. * \brief Set the entries row id in the database
  77. */
  78. void setRowId(int value);
  79. OPL::DbTable getTable() const;
  80. /*!
  81. * \brief returns a string representation of the entries position in the database (Table and Row ID)
  82. */
  83. const QString getPosition() const;
  84. /*!
  85. * \brief get the name of the table in the sql database.
  86. * \details This method has to be overwritten in any subclass to return the table name, this should be
  87. * a purely virtual function but in order to be able to use row class instances this function is implemented
  88. * to return an empty strring in the base class.
  89. * \return The name of the table in the database containing a valid row, or an empty String
  90. */
  91. virtual const QString getTableName() const;
  92. /*!
  93. * \brief A Row entry is valid if its table and row are specified and if it contains row data.
  94. */
  95. bool isValid() const;
  96. /*!
  97. * \brief operator QString can be used for printing debug information to stdout
  98. */
  99. operator QString() const;
  100. private:
  101. OPL::DbTable table;
  102. int rowId;
  103. RowData_T rowData;
  104. protected:
  105. bool hasData;
  106. bool valid = true;
  107. };
  108. } // namespace OPL
  109. #endif // ROW_H