db.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #ifndef DB_H
  19. #define DB_H
  20. #include <QCoreApplication>
  21. #include <QSqlDatabase>
  22. #include <QSqlDriver>
  23. #include <QSqlError>
  24. #include <QSqlQuery>
  25. #include <QSqlRecord>
  26. #include <QSqlField>
  27. #include <QDir>
  28. #include <QDebug>
  29. class sql
  30. {
  31. public:
  32. enum tableName {flights, pilots, tails, aircraft, airports };
  33. enum editRole {createNew, editExisting};
  34. enum matchType {exactMatch, partialMatch};
  35. };
  36. class db
  37. {
  38. private:
  39. bool retreiveData();
  40. public:
  41. db(sql::tableName, int row_ID);
  42. db(sql::tableName, QMap<QString, QString> newData);
  43. bool isValid = false;
  44. QMap<QString, QString> data;
  45. QString table = QString();
  46. int row_id = 0;
  47. void setData(const QMap<QString, QString> &value);
  48. QMap<QString, QString> getData() const;
  49. //Functions
  50. bool update();
  51. bool commit();
  52. static void connect();
  53. static QVector<QString> getColumnNames(QString table);
  54. static bool exists(QString column, QString table, QString checkColumn, QString value, sql::matchType match);
  55. static QString singleSelect(QString column, QString table, QString checkColumn, QString value, sql::matchType match);
  56. static QVector<QString> multiSelect(QVector<QString> columns, QString table, QString checkColumn, QString value, sql::matchType match);
  57. static QVector<QString> multiSelect(QVector<QString> columns, QString table);
  58. static bool singleUpdate(QString table, QString column, QString value, QString checkColumn, QString checkvalue, sql::matchType match);
  59. static bool deleteRow(QString table, QString column, QString value, sql::matchType match);
  60. static QVector<QString> customQuery(QString query, int returnValues);
  61. // Debug functionality
  62. void print();
  63. QString debug();
  64. operator QString() { return debug(); } //overload for compatibility with qDebug()
  65. };
  66. #endif // DB_H