dbman.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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+"/logbook.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 flights (id INTEGER PRIMARY KEY, date NUMERIC)");
  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. * Pilots Database Related Functions
  91. */
  92. /*!
  93. * \brief RetreivePilotNameFromID Looks up pilot ID in database
  94. * \param pilotID pilot_id in database
  95. * \return Pilot Name
  96. */
  97. static QString RetreivePilotNameFromID(QString pilotID)
  98. {
  99. QString pilotName("");
  100. if (pilotID == "1")
  101. {
  102. pilotName = "self";
  103. return pilotName;
  104. }
  105. QSqlQuery query;
  106. query.prepare("SELECT piclastname, picfirstname, alias FROM pilots WHERE pilot_id == ?");
  107. query.addBindValue(pilotID.toInt());
  108. query.exec();
  109. while (query.next()) {
  110. pilotName.append(query.value(0).toString());
  111. pilotName.append(", ");
  112. pilotName.append(query.value(1).toString());//.left(1));
  113. }
  114. if(pilotName.length() == 0)
  115. {
  116. qDebug() << ("No Pilot with this ID found");
  117. }
  118. return pilotName;
  119. }
  120. static QString RetreivePilotIdFromString(QString lastname, QString firstname)
  121. {
  122. QSqlQuery query;
  123. query.prepare("SELECT pilot_id from pilots "
  124. "WHERE piclastname = ? AND picfirstname LIKE ?");
  125. query.addBindValue(lastname);
  126. firstname.prepend("%"); firstname.append("%");
  127. query.addBindValue(firstname);
  128. query.exec();
  129. QString id;
  130. if(query.first()){id.append(query.value(0).toString());}
  131. return id;
  132. }
  133. static QStringList RetreivePilotNameFromString(QString searchstring)
  134. /* Searches the pilot Name in the Database and returns the name as a vector of results
  135. * unless the pilot in command is the logbook owner.
  136. */
  137. {
  138. QString firstname = searchstring; //To Do: Two control paths, one for single word, query as before with only searchstring
  139. QString lastname = searchstring; // second control path with comma, lastname like AND firstname like
  140. if(searchstring.contains(QLatin1Char(',')))
  141. {
  142. QStringList namelist = searchstring.split(QLatin1Char(','));
  143. QString lastname = namelist[0].trimmed();
  144. lastname = lastname.toLower();
  145. lastname[0] = lastname[0].toUpper();
  146. lastname.prepend("%"), lastname.append("%");
  147. QString firstname = namelist[1].trimmed();
  148. if(firstname.length()>1)
  149. {
  150. firstname = firstname.toLower();
  151. firstname[0] = firstname[0].toUpper();
  152. firstname.prepend("%"), firstname.append("%");
  153. }
  154. qDebug() << "db::RetreivePilotNameFromString: first last after comma";
  155. qDebug() << firstname << lastname;
  156. }
  157. QSqlQuery query;
  158. query.prepare("SELECT piclastname, picfirstname, alias "
  159. "FROM pilots WHERE "
  160. "picfirstname LIKE ? OR piclastname LIKE ? OR alias LIKE ?");
  161. searchstring.prepend("%");
  162. searchstring.append("%");
  163. query.addBindValue(firstname);
  164. query.addBindValue(lastname);
  165. query.addBindValue(searchstring);
  166. query.exec();
  167. QStringList result;
  168. while (query.next()) {
  169. QString piclastname = query.value(0).toString();
  170. QString picfirstname = query.value(1).toString();
  171. QString alias = query.value(2).toString();
  172. QString name = piclastname + ", " + picfirstname;
  173. result.append(name);
  174. }
  175. qDebug() << "db::RetreivePilotNameFromString Result: " << result;
  176. //qDebug() << query.lastError();
  177. if(result.size() == 0)
  178. {
  179. qDebug() << ("db::RetreivePilotNameFromString: No Pilot found");
  180. return result;
  181. }
  182. return result;
  183. }
  184. /*!
  185. * \brief newPicGetString This function is returning a QStringList for the QCompleter in the NewFlight::newPic line edit
  186. * A regular expression limits the input possibilities to only characters,
  187. * followed by an optional ',' and 1 whitespace, e.g.:
  188. * Miller, Jim ->valid / Miller, Jim -> invalid / Miller,, Jim -> invalid
  189. * Miller Jim -> valid / Miller Jim ->invalid
  190. * Jim Miller-> valid
  191. * \param searchstring
  192. * \return
  193. */
  194. static QStringList newPicGetString(QString searchstring)
  195. {
  196. QStringList result;
  197. QStringList searchlist;
  198. if(searchstring == "self")
  199. {
  200. result.append("self");
  201. qDebug() << "Pilot is self";
  202. return result;
  203. }
  204. //Fall 1) Nachname, Vorname
  205. if(searchstring.contains(QLatin1Char(',')))
  206. {
  207. QStringList namelist = searchstring.split(QLatin1Char(','));
  208. QString name1 = namelist[0].trimmed();
  209. name1 = name1.toLower();
  210. name1[0] = name1[0].toUpper();
  211. searchlist.append(name1);
  212. if(namelist[1].length() > 1)
  213. {
  214. QString name2 = namelist[1].trimmed();
  215. name2 = name2.toLower();
  216. name2[0] = name2[0].toUpper();
  217. searchlist.append(name2);
  218. }
  219. }
  220. //Fall 2) Vorname Nachname
  221. if(searchstring.contains(" ") && !searchstring.contains(QLatin1Char(',')))
  222. {
  223. QStringList namelist = searchstring.split(" ");
  224. QString name1 = namelist[0].trimmed();
  225. name1 = name1.toLower();
  226. name1[0] = name1[0].toUpper();
  227. searchlist.append(name1);
  228. if(namelist[1].length() > 1) //To avoid index out of range if the searchstring is one word followed by only one whitespace
  229. {
  230. QString name2 = namelist[1].trimmed();
  231. name2 = name2.toLower();
  232. name2[0] = name2[0].toUpper();
  233. searchlist.append(name2);
  234. }
  235. }
  236. //Fall 3) Name
  237. if(!searchstring.contains(" ") && !searchstring.contains(QLatin1Char(',')))
  238. {
  239. QString name1 = searchstring.toLower();
  240. name1[0] = name1[0].toUpper();
  241. searchlist.append(name1);
  242. }
  243. if(searchlist.length() == 1)
  244. {
  245. QSqlQuery query;
  246. query.prepare("SELECT piclastname, picfirstname FROM pilots "
  247. "WHERE piclastname LIKE ?");
  248. query.addBindValue(searchlist[0] + '%');
  249. query.exec();
  250. while(query.next())
  251. {
  252. result.append(query.value(0).toString() + ", " + query.value(1).toString());
  253. }
  254. QSqlQuery query2;
  255. query2.prepare("SELECT piclastname, picfirstname FROM pilots "
  256. "WHERE picfirstname LIKE ?");
  257. query2.addBindValue(searchlist[0] + '%');
  258. query2.exec();
  259. while(query2.next())
  260. {
  261. result.append(query2.value(0).toString() + ", " + query2.value(1).toString());
  262. }
  263. }else
  264. {
  265. QSqlQuery query;
  266. query.prepare("SELECT piclastname, picfirstname FROM pilots "
  267. "WHERE piclastname LIKE ? AND picfirstname LIKE ?");
  268. query.addBindValue(searchlist[0] + '%');
  269. query.addBindValue(searchlist[1] + '%');
  270. query.exec();
  271. while(query.next())
  272. {
  273. result.append(query.value(0).toString() + ", " + query.value(1).toString());
  274. }
  275. QSqlQuery query2;
  276. query2.prepare("SELECT piclastname, picfirstname FROM pilots "
  277. "WHERE picfirstname LIKE ? AND piclastname LIKE ?");
  278. query2.addBindValue(searchlist[0] + '%');
  279. query2.addBindValue(searchlist[1] + '%');
  280. query2.exec();
  281. while(query2.next())
  282. {
  283. result.append(query2.value(0).toString() + ", " + query2.value(1).toString());
  284. }
  285. }
  286. qDebug() << "db::newPic Result" << result.length() << result;
  287. if(result.length() == 0)
  288. {
  289. //try first name search
  290. qDebug() << "No Pilot with this last name found. trying first name search.";
  291. return result;
  292. }else
  293. {
  294. return result;
  295. }
  296. }
  297. static QString newPicGetId(QString name)
  298. {
  299. QString result;
  300. QStringList nameparts = name.split(QLatin1Char(','));
  301. QString lastname = nameparts[0].trimmed();
  302. lastname = lastname.toLower(); lastname[0] = lastname[0].toUpper();
  303. QString firstname = nameparts[1].trimmed();
  304. firstname = firstname.toLower(); firstname[0] = firstname[0].toUpper();
  305. firstname.prepend("%"); firstname.append("%");
  306. QSqlQuery query;
  307. query.prepare("SELECT pilot_id FROM pilots "
  308. "WHERE piclastname = ? AND picfirstname LIKE ?");
  309. query.addBindValue(lastname);
  310. query.addBindValue(firstname);
  311. query.exec();
  312. while (query.next())
  313. {
  314. result.append(query.value(0).toString());
  315. }
  316. qDebug() << "newPicGetId: result = " << result;
  317. return result;
  318. }
  319. /*
  320. * Airport Database Related Functions
  321. */
  322. /*!
  323. * \brief RetreiveAirportNameFromIcaoOrIata Looks up Airport Name
  324. * \param identifier can be ICAO or IATA airport codes.
  325. * \return The name of the airport associated with the above code
  326. */
  327. static QString RetreiveAirportNameFromIcaoOrIata(QString identifier)
  328. {
  329. QString result = "";
  330. QSqlQuery query;
  331. query.prepare("SELECT name "
  332. "FROM airports WHERE icao LIKE ? OR iata LIKE ?");
  333. identifier.append("%");
  334. identifier.prepend("%");
  335. query.addBindValue(identifier);
  336. query.addBindValue(identifier);
  337. query.exec();
  338. if(query.first())
  339. {
  340. result.append(query.value(0).toString());
  341. return result;
  342. }else
  343. {
  344. result = result.left(result.length()-1);
  345. result.append("No matching airport found.");
  346. return result;
  347. }
  348. }
  349. static QString RetreiveAirportIdFromIcao(QString identifier)
  350. {
  351. QString result;
  352. QSqlQuery query;
  353. query.prepare("SELECT airport_id FROM airports WHERE icao = ?");
  354. query.addBindValue(identifier);
  355. query.exec();
  356. while(query.next())
  357. {
  358. result.append(query.value(0).toString());
  359. //qDebug() << "db::RetreiveAirportIdFromIcao says Airport found! #" << result;
  360. }
  361. return result;
  362. }
  363. static QStringList CompleteIcaoOrIata(QString icaoStub)
  364. {
  365. QStringList result;
  366. QSqlQuery query;
  367. query.prepare("SELECT icao FROM airports WHERE icao LIKE ? OR iata LIKE ?");
  368. icaoStub.prepend("%"); icaoStub.append("%");
  369. query.addBindValue(icaoStub);
  370. query.addBindValue(icaoStub);
  371. query.exec();
  372. while(query.next())
  373. {
  374. result.append(query.value(0).toString());
  375. qDebug() << "db::CompleteIcaoOrIata says... Result:" << result;
  376. }
  377. return result;
  378. }
  379. /*!
  380. * \brief CheckICAOValid Verifies if a user input airport exists in the database
  381. * \param identifier can be ICAO or IATA airport codes.
  382. * \return bool if airport is in database.
  383. */
  384. static bool CheckICAOValid(QString identifier)
  385. {
  386. if(identifier.length() == 4)
  387. {
  388. QString check = RetreiveAirportIdFromIcao(identifier);
  389. if(check.length() > 0)
  390. {
  391. //qDebug() << "db::CheckICAOValid says: Check passed!";
  392. return 1;
  393. }else
  394. {
  395. //qDebug() << "db::CheckICAOValid says: Check NOT passed! Lookup unsuccessful";
  396. return 0;
  397. }
  398. }else
  399. {
  400. //qDebug() << "db::CheckICAOValid says: Check NOT passed! Empty String NOT epico!";
  401. return 0;
  402. }
  403. }
  404. /*!
  405. * \brief retreiveIcaoCoordinates Looks up coordinates (lat,long) for a given airport
  406. * \param icao 4-letter code for the airport
  407. * \return {lat,lon} in decimal degrees
  408. */
  409. static QVector<double> retreiveIcaoCoordinates(QString icao)
  410. {
  411. QSqlQuery query;
  412. query.prepare("SELECT lat, long "
  413. "FROM airports "
  414. "WHERE icao = ?");
  415. query.addBindValue(icao);
  416. query.exec();
  417. QVector<double> result;
  418. while(query.next()) {
  419. result.append(query.value(0).toDouble());
  420. result.append(query.value(1).toDouble());
  421. }
  422. return result;
  423. }
  424. /*
  425. * Aircraft Database Related Functions
  426. */
  427. /*!
  428. * \brief RetreiveRegistration Looks up tail_id from Database
  429. * \param tail_ID Primary Key of tails database
  430. * \return Registration
  431. */
  432. static QString RetreiveRegistration(QString tail_ID)
  433. {
  434. QString acftRegistration("");
  435. QSqlQuery query;
  436. query.prepare("SELECT registration FROM tails WHERE tail_id == ?");
  437. query.addBindValue(tail_ID.toInt());
  438. query.exec();
  439. if(query.first());
  440. else
  441. qDebug() << ("No Aircraft with this ID found");
  442. query.previous();//To go back to index 0
  443. while (query.next()) {
  444. acftRegistration.append(query.value(0).toString());
  445. }
  446. return acftRegistration;
  447. }
  448. /*!
  449. * \brief newAcftGetString Looks up an aircraft Registration in the database
  450. * \param searchstring
  451. * \return Registration, make, model and variant
  452. */
  453. static QStringList newAcftGetString(QString searchstring)
  454. {
  455. QStringList result;
  456. if(searchstring.length()<2){return result;}
  457. QSqlQuery query;
  458. query.prepare("SELECT registration, make, model, variant "
  459. "FROM aircraft "
  460. "INNER JOIN tails on tails.aircraft_ID = aircraft.aircraft_id "
  461. "WHERE tails.registration LIKE ?");
  462. searchstring.append("%"); searchstring.prepend("%");
  463. query.addBindValue(searchstring);
  464. query.exec();
  465. while(query.next())
  466. {
  467. result.append(query.value(0).toString() + " (" + query.value(1).toString() + "-" + query.value(2).toString() + "-" + query.value(3).toString() + ")");
  468. }
  469. qDebug() << "newAcftGetString: " << result.length() << result;
  470. return result;
  471. }
  472. static QString newAcftGetId(QString registration)
  473. {
  474. QString result;
  475. QSqlQuery query;
  476. query.prepare("SELECT tail_id "
  477. "FROM tails "
  478. "WHERE registration LIKE ?");
  479. registration.prepend("%"); registration.append("%");
  480. query.addBindValue(registration);
  481. query.exec();
  482. while(query.next())
  483. {
  484. result.append(query.value(0).toString());
  485. }
  486. qDebug() << "newAcftGetId: " << result;
  487. return result;
  488. }
  489. static QVector<QString> RetreiveAircraftTypeFromReg(QString searchstring)
  490. /*
  491. * Searches the tails Database and returns the aircraft Type.
  492. */
  493. {
  494. QSqlQuery query;
  495. query.prepare("SELECT Name, iata, registration, tail_id " //"SELECT Registration, Name, icao, iata "
  496. "FROM aircraft "
  497. "INNER JOIN tails on tails.aircraft_ID = aircraft.aircraft_id "
  498. "WHERE tails.registration LIKE ?");
  499. // Returns Registration/Name/icao/iata
  500. searchstring.prepend("%");
  501. searchstring.append("%");
  502. query.addBindValue(searchstring);
  503. query.exec();
  504. QVector<QString> result;
  505. if(query.first())
  506. {
  507. QString acType = query.value(0).toString();
  508. QString iataCode = query.value(1).toString();
  509. QString registration = query.value(2).toString();
  510. QString tail_id = query.value(3).toString();
  511. //QString formatted = acType + " [ " + registration + " | " + iataCode + " ]";
  512. //qDebug() << formatted;
  513. result.append(registration); result.append(acType);
  514. result.append(iataCode); result.append(tail_id);
  515. return result;
  516. }else
  517. {
  518. return result; // empty vector
  519. }
  520. }
  521. static QStringList RetreiveAircraftMake(QString searchstring)
  522. {
  523. QStringList result;
  524. QSqlQuery query;
  525. query.prepare("SELECT make from aircraft WHERE make LIKE ?");
  526. searchstring.prepend("%"); searchstring.append("%");
  527. query.addBindValue(searchstring);
  528. query.exec();
  529. while(query.next())
  530. {
  531. result.append(query.value(0).toString());
  532. }
  533. qDebug() << "db::RetreiveAircraftMake says... Result:" << result;
  534. return result;
  535. }
  536. static QStringList RetreiveAircraftModel(QString make, QString searchstring)
  537. {
  538. QStringList result;
  539. QSqlQuery query;
  540. query.prepare("SELECT model FROM aircraft WHERE make = ? AND model LIKE ?");
  541. query.addBindValue(make);
  542. searchstring.prepend("%"); searchstring.append("%");
  543. query.addBindValue(searchstring);
  544. query.exec();
  545. while(query.next())
  546. {
  547. result.append(query.value(0).toString());
  548. qDebug() << "db::RetreiveAircraftModel says... Result:" << result;
  549. }
  550. return result;
  551. }
  552. static QStringList RetreiveAircraftVariant(QString make, QString model, QString searchstring)
  553. {
  554. QStringList result;
  555. QSqlQuery query;
  556. query.prepare("SELECT variant from aircraft WHERE make = ? AND model = ? AND variant LIKE ?");
  557. query.addBindValue(make);
  558. query.addBindValue(model);
  559. searchstring.prepend("%"); searchstring.append("%");
  560. query.addBindValue(searchstring);
  561. query.exec();
  562. while(query.next())
  563. {
  564. result.append(query.value(0).toString());
  565. qDebug() << "db::RetreiveAircraftVariant says... Result:" << result;
  566. }
  567. return result;
  568. }
  569. static QString RetreiveAircraftIdFromMakeModelVariant(QString make, QString model, QString variant)
  570. {
  571. QString result;
  572. QSqlQuery query;
  573. query.prepare("SELECT aircraft_id FROM aircraft WHERE make = ? AND model = ? AND variant = ?");
  574. query.addBindValue(make);
  575. query.addBindValue(model);
  576. query.addBindValue(variant);
  577. query.exec();
  578. if(query.first())
  579. {
  580. result.append(query.value(0).toString());
  581. qDebug() << "db::RetreiveAircraftIdFromMakeModelVariant: Aircraft found! ID# " << result;
  582. return result;
  583. }else
  584. {
  585. result = result.left(result.length()-1);
  586. result.append("0");
  587. qDebug() << "db::RetreiveAircraftIdFromMakeModelVariant: ERROR - no AircraftId found.";
  588. return result;
  589. }
  590. }
  591. static bool CommitTailToDb(QString registration, QString aircraft_id, QString company)
  592. {
  593. QSqlQuery commit;
  594. commit.prepare("INSERT INTO tails (registration, aircraft_id, company) VALUES (?,?,?)");
  595. commit.addBindValue(registration);
  596. commit.addBindValue(aircraft_id);
  597. commit.addBindValue(company);
  598. commit.exec();
  599. QString error = commit.lastError().text();
  600. if(error.length() < 0)
  601. {
  602. qDebug() << "db::CommitAircraftToDb:: SQL error:" << error;
  603. return false;
  604. }else
  605. {
  606. return true;
  607. }
  608. }
  609. /*
  610. * Obsolete Functions
  611. */
  612. /*!
  613. * \brief SelectFlightDate Retreives Flights from the database currently not in use.
  614. * \param doft Date of flight for filtering result set. "ALL" means no filter.
  615. * \return Flight(s) for selected date.
  616. */
  617. static QVector<QString> SelectFlightDate(QString doft)
  618. {
  619. QSqlQuery query;
  620. if (doft == "ALL") // Special Selector
  621. {
  622. query.prepare("SELECT * FROM flights ORDER BY doft DESC, tofb ASC");
  623. qDebug() << "All flights selected";
  624. }else
  625. {
  626. query.prepare("SELECT * FROM flights WHERE doft = ? ORDER BY tofb ASC");
  627. query.addBindValue(doft);
  628. qDebug() << "Searching flights for " << doft;
  629. }
  630. query.exec();
  631. if(query.first());
  632. else
  633. {
  634. qDebug() << ("No flight with this date found");
  635. QVector<QString> flight; //return empty
  636. return flight;
  637. }
  638. query.previous();// To go back to index 0
  639. query.last(); // this can be very slow, used to determine query size since .size is not supported by sqlite
  640. int numRows = query.at() + 1; // Number of rows (flights) in the query
  641. query.first();
  642. query.previous();// Go back to index 0
  643. QVector<QString> flight(numRows * 9); // Every flight has 9 fields in the database
  644. int index = 0; // counter for output vector
  645. while (query.next()) {
  646. QString id = query.value(0).toString();
  647. QString doft = query.value(1).toString();
  648. QString dept = query.value(2).toString();
  649. QString tofb = calc::minutes_to_string((query.value(3).toString()));
  650. QString dest = query.value(4).toString();
  651. QString tonb = calc::minutes_to_string((query.value(5).toString()));
  652. QString tblk = calc::minutes_to_string((query.value(6).toString()));
  653. QString pic = db::RetreivePilotNameFromID(query.value(7).toString());
  654. QString acft = db::RetreiveRegistration(query.value(8).toString());
  655. //qDebug() << id << doft << dept << tofb << dest << tonb << tblk << pic << acft << endl;
  656. flight[index] = id;
  657. ++index;
  658. flight[index] = doft;
  659. ++index;
  660. flight[index] = dept;
  661. ++index;
  662. flight[index] = tofb;
  663. ++index;
  664. flight[index] = dest;
  665. ++index;
  666. flight[index] = tonb;
  667. ++index;
  668. flight[index] = tblk;
  669. ++index;
  670. flight[index] = pic;
  671. ++index;
  672. flight[index] = acft;
  673. ++index;
  674. }
  675. return flight;
  676. }
  677. };