Prechádzať zdrojové kódy

Implementation of experimental::DB

Functions of experimental::DB are now implemented. Minor adjustments to member names in Entry function, acc. https://github.com/fiffty-50/openpilotlog/wiki#code-style-conventions
Felix Turo 4 rokov pred
rodič
commit
492433a31d

+ 115 - 1
src/experimental/Db.cpp

@@ -2,7 +2,7 @@
 
 using namespace experimental;
 
-bool DB::init()
+bool DB::connect()
 {
     const QString driver("QSQLITE");
 
@@ -32,3 +32,117 @@ bool DB::init()
     }
     return true;
 }
+
+bool DB::commit(Entry entry)
+{
+    if (exists(entry)) {
+        return update(entry);
+    } else {
+        return insert(entry);
+    }
+}
+
+bool DB::remove(Entry entry)
+{
+    if (!exists(entry)) {
+        DEB("Error: Entry does not exist.");
+        return false;
+    }
+
+    QString statement = "DELETE FROM " + entry.position.first +
+            " WHERE _rowid_=" + QString::number(entry.position.second);
+    QSqlQuery q(statement);
+    //check result.
+    if (q.lastError().type() == QSqlError::NoError)
+    {
+        DEB("Entry " << entry.position.first << entry.position.second << " removed.");
+        return true;
+    } else {
+        DEB("Unable to delete.");
+        DEB("Query: " << statement);
+        DEB("Query Error: " << q.lastError().text());
+        return false;
+    }
+}
+
+bool DB::exists(Entry entry)
+{
+    if (entry.position.second == 0)
+        return false;
+
+    //Check database for row id
+    QString statement = "SELECT COUNT(*) FROM " + entry.position.first +
+            " WHERE _rowid_=" + QString::number(entry.position.second);
+    //this returns either 1 or 0 since row ids are unique
+    QSqlQuery q(statement);
+    q.next();
+    int rowId = q.value(0).toInt();
+    if (rowId) {
+        DEB("Entry exists with row ID: " << rowId);
+        return true;
+    } else {
+        DEB("Entry does not exist.");
+        return false;
+    }
+}
+
+
+bool DB::update(Entry updated_entry)
+{
+    auto data = updated_entry.getData();
+    QString statement = "UPDATE " + updated_entry.position.first + " SET ";
+    for (auto i = data.constBegin(); i != data.constEnd(); ++i) {
+        if (i.key() != QString()) {
+            statement += i.key() + QLatin1String("='") + i.value() + QLatin1String("', ");
+        } else {
+            DEB(i.key() << "is empty key. skipping.");
+        }
+    }
+    statement.chop(2); // Remove last comma
+    statement.append(QLatin1String(" WHERE _rowid_=") + QString::number(updated_entry.position.second));
+
+    DEB("UPDATE QUERY: " << statement);
+    QSqlQuery q(statement);
+    //check result.
+    if (q.lastError().type() == QSqlError::NoError)
+    {
+        DEB("Entry successfully committed.");
+        return true;
+    } else {
+        DEB("Unable to commit.");
+        DEB("Query: " << statement);
+        DEB("Query Error: " << q.lastError().text());
+        return false;
+    }
+}
+
+bool DB::insert(Entry newEntry)
+{
+    auto data = newEntry.getData();
+    DEB("Inserting...");
+    QString statement = "INSERT INTO " + newEntry.position.first + QLatin1String(" (");
+    QMap<QString, QString>::iterator i;
+    for (i = data.begin(); i != data.end(); ++i) {
+        statement += i.key() + QLatin1String(", ");
+    }
+    statement.chop(2);
+    statement += QLatin1String(") VALUES (");
+    for (i = data.begin(); i != data.end(); ++i) {
+        statement += QLatin1String("'") + i.value() + QLatin1String("', ");
+    }
+    statement.chop(2);
+    statement += QLatin1String(")");
+
+    QSqlQuery q(statement);
+    //check result.
+    if (q.lastError().type() == QSqlError::NoError)
+    {
+        DEB("Entry successfully committed.");
+        return true;
+    } else {
+        DEB("Unable to commit.");
+        DEB("Query: " << statement);
+        DEB("Query Error: " << q.lastError().text());
+        return false;
+    }
+}

+ 34 - 88
src/experimental/Db.h

@@ -36,12 +36,12 @@ class Entry {
 public:
     const DataPosition position;
 protected:
-    TableData table_data;
+    TableData tableData;
 public:
     Entry() = delete;
     Entry(DataPosition position_) : position(position_) {}
-    void populate_data(TableData data) { table_data = data; }
-    const TableData& data() { return table_data; }
+    void setData(TableData data) { tableData = data; }
+    const TableData& getData() { return tableData; }
 };
 
 // [George]: Either with polymorphism or simple functions the result will be the same.
@@ -51,43 +51,12 @@ public:
 // constructor
 class PilotEntry : public Entry {
     PilotEntry() = delete;
+public:
     PilotEntry(int row_id) : Entry::Entry(DataPosition("pilots", row_id)) {}
-};
-
-Entry newPilotEntry(int row_id) { return Entry(DataPosition("pilots", row_id)); }
-
-static
-bool insertPilot(UserInput& uin)
-{
-    DEB("Inserting...");
-    auto data = uin.all();
-    auto position = DEFAULT_PILOT_POSITION;
-
-    if (data.isEmpty()) {
-        DEB("Object Contains no data. Aborting.");
-        return false;
-    }
-    QString statement = "INSERT INTO " + position.first + QLatin1String(" (");
-    for (auto i = data.cbegin(); i != data.cend(); ++i) {
-        statement += i.key() + QLatin1String(", ");
+    PilotEntry(TableData fromNewPilotDialog) : Entry::Entry(DataPosition("pilots", 0)) {
+        setData(fromNewPilotDialog);
     }
-    statement.chop(2);
-    statement += QLatin1String(") VALUES (");
-    for (auto 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;
-    }
-}
+};
 
 /*!
  * \brief The DB class encapsulates the SQL database by providing fast access
@@ -95,68 +64,45 @@ bool insertPilot(UserInput& uin)
  */
 class DB {
 private:
-    static QStringList tableNames;
-    static QMap<TableName, QStringList> tableColumns;
+    static inline QStringList                   tableNames;
+    static inline QMap<TableName, QStringList>  tableColumns;
 public:
     /*!
-     * \brief Initialise DB class and populate columns.
+     * \brief Connect to the database and populate database information.
      */
-    static bool init();
+    static bool connect();
 
     /*!
-     * \brief Create new entry in the databse based on UserInput
+     * \brief closes the database connection.
      */
-    static
-    bool insert(UserInput uin)
-    {
-        switch (uin.meta_tag)
-        {
-        case UserInput::MetaTag::Pilot:
-            DEB("Inserting NEW Pilot...");
-            return insertPilot(uin);
-        case UserInput::MetaTag::Flight:
-        case UserInput::MetaTag::Aircraft:
-        default:
-            DEB("FALLTHROUGH in cases. Missing implementations");
-            return false;
-        }
-    }
+    static bool disconnect();
+
+    /*!
+     * \brief Checks if an entry exists in the database, based on position data
+     */
+    static bool exists(Entry entry);
 
-    static bool remove(UserInput uin) { return false; }
+    /*!
+     * \brief commits an entry to the database, calls either insert or update,
+     * based on position data
+     */
+    static bool commit(Entry entry);
 
-    static bool exists(UserInput uin) { return false; }
+    /*!
+     * \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.
-     * New entry data is verified before commiting.
      */
-    static bool update(Entry updated_entry)
-    {
-        auto position = updated_entry.position;
-        auto data = updated_entry.data();
-        QString statement = "UPDATE " + position.first + " SET ";
-        for (auto i = data.constBegin(); i != data.constEnd(); ++i) {
-            if (i.key() != QString()) {
-                statement += i.key() + QLatin1String("='") + i.value() + QLatin1String("', ");
-            } else {
-                DEB(i.key() << "is empty key. skipping.");
-            }
-        }
-        statement.chop(2); // Remove last comma
-        statement.append(QLatin1String(" WHERE _rowid_=") + QString::number(position.second));
-
-        DEB("UPDATE QUERY: " << statement);
-        QSqlQuery q(statement);
-        //check result. Upon success, error should be " "
-        if (q.lastError().type() == QSqlError::NoError)
-        {
-            DEB("Entry successfully committed.");
-            return true;
-        } else {
-            DEB("Unable to commit. Query Error: " << q.lastError().text());
-            return false;
-        }
-    }
+    static bool update(Entry updated_entry);
+
+    /*!
+     * \brief deletes an entry from the database.
+     */
+    static bool remove(Entry entry);
+
 };
 
 }  // namespace experimental

+ 13 - 11
src/gui/dialogues/newpilotdialog.cpp

@@ -149,7 +149,7 @@ void NewPilotDialog::submitForm()
     DEB("Creating Database Object...");
     QMap<QString, QString> newData;
 
-    auto line_edits = parent()->findChildren<QLineEdit *>();
+    auto line_edits = this->findChildren<QLineEdit *>();
 
     for (const auto &le : line_edits) {
         QString key = le->objectName();
@@ -167,18 +167,20 @@ void NewPilotDialog::submitForm()
     newData.insert("displayname",displayName);
 
     using namespace experimental;
-    auto uin = newPilotInput(newData);
+    //auto uin = newPilotInput(newData);
+    auto entry = PilotEntry(newData);
+
+    if (role == Db::editExisting)
+        //entry.setPosition = oldEntry.position; //needs implementation in Entry class
 
-    switch (role) {
-    case Db::createNew:
-        DEB("New Object: " << newData);
+    if (!DB::commit(entry))
+    {
         /// [George]: we should check if db operation was succesful
         /// if not i assume we should just emit inputRejected or smth?
-        if(!DB::insert(uin)) emit QDialog::rejected();
-        break;
-    case Db::editExisting:
-        DEB("updating entry with: " << newData);
-        if(!DB::update(uin)) emit QDialog::rejected();
-        break;
+        /// [F] We should do something, emitting reject might not be the best
+        /// option since it closes the dialog. Maybe showing a QMessageBox with
+        /// what has gone wrong?
+        DEB("Commit unsucessful.");
     }
+
 }

+ 2 - 0
src/gui/dialogues/newpilotdialog.h

@@ -49,6 +49,8 @@ private:
 
     Pilot oldEntry;
 
+    QPair<QString, int> oldPosition;
+
     void setupValidators();
 
     void setupCompleter();