Browse Source

changed control flow of init

changed control flow of init and renamed DB member variables for clarity.

Also added an overview of planned DB functionality for discussion. Thank you for your comments!
Felix Turo 4 years ago
parent
commit
8c96689c08
2 changed files with 49 additions and 33 deletions
  1. 20 28
      src/experimental/Db.cpp
  2. 29 5
      src/experimental/Db.h

+ 20 - 28
src/experimental/Db.cpp

@@ -6,37 +6,29 @@ bool DB::init()
 {
     const QString driver("QSQLITE");
 
-    if (QSqlDatabase::isDriverAvailable(driver)) {
+    if (!QSqlDatabase::isDriverAvailable(driver)) return false;
 
-        QDir directory("data");
-        QString databaseLocation = directory.filePath("logbook.db");
-        QSqlDatabase db = QSqlDatabase::addDatabase(driver);
-        db.setDatabaseName(databaseLocation);
+    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();
+    if (!db.open()) return false;
 
-            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;
+    DEB("Database connection established.");
+    // Enable foreign key restrictions
+    QSqlQuery query("PRAGMA foreign_keys = ON;");
+    // Retreive database layout and store in member variables
+    tableNames = db.tables();
+
+    QStringList columnNames;
+    for (const auto &table : tableNames) {
+        columnNames.clear();
+        QSqlRecord fields = db.record(table);
+        for (int i = 0; i < fields.count(); i++) {
+            columnNames.append(fields.field(i).name());
+            tableColumns.insert(table, columnNames);
         }
-    } else {
-        DEB("DatabaseConnect - ERROR: no driver " << driver << " available");
-        return false;
     }
+    return true;
 }

+ 29 - 5
src/experimental/Db.h

@@ -13,6 +13,27 @@
 
 namespace experimental {
 
+/// [F] Just to put it somewhere planned DB member functions:
+/// static bool init()                  -   maybe call it connect()?
+/// static bool disconnect()            -   close the database connection
+/// static bool commit(Entry entry)     -   upload to database, calls either INSERT or UPDATE
+/// bool insert(Entry entry)            -   (INSERT INTO tablename VALUES...)
+/// bool update(Entry entry)            -   (UPDATE tablename SET...)
+/// static bool delete(Entry entry)     -   (DELETE FROM tableName WHERE ...)
+/// static bool exists(Entry entry)     -   Check if entry exists in DB
+///
+/// So a user input would pass the following stages:
+/// 1) Create UserInput object from Line Edits
+/// 2) process UserInput to obtain Entry object
+/// 3) Db operates on entry object (commit, delete, whatever)
+///
+/// Idea for processing:
+/// static Entry processUserInput(UserInput userInput) - check the user input, perform matching of foreign keys and return an entry object ready for submission
+/// ColumnData matchForeignKey (ColName, ColData) - matches a user input to a foreign key, i.e. a registration to a tail_id or a Pilot Name to a pilot_id
+/// Entry prepareDataForCommit(Entry entry) - checks data is ready for submission
+///
+///
+
 /// SELF DOCUMENTING CODE
 using ColName = QString;
 using ColData = QString;
@@ -20,6 +41,7 @@ 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);
@@ -91,8 +113,8 @@ bool insertPilot(UserInput& uin)
  */
 class DB {
 private:
-    static QMap<QString, QStringList> database_layout; // contains the column names for all tables.
-    static QStringList tables; //contains the table names
+    static QStringList tableNames;
+    static QMap<TableName, QStringList> tableColumns;
 public:
     /*!
      * \brief Initialise DB class and populate columns.
@@ -158,11 +180,13 @@ public:
      * \brief Verify entry data is sane for the database.
      */
     static
-    bool verify(Entry entry)
+    Entry prepareDataForCommit(Entry entry)
+    /// [F] this function does not really verify data, it just removes bad entries,
+    /// maybe returning the 'cleaned up' entry would be better?
     {
         TableData data = entry.data();
         auto position = entry.position;
-        auto good_columns = database_layout.value(entry.position.first);
+        auto good_columns = tableColumns.value(entry.position.first);
 
         //Check validity of newData
         QStringList badkeys;
@@ -176,7 +200,7 @@ public:
             data.remove(var);
         }
         entry.populate_data(data);
-        return update(entry);
+        return entry;
         ///[F] maybe this should be the other way around, i.e. update calls verify instead of verify calling update?
     }