dbsetup.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 "dbsetup.h"
  19. /// db connection for debugging
  20. void dbSetup::connect()
  21. {
  22. const QString DRIVER("QSQLITE");
  23. if(QSqlDatabase::isDriverAvailable(DRIVER))
  24. {
  25. QSqlDatabase db = QSqlDatabase::addDatabase(DRIVER);
  26. db.setDatabaseName("aaadebug.db");
  27. if(!db.open())
  28. qWarning() << "DatabaseConnect - ERROR: " << db.lastError().text();
  29. }
  30. else
  31. qWarning() << "DatabaseConnect - ERROR: no driver " << DRIVER << " available";
  32. }
  33. const QString createTablePilots = "CREATE TABLE pilots ( "
  34. "pilot_id INTEGER, "
  35. "picfirstname TEXT, "
  36. "piclastname TEXT NOT NULL, "
  37. "alias TEXT, "
  38. "phone TEXT, "
  39. "email TEXT, "
  40. "rostername TEXT, "
  41. "PRIMARY KEY(pilot_id) "
  42. ")";
  43. const QString createTableAircraft = "CREATE TABLE aircraft ( "
  44. "aircraft_id integer, "
  45. "make TEXT, "
  46. "model TEXT, "
  47. "variant text, "
  48. "name TEXT, "
  49. "iata TEXT, "
  50. "icao TEXT, "
  51. "singlepilot INTEGER, "
  52. "multipilot INTEGER, "
  53. "singleengine INTEGER, "
  54. "multiengine INTEGER, "
  55. "turboprop INTEGER, "
  56. "jet INTEGER, "
  57. "heavy INTEGER, "
  58. "PRIMARY KEY(aircraft_id) "
  59. ") ";
  60. const QString createTableTails = "CREATE TABLE tails ( "
  61. "tail_id INTEGER, "
  62. "registration TEXT NOT NULL, "
  63. "aircraft_id INTEGER NOT NULL, "
  64. "company TEXT, "
  65. "PRIMARY KEY(tail_id), "
  66. "FOREIGN KEY(aircraft_id) REFERENCES aircraft(aircraft_id) "
  67. ")";
  68. const QString createTableFlights = "CREATE TABLE flights ( "
  69. "id INTEGER, "
  70. "doft NUMERIC NOT NULL, "
  71. "dept TEXT NOT NULL, "
  72. "tofb INTEGER NOT NULL, "
  73. "dest TEXT NOT NULL, "
  74. "tonb INTEGER NOT NULL, "
  75. "tblk INTEGER NOT NULL, "
  76. "pic INTEGER, "
  77. "acft INTEGER, "
  78. "PRIMARY KEY(id), "
  79. "FOREIGN KEY(pic) REFERENCES pilots(pilot_id), "
  80. "FOREIGN KEY(acft) REFERENCES tails(tail_id) "
  81. ")";
  82. //extras table might eventually be merged into flights table.
  83. const QString createTableExtras = "CREATE TABLE extras ( "
  84. "extras_id INTEGER NOT NULL, "
  85. "PilotFlying INTEGER, "
  86. "TOday INTEGER, "
  87. "TOnight INTEGER, "
  88. "LDGday INTEGER, "
  89. "LDGnight INTEGER, "
  90. "autoland INTEGER, "
  91. "tSPSE INTEGER, "
  92. "tSPME INTEGER, "
  93. "tMPME INTEGER, "
  94. "tNight INTEGER, "
  95. "tIFR INTEGER, "
  96. "tPIC INTEGER, "
  97. "tSIC INTEGER, "
  98. "tDual INTEGER, "
  99. "tInstructor INTEGER, "
  100. "tSIM INTEGER, "
  101. "ApproachType TEXT, "
  102. "FlightNumber TEXT, "
  103. "Remarks TEXT, "
  104. "PRIMARY KEY(extras_id) "
  105. ")";
  106. const QString createTableAirports = "CREATE TABLE airports( "
  107. "airport_id INTEGER primary key, "
  108. "icao TEXT NOT NULL, "
  109. "iata TEXT, "
  110. "name TEXT, "
  111. "lat REAL, "
  112. "long REAL, "
  113. "country TEXT, "
  114. "alt INTEGER, "
  115. "utcoffset INTEGER, "
  116. "tzolson TEXT "
  117. ")";
  118. const QString createTableScratchpad = "CREATE TABLE scratchpad ( "
  119. "id INTEGER, "
  120. "doft NUMERIC, "
  121. "dept TEXT, "
  122. "tofb INTEGER, "
  123. "dest TEXT, "
  124. "tonb INTEGER, "
  125. "tblk INTEGER, "
  126. "pic INTEGER, "
  127. "acft INTEGER, "
  128. "PRIMARY KEY(id) "
  129. ") ";
  130. const QString createTableSettings = "CREATE TABLE settings ( "
  131. "setting_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
  132. "setting TEXT, "
  133. "description TEXT "
  134. ")";
  135. void dbSetup::createTables()
  136. {
  137. QSqlQuery query;
  138. query.prepare(createTablePilots);
  139. query.exec();
  140. if(!query.isActive())
  141. qWarning() << "DatabaseInit - ERROR: " << query.lastError().text();
  142. query.prepare(createTableAircraft);
  143. query.exec();
  144. if(!query.isActive())
  145. qWarning() << "DatabaseInit - ERROR: " << query.lastError().text();
  146. query.prepare(createTableTails);
  147. query.exec();
  148. if(!query.isActive())
  149. qWarning() << "DatabaseInit - ERROR: " << query.lastError().text();
  150. query.prepare(createTableFlights);
  151. query.exec();
  152. if(!query.isActive())
  153. qWarning() << "DatabaseInit - ERROR: " << query.lastError().text();
  154. query.prepare(createTableExtras);
  155. query.exec();
  156. if(!query.isActive())
  157. qWarning() << "DatabaseInit - ERROR: " << query.lastError().text();
  158. query.prepare(createTableAirports);
  159. query.exec();
  160. if(!query.isActive())
  161. qWarning() << "DatabaseInit - ERROR: " << query.lastError().text();
  162. query.prepare(createTableScratchpad);
  163. query.exec();
  164. if(!query.isActive())
  165. qWarning() << "DatabaseInit - ERROR: " << query.lastError().text();
  166. query.prepare(createTableSettings);
  167. query.exec();
  168. if(!query.isActive())
  169. qWarning() << "DatabaseInit - ERROR: " << query.lastError().text();
  170. query.prepare("SELECT name FROM sqlite_master WHERE type='table'");
  171. query.exec();
  172. while (query.next()) {
  173. qDebug() << "Table: " << query.value(0).toString();
  174. }
  175. }