Commit ac2edd6f authored by Karel Slaný's avatar Karel Slaný

Added SQLiteDb::reopenDb().

parent e313459c
......@@ -241,9 +241,18 @@ fail:
bool SQLiteDb::copyDb(const QString &newFileName,
const QList<class SQLiteTbl *> &tables)
{
if (Q_UNLIKELY(newFileName.isEmpty())) {
Q_ASSERT(0);
return false;
}
if (fileName() == memoryLocation) {
logErrorNL("%s",
"Trying to copy database that resides i memory.");
"Trying to copy database that resides in memory.");
Q_ASSERT(0);
return false;
}
if (newFileName == memoryLocation) {
/* TODO -- handle this situation. */
Q_ASSERT(0);
return false;
}
......@@ -289,30 +298,63 @@ bool SQLiteDb::copyDb(const QString &newFileName,
return copy_ret;
}
bool SQLiteDb::createDb(const QString &newLocDir,
bool SQLiteDb::reopenDb(const QString &newFileName,
const QList<class SQLiteTbl *> &tables)
{
if (Q_UNLIKELY(newFileName.isEmpty())) {
Q_ASSERT(0);
return false;
}
if (fileName() == memoryLocation) {
logErrorNL("%s",
"Trying to reopen database that resides in memory.");
Q_ASSERT(0);
return false;
}
if (newFileName == memoryLocation) {
/* TODO -- handle this situation. */
Q_ASSERT(0);
return false;
}
bool reopen_ret, open_ret;
/* Close database. */
m_db.close();
/* Backup old file name. */
QString oldFileName = fileName();
QFileInfo fi(oldFileName);
QString fileName = fi.fileName();
QString newFileName =
newLocDir + QDir::separator() + QDir::toNativeSeparators(fileName);
logInfoNL("Closing database file '%s' re-opening file '%s'.",
oldFileName.toUtf8().constData(),
newFileName.toUtf8().constData());
/* Fail if target equals the source. */
/* TODO -- Perform a more reliable check than string comparison. */
if (oldFileName == newFileName) {
logWarningNL(
"Re-opening of database file '%s' aborted. Target and source are equal.",
oldFileName.toUtf8().constData());
return false;
}
/* Erase target if exists. */
QFile::remove(newFileName);
/* Open new database in the new location.*/
return openDb(newFileName, true, tables);
/* Open new database file. */
reopen_ret = openDb(newFileName, false, tables);
/* Open database. */
if (!reopen_ret) {
open_ret = openDb(oldFileName, false, tables);
if (!open_ret) {
logErrorNL("File '%s' could not be opened.",
oldFileName.toUtf8().constData());
/* TODO -- qFatal() ? */
return false;
}
}
return reopen_ret;
}
bool SQLiteDb::deleteDb(const QString &LocDir)
......@@ -335,9 +377,18 @@ bool SQLiteDb::deleteDb(const QString &LocDir)
bool SQLiteDb::moveDb(const QString &newFileName,
const QList<class SQLiteTbl *> &tables)
{
if (Q_UNLIKELY(newFileName.isEmpty())) {
Q_ASSERT(0);
return false;
}
if (fileName() == memoryLocation) {
logErrorNL("%s",
"Trying to copy database that resides i memory.");
"Trying to move database that resides in memory.");
Q_ASSERT(0);
return false;
}
if (newFileName == memoryLocation) {
/* TODO -- handle this situation. */
Q_ASSERT(0);
return false;
}
......
......@@ -138,14 +138,16 @@ protected:
const QList<class SQLiteTbl *> &tables);
/*!
* @brief Create new database in the new location.
* @brief Open a new empty database file.
*
* @param[in] newLocDir New location name.
* @param[in] newFileName New file path.
* @param[in] tables List of table prototypes that should be created
* if missing.
* @return True on success.
*
* @note The old database file is left untouched.
*/
bool createDb(const QString &newLocDir,
bool reopenDb(const QString &newFileName,
const QList<class SQLiteTbl *> &tables);
/*!
......
......@@ -501,7 +501,8 @@ bool Messages::relocateDatabases(const QString &newLocationDir,
newLocationDir + QDir::separator() + oldFileName);
break;
case RA_CREATE_NEW:
relocationSucceeded = mDb->createDb(newLocationDir);
relocationSucceeded = mDb->reopenDb(
newLocationDir + QDir::separator() + oldFileName);
break;
default:
Q_ASSERT(0);
......@@ -528,7 +529,8 @@ bool Messages::relocateDatabases(const QString &newLocationDir,
newLocationDir + QDir::separator() + oldFileName);
break;
case RA_CREATE_NEW:
relocationSucceeded = fDb->createDb(newLocationDir);
relocationSucceeded = fDb->reopenDb(
newLocationDir + QDir::separator() + oldFileName);
break;
default:
Q_ASSERT(0);
......
......@@ -108,9 +108,9 @@ int FileDb::countFilesInDb(void)
return -1;
}
bool FileDb::createDb(const QString &newLocDir)
bool FileDb::reopenDb(const QString &newFileName)
{
return SQLiteDb::createDb(newLocDir, listOfTables());
return SQLiteDb::reopenDb(newFileName, listOfTables());
}
bool FileDb::deleteDb(const QString &LocDir)
......
......@@ -76,12 +76,12 @@ public:
int countFilesInDb(void);
/*!
* @brief Create new database in the new location.
* @brief Open a new empty database file.
*
* @param[in] newLocDir New location name.
* @param[in] newFileName New file path.
* @return True on success.
*/
bool createDb(const QString &newLocDir);
bool reopenDb(const QString &newFileName);
/*!
* @brief Delete database from location.
......
......@@ -61,9 +61,9 @@ bool MessageDb::copyDb(const QString &newFileName)
return SQLiteDb::copyDb(newFileName, listOfTables());
}
bool MessageDb::createDb(const QString &newLocDir)
bool MessageDb::reopenDb(const QString &newFileName)
{
return SQLiteDb::createDb(newLocDir, listOfTables());
return SQLiteDb::reopenDb(newFileName, listOfTables());
}
bool MessageDb::deleteDb(const QString &LocDir)
......
......@@ -64,12 +64,12 @@ public:
bool copyDb(const QString &newFileName);
/*!
* @brief Create new database in the new location.
* @brief Open a new empty database file.
*
* @param[in] newLocDir New location name.
* @param[in] newFileName New file path.
* @return True on success.
*/
bool createDb(const QString &newLocDir);
bool reopenDb(const QString &newFileName);
/*!
* @brief Delete database from location.
......
......@@ -90,11 +90,6 @@ fail:
return false;
}
bool ZfoDb::createDb(const QString &newLocDir)
{
return SQLiteDb::createDb(newLocDir, listOfTables());
}
bool ZfoDb::deleteDb(const QString &LocDir)
{
return SQLiteDb::deleteDb(LocDir);
......
......@@ -41,14 +41,6 @@ public:
explicit ZfoDb(const QString &connectionName);
/*!
* @brief Create new database in the new location.
*
* @param[in] newLocDir New location name.
* @return True on success.
*/
bool createDb(const QString &newLocDir);
/*!
* @brief Delete database from location.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment