Przeglądaj źródła

Refactor database directory creation into own method

Stefan Dinkelacker 7 lat temu
rodzic
commit
93c2ce6090

+ 13 - 12
Libs/PluginFramework/ctkPluginStorageSQL.cpp

@@ -114,25 +114,26 @@ QString ctkPluginStorageSQL::getConnectionName() const
 }
 
 //----------------------------------------------------------------------------
-void ctkPluginStorageSQL::open()
+void ctkPluginStorageSQL::createDatabaseDirectory() const
 {
-  QString path;
-
-  //Create full path to database
-  if(m_databasePath.isEmpty ())
-    m_databasePath = getDatabasePath();
+  QString path = getDatabasePath();
 
-  path = m_databasePath;
-  QFileInfo dbFileInfo(path);
-  if (!dbFileInfo.dir().exists())
+  QFileInfo fileInfo(path);
+  if (!fileInfo.dir().exists())
   {
-    if(!QDir::root().mkpath(dbFileInfo.path()))
+    if (!QDir::root().mkpath(fileInfo.path()))
     {
       close();
-      QString errorText("Could not create database directory: %1");
-      throw ctkPluginDatabaseException(errorText.arg(dbFileInfo.path()), ctkPluginDatabaseException::DB_CREATE_DIR_ERROR);
+      throw ctkPluginDatabaseException(QString("Could not create database directory: %1").arg(fileInfo.path()),
+                                    ctkPluginDatabaseException::DB_CREATE_DIR_ERROR);
     }
   }
+}
+
+//----------------------------------------------------------------------------
+void ctkPluginStorageSQL::open()
+{
+  createDatabaseDirectory();
 
   QSqlDatabase database = getConnection();
 

+ 7 - 0
Libs/PluginFramework/ctkPluginStorageSQL_p.h

@@ -281,6 +281,13 @@ private:
   QString getConnectionName() const;
 
   /**
+   * Creates the directory for the database.
+   *
+   * @throws ctkPluginDatabaseException
+   */
+  void createDatabaseDirectory() const;
+
+  /**
    * Compares the persisted plugin modification time with the
    * file system modification time and updates the database
    * if the persisted data is outdated.