dbman.cpp 26 KB

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