Browse Source

Wont compile. Entry Database polished.

Georgios Kotzampopoulos 4 years ago
parent
commit
3c3e64e1f5

+ 7 - 5
openPilotLog.pro

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

+ 23 - 9
src/experimental/Db.cpp → src/experimental/DataBase.cpp

@@ -1,8 +1,17 @@
-#include "Db.h"
+#include "DataBase.h"
 
-using namespace experimental;
+namespace experimental {
 
-bool DB::connect()
+DataBase* DataBase::instance = nullptr;
+
+DataBase* DataBase::getInstance()
+{
+    return instance ?: new DataBase();
+//    if(!instance)
+//    return instance;
+}
+
+bool DataBase::connect()
 {
     const QString driver("QSQLITE");
 
@@ -33,7 +42,7 @@ bool DB::connect()
     return true;
 }
 
-bool DB::commit(Entry entry)
+bool DataBase::commit(Entry entry)
 {
     if (exists(entry)) {
         return update(entry);
@@ -42,7 +51,7 @@ bool DB::commit(Entry entry)
     }
 }
 
-bool DB::remove(Entry entry)
+bool DataBase::remove(Entry entry)
 {
     if (!exists(entry)) {
         DEB("Error: Entry does not exist.");
@@ -65,9 +74,9 @@ bool DB::remove(Entry entry)
     }
 }
 
-bool DB::exists(Entry entry)
+bool DataBase::exists(Entry entry)
 {
-    if (entry.position.second == 0)
+    if(entry.position == DEFAULT_PILOT_POSITION)
         return false;
 
     //Check database for row id
@@ -87,7 +96,7 @@ bool DB::exists(Entry entry)
 }
 
 
-bool DB::update(Entry updated_entry)
+bool DataBase::update(Entry updated_entry)
 {
     auto data = updated_entry.getData();
     QString statement = "UPDATE " + updated_entry.position.first + " SET ";
@@ -116,7 +125,7 @@ bool DB::update(Entry updated_entry)
     }
 }
 
-bool DB::insert(Entry newEntry)
+bool DataBase::insert(Entry newEntry)
 {
     auto data = newEntry.getData();
     DEB("Inserting...");
@@ -145,4 +154,9 @@ bool DB::insert(Entry newEntry)
         DEB("Query Error: " << q.lastError().text());
         return false;
     }
+
+}
+
+DataBase* DB() { return DataBase::getInstance(); }
+
 }

+ 89 - 0
src/experimental/DataBase.h

@@ -0,0 +1,89 @@
+#ifndef __DB_H__
+#define __DB_H__
+
+#include <QPair>
+#include <QMap>
+#include <QString>
+#include <QSqlQuery>
+#include <QSqlError>
+#include "src/database/dbinfo.h"
+#include "debug.h"
+
+#include "src/experimental/UserInput.h"
+//#include "Decl.h"
+#include "Entry.h"
+
+namespace experimental {
+
+/// [F] ideas for functions of db class:
+/// https://github.com/fiffty-50/openpilotlog/wiki/New-DB-class-brainstorming    
+
+/*!
+ * \brief The DB class encapsulates the SQL database by providing fast access
+ * to hot database data.
+ */
+class DataBase {
+private:
+    QStringList tableNames;
+    QMap<QString, QStringList> tableColumns;
+    static DataBase* instance;
+    DataBase() = default;
+signals:
+    void commitSuccessful();
+    void commitUnsuccessful(QString message);
+public:
+    // Ensure DB is not copiable or assignable
+    DataBase(const DataBase&) = delete;
+    void operator=(const DataBase&) = delete;
+    static DataBase* getInstance();
+
+    /*!
+     * \brief Connect to the database and populate database information.
+     */
+    bool connect();
+
+    /*!
+     * \brief closes the database connection.
+     */
+    bool disconnect();
+
+    /*!
+     * \brief Checks if an entry exists in the database, based on position data
+     */
+    bool exists(Entry entry);
+
+    /*!
+     * \brief commits an entry to the database, calls either insert or update,
+     * based on position data
+     */
+    bool commit(Entry entry);
+
+    /*!
+     * \brief Create new entry in the databse based on UserInput
+     */
+    bool insert(Entry newEntry);
+
+    /*!
+     * \brief Updates entry in database from existing entry tweaked by the user.
+     */
+    bool update(Entry updated_entry);
+
+    /*!
+     * \brief deletes an entry from the database.
+     */
+    bool remove(Entry entry);
+
+};
+
+/*!
+ * \brief Convinience function that returns instance of DataBase.
+ * Instead of this:
+ * DataBase::getInstance().commit(...)
+ * Write this:
+ * DB().commit(...)
+ */
+DataBase* DB();
+
+}  // namespace experimental
+
+#endif

+ 0 - 112
src/experimental/Db.h

@@ -1,112 +0,0 @@
-#ifndef __DB_H__
-#define __DB_H__
-
-#include <QPair>
-#include <QMap>
-#include <QString>
-#include <QSqlQuery>
-#include <QSqlError>
-#include "src/database/dbinfo.h"
-#include "debug.h"
-
-#include "src/experimental/UserInput.h"
-
-namespace experimental {
-
-/// [F] ideas for functions of db class:
-/// https://github.com/fiffty-50/openpilotlog/wiki/New-DB-class-brainstorming    
-
-/// SELF DOCUMENTING CODE
-using ColName = QString;
-using ColData = QString;
-using TableName = QString;
-using RowId = int;
-using DataPosition = QPair<TableName, RowId>;
-using TableData = QMap<ColName, ColData>;
-using ColumnData = QPair<ColName, ColData>;
-
-// DEFAULTS
-auto const DEFAULT_PILOT_POSITION = DataPosition("pilots", 0);
-
-/*!
- * \brief The Entry class encapsulates table metadata(table name, row id)
- *  and data for new and existing entries in the database to operate on.
- */
-class Entry {
-public:
-    DataPosition position;
-protected:
-    TableData tableData;
-public:
-    Entry() {};
-    Entry(const Entry&) = default;
-    Entry(DataPosition position_) : position(position_) {}
-    void setData(TableData data) { tableData = data; }
-    const TableData& getData() { return tableData; }
-};
-
-// [George]: Either with polymorphism or simple functions the result will be the same.
-// if the following syntax is more clear to you we can switch to it.
-// ... = new NewPilotDialog(Pilot(selectedPilots.first(), ...);
-// the only difference will be that we will subclass Entry to have specialised
-// constructor
-class PilotEntry : public Entry {
-public:
-    PilotEntry() {};
-    PilotEntry(const PilotEntry&) = default;
-    PilotEntry(int row_id) : Entry::Entry(DataPosition("pilots", row_id)) {}
-    PilotEntry(TableData fromNewPilotDialog) : Entry::Entry(DataPosition("pilots", 0)) {
-        setData(fromNewPilotDialog);
-    }
-};
-
-/*!
- * \brief The DB class encapsulates the SQL database by providing fast access
- * to hot database data.
- */
-class DB {
-private:
-    static inline QStringList                   tableNames;
-    static inline QMap<TableName, QStringList>  tableColumns;
-public:
-    /*!
-     * \brief Connect to the database and populate database information.
-     */
-    static bool connect();
-
-    /*!
-     * \brief closes the database connection.
-     */
-    static bool disconnect();
-
-    /*!
-     * \brief Checks if an entry exists in the database, based on position data
-     */
-    static bool exists(Entry entry);
-
-    /*!
-     * \brief commits an entry to the database, calls either insert or update,
-     * based on position data
-     */
-    static bool commit(Entry entry);
-
-    /*!
-     * \brief Create new entry in the databse based on UserInput
-     */
-    static bool insert(Entry newEntry);
-
-    /*!
-     * \brief Updates entry in database from existing entry tweaked by the user.
-     */
-    static bool update(Entry updated_entry);
-
-    /*!
-     * \brief deletes an entry from the database.
-     */
-    static bool remove(Entry entry);
-
-};
-
-}  // namespace experimental
-
-#endif

+ 35 - 0
src/experimental/Decl.h

@@ -0,0 +1,35 @@
+#ifndef DECL_H
+#define DECL_H
+
+#include <QString>
+#include <QPair>
+#include <QMap>
+
+namespace experimental {
+
+//using ColName = QString;
+//using ColData = QString;
+//using TableName = QString;
+//using RowId = int;
+
+//using TableNames = QStringList;
+//using TableData = QMap<ColName, ColData>;
+//using ColumnData = QPair<ColName, ColData>;
+//using ColumnNames = QStringList;
+//using TableColumns = QMap<TableName, ColumnNames>;
+
+struct DataPosition : QPair<QString, int> {
+    QString& tableName;
+    int& rowId;
+    DataPosition()
+        : QPair<QString, int>(), tableName(first), rowId(second)
+    {}
+    DataPosition(QString tn, int ri)
+        : QPair<QString, int>(tn, ri), tableName(first), rowId(second)
+    {}
+};
+
+auto const DEFAULT_PILOT_POSITION = DataPosition("pilots", 0);
+}
+
+#endif // DECL_H

+ 33 - 0
src/experimental/Entry.cpp

@@ -0,0 +1,33 @@
+#include "Entry.h"
+
+namespace experimental {
+
+Entry::Entry(DataPosition position_)
+    : position(position_)
+{}
+
+void Entry::setData(QMap<QString, QString> data)
+{
+    tableData = data;
+}
+
+const QMap<QString, QString>& Entry::getData()
+{
+    return tableData;
+}
+
+PilotEntry::PilotEntry(const PilotEntry& pe)
+    : Entry::Entry(pe)
+{}
+
+PilotEntry::PilotEntry(int row_id)
+    : Entry::Entry(DataPosition("pilots", row_id))
+{}
+
+PilotEntry::PilotEntry(QMap<QString, QString> fromNewPilotDialog)
+    : Entry::Entry(DEFAULT_PILOT_POSITION)
+{
+    setData(fromNewPilotDialog);
+}
+
+}  // namespace experimental

+ 40 - 0
src/experimental/Entry.h

@@ -0,0 +1,40 @@
+#ifndef ENTRY_H
+#define ENTRY_H
+
+#include <QString>
+#include <QStringList>
+#include <QMap>
+#include <QPair>
+
+#include "Decl.h"
+
+namespace experimental {
+
+/*!
+ * \brief The Entry class encapsulates table metadata(table name, row id)
+ *  and data for new and existing entries in the database to operate on.
+ */
+class Entry {
+public:
+    DataPosition position;
+protected:
+    QMap<QString, QString> tableData;
+public:
+    Entry() = default;
+    Entry(const Entry&) = default;
+    Entry(DataPosition position_);
+    void setData(QMap<QString, QString> data);
+    const QMap<QString, QString>& getData();
+};
+
+class PilotEntry : public Entry {
+public:
+    PilotEntry() = default;
+    PilotEntry(const PilotEntry& pe);
+    PilotEntry(int row_id);
+    PilotEntry(QMap<QString, QString> fromNewPilotDialog);
+};
+
+}
+
+#endif // ENTRY_H

+ 0 - 0
src/experimental/UserInput.cpp


+ 6 - 3
src/gui/dialogues/newpilotdialog.cpp

@@ -19,7 +19,7 @@
 #include "ui_newpilot.h"
 #include "debug.h"
 
-#include "src/experimental/Db.h"
+#include "src/experimental/DataBase.h"
 
 /* Examples for names around the world:
  * José Eduardo Santos Tavares Melo Silva
@@ -87,6 +87,9 @@ NewPilotDialog::NewPilotDialog(experimental::PilotEntry oldEntry, Db::editRole,
     QDialog(parent),
     ui(new Ui::NewPilot)
 {
+    using namespace experimental;
+    connect(DB(), &DataBase::commitUnsuccessful,
+            this, &NewPilotDialog::onCommitUnsuccessful);
     oldPilotEntry = oldEntry;
     //to do
 }
@@ -176,12 +179,12 @@ void NewPilotDialog::submitForm()
     switch (role) {
     case Db::editExisting:
         oldEntry.setData(newData);
-        DB::commit(oldPilotEntry);
+        DataBase::commit(oldPilotEntry);
         // to do: handle unsuccessful commit
         break;
     case Db::createNew:
         auto newEntry = PilotEntry(newData);
-        DB::commit(newEntry);
+        DataBase::commit(newEntry);
         // to do: handle unsuccessful commit
         break;
     }

+ 3 - 4
src/gui/dialogues/newpilotdialog.h

@@ -25,7 +25,7 @@
 #include <QCompleter>
 #include "src/classes/pilot.h"
 #include "src/classes/completionlist.h"
-#include "src/experimental/Db.h"
+#include "src/experimental/Entry.h"
 
 namespace Ui {
 class NewPilot;
@@ -40,15 +40,13 @@ public:
     explicit NewPilotDialog(Pilot, Db::editRole, QWidget *parent = nullptr);
     explicit NewPilotDialog(experimental::PilotEntry oldEntry, Db::editRole, QWidget *parent = nullptr);
     ~NewPilotDialog();
-
 private slots:
     void on_buttonBox_accepted();
-
+    void onCommitUnsuccessful();
 private:
     Ui::NewPilot *ui;
 
     Db::editRole role;
-
     Pilot oldEntry;
 
     experimental::PilotEntry oldPilotEntry;
@@ -62,4 +60,5 @@ private:
     void submitForm();
 };
 
+
 #endif // NEWPILOT_H