Implemented dependency solving for db schemas creation. Added macro for

begin/end iterator.
print
Josef Rokos 9 years ago
parent d4da3cb6cf
commit 07bb708437

@ -6,6 +6,7 @@
#include <QMetaProperty> #include <QMetaProperty>
#include <QModelIndex> #include <QModelIndex>
#include "define.h"
#include "core_global.h" #include "core_global.h"
template<class T> template<class T>
@ -94,7 +95,7 @@ public:
QObject *rawEntity = (QObject*)m_list.at(0).data(); QObject *rawEntity = (QObject*)m_list.at(0).data();
const char *prop = rawEntity->metaObject()->property(column + 1).name(); const char *prop = rawEntity->metaObject()->property(column + 1).name();
std::sort(m_list.begin(), m_list.end(), [prop, order](QSharedPointer<T> entA, QSharedPointer<T> entB) -> bool { std::sort(ALL(m_list), [prop, order](QSharedPointer<T> entA, QSharedPointer<T> entB) -> bool {
if (order == Qt::AscendingOrder) { if (order == Qt::AscendingOrder) {
return ((QObject*)entA.data())->property(prop) < ((QObject*)entB.data())->property(prop); return ((QObject*)entA.data())->property(prop) < ((QObject*)entB.data())->property(prop);
} else { } else {

@ -25,7 +25,7 @@ QList<IPlugin *> Context::plugins()
IPlugin *Context::plugin(const QString &pluginId) IPlugin *Context::plugin(const QString &pluginId)
{ {
QList<IPlugin*>::iterator it = std::find_if(m_plugins.begin(), m_plugins.end(), [&pluginId](IPlugin *p) { return p->pluginId() == pluginId; }); QList<IPlugin*>::iterator it = std::find_if(ALL(m_plugins), [&pluginId](IPlugin *p) { return p->pluginId() == pluginId; });
if (it != m_plugins.end()) if (it != m_plugins.end())
{ {
return *it; return *it;
@ -100,6 +100,30 @@ void Context::checkDb(const QString &path)
void Context::checkSchema(const QSqlDatabase &db, const QMap<QString, int> &schemaMap) void Context::checkSchema(const QSqlDatabase &db, const QMap<QString, int> &schemaMap)
{ {
foreach (IPlugin *plugin, m_plugins) { foreach (IPlugin *plugin, m_plugins) {
solveDep(plugin, db, schemaMap);
}
}
void Context::solveDep(IPlugin *plugin, const QSqlDatabase &db, const QMap<QString, int> &schemaMap)
{
foreach (QString dep, plugin->dependsOn()) {
QList<IPlugin*>::iterator it = std::find_if(ALL(m_plugins), [&dep](IPlugin *p) { return p->pluginId() == dep; });
if (it != m_plugins.end())
{
solveDep(*it, db, schemaMap);
}
if (m_solved.contains(plugin->pluginId()))
{
createSchema(plugin, db, schemaMap);
m_solved.append(plugin->pluginId());
}
}
}
void Context::createSchema(IPlugin *plugin, const QSqlDatabase &db, const QMap<QString, int> &schemaMap)
{
if (!schemaMap.contains(plugin->pluginId()) || schemaMap[plugin->pluginId()] < plugin->schemaVersion()) if (!schemaMap.contains(plugin->pluginId()) || schemaMap[plugin->pluginId()] < plugin->schemaVersion())
{ {
int ver = schemaMap[plugin->pluginId()]; int ver = schemaMap[plugin->pluginId()];
@ -112,14 +136,21 @@ void Context::checkSchema(const QSqlDatabase &db, const QMap<QString, int> &sche
QSqlQuery q(db); QSqlQuery q(db);
QStringList sqlList = sql.split(";"); QStringList sqlList = sql.split(";");
bool error = false;
foreach (QString s, sqlList) { foreach (QString s, sqlList) {
if (!q.exec(s)) if (!q.exec(s))
{ {
qDebug() << q.lastError().text(); qDebug() << q.lastError().text();
error = true;
} }
} }
if (error)
{
continue;
}
if (ver == 0) if (ver == 0)
{ {
sql = "INSERT INTO system(pluginId, schemaVersion) VALUES('" + plugin->pluginId() + "', 1)"; sql = "INSERT INTO system(pluginId, schemaVersion) VALUES('" + plugin->pluginId() + "', 1)";
@ -133,4 +164,3 @@ void Context::checkSchema(const QSqlDatabase &db, const QMap<QString, int> &sche
} }
} }
} }
}

@ -5,7 +5,9 @@
#include <QMap> #include <QMap>
#include <QString> #include <QString>
#include <QSqlDatabase> #include <QSqlDatabase>
#include <QStringList>
#include "define.h"
#include "core_global.h" #include "core_global.h"
#include "transaction.h" #include "transaction.h"
@ -28,8 +30,12 @@ private:
QList<IPlugin*> m_plugins; QList<IPlugin*> m_plugins;
odb::database *m_db; odb::database *m_db;
QStringList m_solved;
void checkDb(const QString &path); void checkDb(const QString &path);
void checkSchema(const QSqlDatabase &db, const QMap<QString, int> &schemaMap); void checkSchema(const QSqlDatabase &db, const QMap<QString, int> &schemaMap);
void solveDep(IPlugin *plugin, const QSqlDatabase &db, const QMap<QString, int> &schemaMap);
void createSchema(IPlugin *plugin, const QSqlDatabase &db, const QMap<QString, int> &schemaMap);
}; };
#endif // CONTEXT_H #endif // CONTEXT_H

@ -1,6 +1,7 @@
#ifndef CORE_H #ifndef CORE_H
#define CORE_H #define CORE_H
#include "define.h"
#include "context.h" #include "context.h"
#include "iplugin.h" #include "iplugin.h"
#include "imetadataplugin.h" #include "imetadataplugin.h"

@ -36,7 +36,8 @@ HEADERS += core.h\
data/role.h \ data/role.h \
data/permission.h \ data/permission.h \
data/core-data.h \ data/core-data.h \
coreplugin.h coreplugin.h \
define.h
unix { unix {
target.path = /usr/lib target.path = /usr/lib

@ -0,0 +1,7 @@
#ifndef DEFINE_H
#define DEFINE_H
#define ALL(arr) arr.begin(), arr.end()
#endif // DEFINE_H

@ -38,6 +38,11 @@ QStringList IMetaDataPlugin::schemas()
return m_schemas; return m_schemas;
} }
QStringList IMetaDataPlugin::dependsOn()
{
return m_dependsOn;
}
void IMetaDataPlugin::init(const QJsonObject &metaData) void IMetaDataPlugin::init(const QJsonObject &metaData)
{ {
parseMetaData(metaData); parseMetaData(metaData);
@ -60,6 +65,10 @@ void IMetaDataPlugin::parseMetaData(const QJsonObject &metaData)
foreach (QJsonValue schVal, data.toObject()["sql"].toArray()) { foreach (QJsonValue schVal, data.toObject()["sql"].toArray()) {
m_schemas.append(schVal.toString()); m_schemas.append(schVal.toString());
} }
foreach (QJsonValue depVal, data.toObject()["dependecies"].toArray()) {
m_dependsOn.append(depVal.toString());
}
} }
QString IMetaDataPlugin::parseLocaleText(const QJsonObject &object) QString IMetaDataPlugin::parseLocaleText(const QJsonObject &object)

@ -19,6 +19,8 @@ public:
virtual QString pluginDescription(); virtual QString pluginDescription();
virtual int schemaVersion(); virtual int schemaVersion();
virtual QStringList schemas(); virtual QStringList schemas();
virtual QStringList dependsOn();
virtual void init(const QJsonObject &metaData); virtual void init(const QJsonObject &metaData);
protected: protected:
@ -31,6 +33,7 @@ private:
QString m_description; QString m_description;
int m_schemaVersion; int m_schemaVersion;
QStringList m_schemas; QStringList m_schemas;
QStringList m_dependsOn;
QString parseLocaleText(const QJsonObject &object); QString parseLocaleText(const QJsonObject &object);
}; };

@ -23,10 +23,14 @@ public:
virtual QString pluginDescription() = 0; virtual QString pluginDescription() = 0;
virtual int schemaVersion() = 0; virtual int schemaVersion() = 0;
virtual QStringList schemas() = 0; virtual QStringList schemas() = 0;
virtual QStringList dependsOn() = 0;
virtual void init(const QJsonObject &metaData) = 0; virtual void init(const QJsonObject &metaData) = 0;
virtual QWidget *ui() { virtual QWidget *ui() {
return m_ui; return m_ui;
} }
template<class T> template<class T>
Service<T> *service() { Service<T> *service() {
return (Service<T>*)m_service; return (Service<T>*)m_service;

@ -15,6 +15,8 @@ public:
m_errMessage = errMessage; m_errMessage = errMessage;
} }
virtual ~IValidator() {}
virtual bool validate() = 0; virtual bool validate() = 0;
QString errMessage() QString errMessage()

Loading…
Cancel
Save