Implemented dependency solving for db schemas creation. Added macro for
begin/end iterator.
This commit is contained in:
+53
-23
@@ -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 (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()];
|
||||
|
||||
for (int i = 0; i < plugin->schemas().count(); i++)
|
||||
{
|
||||
if (!schemaMap.contains(plugin->pluginId()) || i >= ver)
|
||||
{
|
||||
if (!schemaMap.contains(plugin->pluginId()) || i >= ver)
|
||||
{
|
||||
QString sql = plugin->schemas()[i];
|
||||
QSqlQuery q(db);
|
||||
QString sql = plugin->schemas()[i];
|
||||
QSqlQuery q(db);
|
||||
|
||||
QStringList sqlList = sql.split(";");
|
||||
QStringList sqlList = sql.split(";");
|
||||
bool error = false;
|
||||
|
||||
foreach (QString s, sqlList) {
|
||||
if (!q.exec(s))
|
||||
{
|
||||
qDebug() << q.lastError().text();
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user