dbman.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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 <chrono>
  26. #include <QRandomGenerator>
  27. #include <QStandardPaths>
  28. class db
  29. {
  30. public:
  31. static void connect()
  32. {
  33. const QString DRIVER("QSQLITE");
  34. if(QSqlDatabase::isDriverAvailable(DRIVER))
  35. {
  36. QSqlDatabase db = QSqlDatabase::addDatabase(DRIVER);
  37. //QString pathtodb = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
  38. //db.setDatabaseName(pathtodb+"/flog.db");
  39. //qDebug() << "Database: " << pathtodb+"/logbook.db";
  40. db.setDatabaseName("logbook.db");
  41. if(!db.open())
  42. qWarning() << "MainWindow::DatabaseConnect - ERROR: " << db.lastError().text();
  43. }
  44. else
  45. qWarning() << "MainWindow::DatabaseConnect - ERROR: no driver " << DRIVER << " available";
  46. }
  47. static void initexample()
  48. {
  49. QSqlQuery query("CREATE TABLE people (id INTEGER PRIMARY KEY, name TEXT)");
  50. if(!query.isActive())
  51. qWarning() << "MainWindow::DatabaseInit - ERROR: " << query.lastError().text();
  52. }
  53. static void queryexamplenamedbinding()
  54. {
  55. QSqlQuery query;
  56. //query.prepare("SELECT * FROM people");
  57. //query.prepare("SELECT * FROM people WHERE name LIKE 'Linus' OR id = :id");
  58. query.prepare("SELECT * from people WHERE name LIKE :name");
  59. query.bindValue(":name", "%Linus%");
  60. query.bindValue(":id",2);
  61. query.exec();
  62. /*
  63. * QSqlQuery provides access to the result set one record at a time. After the call to exec(),
  64. * QSqlQuery's internal pointer is located one position before the first record.
  65. * We must call QSqlQuery::next() once to advance to the first record, then next() again
  66. * repeatedly to access the other records, until it returns false. Here's a typical loop that
  67. * iterates over all the records in order:
  68. * After a SELECT query is executed we have to browse the records (result rows) returned to access
  69. * the data. In this case we try to retrieve the first record calling the function first which
  70. * returns true when the query has been successful and false otherwise.
  71. */
  72. if(query.first());
  73. else
  74. qDebug() << ("No entry found");
  75. query.previous();//To go back to index 0
  76. while (query.next()) {
  77. QString name = query.value(1).toString();
  78. int id = query.value(0).toInt();
  79. qDebug() << name << id;
  80. }
  81. /*
  82. *The QSqlQuery::value() function returns the value of a field in the current record. Fields are
  83. * specified as zero-based indexes. QSqlQuery::value() returns a QVariant, a type that can hold
  84. * various C++ and core Qt data types such as int, QString, and QByteArray. The different database
  85. * types are automatically mapped into the closest Qt equivalent. In the code snippet, we call
  86. * QVariant::toString() and QVariant::toInt() to convert variants to QString and int.
  87. */
  88. }
  89. /*
  90. * Flights Database Related Functions
  91. */
  92. static QVector<QString> SelectFlightDate(QString doft)
  93. {
  94. QSqlQuery query;
  95. if (doft == "ALL")
  96. {
  97. query.prepare("SELECT * FROM flights ORDER BY doft DESC, tofb ASC");
  98. qDebug() << "All flights selected";
  99. }else
  100. {
  101. query.prepare("SELECT * FROM flights WHERE doft = ? ORDER BY tofb ASC");
  102. query.addBindValue(doft);
  103. qDebug() << "Searching flights for " << doft;
  104. }
  105. query.exec();
  106. if(query.first());
  107. else
  108. {
  109. qDebug() << ("No flight with this date found");
  110. QVector<QString> flight; //return empty
  111. return flight;
  112. }
  113. query.previous();// To go back to index 0
  114. query.last(); // this can be very slow, used to determine query size since .size is not supported by sqlite
  115. int numRows = query.at() + 1; // Number of rows (flights) in the query
  116. query.first();
  117. query.previous();// Go back to index 0
  118. QVector<QString> flight(numRows * 9); // Every flight has 9 fields in the database
  119. int index = 0; // counter for output vector
  120. while (query.next()) {
  121. QString id = query.value(0).toString();
  122. QString doft = query.value(1).toString();
  123. QString dept = query.value(2).toString();
  124. QString tofb = calc::minutes_to_string((query.value(3).toString()));
  125. QString dest = query.value(4).toString();
  126. QString tonb = calc::minutes_to_string((query.value(5).toString()));
  127. QString tblk = calc::minutes_to_string((query.value(6).toString()));
  128. QString pic = db::RetreivePilotNameFromID(query.value(7).toString());
  129. QString acft = db::RetreiveRegistration(query.value(8).toString());
  130. //qDebug() << id << doft << dept << tofb << dest << tonb << tblk << pic << acft << endl;
  131. flight[index] = id;
  132. ++index;
  133. flight[index] = doft;
  134. ++index;
  135. flight[index] = dept;
  136. ++index;
  137. flight[index] = tofb;
  138. ++index;
  139. flight[index] = dest;
  140. ++index;
  141. flight[index] = tonb;
  142. ++index;
  143. flight[index] = tblk;
  144. ++index;
  145. flight[index] = pic;
  146. ++index;
  147. flight[index] = acft;
  148. ++index;
  149. }
  150. return flight;
  151. }
  152. static QVector<QString> SelectFlightById(QString flight_id)
  153. {
  154. QSqlQuery query;
  155. query.prepare("SELECT * FROM flights WHERE id = ?");
  156. query.addBindValue(flight_id);
  157. query.exec();
  158. if(query.first());
  159. else
  160. {
  161. qDebug() << "db::SelectFlightById - No Flight with this ID found";
  162. QVector<QString> flight; //return empty
  163. return flight;
  164. }
  165. QVector<QString> flight;
  166. flight.append(query.value(0).toString());
  167. flight.append(query.value(1).toString());
  168. flight.append(query.value(2).toString());
  169. flight.append(query.value(3).toString());
  170. flight.append(query.value(4).toString());
  171. flight.append(query.value(5).toString());
  172. flight.append(query.value(6).toString());
  173. flight.append(query.value(7).toString());
  174. flight.append(query.value(8).toString());
  175. qDebug() << "db::SelectFlightById - retreived flight: " << flight;
  176. return flight;
  177. }
  178. static QVector<QString> CreateFlightVectorFromInput(QString doft, QString dept, QTime tofb, QString dest, QTime tonb, QTime tblk, QString pic, QString acft)
  179. {
  180. QVector<QString> flight;
  181. flight.insert(0, ""); // ID, created as primary key during commit
  182. flight.insert(1, doft);
  183. flight.insert(2, dept);
  184. flight.insert(3, QString::number(calc::time_to_minutes(tofb)));
  185. flight.insert(4, dest);
  186. flight.insert(5, QString::number(calc::time_to_minutes(tonb)));
  187. flight.insert(6, QString::number(calc::time_to_minutes(tblk)));
  188. flight.insert(7, pic); // lookup and matching tbd
  189. flight.insert(8, acft);// lookup and matching tbd
  190. //qDebug() << flight;
  191. return flight;
  192. }
  193. static int CommitFlight(QVector<QString> flight)// flight vector shall always have length 9
  194. {
  195. QSqlQuery query;
  196. query.prepare("INSERT INTO flights (doft, dept, tofb, dest, tonb, tblk, pic, acft) "
  197. "VALUES (:doft, :dept, :tofb, :dest, :tonb, :tblk, :pic, :acft)");
  198. //flight[0] is primary key, not required for commit
  199. query.bindValue(":doft", flight[1]); //string
  200. query.bindValue(":dept", flight[2]);
  201. query.bindValue(":tofb", flight[3].toInt()); //int
  202. query.bindValue(":dest", flight[4]);
  203. query.bindValue(":tonb", flight[5].toInt());
  204. query.bindValue(":tblk", flight[6].toInt());
  205. query.bindValue(":pic", flight[7].toInt());
  206. query.bindValue(":acft", flight[8].toInt());
  207. query.exec();
  208. qDebug() << "Error message for commiting flight: " << query.lastError().text();
  209. QSqlQuery query2;
  210. query2.prepare("INSERT INTO extras DEFAULT VALUES");
  211. query2.exec();
  212. qDebug() << "Creating extras entry" << query2.lastError().text();
  213. return 0;
  214. }
  215. static int CommitToScratchpad(QVector<QString> flight)// to store input mask
  216. {
  217. //qDebug() << "Saving invalid flight to scratchpad";
  218. QSqlQuery query;
  219. query.prepare("INSERT INTO scratchpad (doft, dept, tofb, dest, tonb, tblk, pic, acft) "
  220. "VALUES (:doft, :dept, :tofb, :dest, :tonb, :tblk, :pic, :acft)");
  221. //flight[0] is primary key, not required for commit
  222. query.bindValue(":doft", flight[1]); //string
  223. query.bindValue(":dept", flight[2]);
  224. query.bindValue(":tofb", flight[3].toInt()); //int
  225. query.bindValue(":dest", flight[4]);
  226. query.bindValue(":tonb", flight[5].toInt());
  227. query.bindValue(":tblk", flight[6].toInt());
  228. query.bindValue(":pic", flight[7].toInt());
  229. query.bindValue(":acft", flight[8].toInt());
  230. query.exec();
  231. qDebug() << query.lastError().text();
  232. return 0;
  233. }
  234. static bool CheckScratchpad() // see if scratchpad is empty
  235. {
  236. //qDebug() << "Checking if scratchpad contains data";
  237. QSqlQuery query;
  238. query.prepare("SELECT * FROM scratchpad");
  239. query.exec();
  240. if(query.first())
  241. {
  242. //qDebug() << "Scratchpad contains data";
  243. return 1;
  244. }
  245. else
  246. {
  247. //qDebug() << ("Scratchpad contains NO data");
  248. return 0;
  249. }
  250. }
  251. static QVector<QString> RetreiveScratchpad()
  252. {
  253. //qDebug() << "Retreiving invalid flight from scratchpad";
  254. QSqlQuery query;
  255. query.prepare("SELECT * FROM scratchpad");
  256. query.exec();
  257. if(query.first());
  258. else
  259. {
  260. //qDebug() << ("scratchpad empty");
  261. QVector<QString> flight; //return empty
  262. return flight;
  263. }
  264. query.previous();// To go back to index 0
  265. //query.last(); // this can be very slow, used to determine query size since .size is not supported by sqlite
  266. //int numRows = query.at() + 1; // Number of rows (flights) in the query
  267. //query.first();
  268. //query.previous();// Go back to index 0
  269. //QVector<QString> flight(numRows * 9); // Every flight has 9 fields in the database
  270. QVector<QString> flight(9);
  271. int index = 0; // counter for output vector
  272. while (query.next()) {
  273. QString id = query.value(0).toString();
  274. QString doft = query.value(1).toString();
  275. QString dept = query.value(2).toString();
  276. QString tofb = calc::minutes_to_string((query.value(3).toString()));
  277. QString dest = query.value(4).toString();
  278. QString tonb = calc::minutes_to_string((query.value(5).toString()));
  279. QString tblk = calc::minutes_to_string((query.value(6).toString()));
  280. QString pic = query.value(7).toString();
  281. QString acft = query.value(8).toString();
  282. //qDebug() << id << doft << dept << tofb << dest << tonb << tblk << pic << acft << endl;
  283. flight[index] = id;
  284. ++index;
  285. flight[index] = doft;
  286. ++index;
  287. flight[index] = dept;
  288. ++index;
  289. flight[index] = tofb;
  290. ++index;
  291. flight[index] = dest;
  292. ++index;
  293. flight[index] = tonb;
  294. ++index;
  295. flight[index] = tblk;
  296. ++index;
  297. flight[index] = pic;
  298. ++index;
  299. flight[index] = acft;
  300. ++index;
  301. }
  302. ClearScratchpad();
  303. return flight;
  304. }
  305. static void ClearScratchpad()
  306. {
  307. qDebug() << "Deleting scratchpad";
  308. QSqlQuery query;
  309. query.prepare("DELETE FROM scratchpad;");
  310. query.exec();
  311. }
  312. static bool DeleteFlightById(QString flight_id)
  313. {
  314. QSqlQuery query;
  315. query.prepare("DELETE FROM flights WHERE id = ?");
  316. query.addBindValue(flight_id);
  317. query.exec();
  318. QString error = query.lastError().text();
  319. qDebug() << "db::DeleteFlightById: Removing flight with ID#: " << flight_id << "Query Error: " << error;
  320. if(error.length() > 0)
  321. {
  322. return false;
  323. }else
  324. {
  325. return true;
  326. }
  327. }
  328. /*
  329. * Pilots Database Related Functions
  330. */
  331. /*!
  332. * \brief RetreivePilotNameFromID Looks up pilot ID in database
  333. * \param pilotID pilot_id in database
  334. * \return Pilot Name
  335. */
  336. static QString RetreivePilotNameFromID(QString pilotID)
  337. {
  338. QString pilotName("");
  339. if (pilotID == "1")
  340. {
  341. pilotName = "self";
  342. return pilotName;
  343. }
  344. QSqlQuery query;
  345. query.prepare("SELECT piclastname, picfirstname, alias FROM pilots WHERE pilot_id == ?");
  346. query.addBindValue(pilotID.toInt());
  347. query.exec();
  348. while (query.next()) {
  349. pilotName.append(query.value(0).toString());
  350. pilotName.append(", ");
  351. pilotName.append(query.value(1).toString());//.left(1));
  352. }
  353. if(pilotName.length() == 0)
  354. {
  355. qDebug() << ("No Pilot with this ID found");
  356. }
  357. return pilotName;
  358. }
  359. static QString RetreivePilotIdFromString(QString lastname, QString firstname)
  360. {
  361. QSqlQuery query;
  362. query.prepare("SELECT pilot_id from pilots "
  363. "WHERE piclastname = ? AND picfirstname LIKE ?");
  364. query.addBindValue(lastname);
  365. firstname.prepend("%"); firstname.append("%");
  366. query.addBindValue(firstname);
  367. query.exec();
  368. QString id;
  369. if(query.first()){id.append(query.value(0).toString());}
  370. return id;
  371. }
  372. static QStringList RetreivePilotNameFromString(QString searchstring)
  373. /* Searches the pilot Name in the Database and returns the name as a vector of results
  374. * unless the pilot in command is the logbook owner.
  375. */
  376. {
  377. QString firstname = searchstring; //To Do: Two control paths, one for single word, query as before with only searchstring
  378. QString lastname = searchstring; // second control path with comma, lastname like AND firstname like
  379. if(searchstring.contains(QLatin1Char(',')))
  380. {
  381. QStringList namelist = searchstring.split(QLatin1Char(','));
  382. QString lastname = namelist[0].trimmed();
  383. lastname = lastname.toLower();
  384. lastname[0] = lastname[0].toUpper();
  385. lastname.prepend("%"), lastname.append("%");
  386. QString firstname = namelist[1].trimmed();
  387. if(firstname.length()>1)
  388. {
  389. firstname = firstname.toLower();
  390. firstname[0] = firstname[0].toUpper();
  391. firstname.prepend("%"), firstname.append("%");
  392. }
  393. qDebug() << "db::RetreivePilotNameFromString: first last after comma";
  394. qDebug() << firstname << lastname;
  395. }
  396. QSqlQuery query;
  397. query.prepare("SELECT piclastname, picfirstname, alias "
  398. "FROM pilots WHERE "
  399. "picfirstname LIKE ? OR piclastname LIKE ? OR alias LIKE ?");
  400. searchstring.prepend("%");
  401. searchstring.append("%");
  402. query.addBindValue(firstname);
  403. query.addBindValue(lastname);
  404. query.addBindValue(searchstring);
  405. query.exec();
  406. QStringList result;
  407. while (query.next()) {
  408. QString piclastname = query.value(0).toString();
  409. QString picfirstname = query.value(1).toString();
  410. QString alias = query.value(2).toString();
  411. QString name = piclastname + ", " + picfirstname;
  412. result.append(name);
  413. }
  414. qDebug() << "db::RetreivePilotNameFromString Result: " << result;
  415. //qDebug() << query.lastError();
  416. if(result.size() == 0)
  417. {
  418. qDebug() << ("db::RetreivePilotNameFromString: No Pilot found");
  419. return result;
  420. }
  421. return result;
  422. }
  423. /*!
  424. * \brief newPicGetString This function is returning a QStringList for the QCompleter in the NewFlight::newPic line edit
  425. * A regular expression limits the input possibilities to only characters,
  426. * followed by an optional ',' and 1 whitespace, e.g.:
  427. * Miller, Jim ->valid / Miller, Jim -> invalid / Miller,, Jim -> invalid
  428. * Miller Jim -> valid / Miller Jim ->invalid
  429. * Jim Miller-> valid
  430. * \param searchstring
  431. * \return
  432. */
  433. static QStringList newPicGetString(QString searchstring)
  434. {
  435. QStringList result;
  436. QStringList searchlist;
  437. if(searchstring == "self")
  438. {
  439. result.append("self");
  440. qDebug() << "Pilot is self";
  441. return result;
  442. }
  443. //Fall 1) Nachname, Vorname
  444. if(searchstring.contains(QLatin1Char(',')))
  445. {
  446. QStringList namelist = searchstring.split(QLatin1Char(','));
  447. QString name1 = namelist[0].trimmed();
  448. name1 = name1.toLower();
  449. name1[0] = name1[0].toUpper();
  450. searchlist.append(name1);
  451. if(namelist[1].length() > 1)
  452. {
  453. QString name2 = namelist[1].trimmed();
  454. name2 = name2.toLower();
  455. name2[0] = name2[0].toUpper();
  456. searchlist.append(name2);
  457. }
  458. }
  459. //Fall 2) Vorname Nachname
  460. if(searchstring.contains(" ") && !searchstring.contains(QLatin1Char(',')))
  461. {
  462. QStringList namelist = searchstring.split(" ");
  463. QString name1 = namelist[0].trimmed();
  464. name1 = name1.toLower();
  465. name1[0] = name1[0].toUpper();
  466. searchlist.append(name1);
  467. if(namelist[1].length() > 1) //To avoid index out of range if the searchstring is one word followed by only one whitespace
  468. {
  469. QString name2 = namelist[1].trimmed();
  470. name2 = name2.toLower();
  471. name2[0] = name2[0].toUpper();
  472. searchlist.append(name2);
  473. }
  474. }
  475. //Fall 3) Name
  476. if(!searchstring.contains(" ") && !searchstring.contains(QLatin1Char(',')))
  477. {
  478. QString name1 = searchstring.toLower();
  479. name1[0] = name1[0].toUpper();
  480. searchlist.append(name1);
  481. }
  482. if(searchlist.length() == 1)
  483. {
  484. QSqlQuery query;
  485. query.prepare("SELECT piclastname, picfirstname FROM pilots "
  486. "WHERE piclastname LIKE ?");
  487. query.addBindValue(searchlist[0] + '%');
  488. query.exec();
  489. while(query.next())
  490. {
  491. result.append(query.value(0).toString() + ", " + query.value(1).toString());
  492. }
  493. QSqlQuery query2;
  494. query2.prepare("SELECT piclastname, picfirstname FROM pilots "
  495. "WHERE picfirstname LIKE ?");
  496. query2.addBindValue(searchlist[0] + '%');
  497. query2.exec();
  498. while(query2.next())
  499. {
  500. result.append(query2.value(0).toString() + ", " + query2.value(1).toString());
  501. }
  502. }else
  503. {
  504. QSqlQuery query;
  505. query.prepare("SELECT piclastname, picfirstname FROM pilots "
  506. "WHERE piclastname LIKE ? AND picfirstname LIKE ?");
  507. query.addBindValue(searchlist[0] + '%');
  508. query.addBindValue(searchlist[1] + '%');
  509. query.exec();
  510. while(query.next())
  511. {
  512. result.append(query.value(0).toString() + ", " + query.value(1).toString());
  513. }
  514. QSqlQuery query2;
  515. query2.prepare("SELECT piclastname, picfirstname FROM pilots "
  516. "WHERE picfirstname LIKE ? AND piclastname LIKE ?");
  517. query2.addBindValue(searchlist[0] + '%');
  518. query2.addBindValue(searchlist[1] + '%');
  519. query2.exec();
  520. while(query2.next())
  521. {
  522. result.append(query2.value(0).toString() + ", " + query2.value(1).toString());
  523. }
  524. }
  525. qDebug() << "db::newPic Result" << result.length() << result;
  526. if(result.length() == 0)
  527. {
  528. //try first name search
  529. qDebug() << "No Pilot with this last name found. trying first name search.";
  530. return result;
  531. }else
  532. {
  533. return result;
  534. }
  535. }
  536. static QString newPicGetId(QString name)
  537. {
  538. QString result;
  539. QStringList nameparts = name.split(QLatin1Char(','));
  540. QString lastname = nameparts[0].trimmed();
  541. lastname = lastname.toLower(); lastname[0] = lastname[0].toUpper();
  542. QString firstname = nameparts[1].trimmed();
  543. firstname = firstname.toLower(); firstname[0] = firstname[0].toUpper();
  544. firstname.prepend("%"); firstname.append("%");
  545. QSqlQuery query;
  546. query.prepare("SELECT pilot_id FROM pilots "
  547. "WHERE piclastname = ? AND picfirstname LIKE ?");
  548. query.addBindValue(lastname);
  549. query.addBindValue(firstname);
  550. query.exec();
  551. while (query.next())
  552. {
  553. result.append(query.value(0).toString());
  554. }
  555. qDebug() << "newPicGetId: result = " << result;
  556. return result;
  557. }
  558. /*
  559. * Airport Database Related Functions
  560. */
  561. /*!
  562. * \brief RetreiveAirportNameFromIcaoOrIata Looks up Airport Name
  563. * \param identifier can be ICAO or IATA airport codes.
  564. * \return The name of the airport associated with the above code
  565. */
  566. static QString RetreiveAirportNameFromIcaoOrIata(QString identifier)
  567. {
  568. QString result = "";
  569. QSqlQuery query;
  570. query.prepare("SELECT name "
  571. "FROM airports WHERE icao LIKE ? OR iata LIKE ?");
  572. identifier.append("%");
  573. identifier.prepend("%");
  574. query.addBindValue(identifier);
  575. query.addBindValue(identifier);
  576. query.exec();
  577. if(query.first())
  578. {
  579. result.append(query.value(0).toString());
  580. return result;
  581. }else
  582. {
  583. result = result.left(result.length()-1);
  584. result.append("No matching airport found.");
  585. return result;
  586. }
  587. }
  588. static QString RetreiveAirportIdFromIcao(QString identifier)
  589. {
  590. QString result;
  591. QSqlQuery query;
  592. query.prepare("SELECT airport_id FROM airports WHERE icao = ?");
  593. query.addBindValue(identifier);
  594. query.exec();
  595. while(query.next())
  596. {
  597. result.append(query.value(0).toString());
  598. //qDebug() << "db::RetreiveAirportIdFromIcao says Airport found! #" << result;
  599. }
  600. return result;
  601. }
  602. static QStringList CompleteIcaoOrIata(QString icaoStub)
  603. {
  604. QStringList result;
  605. QSqlQuery query;
  606. query.prepare("SELECT icao FROM airports WHERE icao LIKE ? OR iata LIKE ?");
  607. icaoStub.prepend("%"); icaoStub.append("%");
  608. query.addBindValue(icaoStub);
  609. query.addBindValue(icaoStub);
  610. query.exec();
  611. while(query.next())
  612. {
  613. result.append(query.value(0).toString());
  614. qDebug() << "db::CompleteIcaoOrIata says... Result:" << result;
  615. }
  616. return result;
  617. }
  618. /*!
  619. * \brief CheckICAOValid Verifies if a user input airport exists in the database
  620. * \param identifier can be ICAO or IATA airport codes.
  621. * \return bool if airport is in database.
  622. */
  623. static bool CheckICAOValid(QString identifier)
  624. {
  625. if(identifier.length() == 4)
  626. {
  627. QString check = RetreiveAirportIdFromIcao(identifier);
  628. if(check.length() > 0)
  629. {
  630. //qDebug() << "db::CheckICAOValid says: Check passed!";
  631. return 1;
  632. }else
  633. {
  634. //qDebug() << "db::CheckICAOValid says: Check NOT passed! Lookup unsuccessful";
  635. return 0;
  636. }
  637. }else
  638. {
  639. //qDebug() << "db::CheckICAOValid says: Check NOT passed! Empty String NOT epico!";
  640. return 0;
  641. }
  642. }
  643. /*!
  644. * \brief retreiveIcaoCoordinates Looks up coordinates (lat,long) for a given airport
  645. * \param icao 4-letter code for the airport
  646. * \return {lat,lon} in decimal degrees
  647. */
  648. static QVector<double> retreiveIcaoCoordinates(QString icao)
  649. {
  650. QSqlQuery query;
  651. query.prepare("SELECT lat, long "
  652. "FROM airports "
  653. "WHERE icao = ?");
  654. query.addBindValue(icao);
  655. query.exec();
  656. QVector<double> result;
  657. while(query.next()) {
  658. result.append(query.value(0).toDouble());
  659. result.append(query.value(1).toDouble());
  660. }
  661. return result;
  662. }
  663. /*
  664. * Aircraft Database Related Functions
  665. */
  666. /*!
  667. * \brief RetreiveRegistration Looks up tail_id from Database
  668. * \param tail_ID Primary Key of tails database
  669. * \return Registration
  670. */
  671. static QString RetreiveRegistration(QString tail_ID)
  672. {
  673. QString acftRegistration("");
  674. QSqlQuery query;
  675. query.prepare("SELECT registration FROM tails WHERE tail_id == ?");
  676. query.addBindValue(tail_ID.toInt());
  677. query.exec();
  678. if(query.first());
  679. else
  680. qDebug() << ("No Aircraft with this ID found");
  681. query.previous();//To go back to index 0
  682. while (query.next()) {
  683. acftRegistration.append(query.value(0).toString());
  684. }
  685. return acftRegistration;
  686. }
  687. static QStringList newAcftGetString(QString searchstring)
  688. {
  689. QStringList result;
  690. if(searchstring.length()<2){return result;}
  691. QSqlQuery query;
  692. query.prepare("SELECT registration, make, model, variant "
  693. "FROM aircraft "
  694. "INNER JOIN tails on tails.aircraft_ID = aircraft.aircraft_id "
  695. "WHERE tails.registration LIKE ?");
  696. searchstring.append("%"); searchstring.prepend("%");
  697. query.addBindValue(searchstring);
  698. query.exec();
  699. while(query.next())
  700. {
  701. result.append(query.value(0).toString() + " (" + query.value(1).toString() + "-" + query.value(2).toString() + "-" + query.value(3).toString() + ")");
  702. }
  703. qDebug() << "newAcftGetString: " << result.length() << result;
  704. return result;
  705. }
  706. static QString newAcftGetId(QString registration)
  707. {
  708. QString result;
  709. QSqlQuery query;
  710. query.prepare("SELECT tail_id "
  711. "FROM tails "
  712. "WHERE registration LIKE ?");
  713. registration.prepend("%"); registration.append("%");
  714. query.addBindValue(registration);
  715. query.exec();
  716. while(query.next())
  717. {
  718. result.append(query.value(0).toString());
  719. }
  720. qDebug() << "newAcftGetId: " << result;
  721. return result;
  722. }
  723. static QVector<QString> RetreiveAircraftTypeFromReg(QString searchstring)
  724. /*
  725. * Searches the tails Database and returns the aircraft Type.
  726. */
  727. {
  728. QSqlQuery query;
  729. query.prepare("SELECT Name, iata, registration, tail_id " //"SELECT Registration, Name, icao, iata "
  730. "FROM aircraft "
  731. "INNER JOIN tails on tails.aircraft_ID = aircraft.aircraft_id "
  732. "WHERE tails.registration LIKE ?");
  733. // Returns Registration/Name/icao/iata
  734. searchstring.prepend("%");
  735. searchstring.append("%");
  736. query.addBindValue(searchstring);
  737. query.exec();
  738. QVector<QString> result;
  739. if(query.first())
  740. {
  741. QString acType = query.value(0).toString();
  742. QString iataCode = query.value(1).toString();
  743. QString registration = query.value(2).toString();
  744. QString tail_id = query.value(3).toString();
  745. //QString formatted = acType + " [ " + registration + " | " + iataCode + " ]";
  746. //qDebug() << formatted;
  747. result.append(registration); result.append(acType);
  748. result.append(iataCode); result.append(tail_id);
  749. return result;
  750. }else
  751. {
  752. return result; // empty vector
  753. }
  754. }
  755. static QStringList RetreiveAircraftMake(QString searchstring)
  756. {
  757. QStringList result;
  758. QSqlQuery query;
  759. query.prepare("SELECT make from aircraft WHERE make LIKE ?");
  760. searchstring.prepend("%"); searchstring.append("%");
  761. query.addBindValue(searchstring);
  762. query.exec();
  763. while(query.next())
  764. {
  765. result.append(query.value(0).toString());
  766. }
  767. qDebug() << "db::RetreiveAircraftMake says... Result:" << result;
  768. return result;
  769. }
  770. static QStringList RetreiveAircraftModel(QString make, QString searchstring)
  771. {
  772. QStringList result;
  773. QSqlQuery query;
  774. query.prepare("SELECT model FROM aircraft WHERE make = ? AND model LIKE ?");
  775. query.addBindValue(make);
  776. searchstring.prepend("%"); searchstring.append("%");
  777. query.addBindValue(searchstring);
  778. query.exec();
  779. while(query.next())
  780. {
  781. result.append(query.value(0).toString());
  782. qDebug() << "db::RetreiveAircraftModel says... Result:" << result;
  783. }
  784. return result;
  785. }
  786. static QStringList RetreiveAircraftVariant(QString make, QString model, QString searchstring)
  787. {
  788. QStringList result;
  789. QSqlQuery query;
  790. query.prepare("SELECT variant from aircraft WHERE make = ? AND model = ? AND variant LIKE ?");
  791. query.addBindValue(make);
  792. query.addBindValue(model);
  793. searchstring.prepend("%"); searchstring.append("%");
  794. query.addBindValue(searchstring);
  795. query.exec();
  796. while(query.next())
  797. {
  798. result.append(query.value(0).toString());
  799. qDebug() << "db::RetreiveAircraftVariant says... Result:" << result;
  800. }
  801. return result;
  802. }
  803. static QString RetreiveAircraftIdFromMakeModelVariant(QString make, QString model, QString variant)
  804. {
  805. QString result;
  806. QSqlQuery query;
  807. query.prepare("SELECT aircraft_id FROM aircraft WHERE make = ? AND model = ? AND variant = ?");
  808. query.addBindValue(make);
  809. query.addBindValue(model);
  810. query.addBindValue(variant);
  811. query.exec();
  812. if(query.first())
  813. {
  814. result.append(query.value(0).toString());
  815. qDebug() << "db::RetreiveAircraftIdFromMakeModelVariant: Aircraft found! ID# " << result;
  816. return result;
  817. }else
  818. {
  819. result = result.left(result.length()-1);
  820. result.append("0");
  821. qDebug() << "db::RetreiveAircraftIdFromMakeModelVariant: ERROR - no AircraftId found.";
  822. return result;
  823. }
  824. }
  825. static bool CommitTailToDb(QString registration, QString aircraft_id, QString company)
  826. {
  827. QSqlQuery commit;
  828. commit.prepare("INSERT INTO tails (registration, aircraft_id, company) VALUES (?,?,?)");
  829. commit.addBindValue(registration);
  830. commit.addBindValue(aircraft_id);
  831. commit.addBindValue(company);
  832. commit.exec();
  833. QString error = commit.lastError().text();
  834. if(error.length() < 0)
  835. {
  836. qDebug() << "db::CommitAircraftToDb:: SQL error:" << error;
  837. return false;
  838. }else
  839. {
  840. return true;
  841. }
  842. }
  843. /*
  844. * Aircraft Database Related Functions
  845. */
  846. static QVector<QString> retreiveSetting(QString setting_id)
  847. {
  848. QSqlQuery query;
  849. query.prepare("SELECT * FROM settings WHERE setting_id = ?");
  850. query.addBindValue(setting_id);
  851. query.exec();
  852. QVector<QString> setting;
  853. while(query.next()){
  854. setting.append(query.value(0).toString());
  855. setting.append(query.value(1).toString());
  856. setting.append(query.value(2).toString());
  857. }
  858. return setting;
  859. }
  860. static void storesetting(int setting_id, QString setting_value)
  861. {
  862. QSqlQuery query;
  863. query.prepare("UPDATE settings "
  864. "SET setting = ? "
  865. "WHERE setting_id = ?");
  866. query.addBindValue(setting_value);
  867. query.addBindValue(setting_id);
  868. query.exec();
  869. }
  870. };