Browse Source

Rework of Database interface

fiffty-50 4 years ago
parent
commit
0d0a92af91
30 changed files with 114 additions and 239 deletions
  1. 1 1
      calc.cpp
  2. 17 4
      dbaircraft.cpp
  3. 3 0
      dbaircraft.h
  4. 0 1
      dbairport.cpp
  5. 3 0
      dbairport.h
  6. 1 1
      dbflight.cpp
  7. 3 0
      dbflight.h
  8. 0 178
      dbman.cpp
  9. 1 1
      dbpilots.cpp
  10. 3 0
      dbpilots.h
  11. 1 1
      dbsettings.cpp
  12. 3 0
      dbsettings.h
  13. 1 1
      dbstat.cpp
  14. 4 0
      dbstat.h
  15. 0 1
      easaview.cpp
  16. 3 1
      editflight.cpp
  17. 1 1
      homewidget.cpp
  18. 0 5
      logbookwidget.cpp
  19. 4 0
      logbookwidget.h
  20. 30 4
      main.cpp
  21. 0 15
      mainwindow.cpp
  22. 7 0
      mainwindow.h
  23. 0 9
      newacft.cpp
  24. 3 2
      newflight.cpp
  25. 0 1
      openLog.pro
  26. 1 6
      settingswidget.cpp
  27. 5 0
      settingswidget.h
  28. 1 6
      showaircraftlist.cpp
  29. 5 0
      showaircraftlist.h
  30. 13 0
      strictregularexpressionvalidator.h

+ 1 - 1
calc.cpp

@@ -16,7 +16,7 @@
  *along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 #include "calc.h"
-#include "dbman.cpp"
+//#include "dbman.cpp"
 /*!
  * \brief calc::blocktime Calculates Block Time for a given departure and arrival time
  * \param tofb QTime Time Off Blocks

+ 17 - 4
dbaircraft.cpp

@@ -1,8 +1,21 @@
+/*
+ *openPilot Log - A FOSS Pilot Logbook Application
+ *Copyright (C) 2020  Felix Turowsky
+ *
+ *This program is free software: you can redistribute it and/or modify
+ *it under the terms of the GNU General Public License as published by
+ *the Free Software Foundation, either version 3 of the License, or
+ *(at your option) any later version.
+ *
+ *This program is distributed in the hope that it will be useful,
+ *but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *GNU General Public License for more details.
+ *
+ *You should have received a copy of the GNU General Public License
+ *along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
 #include "dbaircraft.h"
-#include "dbman.cpp"
-
-
-
 /*!
  * \brief RetreiveRegistration Looks up tail_id from Database
  * \param tail_ID Primary Key of tails database

+ 3 - 0
dbaircraft.h

@@ -2,6 +2,9 @@
 #define DBAIRCRAFT_H
 
 #include <QCoreApplication>
+#include <QSqlQuery>
+#include <QSqlError>
+#include <QDebug>
 
 /*!
  * \brief The dbAircraft class provides functionality for retreiving and storing

+ 0 - 1
dbairport.cpp

@@ -16,7 +16,6 @@
  *along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 #include "dbairport.h"
-#include "dbman.cpp"
 
 /*!
      * \brief RetreiveAirportNameFromIcaoOrIata Looks up Airport Name

+ 3 - 0
dbairport.h

@@ -19,6 +19,9 @@
 #define DBAIRPORT_H
 
 #include <QCoreApplication>
+#include <QSqlQuery>
+#include <QSqlError>
+#include <QDebug>
 
 /*!
  * \brief The dbAirport class provides functionality for retreiving airport related

+ 1 - 1
dbflight.cpp

@@ -16,7 +16,7 @@
  *along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 #include "dbflight.h"
-#include "dbman.cpp"
+#include "calc.h"
 
 /*!
  * \brief SelectFlightById Retreives a single flight from the database.

+ 3 - 0
dbflight.h

@@ -19,6 +19,9 @@
 #define DBFLIGHT_H
 
 #include <QCoreApplication>
+#include <QSqlQuery>
+#include <QSqlError>
+#include <QDebug>
 
 /*!
  * \brief The dbFlight class provides a databank interface for actions related to the

+ 0 - 178
dbman.cpp

@@ -1,178 +0,0 @@
-/*
- *openPilot Log - A FOSS Pilot Logbook Application
- *Copyright (C) 2020  Felix Turowsky
- *
- *This program is free software: you can redistribute it and/or modify
- *it under the terms of the GNU General Public License as published by
- *the Free Software Foundation, either version 3 of the License, or
- *(at your option) any later version.
- *
- *This program is distributed in the hope that it will be useful,
- *but WITHOUT ANY WARRANTY; without even the implied warranty of
- *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *GNU General Public License for more details.
- *
- *You should have received a copy of the GNU General Public License
- *along with this program.  If not, see <https://www.gnu.org/licenses/>.
- */
-#include <QCoreApplication>
-#include <QDebug>
-#include <QSqlDatabase>
-#include <QSqlDriver>
-#include <QSqlError>
-#include <QSqlQuery>
-#include "calc.h"
-#include "dbpilots.h"
-#include "dbaircraft.h"
-#include <chrono>
-#include <QRandomGenerator>
-#include <QStandardPaths>
-
-
-class db
-{
-public:
-    static void connect()
-    {
-        const QString DRIVER("QSQLITE");
-
-        if(QSqlDatabase::isDriverAvailable(DRIVER))
-        {
-            QSqlDatabase db = QSqlDatabase::addDatabase(DRIVER);
-
-            //QString pathtodb = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
-            //db.setDatabaseName(pathtodb+"/logbook.db");
-            //qDebug() << "Database: " << pathtodb+"/logbook.db";
-            db.setDatabaseName("logbook.db");
-
-
-            if(!db.open())
-                qWarning() << "MainWindow::DatabaseConnect - ERROR: " << db.lastError().text();
-        }
-        else
-            qWarning() << "MainWindow::DatabaseConnect - ERROR: no driver " << DRIVER << " available";
-    }
-    static void initexample()
-    {
-        QSqlQuery query("CREATE TABLE flights (id INTEGER PRIMARY KEY, date NUMERIC)");
-
-        if(!query.isActive())
-            qWarning() << "MainWindow::DatabaseInit - ERROR: " << query.lastError().text();
-
-    }
-
-    static void queryexamplenamedbinding()
-    {
-        QSqlQuery query;
-        //query.prepare("SELECT * FROM people");
-        //query.prepare("SELECT * FROM people WHERE name LIKE 'Linus' OR id = :id");
-        query.prepare("SELECT * from people WHERE name LIKE :name");
-        query.bindValue(":name", "%Linus%");
-        query.bindValue(":id",2);
-        query.exec();
-        /*
-         * QSqlQuery provides access to the result set one record at a time. After the call to exec(),
-         * QSqlQuery's internal pointer is located one position before the first record.
-         * We must call QSqlQuery::next() once to advance to the first record, then next() again
-         * repeatedly to access the other records, until it returns false. Here's a typical loop that
-         * iterates over all the records in order:
-         * After a SELECT query is executed we have to browse the records (result rows) returned to access
-         * the data. In this case we try to retrieve the first record calling the function first which
-         * returns true when the query has been successful and false otherwise.
-         */
-        if(query.first());
-        else
-            qDebug() << ("No entry found");
-        query.previous();//To go back to index 0
-        while (query.next()) {
-            QString name = query.value(1).toString();
-            int id = query.value(0).toInt();
-            qDebug() << name << id;
-        }
-        /*
-         *The QSqlQuery::value() function returns the value of a field in the current record. Fields are
-         * specified as zero-based indexes. QSqlQuery::value() returns a QVariant, a type that can hold
-         * various C++ and core Qt data types such as int, QString, and QByteArray. The different database
-         * types are automatically mapped into the closest Qt equivalent. In the code snippet, we call
-         * QVariant::toString() and QVariant::toInt() to convert variants to QString and int.
-         */
-    }
-
-
-
-
-
-/*
- *  Obsolete Functions
- */
-    /*!
-     * \brief SelectFlightDate Retreives Flights from the database currently not in use.
-     * \param doft Date of flight for filtering result set. "ALL" means no filter.
-     * \return Flight(s) for selected date.
-     */
-    static QVector<QString> SelectFlightDate(QString doft)
-    {
-        QSqlQuery query;
-        if (doft == "ALL") // Special Selector
-        {
-            query.prepare("SELECT * FROM flights ORDER BY doft DESC, tofb ASC");
-            qDebug() << "All flights selected";
-        }else
-        {
-            query.prepare("SELECT * FROM flights WHERE doft = ? ORDER BY tofb ASC");
-            query.addBindValue(doft);
-            qDebug() << "Searching flights for " << doft;
-        }
-
-        query.exec();
-
-        if(query.first());
-        else
-        {
-            qDebug() << ("No flight with this date found");
-            QVector<QString> flight; //return empty
-            return flight;
-        }
-
-        query.previous();// To go back to index 0
-        query.last(); // this can be very slow, used to determine query size since .size is not supported by sqlite
-        int numRows = query.at() + 1; // Number of rows (flights) in the query
-        query.first();
-        query.previous();// Go back to index 0
-
-        QVector<QString> flight(numRows * 9); // Every flight has 9 fields in the database
-        int index = 0; // counter for output vector
-
-        while (query.next()) {
-            QString id = query.value(0).toString();
-            QString doft = query.value(1).toString();
-            QString dept = query.value(2).toString();
-            QString tofb = calc::minutes_to_string((query.value(3).toString()));
-            QString dest = query.value(4).toString();
-            QString tonb = calc::minutes_to_string((query.value(5).toString()));
-            QString tblk = calc::minutes_to_string((query.value(6).toString()));
-            QString pic = dbPilots::retreivePilotNameFromID(query.value(7).toString());
-            QString acft = dbAircraft::retreiveRegistration(query.value(8).toString());
-            //qDebug() << id << doft << dept << tofb << dest << tonb << tblk << pic << acft << endl;
-            flight[index] = id;
-            ++index;
-            flight[index] = doft;
-            ++index;
-            flight[index] = dept;
-            ++index;
-            flight[index] = tofb;
-            ++index;
-            flight[index] = dest;
-            ++index;
-            flight[index] = tonb;
-            ++index;
-            flight[index] = tblk;
-            ++index;
-            flight[index] = pic;
-            ++index;
-            flight[index] = acft;
-            ++index;
-        }
-        return flight;
-    }
-};

+ 1 - 1
dbpilots.cpp

@@ -16,7 +16,7 @@
  *along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 #include "dbpilots.h"
-#include "dbman.cpp"
+
 
 /*!
  * \brief RetreivePilotNameFromID Looks up pilot ID in database

+ 3 - 0
dbpilots.h

@@ -19,6 +19,9 @@
 #define DBPILOTS_H
 
 #include <QCoreApplication>
+#include <QSqlQuery>
+#include <QSqlError>
+#include <QDebug>
 
 /*!
  * \brief The dbPilots class class provides a databank interface for actions related to the

+ 1 - 1
dbsettings.cpp

@@ -16,7 +16,7 @@
  *along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 #include "dbsettings.h"
-#include "dbman.cpp"
+
 
 /*
  * Settings Database Related Functions

+ 3 - 0
dbsettings.h

@@ -19,6 +19,9 @@
 #define DBSETTINGS_H
 
 #include <QCoreApplication>
+#include <QSqlQuery>
+#include <QSqlError>
+#include <QDebug>
 
 /*!
  * \brief The dbSettings class provides functionality for retreiving settings

+ 1 - 1
dbstat.cpp

@@ -16,7 +16,7 @@
  *along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 #include "dbstat.h"
-#include "dbman.cpp"
+
 
 /*!
  * \brief retreiveTotalTime Looks up Total Blocktime in the flights database

+ 4 - 0
dbstat.h

@@ -19,6 +19,10 @@
 #define DBSTAT_H
 
 #include <QCoreApplication>
+#include <QDate>
+#include <QSqlQuery>
+#include <QSqlError>
+#include <QDebug>
 
 
 /*!

+ 0 - 1
easaview.cpp

@@ -17,7 +17,6 @@
  */
 #include "easaview.h"
 #include "ui_easaview.h"
-#include "dbman.cpp"
 #include <QSqlTableModel>
 
 EasaView::EasaView(QWidget *parent) :

+ 3 - 1
editflight.cpp

@@ -18,8 +18,10 @@
 #include "editflight.h"
 #include "ui_editflight.h"
 #include "calc.h"
-#include "dbman.cpp"
 #include "dbflight.h"
+#include "dbaircraft.h"
+#include "dbairport.h"
+#include "dbpilots.h"
 #include <QMessageBox>
 #include <QDebug>
 

+ 1 - 1
homewidget.cpp

@@ -18,8 +18,8 @@
 #include "homewidget.h"
 #include "ui_homewidget.h"
 #include "calc.h"
-#include "dbman.cpp"
 #include "dbstat.h"
+#include "dbpilots.h"
 
 #include <QDebug>
 

+ 0 - 5
logbookwidget.cpp

@@ -17,15 +17,10 @@
  */
 #include "logbookwidget.h"
 #include "ui_logbookwidget.h"
-#include <QSqlTableModel>
-#include <QMessageBox>
-#include "dbman.cpp"
 #include "dbflight.h"
 #include "newflight.h"
 #include "editflight.h"
 
-#include <chrono>
-#include <QDebug>
 
 //To Do: Update Selection in Tableview on arrow key press.
 

+ 4 - 0
logbookwidget.h

@@ -20,6 +20,10 @@
 
 #include <QWidget>
 #include <QItemSelection>
+#include <QSqlTableModel>
+#include <QMessageBox>
+#include <chrono>
+#include <QDebug>
 
 namespace Ui {
 class logbookWidget;

+ 30 - 4
main.cpp

@@ -23,19 +23,47 @@
 #include <QDir>
 #include <QPalette>
 #include <QColor>
-#include "dbman.cpp"
+#include <QSqlDatabase>
+#include <QSqlDriver>
+#include <QSqlError>
+#include <QSqlQuery>
 #include "dbsettings.h"
 #include <QDebug>
 
 int selectedtheme = 1; //Variable to store theming information
 
+void connectToDatabase()
+{
+    const QString DRIVER("QSQLITE");
+
+    if(QSqlDatabase::isDriverAvailable(DRIVER))
+    {
+        QSqlDatabase db = QSqlDatabase::addDatabase(DRIVER);
+
+        /*For a release, we need to decide where to put the database, for now
+         *it is in the executable working directory for ease of developtment*/
+
+        //QString pathtodb = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
+        //db.setDatabaseName(pathtodb+"/logbook.db");
+        //qDebug() << "Database: " << pathtodb+"/logbook.db";
+        db.setDatabaseName("logbook.db");
+
+        if(!db.open())
+            qWarning() << "MainWindow::DatabaseConnect - ERROR: " << db.lastError().text();
+    }
+    else
+        qWarning() << "MainWindow::DatabaseConnect - ERROR: no driver " << DRIVER << " available";
+}
+
+
 int main(int argc, char *argv[])
 {
     QCoreApplication::setOrganizationName("Fiffty50");
     QCoreApplication::setOrganizationDomain("f-cloud.ch");
     QCoreApplication::setApplicationName("openLog");
     QApplication openLog(argc, argv);
-    db::connect();  //connect to the database
+
+    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
@@ -55,8 +83,6 @@ int main(int argc, char *argv[])
         openLog.setStyleSheet(stream.readAll());
     }
 
-
-
     MainWindow w;
     w.show();
     return openLog.exec();

+ 0 - 15
mainwindow.cpp

@@ -21,25 +21,10 @@
 #include "editflight.h"
 #include "newacft.h"
 #include "easaview.h"
-#include "dbman.cpp"
 #include "calc.h"
 #include "homewidget.h"
 #include "logbookwidget.h"
 #include "settingswidget.h"
-#include <QTime>
-#include <QSqlDatabase>
-#include <QSqlDriver>
-#include <QSqlError>
-#include <QSqlQuery>
-#include <QSqlTableModel>
-#include <QTableView>
-#include <chrono>
-#include <QMessageBox>
-
-#include <QDir>
-#include <QFile>
-
-qlonglong SelectedFlightold = -1;
 
 MainWindow::MainWindow(QWidget *parent)
     : QMainWindow(parent)

+ 7 - 0
mainwindow.h

@@ -19,6 +19,13 @@
 #define MAINWINDOW_H
 
 #include <QMainWindow>
+#include <QTime>
+#include <QSqlTableModel>
+#include <QTableView>
+#include <chrono>
+#include <QMessageBox>
+#include <QDir>
+#include <QFile>
 
 QT_BEGIN_NAMESPACE
 namespace Ui { class MainWindow; }

+ 0 - 9
newacft.cpp

@@ -17,7 +17,6 @@
  */
 #include "newacft.h"
 #include "ui_newacft.h"
-#include "dbman.cpp"
 #include "showaircraftlist.h"
 #include <QStringListModel>
 #include <QSortFilterProxyModel>
@@ -45,10 +44,6 @@ NewAcft::~NewAcft()
     delete ui;
 }
 
-
-
-
-
 void NewAcft::on_MakeLineEdit_textEdited(const QString &arg1)
 {
     QStringList makeList = dbAircraft::retreiveAircraftMake(arg1);
@@ -75,7 +70,6 @@ void NewAcft::on_MakeLineEdit_editingFinished()
     }
 }
 
-
 void NewAcft::on_ModelLineEdit_textEdited(const QString &arg1)
 {
     QStringList modelList = dbAircraft::retreiveAircraftModel(make, arg1);
@@ -166,9 +160,6 @@ void NewAcft::on_VerifyButton_clicked()
     {
         ui->VerifyLineEdit->setText("EASA FCL.050 compliance checks disabled.");
     }
-
-
-
 }
 
 void NewAcft::on_RegistrationLineEdit_editingFinished()

+ 3 - 2
newflight.cpp

@@ -17,7 +17,6 @@
  */
 #include "newflight.h"
 #include "ui_newflight.h"
-#include "dbman.cpp"
 
 
 /// =======================================================
@@ -241,8 +240,10 @@ NewFlight::~NewFlight()
     delete ui;
 }
 
-/*
+/*!
+ * ============================================================================
  * Slots
+ * ============================================================================
  */
 
 /*!

+ 0 - 1
openLog.pro

@@ -26,7 +26,6 @@ SOURCES += \
     dbaircraft.cpp \
     dbairport.cpp \
     dbflight.cpp \
-    dbman.cpp \
     dbpilots.cpp \
     dbsettings.cpp \
     dbstat.cpp \

+ 1 - 6
settingswidget.cpp

@@ -17,12 +17,7 @@
  */
 #include "settingswidget.h"
 #include "ui_settingswidget.h"
-#include "dbman.cpp"
-#include <QButtonGroup>
-#include <QRegExp>
-#include <QValidator>
-#include <QMessageBox>
-#include <QDebug>
+
 
 
 

+ 5 - 0
settingswidget.h

@@ -19,6 +19,11 @@
 #define SETTINGSWIDGET_H
 
 #include <QWidget>
+#include <QButtonGroup>
+#include <QRegExp>
+#include <QValidator>
+#include <QMessageBox>
+#include <QDebug>
 #include "dbsettings.h"
 
 namespace Ui {

+ 1 - 6
showaircraftlist.cpp

@@ -15,14 +15,9 @@
  *You should have received a copy of the GNU General Public License
  *along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
-#include "dbman.cpp"
 #include "showaircraftlist.h"
 #include "ui_showaircraftlist.h"
-#include <QStringListModel>
-#include <QDebug>
-#include <QMessageBox>
-#include <QSqlQueryModel>
-#include <QSqlTableModel>
+
 
 ShowAircraftList::ShowAircraftList(QWidget *parent) :
     QDialog(parent),

+ 5 - 0
showaircraftlist.h

@@ -19,6 +19,11 @@
 #define SHOWAIRCRAFTLIST_H
 
 #include <QDialog>
+#include <QStringListModel>
+#include <QDebug>
+#include <QMessageBox>
+#include <QSqlQueryModel>
+#include <QSqlTableModel>
 
 namespace Ui {
 class ShowAircraftList;

+ 13 - 0
strictregularexpressionvalidator.h

@@ -13,3 +13,16 @@ public:
 };
 
 #endif // STRICTREGULAREXPRESSIONVALIDATOR_H
+
+/*class StrictRegularExpressionValidator : public QRegularExpressionValidator {
+public:
+    QValidator::State validate(QString& txt, int& pos) const {
+        {
+            auto validation = QRegularExpressionValidator::validate(txt, pos);
+            if(validation == QValidator::Intermediate) {
+                return QValidator::Invalid;
+            }
+            return validation;
+        }
+    }
+};*/