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

Added code for attaching and detaching database files.

parent 0bd33a20
......@@ -446,6 +446,47 @@ bool SQLiteDb::openDb(const QString &fileName, bool forceInMemory,
return ret;
}
bool SQLiteDb::attachDb2(QSqlQuery &query, const QString &attachFileName)
{
QString queryStr("ATTACH DATABASE :fileName AS " DB2);
if (!query.prepare(queryStr)) {
logErrorNL("Cannot prepare SQL query: %s.",
query.lastError().text().toUtf8().constData());
goto fail;
}
query.bindValue(":fileName", attachFileName);
if (!query.exec()) {
logErrorNL("Cannot execute SQL query: %s.",
query.lastError().text().toUtf8().constData());
goto fail;
}
return true;
fail:
return false;
}
bool SQLiteDb::detachDb2(QSqlQuery &query)
{
QString queryStr = "DETACH DATABASE " DB2;
if (!query.prepare(queryStr)) {
logErrorNL("Cannot prepare SQL query: %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::createEmptyMissingTables(const QList<class SQLiteTbl *> &tables)
{
foreach (const SQLiteTbl *tblPtr, tables) {
......
......@@ -27,6 +27,8 @@
#include <QSqlDatabase>
#include <QString>
#define DB2 "db2"
/*!
* @brief Database prototype.
*/
......@@ -67,7 +69,7 @@ public:
/*!
* @brief Begin named transaction.
*
* @param[in] savePointName Name of the save point.
* @param[in] savePointName Name of the save point.
* @return True on success.
*/
bool savePoint(const QString &savePointName);
......@@ -174,6 +176,26 @@ protected:
bool openDb(const QString &fileName, bool forceInMemory,
const QList<class SQLiteTbl *> &tables);
/*!
* @brief Attaches a database file to opened database.
*
* @param[in,out] query Query to work with.
* @param[in] attachFileName File containing database to be
* attached.
* @return False on error.
*/
static
bool attachDb2(class QSqlQuery &query, const QString &attachFileName);
/*!
* @brief Detaches attached database file from opened database.
*
* @param[in,out] query Query to work with.
* @return False on error.
*/
static
bool detachDb2(class QSqlQuery &query);
QSqlDatabase m_db; /*!< Database. */
private:
......
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