Jelajahi Sumber

implementation of init

init creates a connection and fills in the member variables of the database class.
Felix Turo 4 tahun lalu
induk
melakukan
358530d140
2 mengubah file dengan 48 tambahan dan 6 penghapusan
  1. 42 0
      src/experimental/Db.cpp
  2. 6 6
      src/experimental/Db.h

+ 42 - 0
src/experimental/Db.cpp

@@ -0,0 +1,42 @@
+#include "Db.h"
+
+using namespace experimental;
+
+bool DB::init()
+{
+    const QString driver("QSQLITE");
+
+    if (QSqlDatabase::isDriverAvailable(driver)) {
+
+        QDir directory("data");
+        QString databaseLocation = directory.filePath("logbook.db");
+        QSqlDatabase db = QSqlDatabase::addDatabase(driver);
+        db.setDatabaseName(databaseLocation);
+
+        if (!db.open()) {
+            DEB("DatabaseConnect - ERROR: " << db.lastError().text());
+            return false;
+        } else {
+            DEB("Database connection established.");
+            // Enable foreign key restrictions
+            QSqlQuery query("PRAGMA foreign_keys = ON;");
+            // Retreive database layout and store in member variables
+            tables = db.tables();
+
+            QStringList columnNames;
+            for (const auto &table : tables) {
+                columnNames.clear();
+                QSqlRecord fields = db.record(table);
+                for (int i = 0; i < fields.count(); i++) {
+                    columnNames.append(fields.field(i).name());
+                    database_layout.insert(table, columnNames);
+                    ///[F] database_layout.value("pilots") gives you the columns for the pilots table
+                }
+            }
+            return true;
+        }
+    } else {
+        DEB("DatabaseConnect - ERROR: no driver " << driver << " available");
+        return false;
+    }
+}

+ 6 - 6
src/experimental/Db.h

@@ -91,7 +91,8 @@ bool insertPilot(UserInput& uin)
  */
 class DB {
 private:
-    static QVector<QString> columns;
+    static QMap<QString, QStringList> database_layout; // contains the column names for all tables.
+    static QStringList tables; //contains the table names
 public:
     /*!
      * \brief Initialise DB class and populate columns.
@@ -159,16 +160,14 @@ public:
     static
     bool verify(Entry entry)
     {
-        //retreive database layout
-        const auto dbContent = DbInfo();
-        columns = dbContent.format.value(entry.position.first);
         TableData data = entry.data();
         auto position = entry.position;
+        auto good_columns = database_layout.value(entry.position.first);
 
         //Check validity of newData
-        QVector<QString> badkeys;
+        QStringList badkeys;
         for (auto i = data.cbegin(); i != data.cend(); ++i) {
-            if (!columns.contains(i.key())) {
+            if (!good_columns.contains(i.key())) {
                 DEB(i.key() << "Not in column list for table " << position.first << ". Discarding.");
                 badkeys << i.key();
             }
@@ -178,6 +177,7 @@ public:
         }
         entry.populate_data(data);
         return update(entry);
+        ///[F] maybe this should be the other way around, i.e. update calls verify instead of verify calling update?
     }
 
 };