13#include <QCoreApplication>
21#include <qaglobalutils.h>
27 qRegisterMetaType<QSharedPointer<QH::PKG::DBObject>>();
39 if (DBparamsFile.isEmpty()) {
42 if (!_db->
init(params)) {
48 if (!_db->
init(DBparamsFile)) {
55 qCritical() <<
"Failed to upgrade database";
59 qDebug() <<
"Database loaded from: " <<
dbLocation();
63 Qt::DirectConnection);
67 Qt::DirectConnection);
100 auto writer = _db->
writer();
137 return {
"NetworkMembers",
"MemberPermisions"};
154 "Failed to initialise a Data base patch!"
155 " Patch object is invalid");
159 "Failed to initialise a Data base patch!");
163 _targetDBVersion = std::max(_targetDBVersion, patch.
versionTo);
167 if (
db() &&
db()->writer()) {
181 if (path.isEmpty()) {
185 auto file = path +
"/DBv%0_" + QDateTime::currentDateTimeUtc().toString(
"hh:mm:ss_dd_MM_yyyy") +
".db";
186 file = file.arg(version);
187 if (
db() &&
db()->writer() &&
188 QFile::exists(
db()->writer()->databaseLocation())) {
192 if (!QFile::copy(
db()->writer()->databaseLocation(), file)) {
204bool DataBase::isForbidenTable(
const QString &table) {
213 if (!patchesPack.size()) {
217 int currentVersion = 0;
219 bool fsupportUpgrade =
db()->
doQuery(
"SELECT COUNT(*) FROM DataBaseAttributes", {},
true);
221 if (!fsupportUpgrade) {
223 qCritical() <<
"The data base of application do not support soft upgrade. "
224 "Please remove database monyaly and restart application."
225 "You can disable upgrade functions for this override the upgradeDataBase method. ";
231 if (currentVersion < _targetDBVersion)
234 while (currentVersion < _targetDBVersion) {
237 message =
"Upgrade data base from %0 to %1 versions. %2";
238 message = message.arg(currentVersion);
240 auto patches = patchesPack.value(currentVersion, {});
242 if (!patches.size()) {
243 qCritical() <<
"Failed to " + message.arg(
"Unknown",
"Required patch not found!");
247 auto patch = patches.last();
248 message = message.arg(patch.versionTo);
250 qInfo() << message.arg(
"(Begin)");
252 if (!patch.action(
db())) {
253 qCritical() <<
"Failed to " + message.arg(
"Patch finished with error code!");
257 currentVersion = patch.versionTo;
271 return _localNodeName;
275 _localNodeName = newLocalNodeName;
293 if (
auto&& responce = _db->
getObject(request)) {
294 if (!responce->value().isNull())
295 return responce->
value();
303 auto updateVersionRequest = QSharedPointer<PKG::SetSingleValue>::create(
305 "value", newValue,
"name");
309 qCritical() <<
"Failed to update " << key <<
" attribute";
The AbstractNodeInfo class contains information about client or server connection and tcp socket of n...
The AsyncSqlDbWriter class is some as SqlDBWriter but run all command in own thread....
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 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 ...
virtual void onBeforeDBUpgrade(int currentVerion, int tergetVersion) const
onBeforeDBUpgrade This method will be invoked before upgrade database.
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 initalize 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 inforamation about writer see the SqlDB...
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.
virtual bool init(const QString &initDbParams="")
init This method init the cache object and invoke the SqlDBWriter::initDb method.
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.
void setSQLSources(const QStringList &list) override
setSQLSources This method set sql sources for deployed database.
bool replaceObject(const QSharedPointer< QH::PKG::DBObject > &saveObject, bool wait=false) override
The GetSingleValue class is intended for get a single value from database. The value is selected by p...
QVariant value() const
value This method return Maximum value of a sql tables field.
void softDelete()
softDelete This method remove this object and save all changes into database.
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...
The SqlDB class This is base implementation fo datatbase. The SqlDB do not use caches,...
QSharedPointer< TYPE > getObject(const TYPE &templateVal)
getObject this method return a strong pointer to DBObject created by select method of the template ob...
#define QH_DB_BACKUP_PATH
#define DEFAULT_DB_INIT_FILE_PATH
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 oject to valid.