Commit 5bb5b23c authored by Martin Straka's avatar Martin Straka

Added zfo database

parent 0955098e
......@@ -121,6 +121,7 @@ SOURCES += \
src/sqlite/file_db.cpp \
src/sqlite/message_db_container.cpp \
src/sqlite/message_db.cpp \
src/sqlite/zfo_db.cpp \
src/worker/emitter.cpp \
src/worker/pool.cpp \
src/worker/task.cpp \
......@@ -174,6 +175,7 @@ HEADERS += \
src/sqlite/file_db.h \
src/sqlite/message_db_container.h \
src/sqlite/message_db.h \
src/sqlite/zfo_db.h \
src/worker/emitter.h \
src/worker/pool.h \
src/worker/task.h \
......
......@@ -50,11 +50,12 @@
#include "src/qml_interaction/interaction_filesystem.h"
#include "src/qml_interaction/interaction_zfo_file.h"
#include "src/settings.h"
#include "src/setwrapper.h"
#include "src/sqlite/db_tables.h"
#include "src/sqlite/account_db.h"
#include "src/sqlite/message_db_container.h"
#include "src/sqlite/file_db_container.h"
#include "src/setwrapper.h"
#include "src/sqlite/message_db_container.h"
#include "src/sqlite/zfo_db.h"
/* iOS app_delegate - for interaction with iOS action Open in... */
#if defined Q_OS_IOS
......@@ -325,6 +326,7 @@ int main(int argc, char *argv[])
msgsTbl.reloadLocalisedDescription();
flsTbl.reloadLocalisedDescription();
evntsTbl.reloadLocalisedDescription();
zfosTbl.reloadLocalisedDescription();
/* init and open account database */
AccountDb globAccountDb("ACCOUNTS");
......@@ -344,6 +346,10 @@ int main(int argc, char *argv[])
FileDbContainer globFileDb("FILES");
globFileDbsPtr = &globFileDb;
/* init zfo db database */
ZfoDb globZfoDb("ZFOS");
globZfoDbPtr = &globZfoDb;
/* Load accounts from settings to account model. */
{
QSettings settings(Settings::settingsPath(),
......@@ -355,6 +361,12 @@ int main(int argc, char *argv[])
*/
}
/* true = zfo db store in disk, false = to memory */
if (!globZfoDbPtr->openDb(ZFO_DB_NAME, true)) {
qDebug() << "ERROR: zfo db not found!";
return EXIT_FAILURE;
}
/* load UI (QML) */
engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
......
/*
* Copyright (C) 2014-2016 CZ.NIC
* Copyright (C) 2014-2017 CZ.NIC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -104,7 +104,6 @@ SQLiteTbl accntinfTbl(AccntinfTbl::tabName, AccntinfTbl::knownAttrs,
AccntinfTbl::attrProps, AccntinfTbl::colConstraints,
AccntinfTbl::tblConstraint);
namespace UserinfTbl {
const QString tabName("user_info");
......@@ -169,7 +168,6 @@ SQLiteTbl userinfTbl(UserinfTbl::tabName, UserinfTbl::knownAttrs,
UserinfTbl::attrProps, UserinfTbl::colConstraints,
UserinfTbl::tblConstraint);
namespace MsgsTbl {
const QString tabName("messages");
......@@ -272,7 +270,6 @@ namespace MsgsTbl {
SQLiteTbl msgsTbl(MsgsTbl::tabName, MsgsTbl::knownAttrs, MsgsTbl::attrProps,
MsgsTbl::colConstraints, MsgsTbl::tblConstraint);
namespace EvntsTbl {
const QString tabName("events");
......@@ -308,7 +305,6 @@ namespace EvntsTbl {
SQLiteTbl evntsTbl(EvntsTbl::tabName, EvntsTbl::knownAttrs,
EvntsTbl::attrProps, EvntsTbl::colConstraints, EvntsTbl::tblConstraint);
namespace FlsTbl {
const QString tabName("files");
......@@ -359,3 +355,43 @@ namespace FlsTbl {
} /* namespace FlsTbl */
SQLiteTbl flsTbl(FlsTbl::tabName, FlsTbl::knownAttrs, FlsTbl::attrProps,
FlsTbl::colConstraints, FlsTbl::tblConstraint);
namespace ZfosTbl {
const QString tabName("zfos");
const QVector< QPair<QString, enum EntryType> > knownAttrs = {
{"id", DB_INTEGER}, /* NOT NULL*/
{"dmID", DB_INTEGER}, /* NOT NULL*/
{"testEnv", DB_BOOLEAN},
{"lastAccessTime", DB_INTEGER},
{"size", DB_INTEGER},
{"data", DB_TEXT}
/*
* PRIMARY KEY (id),
* FOREIGN KEY(dmID) REFERENCES messages ("dmID")
*/
};
const QMap<QString, QString> colConstraints = {
{"id", "NOT NULL"},
{"dmID", "NOT NULL"}
};
const QString &tblConstraint(
",\n"
" PRIMARY KEY (id),\n"
" CHECK (testEnv IN (0, 1)),\n"
" FOREIGN KEY(dmID) REFERENCES messages (dmID)"
);
const QMap<QString, SQLiteTbl::AttrProp> attrProps = {
{"id", {DB_INTEGER, ""}},
{"dmID", {DB_INTEGER, ""}},
{"testEnv", {DB_BOOLEAN, ""}},
{"lastAccessTime", {DB_INTEGER, ""}},
{"size", {DB_INTEGER, ""}},
{"data", {DB_TEXT, ""}}
};
} /* namespace ZfosTbl */
SQLiteTbl zfosTbl(ZfosTbl::tabName, ZfosTbl::knownAttrs, ZfosTbl::attrProps,
ZfosTbl::colConstraints, ZfosTbl::tblConstraint);
/*
* Copyright (C) 2014-2016 CZ.NIC
* Copyright (C) 2014-2017 CZ.NIC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -43,4 +43,9 @@ extern SQLiteTbl evntsTbl; /*!< Table 'events'. */
*/
extern SQLiteTbl flsTbl; /*!< Table 'files'. */
/*
* Zfo database.
*/
extern SQLiteTbl zfosTbl; /*!< Table 'zfos'. */
#endif /* _DB_TABLES_H_ */
/*
* Copyright (C) 2014-2017 CZ.NIC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*/
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QObject>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QStandardPaths>
#include "src/io/filesystem.h"
#include "src/sqlite/db_tables.h"
#include "src/sqlite/dbs.h"
#include "src/sqlite/zfo_db.h"
ZfoDb::ZfoDb(const QString &connectionName)
: SQLiteDb(connectionName)
{
}
bool ZfoDb::openDb(const QString &fileName, bool storeToDisk)
{
QString dirName(existingWritableLocation(
QStandardPaths::AppDataLocation));
if (storeToDisk && dirName.isEmpty() & fileName.isEmpty()) {
Q_ASSERT(0);
return false;
}
bool ret = SQLiteDb::openDb(
dirName + QDir::separator() + QDir::toNativeSeparators(fileName),
!storeToDisk, listOfTables());
qDebug() << m_db.databaseName();
return ret;
}
bool ZfoDb::deleteCurrentDbCreateNew(const QString &newLocDir)
{
bool 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);
/* Fail if target equals the source. */
/* TODO -- Perform a more reliable check than string comparison. */
if (oldFileName == newFileName) {
return false;
}
/* Erase target if exists. */
QFile::remove(newFileName);
/*
* Open database in the new location.
* If error durring opening of new dabatase, open back old database.
*/
open_ret = openDb(newFileName, true);
if (!open_ret) {
open_ret = openDb(oldFileName, true);
return false;
}
/* Delete file. */
return QFile::remove(oldFileName);
}
bool ZfoDb::createDb(const QString &newLocDir)
{
/* 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);
/* Fail if target equals the source. */
/* TODO -- Perform a more reliable check than string comparison. */
if (oldFileName == newFileName) {
return false;
}
/* Erase target if exists. */
QFile::remove(newFileName);
/* Open new database in the new location.*/
return openDb(newFileName, true);
}
bool ZfoDb::deleteDb(const QString &LocDir)
{
/* Close database. */
m_db.close();
/* Backup old file name. */
QString oldFileName = fileName();
QFileInfo fi(oldFileName);
QString fileName = fi.fileName();
QString newFileName =
LocDir + QDir::separator() + QDir::toNativeSeparators(fileName);
/* Erase target if exists. */
return QFile::remove(newFileName);
}
bool ZfoDb::copyDb(const QString &newLocDir)
{
bool copy_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);
/* Fail if target equals the source. */
/* TODO -- Perform a more reliable check than string comparison. */
if (oldFileName == newFileName) {
return false;
}
/* Erase target if exists. */
QFile::remove(newFileName);
/* Copy database file. */
copy_ret = QFile::copy(oldFileName, newFileName);
/* Open database. */
open_ret = openDb(newFileName, true);
if (!open_ret) {
Q_ASSERT(0);
return false;
}
return copy_ret;
}
bool ZfoDb::moveDb(const QString &newLocDir)
{
bool move_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);
/* Fail if target equals the source. */
/* TODO -- Perform a more reliable check than string comparison. */
if (oldFileName == newFileName) {
return false;
}
/* Erase target if exists. */
QFile::remove(newFileName);
/* Move database file. */
move_ret = QFile::rename(oldFileName, newFileName);
/* Open database. */
open_ret = openDb(move_ret ? newFileName : oldFileName, true);
if (!open_ret) {
Q_ASSERT(0);
return false;
}
return move_ret;
}
QList<class SQLiteTbl *> ZfoDb::listOfTables(void)
{
static QList<class SQLiteTbl *> tables;
if (tables.isEmpty()) {
tables.append(&zfosTbl);
}
return tables;
}
ZfoDb *globZfoDbPtr = 0;
/*
* Copyright (C) 2014-2017 CZ.NIC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*/
#ifndef _ZFO_DB_H_
#define _ZFO_DB_H_
#include <QList>
#include <QString>
#include "src/common.h"
#include "src/io/sqlite/db.h"
/* Zfo db filename */
#define ZFO_DB_NAME "zfo.db"
/*!
* @brief Encapsulates zfo database.
*/
class ZfoDb : public SQLiteDb {
public:
explicit ZfoDb(const QString &connectionName);
/*!
* @brief Open database file.
*
* @param[in] fileName File name.
* @param[in] storeToDisk Whether to create db in memory.
* @return True on success.
*/
bool openDb(const QString &fileName, bool storeToDisk);
/*!
* @brief Delete current zfo db and create new in new location.
*
* @param[in] newLocDir New location name.
* @return True on success.
*/
bool deleteCurrentDbCreateNew(const QString &newLocDir);
/*!
* @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.
*
* @param[in] LocDir New location name.
* @return True on success.
*/
bool deleteDb(const QString &LocDir);
/*!
* @brief Copy db.
*
* @param[in] newLocDir New location name.
* @return True on success.
*
* @note The copy is continued to be used. Original is closed.
*/
bool copyDb(const QString &newLocDir);
/*!
* @brief Move db.
*
* @param[in] newLocDir New location name.
* @return True on success.
*/
bool moveDb(const QString &newLocDir);
private:
/*!
* @brief Returns list of tables.
*
* @return List of pointers to tables.
*/
static
QList<class SQLiteTbl *> listOfTables(void);
};
/*!
* @brief Global account database.
*/
extern ZfoDb *globZfoDbPtr;
#endif /* _ZFO_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