浏览代码

added Db::disconnect()

fiffty-50 4 年之前
父节点
当前提交
8a24e6b01f
共有 3 个文件被更改,包括 15 次插入4 次删除
  1. 8 0
      src/database/db.cpp
  2. 6 0
      src/database/db.h
  3. 1 4
      src/gui/widgets/debugwidget.cpp

+ 8 - 0
src/database/db.cpp

@@ -45,6 +45,14 @@ void Db::iconnect()
     }
     }
 }
 }
 
 
+void Db::idisconnect()
+{
+    auto db = Db::Database();
+    db.close();
+    db.removeDatabase(db.connectionName());
+    DEB("Database connection closed.");
+}
+
 QSqlDatabase Db::iDatabase()
 QSqlDatabase Db::iDatabase()
 {
 {
     auto db = QSqlDatabase::database("qt_sql_default_connection");
     auto db = QSqlDatabase::database("qt_sql_default_connection");

+ 6 - 0
src/database/db.h

@@ -62,6 +62,11 @@ class Db
          * instantiated with QSqlDatabase::database("qt_sql_default_connection") as required.
          * instantiated with QSqlDatabase::database("qt_sql_default_connection") as required.
          */
          */
         static void             connect(){get().iconnect();}
         static void             connect(){get().iconnect();}
+        /*!
+         * \brief disconnect Closes and removes the default database connection.
+         */
+        static void             disconnect(){get().idisconnect();}
+
         /*!
         /*!
          * \brief Can be used to access the database connection.
          * \brief Can be used to access the database connection.
          * \return a pointer to the default database connection
          * \return a pointer to the default database connection
@@ -140,6 +145,7 @@ class Db
     private:
     private:
         Db() {}
         Db() {}
         void             iconnect();
         void             iconnect();
+        void             idisconnect();
         QSqlDatabase     iDatabase();
         QSqlDatabase     iDatabase();
         bool             iexists(QString column, QString table, QString checkColumn,
         bool             iexists(QString column, QString table, QString checkColumn,
                                        QString value, Db::matchType match);
                                        QString value, Db::matchType match);

+ 1 - 4
src/gui/widgets/debugwidget.cpp

@@ -57,10 +57,7 @@ void DebugWidget::on_resetDatabasePushButton_clicked()
         dl->deleteLater();
         dl->deleteLater();
     }
     }
     //close database connection
     //close database connection
-    auto db = Db::Database();
-    db.close();
-    db.removeDatabase(db.connectionName());
-    DEB("Closing database connection.");
+    Db::disconnect();
 
 
     // back up old database
     // back up old database
     auto oldDatabase = QFile("data/logbook.db");
     auto oldDatabase = QFile("data/logbook.db");