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 <QModelIndex>
#include "define.h"
#include "core_global.h"
template<class T>
@ -94,7 +95,7 @@ public:
QObject *rawEntity = (QObject*)m_list.at(0).data();
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) {
return ((QObject*)entA.data())->property(prop) < ((QObject*)entB.data())->property(prop);
} else {

@ -25,7 +25,7 @@ QList<IPlugin *> Context::plugins()
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())
{
return *it;
@ -100,36 +100,66 @@ void Context::checkDb(const QString &path)
void Context::checkSchema(const QSqlDatabase &db, const QMap<QString, int> &schemaMap)
{
foreach (IPlugin *plugin, m_plugins) {
if (!schemaMap.contains(plugin->pluginId()) || schemaMap[plugin->pluginId()] < plugin->schemaVersion())
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())
{
int ver = schemaMap[plugin->pluginId()];
solveDep(*it, db, schemaMap);
}
for (int i = 0; i < plugin->schemas().count(); i++)
{
if (!schemaMap.contains(plugin->pluginId()) || i >= ver)
{
QString sql = plugin->schemas()[i];
QSqlQuery q(db);
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())
{
int ver = schemaMap[plugin->pluginId()];
QStringList sqlList = sql.split(";");
for (int i = 0; i < plugin->schemas().count(); i++)
{
if (!schemaMap.contains(plugin->pluginId()) || i >= ver)
{
QString sql = plugin->schemas()[i];
QSqlQuery q(db);
foreach (QString s, sqlList) {
if (!q.exec(s))
{
qDebug() << q.lastError().text();
}
}
QStringList sqlList = sql.split(";");
bool error = false;
if (ver == 0)
foreach (QString s, sqlList) {
if (!q.exec(s))
{
sql = "INSERT INTO system(pluginId, schemaVersion) VALUES('" + plugin->pluginId() + "', 1)";
qDebug() << q.lastError().text();
error = true;
}
else
{
sql = "UPDATE system SET schemaVersion = " + QString::number(i + 1) + " WHERE pluginId = '" + plugin->pluginId() + "'";
}
q.exec(sql);
}
if (error)
{
continue;
}
if (ver == 0)
{
sql = "INSERT INTO system(pluginId, schemaVersion) VALUES('" + plugin->pluginId() + "', 1)";
}
else
{
sql = "UPDATE system SET schemaVersion = " + QString::number(i + 1) + " WHERE pluginId = '" + plugin->pluginId() + "'";
}
q.exec(sql);
}
}
}

@ -5,7 +5,9 @@
#include <QMap>
#include <QString>
#include <QSqlDatabase>
#include <QStringList>
#include "define.h"
#include "core_global.h"
#include "transaction.h"
@ -28,8 +30,12 @@ private:
QList<IPlugin*> m_plugins;
odb::database *m_db;
QStringList m_solved;
void checkDb(const QString &path);
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

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

@ -36,7 +36,8 @@ HEADERS += core.h\
data/role.h \
data/permission.h \
data/core-data.h \
coreplugin.h
coreplugin.h \
define.h
unix {
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;
}
QStringList IMetaDataPlugin::dependsOn()
{
return m_dependsOn;
}
void IMetaDataPlugin::init(const QJsonObject &metaData)
{
parseMetaData(metaData);
@ -60,6 +65,10 @@ void IMetaDataPlugin::parseMetaData(const QJsonObject &metaData)
foreach (QJsonValue schVal, data.toObject()["sql"].toArray()) {
m_schemas.append(schVal.toString());
}
foreach (QJsonValue depVal, data.toObject()["dependecies"].toArray()) {
m_dependsOn.append(depVal.toString());
}
}
QString IMetaDataPlugin::parseLocaleText(const QJsonObject &object)

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

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

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

Loading…
Cancel
Save