2
0

dbman.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 <QCoreApplication>
  19. #include <QDebug>
  20. #include <QSqlDatabase>
  21. #include <QSqlDriver>
  22. #include <QSqlError>
  23. #include <QSqlQuery>
  24. #include "calc.h"
  25. #include "dbpilots.h"
  26. #include "dbaircraft.h"
  27. #include <chrono>
  28. #include <QRandomGenerator>
  29. #include <QStandardPaths>
  30. class db
  31. {
  32. public:
  33. static void connect()
  34. {
  35. const QString DRIVER("QSQLITE");
  36. if(QSqlDatabase::isDriverAvailable(DRIVER))
  37. {
  38. QSqlDatabase db = QSqlDatabase::addDatabase(DRIVER);
  39. //QString pathtodb = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
  40. //db.setDatabaseName(pathtodb+"/logbook.db");
  41. //qDebug() << "Database: " << pathtodb+"/logbook.db";
  42. db.setDatabaseName("logbook.db");
  43. if(!db.open())
  44. qWarning() << "MainWindow::DatabaseConnect - ERROR: " << db.lastError().text();
  45. }
  46. else
  47. qWarning() << "MainWindow::DatabaseConnect - ERROR: no driver " << DRIVER << " available";
  48. }
  49. static void initexample()
  50. {
  51. QSqlQuery query("CREATE TABLE flights (id INTEGER PRIMARY KEY, date NUMERIC)");
  52. if(!query.isActive())
  53. qWarning() << "MainWindow::DatabaseInit - ERROR: " << query.lastError().text();
  54. }
  55. static void queryexamplenamedbinding()
  56. {
  57. QSqlQuery query;
  58. //query.prepare("SELECT * FROM people");
  59. //query.prepare("SELECT * FROM people WHERE name LIKE 'Linus' OR id = :id");
  60. query.prepare("SELECT * from people WHERE name LIKE :name");
  61. query.bindValue(":name", "%Linus%");
  62. query.bindValue(":id",2);
  63. query.exec();
  64. /*
  65. * QSqlQuery provides access to the result set one record at a time. After the call to exec(),
  66. * QSqlQuery's internal pointer is located one position before the first record.
  67. * We must call QSqlQuery::next() once to advance to the first record, then next() again
  68. * repeatedly to access the other records, until it returns false. Here's a typical loop that
  69. * iterates over all the records in order:
  70. * After a SELECT query is executed we have to browse the records (result rows) returned to access
  71. * the data. In this case we try to retrieve the first record calling the function first which
  72. * returns true when the query has been successful and false otherwise.
  73. */
  74. if(query.first());
  75. else
  76. qDebug() << ("No entry found");
  77. query.previous();//To go back to index 0
  78. while (query.next()) {
  79. QString name = query.value(1).toString();
  80. int id = query.value(0).toInt();
  81. qDebug() << name << id;
  82. }
  83. /*
  84. *The QSqlQuery::value() function returns the value of a field in the current record. Fields are
  85. * specified as zero-based indexes. QSqlQuery::value() returns a QVariant, a type that can hold
  86. * various C++ and core Qt data types such as int, QString, and QByteArray. The different database
  87. * types are automatically mapped into the closest Qt equivalent. In the code snippet, we call
  88. * QVariant::toString() and QVariant::toInt() to convert variants to QString and int.
  89. */
  90. }
  91. /*
  92. * Obsolete Functions
  93. */
  94. /*!
  95. * \brief SelectFlightDate Retreives Flights from the database currently not in use.
  96. * \param doft Date of flight for filtering result set. "ALL" means no filter.
  97. * \return Flight(s) for selected date.
  98. */
  99. static QVector<QString> SelectFlightDate(QString doft)
  100. {
  101. QSqlQuery query;
  102. if (doft == "ALL") // Special Selector
  103. {
  104. query.prepare("SELECT * FROM flights ORDER BY doft DESC, tofb ASC");
  105. qDebug() << "All flights selected";
  106. }else
  107. {
  108. query.prepare("SELECT * FROM flights WHERE doft = ? ORDER BY tofb ASC");
  109. query.addBindValue(doft);
  110. qDebug() << "Searching flights for " << doft;
  111. }
  112. query.exec();
  113. if(query.first());
  114. else
  115. {
  116. qDebug() << ("No flight with this date found");
  117. QVector<QString> flight; //return empty
  118. return flight;
  119. }
  120. query.previous();// To go back to index 0
  121. query.last(); // this can be very slow, used to determine query size since .size is not supported by sqlite
  122. int numRows = query.at() + 1; // Number of rows (flights) in the query
  123. query.first();
  124. query.previous();// Go back to index 0
  125. QVector<QString> flight(numRows * 9); // Every flight has 9 fields in the database
  126. int index = 0; // counter for output vector
  127. while (query.next()) {
  128. QString id = query.value(0).toString();
  129. QString doft = query.value(1).toString();
  130. QString dept = query.value(2).toString();
  131. QString tofb = calc::minutes_to_string((query.value(3).toString()));
  132. QString dest = query.value(4).toString();
  133. QString tonb = calc::minutes_to_string((query.value(5).toString()));
  134. QString tblk = calc::minutes_to_string((query.value(6).toString()));
  135. QString pic = dbPilots::retreivePilotNameFromID(query.value(7).toString());
  136. QString acft = dbAircraft::retreiveRegistration(query.value(8).toString());
  137. //qDebug() << id << doft << dept << tofb << dest << tonb << tblk << pic << acft << endl;
  138. flight[index] = id;
  139. ++index;
  140. flight[index] = doft;
  141. ++index;
  142. flight[index] = dept;
  143. ++index;
  144. flight[index] = tofb;
  145. ++index;
  146. flight[index] = dest;
  147. ++index;
  148. flight[index] = tonb;
  149. ++index;
  150. flight[index] = tblk;
  151. ++index;
  152. flight[index] = pic;
  153. ++index;
  154. flight[index] = acft;
  155. ++index;
  156. }
  157. return flight;
  158. }
  159. };