Browse Source

new database layout and new function dbSetup::getColumnNames

fiffty-50 4 years ago
parent
commit
70c366a630
10 changed files with 146 additions and 35 deletions
  1. 3 1
      dbflight.cpp
  2. 1 1
      dbflight.h
  3. 20 4
      dbsetup.cpp
  4. 5 0
      dbsetup.h
  5. 82 0
      flight.cpp
  6. 28 23
      flight.h
  7. 0 1
      homewidget.cpp
  8. BIN
      logbook.db
  9. 4 5
      main.cpp
  10. 3 0
      mainwindow.cpp

+ 3 - 1
dbflight.cpp

@@ -26,7 +26,7 @@
  * accordingly. Information about partial validity can be obtained
  * by querying the flight objects invalidItems member.
  */
-void dbFlight::verifyInput(flight object)
+flight dbFlight::verifyInput(flight object)
 {
 
     if (object.doft.isValid()){
@@ -96,6 +96,8 @@ void dbFlight::verifyInput(flight object)
         qDebug() << "Invalid items: " << object.invalidItems;
     }
 
+    return object;
+
     /* To Do: Checks on internal logic, i.e. times <= tblk,
      * restrict what strings are allowed in remarks etc.
 

+ 1 - 1
dbflight.h

@@ -33,7 +33,7 @@ class dbFlight
 {
 public:
 
-    static void verifyInput(flight object);
+    static flight verifyInput(flight object);
 
     static flight retreiveFlight(QString flight_id);
 

+ 20 - 4
dbsetup.cpp

@@ -17,8 +17,7 @@
  */
 #include "dbsetup.h"
 
-
-// Pragmas for creation of database table
+// Statements for creation of database table
 const QString createTablePilots = "CREATE TABLE pilots ( "
                             "pilot_id       INTEGER, "
                             "picfirstname	TEXT, "
@@ -132,7 +131,7 @@ const QString createTableSettings = "CREATE TABLE settings ( "
                              "description	TEXT "
                              ")";
 
-// Pragmas for creation of views in the database
+// Statements for creation of views in the database
 
 const QString createViewQCompleterView = "CREATE VIEW QCompleterView AS "
                               "SELECT airport_id, icao, iata, "
@@ -164,7 +163,6 @@ const QString createViewLogbook = "CREATE VIEW Logbook AS "
                               "INNER JOIN pilots on flights.pic = pilots.pilot_id "
                               "INNER JOIN tails on flights.acft = tails.tail_id "
                               "INNER JOIN aircraft on tails.aircraft_id = aircraft.aircraft_id "
-                              "INNER JOIN extras on extras.extras_id = flights.id "
                               "ORDER BY date DESC ";
 
 //Displays Single Engine, Multi Engine and Multi Pilot Time
@@ -318,3 +316,21 @@ void dbSetup::commitAirportData(QVector<QStringList> airportData)
     query.exec("COMMIT;"); //commit transaction
     qDebug() << "Airport Database updated!";
 }
+//dbSetup::commitAirportData(dbSetup::importCSV("airports.csv")); //import airports and write to db
+
+/*!
+ * \brief dbSetup::getColumnNames Looks up column names of a given table
+ * \param table name of the table in the database
+ */
+QVector<QString> dbSetup::getColumnNames(QString table)
+{
+    QSqlDatabase db = QSqlDatabase::database("qt_sql_default_connection");
+    QVector<QString> columnNames;
+
+    QSqlRecord fields = db.driver()->record(table);
+    for(int i = 0; i < fields.count(); i++){
+        columnNames << fields.field(i).name();
+    }
+    return columnNames;
+}
+

+ 5 - 0
dbsetup.h

@@ -21,6 +21,9 @@
 #include <QCoreApplication>
 #include <QSqlQuery>
 #include <QSqlError>
+#include <QSqlRecord>
+#include <QSqlField>
+#include <QSqlDriver>
 #include <QDebug>
 #include <QFile>
 
@@ -42,6 +45,8 @@ public:
     static QVector<QStringList> importCSV(QString);
 
     static void commitAirportData(QVector<QStringList>);
+
+    static QVector<QString> getColumnNames(QString table);
 };
 
 #endif // DBSETUP_H

+ 82 - 0
flight.cpp

@@ -1,5 +1,87 @@
 #include "flight.h"
 
+flight::flight()
+{
+    isValid = false;
+    invalidItems.append( {                      // Upon verification, verified entries are removed from the list
+        "doft", "dept", "dest", "tofb",
+        "tonb", "pic",  "acft", "tblk"
+    });
+
+    id      = -1;                         //[1] Primary Key in Database, needed for retreival but not for commiting (sqlite autoincrement)
+    doft    = QDate();                    //[2] Date of Flight, initialised invalid
+    dept    = "INVA";                     //[3] Departure, initialised invalid
+    dest    = "INVA";                     //[4] Destination, initialised invalid
+    tofb    = QTime();                    //[5] Time off blocks (UTC), initialised invalid
+    tonb    = QTime();                    //[6] Time on blocks (UTC), initialised invalid
+    pic     = "INVA";                     //[7] Pilot in command (ID), initialised invalid
+    acft    = "INVA";                     //[8] Aircraft Registration (ID), initialised invalid
+
+    tblk    = QTime();                    //[9] Total Blocktime, initialised invalid
+    tSPSE   = QTime(0,0);                 //[10] optional times initialised as 0
+    tSPME   = QTime(0,0);                 //[11]
+    tMP     = QTime(0,0);                 //[12]
+    tNIGHT  = QTime(0,0);                 //[13]
+    tIFR    = QTime(0,0);                 //[14]
+
+    tPIC    = QTime(0,0);                 //[15]
+    tPICUS  = QTime(0,0);                 //[16]
+    tSIC    = QTime(0,0);                 //[17]
+    tDUAL   = QTime(0,0);                 //[18]
+    tFI     = QTime(0,0);                 //[19]
+
+    tSIM    = QTime(0,0);                 //[20]
+}
+
+flight::flight(QVector<QString> details)
+{
+    isValid = false;
+    invalidItems.append({                      // Upon verification, verified entries are removed from the list
+        "doft", "dept", "dest", "tofb",
+        "tonb", "pic",  "acft", "tblk"
+    });
+    if(details.length() != 32){
+        qWarning() << __PRETTY_FUNCTION__ << "Vector needs to be of size 32.";
+        qWarning() << __PRETTY_FUNCTION__ << "Unable to create object.";
+        details = QVector<QString>(32);
+    }
+
+    id      = details[1].toInt();
+    doft    = QDate::fromString(details[2],Qt::ISODate);
+    dept    = details[3];
+    dest    = details[4];
+    tofb    = QTime::fromString(details[5],"hh:mm");
+    tonb    = QTime::fromString(details[6],"hh:mm");
+    pic     = details[7];
+    acft    = details[8];
+    tblk    = QTime::fromString(details[9],"hh:mm");
+    tSPSE   = QTime::fromString(details[10],"hh:mm");
+    tSPME   = QTime::fromString(details[11],"hh:mm");
+    tMP     = QTime::fromString(details[12],"hh:mm");
+    tNIGHT  = QTime::fromString(details[13],"hh:mm");
+    tIFR    = QTime::fromString(details[14],"hh:mm");
+
+    tPIC    = QTime::fromString(details[15],"hh:mm");
+    tPICUS  = QTime::fromString(details[16],"hh:mm");
+    tSIC    = QTime::fromString(details[17],"hh:mm");
+    tDUAL   = QTime::fromString(details[18],"hh:mm");
+    tFI     = QTime::fromString(details[19],"hh:mm");
+    tSIM    = QTime::fromString(details[20],"hh:mm");
+
+    pilotFlying  = details[21].toInt();
+    toDay        = details[22].toInt();
+    toNight      = details[23].toInt();
+    ldgDay       = details[24].toInt();
+    ldgNight     = details[25].toInt();
+    autoland     = details[26].toInt();
+
+    secondPilot  = details[27];
+    thirdPilot   = details[28];
+    approachType = details[29];
+    flightNumber = details[30];
+    remarks      = details[31];
+}
+
 /*!
  * \brief flight::printFlight Displays basic data for debugging
  */

+ 28 - 23
flight.h

@@ -15,35 +15,36 @@
 class flight
 {
 public:
-    bool        isValid = false;
-    QStringList invalidItems = {                      // Upon verification, verified entries are removed from the list
+
+    bool        isValid;
+    QStringList invalidItems;/* = {                      // Upon verification, verified entries are removed from the list
         "doft", "dept", "dest", "tofb",
         "tonb", "pic",  "acft", "tblk"
-    };
+    };*/
 
-    int         id      = -1;                         //[1] Primary Key in Database, needed for retreival but not for commiting (sqlite autoincrement)
-    QDate       doft    = QDate();                    //[2] Date of Flight, initialised invalid
-    QString     dept    = "INVA";                     //[3] Departure, initialised invalid
-    QString     dest    = "INVA";                     //[4] Destination, initialised invalid
-    QTime       tofb    = QTime();                    //[5] Time off blocks (UTC), initialised invalid
-    QTime       tonb    = QTime();                    //[6] Time on blocks (UTC), initialised invalid
-    QString     pic     = "INVA";                     //[7] Pilot in command (ID), initialised invalid
-    QString     acft    = "INVA";                     //[8] Aircraft Registration (ID), initialised invalid
+    int         id;//      = -1;                         //[1] Primary Key in Database, needed for retreival but not for commiting (sqlite autoincrement)
+    QDate       doft;//   = QDate();                    //[2] Date of Flight, initialised invalid
+    QString     dept;//    = "INVA";                     //[3] Departure, initialised invalid
+    QString     dest;//    = "INVA";                     //[4] Destination, initialised invalid
+    QTime       tofb;// = QTime();                    //[5] Time off blocks (UTC), initialised invalid
+    QTime       tonb;//    = QTime();                    //[6] Time on blocks (UTC), initialised invalid
+    QString     pic;//     = "INVA";                     //[7] Pilot in command (ID), initialised invalid
+    QString     acft;//    = "INVA";                     //[8] Aircraft Registration (ID), initialised invalid
 
-    QTime       tblk    = QTime();                    //[9] Total Blocktime, initialised invalid
-    QTime       tSPSE   = QTime(0,0);                 //[10] optional times initialised as 0
-    QTime       tSPME   = QTime(0,0);                 //[11]
-    QTime       tMP     = QTime(0,0);                 //[12]
-    QTime       tNIGHT  = QTime(0,0);                 //[13]
-    QTime       tIFR    = QTime(0,0);                 //[14]
+    QTime       tblk;//    = QTime();                    //[9] Total Blocktime, initialised invalid
+    QTime       tSPSE;//   = QTime(0,0);                 //[10] optional times initialised as 0
+    QTime       tSPME;//   = QTime(0,0);                 //[11]
+    QTime       tMP;//     = QTime(0,0);                 //[12]
+    QTime       tNIGHT;//  = QTime(0,0);                 //[13]
+    QTime       tIFR;//    = QTime(0,0);                 //[14]
 
-    QTime       tPIC    = QTime(0,0);                 //[15]
-    QTime       tPICUS  = QTime(0,0);                 //[16]
-    QTime       tSIC    = QTime(0,0);                 //[17]
-    QTime       tDUAL   = QTime(0,0);                 //[18]
-    QTime       tFI     = QTime(0,0);                 //[19]
+    QTime       tPIC;//    = QTime(0,0);                 //[15]
+    QTime       tPICUS;//  = QTime(0,0);                 //[16]
+    QTime       tSIC;//    = QTime(0,0);                 //[17]
+    QTime       tDUAL;//   = QTime(0,0);                 //[18]
+    QTime       tFI;//     = QTime(0,0);                 //[19]
 
-    QTime       tSIM    = QTime(0,0);                 //[20]
+    QTime       tSIM;//    = QTime(0,0);                 //[20]
 
     int         pilotFlying;                          //[21]
     int         toDay;                                //[22]
@@ -58,6 +59,10 @@ public:
     QString     flightNumber;                         //[30]
     QString     remarks;                              //[31]
 
+    flight();
+    flight(QVector<QString>);
+
+
     // Functions
     static flight           fromVector(QVector<QString>);
     static QVector<QString> toVector(flight);

+ 0 - 1
homewidget.cpp

@@ -30,7 +30,6 @@ homeWidget::homeWidget(QWidget *parent) :
     ui->setupUi(this);
     qDebug() << "homeWidget: Activated";
 
-
     /*
      * To Do: Functions to retreive values from DB
      */

BIN
logbook.db


+ 4 - 5
main.cpp

@@ -28,9 +28,9 @@
 #include <QSqlError>
 #include <QSqlQuery>
 #include "dbsettings.h"
+#include "dbsetup.h"
 #include <QDebug>
 
-int selectedtheme = 1; //Variable to store theming information
 
 void connectToDatabase()
 {
@@ -58,16 +58,16 @@ void connectToDatabase()
 
 int main(int argc, char *argv[])
 {
+    connectToDatabase();
+
     QCoreApplication::setOrganizationName("Fiffty50");
     QCoreApplication::setOrganizationDomain("https://github.com/fiffty-50/openpilotlog");
     QCoreApplication::setApplicationName("openLog");
     QApplication openLog(argc, argv);
 
-    connectToDatabase();
-
     //Theming with CSS inlcues QFile,QTextStream, QDir, themes folder and TARGET = flog, RESOURCES = themes/breeze.qrc in pro
     // credit: https://github.com/Alexhuszagh/BreezeStyleSheets
-    selectedtheme = dbSettings::retreiveSetting(10).toInt();
+    int selectedtheme = dbSettings::retreiveSetting(10).toInt();
     QDir::setCurrent("/themes");
     if (selectedtheme == 1){
         qDebug() << "Loading light theme";
@@ -82,7 +82,6 @@ int main(int argc, char *argv[])
         QTextStream stream(&file);
         openLog.setStyleSheet(stream.readAll());
     }
-
     MainWindow w;
     w.show();
     return openLog.exec();

+ 3 - 0
mainwindow.cpp

@@ -25,6 +25,9 @@
 #include "homewidget.h"
 #include "logbookwidget.h"
 #include "settingswidget.h"
+#include "dbsetup.h"
+
+
 
 MainWindow::MainWindow(QWidget *parent)
     : QMainWindow(parent)