Browse Source

draft of streamlined user-entry-db architecture

Georgios Kotzampopoulos 4 years ago
parent
commit
3e55eb2880

+ 2 - 0
debug.h

@@ -1,6 +1,8 @@
 #ifndef DEBUG_H
 #define DEBUG_H
 
+#include <QDebug>
+
 // Debug Makro
 #define DEB(expr) \
     qDebug() << __PRETTY_FUNCTION__ << "\t" << expr

+ 6 - 2
openPilotLog.pro

@@ -33,6 +33,7 @@ SOURCES += \
     src/database/dbinfo.cpp \
     src/database/dbsetup.cpp \
     src/database/entry.cpp \
+    src/experimental/UserInput.cpp \
     src/gui/dialogues/firstrundialog.cpp \
     src/gui/dialogues/newflightdialog.cpp \
     src/gui/dialogues/newpilotdialog.cpp \
@@ -43,7 +44,8 @@ SOURCES += \
     src/gui/widgets/logbookwidget.cpp \
     src/gui/widgets/pilotswidget.cpp \
     src/gui/widgets/settingswidget.cpp \
-    src/gui/widgets/totalswidget.cpp
+    src/gui/widgets/totalswidget.cpp \
+    src/experimental/Db.cpp
 
 HEADERS += \
     debug.h \
@@ -63,6 +65,7 @@ HEADERS += \
     src/database/dbinfo.h \
     src/database/dbsetup.h \
     src/database/entry.h \
+    src/experimental/UserInput.h \
     src/gui/dialogues/firstrundialog.h \
     src/gui/dialogues/newflightdialog.h \
     src/gui/dialogues/newpilotdialog.h \
@@ -73,7 +76,8 @@ HEADERS += \
     src/gui/widgets/logbookwidget.h \
     src/gui/widgets/pilotswidget.h \
     src/gui/widgets/settingswidget.h \
-    src/gui/widgets/totalswidget.h
+    src/gui/widgets/totalswidget.h \
+    src/experimental/Db.h
 
 FORMS += \
     mainwindow.ui \

+ 4 - 1
src/database/db.h

@@ -81,7 +81,8 @@ class Db
          * \return
          */
         static bool             exists(QString column, QString table, QString checkColumn,
-                                       QString value, Db::matchType match){
+                                       QString value, Db::matchType match)
+        {
             return get().iexists(column, table, checkColumn, value, match);
         }
         /*!
@@ -142,6 +143,7 @@ class Db
         static QVector<QString> customQuery(QString query, int returnValues){
             return get().icustomQuery(query, returnValues);
         }
+
     private:
         Db() {}
         void             iconnect();
@@ -159,6 +161,7 @@ class Db
         QVector<QString> icustomQuery(QString query, int returnValues);
 
     public:
+        /// [George]: Why delete these in particular?
         Db(Db const&)              = delete;
         void operator=(Db const&)  = delete;
 };

+ 6 - 5
src/database/dbsetup.cpp

@@ -248,6 +248,9 @@ const QStringList templateTables= {
 
 bool DbSetup::createDatabase()
 {
+    /// [George]: Not necessary to heap allocate for such a trivial task
+    /// TODO: Since you want to be fancy well do it with some cheeky bit operations
+    /// for the lolz.
     QVector<bool> returnValues;
 
     DEB("Creating tables...");
@@ -258,14 +261,12 @@ bool DbSetup::createDatabase()
     returnValues << importDefaultData();
 
     for (const auto& allGood : returnValues) {
-        if (!allGood) {
+        if (!allGood){
             return false;
-        } else {
-            DEB("Database successfully created!");
-            return true;
         }
     }
-    return false;
+    DEB("Database successfully created!");
+    return true;
 }
 
 

+ 1 - 1
src/database/entry.cpp

@@ -17,10 +17,10 @@
  */
 #include "entry.h"
 #include "debug.h"
+#include "db.h"
 
 Entry::Entry()
 {
-
 }
 
 Entry::Entry(QString table, int row)

+ 2 - 2
src/database/entry.h

@@ -19,8 +19,8 @@
 #define ENTRY_H
 
 #include <QCoreApplication>
-#include "src/database/db.h"
-#include "src/database/dbinfo.h"
+#include "db.h"
+#include "dbinfo.h"
 /*!
  * \brief The Entry class is the base class for database entries.
  * It can be seen as a row in a table within the database.

+ 0 - 0
src/experimental/Db.cpp


+ 93 - 0
src/experimental/Db.h

@@ -0,0 +1,93 @@
+#ifndef __DB_H__
+#define __DB_H__
+
+#include <QPair>
+#include <QString>
+#include <QSqlQuery>
+#include <QSqlError>
+#include "debug.h"
+
+#include "src/experimental/UserInput.h"
+
+namespace experimental {
+
+/*!
+ * \brief findPos: Query database and find table and position of data in uin
+ */
+static
+QPair<QString, int> findPos(UserInput& uin)
+{
+    switch(uin.meta_tag){
+    case UserInput::MetaTag::Pilot:
+//        break;
+    case UserInput::MetaTag::Flight:
+//        break;
+    case UserInput::MetaTag::Aircraft:
+//        break;
+    default:
+        return {"", 0};
+    }
+}
+
+static
+bool insertPilot(UserInput& uin)
+{
+    DEB("Inserting...");
+    auto data = uin.all();
+    auto position = findPos(uin);
+
+    if (data.isEmpty()) {
+        DEB("Object Contains no data. Aborting.");
+        return false;
+    }
+    QString statement = "INSERT INTO " + position.first + QLatin1String(" (");
+    QMap<QString, QString>::const_iterator i;
+    for (i = data.cbegin(); i != data.cend(); ++i) {
+        statement += i.key() + QLatin1String(", ");
+    }
+    statement.chop(2);
+    statement += QLatin1String(") VALUES (");
+    for (i = data.cbegin(); i != data.cend(); ++i) {
+        statement += QLatin1String("'") + i.value() + QLatin1String("', ");
+    }
+    statement.chop(2);
+    statement += QLatin1String(")");
+
+    QSqlQuery q(statement);
+    if (q.lastError().type() == QSqlError::NoError) {
+        DEB("Entry successfully committed.");
+        return true;
+    } else {
+        DEB("Unable to commit. Query Error: " << q.lastError().text());
+        return false;
+    }
+}
+
+namespace DB {
+    bool init();
+    bool commit(UserInput uin)
+    {
+        switch (uin.meta_tag)
+        {
+        case UserInput::MetaTag::Pilot:
+            insertPilot(uin);
+            break;
+        case UserInput::MetaTag::Flight:
+            // Flight commit logic
+            break;
+        case UserInput::MetaTag::Aircraft:
+            // Aircraft commit logic
+            break;
+        default:
+            return false;
+        }
+        return true;
+    }
+    bool remove(UserInput uin) { return false; }
+    bool exists(UserInput uin) { return false; }
+    bool update(UserInput uin) { return false; }
+}
+
+}
+
+#endif

+ 0 - 0
src/experimental/UserInput.cpp


+ 47 - 0
src/experimental/UserInput.h

@@ -0,0 +1,47 @@
+#ifndef __USERINPUT_H__
+#define __USERINPUT_H__
+
+#include <QPair>
+#include <QString>
+#include <QBitArray>
+#include <QMap>
+#include <QStringList>
+#include <algorithm>
+
+namespace experimental {
+
+using EntryData = QMap<QString, QString>;
+
+/*!
+ * \brief The EntryData struct. Contains ALL possible data.
+ * However depending on who is constructing it, different types are initialised.
+ * FUTURE: Would it be necessary to able to change data?
+ * 	 George: I would say no because we dont want to fuck around with the entry.
+ * Collect data from user -> Pack it up in the entry -> Consume it.
+ * \todo Figure out exactly what the database would prefer as return value
+ *   George: I would assume key: value pairs where the keys are what you would
+ * put in the queries. This will affect data aswell (and propably simplify it)
+ */
+class UserInput {
+private:
+    const EntryData data;
+public:
+    const enum class MetaTag {Pilot, Flight, Aircraft} meta_tag;
+
+public:
+    UserInput() = delete;
+    explicit
+    UserInput(EntryData new_data, MetaTag tag)
+        :  data(new_data), meta_tag(tag) {}
+
+    QString only(QString data_type) const { return data.value(data_type); }
+    const EntryData& all() const { return data; }
+};
+
+UserInput newPilotInput(EntryData ed) { return UserInput(ed, UserInput::MetaTag::Pilot); }
+UserInput newFlightInput(EntryData ed) { return UserInput(ed, UserInput::MetaTag::Flight); }
+UserInput newAircraftInput(EntryData ed) { return UserInput(ed, UserInput::MetaTag::Aircraft); }
+
+}
+
+#endif

+ 14 - 9
src/gui/dialogues/newpilotdialog.cpp

@@ -19,6 +19,9 @@
 #include "ui_newpilot.h"
 #include "debug.h"
 
+#include "src/experimental/Db.h"
+#include "src/experimental/UserInput.h"
+
 /* Examples for names around the world:
  * José Eduardo Santos Tavares Melo Silva
  * María José Carreño Quiñones
@@ -162,18 +165,20 @@ void NewPilotDialog::submitForm()
     displayName.append(ui->picfirstnameLineEdit->text().left(1));
     displayName.append(QLatin1Char('.'));
     newData.insert("displayname",displayName);
-    //create db object
+
+    using namespace experimental;
+    auto uin = newPilotInput(newData);
+
     switch (role) {
-    case Db::createNew: {
-        auto newEntry = Pilot(newData);;
-        DEB("New Object: " << newEntry);
-        newEntry.commit();
+    case Db::createNew:
+        DEB("New Object: " << newData);
+        /// [George]: we should check if db operation was succesful
+        /// if not i assume we should just emit inputRejected or smth?
+        if(!DB::commit(uin)) emit QDialog::rejected();
         break;
-    }
     case Db::editExisting:
-        oldEntry.setData(newData);
-        DEB("updated entry: " << oldEntry);
-        oldEntry.commit();
+        DEB("updating entry with: " << newData);
+        if(!DB::update(uin)) emit QDialog::rejected();
         break;
     }
 }