12#include <QCoreApplication>
20#include <qaglobalutils.h>
26 qRegisterMetaType<QSharedPointer<QH::PKG::DBObject>>();
38 if (DBparamsFile.isEmpty()) {
41 if (!_db->init(params)) {
47 if (!_db->init(DBparamsFile)) {
54 qCritical() <<
"Failed to upgrade database";
58 qDebug() <<
"Database loaded from: " <<
dbLocation();
62 Qt::DirectConnection);
66 Qt::DirectConnection);
99 auto writer = _db->writer();
136 return {
"NetworkMembers",
"MemberPermisions"};
153 "Failed to initialise a Data base patch!"
154 " Patch object is invalid");
158 "Failed to initialise a Data base patch!");
162 _targetDBVersion = std::max(_targetDBVersion, patch.
versionTo);
166 if (
db() &&
db()->writer()) {
180 if (path.isEmpty()) {
184 auto file = path +
"/DBv%0_" + QDateTime::currentDateTimeUtc().toString(
"hh:mm:ss_dd_MM_yyyy") +
".db";
185 file = file.arg(version);
186 if (
db() &&
db()->writer() &&
187 QFile::exists(
db()->writer()->databaseLocation())) {
191 if (!QFile::copy(
db()->writer()->databaseLocation(), file)) {
203bool DataBase::isForbiddenTable(
const QString &table) {
212 if (!patchesPack.size()) {
216 int currentVersion = 0;
218 bool fsupportUpgrade =
db()->
doQuery(
"SELECT COUNT(*) FROM DataBaseAttributes", {},
true);
220 if (!fsupportUpgrade) {
222 qCritical() <<
"The data base of application do not support soft upgrade. "
223 "Please remove database monyaly and restart application."
224 "You can disable upgrade functions for this override the upgradeDataBase method. ";
230 if (currentVersion < _targetDBVersion)
233 while (currentVersion < _targetDBVersion) {
236 message =
"Upgrade data base from %0 to %1 versions. %2";
237 message = message.arg(currentVersion);
239 auto patches = patchesPack.value(currentVersion, {});
241 if (!patches.size()) {
242 qCritical() <<
"Failed to " + message.arg(
"Unknown",
"Required patch not found!");
246 auto patch = patches.last();
247 message = message.arg(patch.versionTo);
249 qInfo() << message.arg(
"(Begin)");
251 if (!patch.action(
db())) {
252 qCritical() <<
"Failed to " + message.arg(
"Patch finished with error code!");
256 currentVersion = patch.versionTo;
270 return _localNodeName;
274 _localNodeName = newLocalNodeName;
292 if (
auto&& response = _db->getObject(request)) {
293 if (!response->value().isNull())
294 return response->value();
302 auto updateVersionRequest = QSharedPointer<PKG::SetSingleValue>::create(
304 "value", newValue,
"name");
306 if (!_db->replaceObject(updateVersionRequest,
true)) {
308 qCritical() <<
"Failed to update " << key <<
" attribute";
The AbstractNodeInfo class contains information about client or server connection and tcp socket of n...
void sigObjectChanged(const QSharedPointer< QH::PKG::DBObject > &obj)
sigItemChanged This signal emitted when database object is changed.
bool isSqlInited() const
isSqlInited This method return true if database initialized successful.
void stop()
stop This method stop and diskonnect the database.
virtual bool upgradeDataBase()
upgradeDataBase This method upgrade data base to actyaly database version.
void setLocalNodeName(const QString &newLocalNodeName)
setLocalNodeName This method sets new local name of database file.
DataBase(QObject *ptr=nullptr)
bool run()
run This method start and initialize the data base connection.
QVariant getDBAttribute(const QString &key, const QVariant &defaultVal)
getDBAttribute This method gets value from the default of QH DB table "DataBaseAttributes".
virtual void initDefaultDbObjects(ISqlDB *cache, SqlDBWriter *writer)
initDefaultDbObjects This method create a default cache and database writer objects if the input poin...
bool setDBAttribute(const QString &key, const QVariant &newValue)
setDBAttribute This method sets value for the default of QH DB table "DataBaseAttributes".
virtual const DBPatchMap dbPatches() const
dbPatches This method should be return map with functions that upgrade production data base....
virtual void onBeforeDBUpgrade(int currentVersion, int tergetVersion) const
onBeforeDBUpgrade This method will be invoked before upgrade database.
virtual QVariantMap defaultDbParams() const
defaultDbParams This method return default database parameters for this node. Override this method fo...
void sigObjectDeleted(const QH::DbAddress &obj)
sigItemDeleted This signal emitted when database object is deleted.
virtual QStringList SQLSources() const
SQLSources This method contains list of sqldatabase sources. This method will be invoked into initial...
virtual bool welcomeAddress(AbstractNodeInfo *node)
welcomeAddress This method send to the node information about self. Override this method if you want ...
const QString & localNodeName() const
localNodeName This method return local node name.
virtual void objectRemoved(const DbAddress &address)
objectRemoved This method invoked when object with address removed from database. Oberride this metho...
QString dbLocation() const
dbLocation This method return location of nodes or clients database.
void addDBPatch(const DBPatch &patch)
addDBPatch This method add database patch to the data base object.
virtual QSet< QString > systemTables() const
systemTables This method return the set of tables that forbidden for users. By default is NetworkMemb...
ISqlDB * db() const
db This node return pointer to database object.
virtual void objectChanged(const QSharedPointer< PKG::DBObject > &obj)
objectChanged This method invoked when object with address changed in database. Override this method ...
QString backUp(int version) const
backUp This method make a backup of database.
virtual bool initSqlDb(QString DBparamsFile="", ISqlDB *cache=nullptr, SqlDBWriter *writer=nullptr)
intSqlDb This method initialize database of this node or server.
The DbAddress class use to work with database addresses. Database Address it is structure with 2 valu...
The ISqlDB class it is db cache and bridge for DbWriters. Work Scheme of the database cache:
void setWriter(SqlDBWriter *writer)
setWriter This method set new writer for this cache.
SqlDBWriter * writer() const
writer This method return is database writer object. For more information about writer see the SqlDBW...
bool doQuery(const QString &query, const QVariantMap &bindValues, bool wait=false, QSqlQuery *result=nullptr) const override
doQuery This method execute a query in this database.
void sigItemChanged(const QSharedPointer< QH::PKG::DBObject > &obj)
sigItemChanged This signal emitted when database object is changed.
void sigItemDeleted(const QH::DbAddress &obj)
sigItemDeleted This signal emitted when database object is deleted.
The GetSingleValue class is intended for get a single value from database. The value is selected by p...
The SqlDBWriter class. This class write and read objects from database (hard level)....
QString databaseLocation() const
databaseLocation This method return location of database. If it is sqlite then return path to db file...
#define QH_DB_BACKUP_PATH
#define DEFAULT_DB_INIT_FILE_PATH
The PKG namespace - this namespace contains all default packages of the Heart library....
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...
QMap< unsigned short, QMap< unsigned short, DBPatch > > DBPatchMap
DBPatchMap This is 2 depch map of the DBPatch structure when the first key it is version (from) and s...
DBPatch This is function that should be upgrade database.
unsigned short versionFrom
bool isValid() const
isValid This method check this object to valid.