Quellcode durchsuchen

Working on comments

- tableNames and tableColums variables of ADatabase are back as fast access, but can be updated whenever the database is changed.
- Added update steps where required
- Adjusted debugWidget to avoid pollution of database when resetting or filling debug data.
Felix vor 4 Jahren
Ursprung
Commit
e8f19deaaa

+ 25 - 16
src/database/adatabase.cpp

@@ -33,33 +33,41 @@ QString ADatabaseError::text() const
 
 ADatabase* ADatabase::instance = nullptr;
 
-//[F]: This is what I mentioned the other day, instead of writing the table names
-// to a member variable once at connect(), we are accessing the layout at the moment we query it.
-// While being a little more expensive, this makes the app much more robust because
-// it always returns an accurate description of the database, regardless of whether
-// it is already created or not. Also it encapsulates this functionality here instead
-// of being included in connect()
 /*!
- * \brief Return the names of the tables in the database.
+ * \brief Return the names of a given table in the database.
+ */
+ColumnNames ADatabase::getTableColumns(TableName table_name) const
+{
+    return tableColumns.value(table_name);
+}
+
+/*!
+ * \brief Return the names of all tables in the database
  */
 TableNames ADatabase::getTableNames() const
 {
-    auto db = ADatabase::database();
-    return db.tables();
+    return tableNames;
 }
 
 /*!
- * \brief Return the names of the columns for a table in the database
+ * \brief Updates the member variables tableNames and tableColumns with up-to-date layout information
+ * if the database has been altered. This function is normally only required during database setup or maintenance.
  */
-TableColumns ADatabase::getTableColumns(TableName table_name) const
+void ADatabase::updateLayout()
 {
     auto db = ADatabase::database();
-    TableColumns table_columns;
-    QSqlRecord fields = db.record(table_name);
-    for (int i = 0; i < fields.count(); i++) {
-        table_columns.append(fields.field(i).name());
+    tableNames = db.tables();
+
+    tableColumns.clear();
+    for (const auto &table_name : tableNames) {
+        ColumnNames table_columns;
+        QSqlRecord fields = db.record(table_name);
+        for (int i = 0; i < fields.count(); i++) {
+            table_columns.append(fields.field(i).name());
+        }
+        tableColumns.insert(table_name, table_columns);
     }
-   return table_columns;
+    emit dataBaseUpdated();
 }
 
 ADatabase* ADatabase::getInstance()
@@ -101,6 +109,7 @@ bool ADatabase::connect()
     DEB << "Database connection established." << db.lastError().text();
     // Enable foreign key restrictions
     QSqlQuery query(QStringLiteral("PRAGMA foreign_keys = ON;"));
+    updateLayout();
     return true;
 }
 

+ 6 - 1
src/database/adatabase.h

@@ -79,13 +79,16 @@ class ADatabase : public QObject {
 private:
     static ADatabase* instance;
     ADatabase();
+    TableNames tableNames;
+    TableColumns tableColumns;
 public:
     // Ensure DB is not copiable or assignable
     ADatabase(const ADatabase&) = delete;
     void operator=(const ADatabase&) = delete;
     static ADatabase* getInstance();
     TableNames getTableNames() const;
-    TableColumns getTableColumns(TableName table_name) const;
+    ColumnNames getTableColumns(TableName table_name) const;
+    void updateLayout();
     const QString sqliteVersion();
 
     ADatabaseError lastError;
@@ -235,6 +238,8 @@ public:
 
 
 
+
+
 signals:
     /*!
      * \brief updated is emitted whenever the database contents have been updated.

+ 2 - 0
src/database/adatabasesetup.cpp

@@ -266,6 +266,8 @@ bool ADataBaseSetup::createDatabase()
         return false;
     }
 
+    aDB()->updateLayout();
+
     DEB << "Populating tables...";
     if (!importDefaultData()) {
         DEB << "Populating tables failed.";

+ 1 - 25
src/database/declarations.h

@@ -17,35 +17,11 @@ using TableName = QString;
 using RowId = int;
 
 using TableNames = QStringList;
-// [G]: May lead to some confusion. TableData suggest data for the entire table.
-// but in reallity it is data per column *of single row* (unless i misunderstand)
-// [F]: That's correct. We could maybe call it EntryData or RowData?
 using RowData = QMap<ColName, ColData>;
 using ColumnData = QPair<ColName, ColData>;
 using ColumnNames = QStringList;
-using TableColumns = QStringList;
-
-// [G]: Needs some work. Inheriting from QPair may be helpful but
-// may also be overkill. Lets determine the specific uses of DataPosition
-// and provide our own interface i would say.
-// [F]: Good idea! Implementing something similar to first and second methods
-// of QPair would be useful to carry over, or some other way of quickly and
-// unambiguously accessing the elements.
-/*struct DataPosition : QPair<TableName, RowId> {
-    TableName tableName;
-    RowId rowId;
-    DataPosition()
-        : tableName(first), rowId(second)
-    {}
-    DataPosition(TableName table_name, RowId row_id)
-        : QPair<TableName, RowId>::QPair(table_name, row_id),
-          tableName(first), rowId(second)
-    {}
-    DataPosition(const DataPosition& other) = default;
-    DataPosition& operator=(const DataPosition& other) = default;
-};*/
+using TableColumns = QMap<TableName, ColumnNames>;
 
-//[F]: How about something like this?
 struct DataPosition {
     TableName tableName;
     RowId rowId;

+ 5 - 4
src/gui/dialogues/firstrundialog.cpp

@@ -114,13 +114,13 @@ bool FirstRunDialog::finish()
     auto db_fail_msg_box = QMessageBox(QMessageBox::Critical, QStringLiteral("Database setup failed"),
                                        QStringLiteral("Errors have ocurred creating the database."
                                                       "Without a working database The application will not be usable."));
-    // [G]: Im abit confused on the logic here
-    // why do you write setup complete twice?
     if (!setupDatabase()) {
         db_fail_msg_box.exec();
         return false;
     }
 
+    aDB()->updateLayout();
+
     auto pilot = APilotEntry(1);
     pilot.setData(data);
     if(!aDB()->commit(pilot)){
@@ -147,10 +147,11 @@ bool FirstRunDialog::setupDatabase()
     aDB()->connect();
 
     // [F]: todo: handle unsuccessful steps
-    // [G]: only two / for comments
-    // three are shorthand for documentation
     if(!ADataBaseSetup::createDatabase())
         return false;
+
+    aDB()->updateLayout();
+
     if(!ADataBaseSetup::importDefaultData())
         return false;
     return true;

+ 3 - 16
src/gui/widgets/debugwidget.cpp

@@ -29,7 +29,7 @@ void DebugWidget::on_resetUserTablesPushButton_clicked()
     if (ADataBaseSetup::resetToDefault()){
         result.setText("Database successfully reset");
         result.exec();
-        touchDatabase();
+        emit aDB()->dataBaseUpdated();
     } else {
         result.setText("Errors have occurred. Check console for Debug output. ");
         result.exec();
@@ -74,7 +74,7 @@ void DebugWidget::on_resetDatabasePushButton_clicked()
     }
     if (ADataBaseSetup::importDefaultData()) {
         message_box.setText("Database has been successfully reset.");
-        touchDatabase();
+        emit aDB()->dataBaseUpdated();
         message_box.exec();
     } else {
         message_box.setText("Errors have ocurred while importing templates.<br>"
@@ -127,7 +127,7 @@ void DebugWidget::on_fillUserDataPushButton_clicked()
 
     message_box.setText("User tables successfully populated.");
     message_box.exec();
-    touchDatabase();
+    emit aDB()->dataBaseUpdated();
 }
 
 void DebugWidget::on_selectCsvPushButton_clicked()
@@ -165,20 +165,7 @@ void DebugWidget::on_importCsvPushButton_clicked()
 
 void DebugWidget::on_debugPushButton_clicked()
 {
-    touchDatabase();
-}
 
-/*!
- * \brief Acess the database to trigger aDB()::databaseUpdated
- */
-void DebugWidget::touchDatabase()
-{
-    QMap<QString, QVariant> debugData;
-    debugData.insert("lastname","debugLastName");
-    debugData.insert("firstname","debugFirstName");
-    auto pilot = APilotEntry(1);
-    pilot.setData(debugData);
-    aDB()->commit(pilot);
 }
 
 /* //Comparing two functions template

+ 0 - 2
src/gui/widgets/debugwidget.h

@@ -51,8 +51,6 @@ private:
     Ui::DebugWidget *ui;
 
     bool downloadComplete = false;
-
-    void touchDatabase();
 };
 
 #endif // DEBUGWIDGET_H