Quellcode durchsuchen

First draft of AFlightEntry

Felix Turo vor 4 Jahren
Ursprung
Commit
176826940b

+ 2 - 0
openPilotLog.pro

@@ -32,6 +32,7 @@ SOURCES += \
     src/experimental/aaircraftentry.cpp \
     src/experimental/adatabase.cpp \
     src/experimental/aentry.cpp \
+    src/experimental/aflightentry.cpp \
     src/experimental/apilotentry.cpp \
     src/experimental/atailentry.cpp \
     src/functions/acalc.cpp \
@@ -68,6 +69,7 @@ HEADERS += \
     src/experimental/aaircraftentry.h \
     src/experimental/adatabase.h \
     src/experimental/aentry.h \
+    src/experimental/aflightentry.h \
     src/experimental/apilotentry.h \
     src/experimental/atailentry.h \
     src/experimental/decl.h \

+ 8 - 0
src/experimental/adatabase.cpp

@@ -288,6 +288,13 @@ AAircraftEntry ADataBase::getAircraftEntry(RowId row_id)
     return aircraft_entry;
 }
 
+AFlightEntry ADataBase::getFlightEntry(RowId row_id)
+{
+    AFlightEntry flight_entry(row_id);
+    flight_entry.setData(getEntryData(flight_entry.getPosition()));
+    return flight_entry;
+}
+
 const QStringList ADataBase::getCompletionList(ADataBase::CompleterTarget target)
 {
     QString statement;
@@ -374,6 +381,7 @@ QVector<QString> ADataBase::customQuery(QString statement, int return_values)
     if (!query.first()) {
         DEB("No result found. Check Query and Error.");
         DEB("Error: " << query.lastError().text());
+        DEB("Statement: " << statement);
         emit sqlError(query.lastError(), statement);
         return QVector<QString>();
     } else {

+ 11 - 0
src/experimental/adatabase.h

@@ -31,6 +31,7 @@
 #include "apilotentry.h"
 #include "atailentry.h"
 #include "aaircraftentry.h"
+#include "aflightentry.h"
 
 namespace experimental {
 
@@ -146,6 +147,16 @@ public:
      */
     AAircraftEntry getAircraftEntry(RowId row_id);
 
+    /*!
+     * \brief retreives a flight entry from the database.
+     *
+     * This function is a wrapper for DataBase::getEntry(DataPosition),
+     * where the table is already set and which returns an AFlightEntry
+     * instead of an AEntry. It allows for easy access to a flight entry
+     * with only the RowId required as input.
+     */
+    AFlightEntry getFlightEntry(RowId row_id);
+
     /*!
      * \brief getCompletionList returns a QStringList of values for a
      * QCompleter based on database values

+ 81 - 0
src/experimental/aflightentry.cpp

@@ -0,0 +1,81 @@
+#include "aflightentry.h"
+#include "src/experimental/adatabase.h"
+
+namespace experimental {
+
+AFlightEntry::AFlightEntry()
+    : AEntry::AEntry(DEFAULT_FLIGHT_POSITION)
+{}
+
+AFlightEntry::AFlightEntry(int row_id)
+    : AEntry::AEntry(DataPosition("flights", row_id))
+{}
+
+AFlightEntry::AFlightEntry(TableData table_data)
+    : AEntry::AEntry(DEFAULT_FLIGHT_POSITION, table_data)
+{}
+
+QString AFlightEntry::summary()
+{
+    if(tableData.isEmpty())
+        return QString();
+
+    QString flight_summary;
+    flight_summary.append(tableData.value("doft") + " ");
+    flight_summary.append(tableData.value("dept") + " ");
+    flight_summary.append(ACalc::minutesToString(tableData.value("tofb")) + " ");
+    flight_summary.append(ACalc::minutesToString(tableData.value("tonb")) + " ");
+    flight_summary.append(tableData.value("dest") + " ");
+
+    return flight_summary;
+}
+
+QString AFlightEntry::registration()
+{
+    QString tail_id = tableData.value("acft");
+    if(tail_id.isEmpty())
+        return QString();
+
+    QString statement = "SELECT registration "
+                        "FROM tails "
+                        "WHERE ROWID =" + tail_id;
+
+    auto tail_registration = aDB()->customQuery(statement, 1);
+
+    if(tail_registration.isEmpty()) {
+        return QString();
+    } else {
+        return tail_registration.first();
+    }
+}
+
+QString AFlightEntry::pilotName(pilot pilot_)
+{
+    QString row_id;
+    switch (pilot_) {
+    case pilot::pic:
+        row_id = tableData.value("pic");
+        break;
+    case pilot::sic:
+        row_id = tableData.value("sic");
+        break;
+    case pilot::thirdPilot:
+        row_id = tableData.value("thirdPilot");
+        break;
+    }
+    if(row_id == QString())
+        return row_id;
+
+    QString statement = "SELECT piclastname||\", \"||picfirstname "
+                        "FROM pilots "
+                        "WHERE ROWID =" + row_id;
+
+    auto pilot_name = aDB()->customQuery(statement, 1);
+    if(pilot_name.isEmpty()) {
+        return QString();
+    } else {
+        return pilot_name.first();
+    }
+}
+
+} // namespace experimental

+ 39 - 0
src/experimental/aflightentry.h

@@ -0,0 +1,39 @@
+#ifndef AFLIGHTENTRY_H
+#define AFLIGHTENTRY_H
+
+#include "src/experimental/aentry.h"
+#include "src/functions/acalc.h"
+
+namespace experimental {
+
+class AFlightEntry : public AEntry {
+public:
+    AFlightEntry();
+    AFlightEntry(const AFlightEntry& pe) = default;
+    AFlightEntry& operator=(const AFlightEntry& pe) = default;
+    AFlightEntry(int row_id);
+    AFlightEntry(TableData table_data);
+
+    enum pilot {pic, sic, thirdPilot };
+
+    /*!
+     * \brief Returs a summary of the flight data
+     * \return "doft, dept, tofb, dest, tonb"
+     */
+    QString summary();
+    /*!
+     * \brief Returns the tails' registration from the database.
+     */
+    QString registration();
+    /*!
+     * \brief Returns the pilots name from the Database
+     *
+     * \param pilot_number - 1=pic, 2=second Pilot, 3 = third Pilot
+     * \return "Lastname, Firstname"
+     */
+    QString pilotName(pilot);
+};
+
+} // namespace experimental
+
+#endif // AFLIGHTENTRY_H

+ 1 - 0
src/experimental/decl.h

@@ -40,6 +40,7 @@ struct DataPosition : QPair<TableName, RowId> {
 auto const DEFAULT_PILOT_POSITION = DataPosition("pilots", 0);
 auto const DEFAULT_TAIL_POSITION = DataPosition("tails", 0);
 auto const DEFAULT_AIRCRAFT_POSITION = DataPosition("aircraft", 0);
+auto const DEFAULT_FLIGHT_POSITION = DataPosition("flights", 0);
 
 }
 

+ 6 - 24
src/gui/widgets/debugwidget.cpp

@@ -164,33 +164,15 @@ void DebugWidget::on_importCsvPushButton_clicked()
 
 void DebugWidget::on_debugPushButton_clicked()
 {
-    qlonglong number_of_runs = 5000;
-    long time1 = 0;
-    long time2 = 0;
     using namespace experimental;
-    {
-        ATimer timer;
-        for (int i = 0; i < number_of_runs; i++) {
-            // first block, do stuff here...
-            aDB()->getEntry({"pilots", i});
-        }
-
-        time1 = timer.timeNow();
-    }
-    {
-        ATimer timer;
-        for (int i = 0; i < number_of_runs; i++) {
-            // second block, do stuff here...
-            aDB()->getPilotEntry(i);
-        }
-        time2 = timer.timeNow();
-    }
-
-    DEB("First block executed  " << number_of_runs << " times for a total of " << time1 << " milliseconds.");
-    DEB("Second block executed " << number_of_runs << " times for a total of " << time2 << " milliseconds.");
+    auto flight = aDB()->getFlightEntry(775);
+    DEB(flight.getData());
+    DEB(flight.summary());
+    DEB(flight.pilotName(experimental::AFlightEntry::pic));
+    DEB(flight.registration());
 }
 
-/*
+/* //Comparing two functions template
     qlonglong number_of_runs = 5000;
     long time1 = 0;
     long time2 = 0;