Commit 812a97a5 authored by Karel Slaný's avatar Karel Slaný

Modified SQLiteDb::moveDb() to take new file path instead of directory name.

parent f3b80b7c
......@@ -370,38 +370,16 @@ bool SQLiteDb::deleteDb(const QString &LocDir)
return QFile::remove(newFileName);
}
bool SQLiteDb::openDb(const QString &fileName, bool forceInMemory,
bool SQLiteDb::moveDb(const QString &newFileName,
const QList<class SQLiteTbl *> &tables)
{
bool ret;
if (m_db.isOpen()) {
m_db.close();
}
if (!forceInMemory) {
m_db.setDatabaseName(QDir::toNativeSeparators(fileName));
} else {
m_db.setDatabaseName(memoryLocation);
}
ret = m_db.open();
if (ret) {
/* Ensure database contains all tables. */
ret = createEmptyMissingTables(tables);
}
if (!ret) {
m_db.close();
if (fileName() == memoryLocation) {
logErrorNL("%s",
"Trying to copy database that resides i memory.");
Q_ASSERT(0);
return false;
}
return ret;
}
bool SQLiteDb::moveDb(const QString &newLocDir,
const QList<class SQLiteTbl *> &tables)
{
bool move_ret, open_ret;
/* Close database. */
......@@ -409,15 +387,16 @@ bool SQLiteDb::moveDb(const QString &newLocDir,
/* Backup old file name. */
QString oldFileName = fileName();
QFileInfo fi(oldFileName);
QString fileName = fi.fileName();
QString newFileName =
newLocDir + QDir::separator() + QDir::toNativeSeparators(fileName);
logInfoNL("Moving database file '%s' to location '%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(
"Moving of database file '%s' aborted. Target and source are equal.",
oldFileName.toUtf8().constData());
return false;
}
......@@ -428,15 +407,49 @@ bool SQLiteDb::moveDb(const QString &newLocDir,
move_ret = QFile::rename(oldFileName, newFileName);
/* Open database. */
open_ret = openDb(move_ret ? newFileName : oldFileName, true, tables);
open_ret = openDb(move_ret ? newFileName : oldFileName, false, tables);
if (!open_ret) {
Q_ASSERT(0);
logErrorNL("File '%s' could not be opened.",
move_ret ?
newFileName.toUtf8().constData() :
oldFileName.toUtf8().constData());
/* TODO -- qFatal() ? */
return false;
}
return move_ret;
}
bool SQLiteDb::openDb(const QString &fileName, bool forceInMemory,
const QList<class SQLiteTbl *> &tables)
{
bool ret;
if (m_db.isOpen()) {
m_db.close();
}
if (!forceInMemory) {
m_db.setDatabaseName(QDir::toNativeSeparators(fileName));
} else {
m_db.setDatabaseName(memoryLocation);
}
ret = m_db.open();
if (ret) {
/* Ensure database contains all tables. */
ret = createEmptyMissingTables(tables);
}
if (!ret) {
m_db.close();
}
return ret;
}
bool SQLiteDb::createEmptyMissingTables(const QList<class SQLiteTbl *> &tables)
{
foreach (const SQLiteTbl *tblPtr, tables) {
......
......@@ -170,12 +170,12 @@ protected:
/*!
* @brief Move db.
*
* @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.
*/
bool moveDb(const QString &newLocDir,
bool moveDb(const QString &newFileName,
const QList<class SQLiteTbl *> &tables);
/*!
......
......@@ -302,11 +302,6 @@ fail:
return false;
}
bool FileDb::moveDb(const QString &newLocDir)
{
return SQLiteDb::moveDb(newLocDir, listOfTables());
}
bool FileDb::openDb(const QString &fileName, bool storeToDisk)
{
if (storeToDisk && fileName.isEmpty()) {
......
......@@ -146,14 +146,6 @@ public:
*/
bool insertUpdateFilesIntoDb(const AttachmentData &fileData);
/*!
* @brief Move db.
*
* @param[in] newLocDir New location name.
* @return True on success.
*/
bool moveDb(const QString &newLocDir);
/*!
* @brief Open database file.
*
......
......@@ -719,11 +719,6 @@ bool MessageDb::markMessagesLocallyRead(enum MessageType messageType, bool read)
return true;
}
bool MessageDb::moveDb(const QString &newLocDir)
{
return SQLiteDb::moveDb(newLocDir, listOfTables());
}
bool MessageDb::openDb(const QString &fileName, bool storeToDisk)
{
if (storeToDisk && fileName.isEmpty()) {
......
......@@ -216,14 +216,6 @@ public:
*/
bool markMessagesLocallyRead(enum MessageType messageType, bool read);
/*!
* @brief Move db.
*
* @param[in] newLocDir New location name.
* @return True on success.
*/
bool moveDb(const QString &newLocDir);
/*!
* @brief Open database file.
*
......
......@@ -105,11 +105,6 @@ bool ZfoDb::deleteDb(const QString &LocDir)
return SQLiteDb::deleteDb(LocDir);
}
bool ZfoDb::moveDb(const QString &newLocDir)
{
return SQLiteDb::moveDb(newLocDir, listOfTables());
}
void ZfoDb::deleteZfo(qint64 msgId, bool isTestAccount, int zfoSize)
{
QSqlQuery query(m_db);
......
......@@ -113,14 +113,6 @@ public:
*/
bool isDbSizeExceeded(unsigned int currentLimit);
/*!
* @brief Move db.
*
* @param[in] newLocDir New location name.
* @return True on success.
*/
bool moveDb(const QString &newLocDir);
/*!
* @brief Open database file.
*
......
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