Commit 67e1c59b authored by Karel Slaný's avatar Karel Slaný

Added SQLiteDb::checkDb() and SQLiteDb::vacuum().

parent f122b56f
......@@ -21,7 +21,6 @@
* the two.
*/
#include <QDebug>
#include <QDir>
#include <QSqlError>
#include <QSqlQuery>
......@@ -182,6 +181,63 @@ void SQLiteDb::closeDb(void)
m_db.close();
}
bool SQLiteDb::checkDb(bool quick)
{
QSqlQuery query(m_db);
bool ret = false;
QString queryStr;
if (quick) {
queryStr = "PRAGMA quick_check";
} else {
queryStr = "PRAGMA integrity_check";
}
if (!query.prepare(queryStr)) {
logErrorNL("Cannot prepare SQL query: %s.",
query.lastError().text().toUtf8().constData());
goto fail;
}
if (query.exec() && query.isActive()) {
query.first();
if (query.isValid()) {
ret = query.value(0).toBool();
} else {
ret = false;
}
} else {
logErrorNL("Cannot execute SQL query: %s.",
query.lastError().text().toUtf8().constData());
goto fail;
}
return ret;
fail:
return false;
}
bool SQLiteDb::vacuum(void)
{
QSqlQuery query(m_db);
QString queryStr("VACUUM");
if (!query.prepare(queryStr)) {
logErrorNL("Cannot prepare SQL command: %s.",
query.lastError().text().toUtf8().constData());
goto fail;
}
if (!query.exec()) {
logErrorNL("Cannot execute SQL query: %s.",
query.lastError().text().toUtf8().constData());
goto fail;
}
return true;
fail:
return false;
}
bool SQLiteDb::copyDb(const QString &newLocDir,
const QList<class SQLiteTbl *> &tables)
{
......@@ -245,20 +301,6 @@ bool SQLiteDb::createDb(const QString &newLocDir,
return openDb(newFileName, true, tables);
}
bool SQLiteDb::createEmptyMissingTables(const QList<class SQLiteTbl *> &tables)
{
foreach (const SQLiteTbl *tblPtr, tables) {
Q_ASSERT(0 != tblPtr);
if (!tblPtr->createEmpty(m_db)) {
goto fail; /* TODO -- Proper recovery? */
}
}
return true;
fail:
return false;
}
bool SQLiteDb::deleteCurrentDbCreateNew(const QString &newLocDir,
const QList<class SQLiteTbl *> &tables)
{
......@@ -340,8 +382,6 @@ bool SQLiteDb::openDb(const QString &fileName, bool forceInMemory,
m_db.close();
}
qDebug() << m_db.databaseName();
return ret;
}
......@@ -382,3 +422,17 @@ bool SQLiteDb::moveDb(const QString &newLocDir,
return move_ret;
}
bool SQLiteDb::createEmptyMissingTables(const QList<class SQLiteTbl *> &tables)
{
foreach (const SQLiteTbl *tblPtr, tables) {
Q_ASSERT(0 != tblPtr);
if (!tblPtr->createEmpty(m_db)) {
goto fail; /* TODO -- Proper recovery? */
}
}
return true;
fail:
return false;
}
......@@ -21,8 +21,7 @@
* the two.
*/
#ifndef _SQLITE_DB_H_
#define _SQLITE_DB_H_
#pragma once
#include <QList>
#include <QSqlDatabase>
......@@ -106,12 +105,25 @@ public:
bool dbDriverSupport(void);
protected:
/*!
* @brief Close database file.
*/
void closeDb(void);
/*!
* @brief Perform a database integrity check.
*
* @return False if check fails.
*/
bool checkDb(bool quick);
/*!
* @brief Performs database clean-up (VACUUM).
*
* @return False on error.
*/
bool vacuum(void);
/*!
* @brief Copy db.
*
......@@ -191,5 +203,3 @@ private:
*/
bool createEmptyMissingTables(const QList<class SQLiteTbl *> &tables);
};
#endif /* _SQLITE_DB_H_ */
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