Added support for database maintenance. Added core plugin.
This commit is contained in:
@@ -1,10 +1,16 @@
|
||||
#include <QDir>
|
||||
#include <QApplication>
|
||||
#include <QPluginLoader>
|
||||
#include <QSqlDatabase>
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlResult>
|
||||
#include <QSqlError>
|
||||
#include <QDebug>
|
||||
|
||||
#include <odb/sqlite/database.hxx>
|
||||
|
||||
#include "core.h"
|
||||
#include "coreplugin.h"
|
||||
|
||||
Context &Context::instance()
|
||||
{
|
||||
@@ -17,8 +23,22 @@ QList<IPlugin *> Context::plugins()
|
||||
return m_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; });
|
||||
if (it != m_plugins.end())
|
||||
{
|
||||
return *it;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void Context::loadPlugins()
|
||||
{
|
||||
IPlugin *corePlugin = new CorePlugin();
|
||||
m_plugins.append(corePlugin);
|
||||
|
||||
QDir pluginsDir(qApp->applicationDirPath() + "/../../plugins");
|
||||
|
||||
foreach (QString fileName, pluginsDir.entryList(QStringList() << "*.so" << "*.dll")) {
|
||||
@@ -41,6 +61,7 @@ void Context::openDb(const QString &path)
|
||||
delete m_db;
|
||||
}
|
||||
|
||||
checkDb(path);
|
||||
m_db = new odb::sqlite::database(path.toStdString());
|
||||
}
|
||||
|
||||
@@ -48,3 +69,68 @@ Context::Context()
|
||||
{
|
||||
m_db = NULL;
|
||||
}
|
||||
|
||||
void Context::checkDb(const QString &path)
|
||||
{
|
||||
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
|
||||
db.setDatabaseName(path);
|
||||
db.open();
|
||||
QSqlQuery q(db);
|
||||
QString verSql = "SELECT pluginId, schemaVersion FROM system";
|
||||
QString createSysSql = "CREATE TABLE \"system\" (\"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, \"pluginId\" TEXT NULL, \"schemaVersion\" TEXT NULL)";
|
||||
|
||||
if (q.exec(verSql))
|
||||
{
|
||||
QMap<QString, int> schemas;
|
||||
while (q.next())
|
||||
{
|
||||
schemas[q.value("pluginId").toString()] = q.value("schemaVersion").toInt();
|
||||
}
|
||||
checkSchema(db, schemas);
|
||||
}
|
||||
else
|
||||
{
|
||||
q.exec(createSysSql);
|
||||
checkSchema(db, QMap<QString, int>());
|
||||
}
|
||||
|
||||
db.close();
|
||||
}
|
||||
|
||||
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())
|
||||
{
|
||||
int ver = schemaMap[plugin->pluginId()];
|
||||
|
||||
for (int i = 0; i < plugin->schemas().count(); i++)
|
||||
{
|
||||
if (!schemaMap.contains(plugin->pluginId()) || i >= ver)
|
||||
{
|
||||
QString sql = plugin->schemas()[i];
|
||||
QSqlQuery q(db);
|
||||
|
||||
QStringList sqlList = sql.split(";");
|
||||
|
||||
foreach (QString s, sqlList) {
|
||||
if (!q.exec(s))
|
||||
{
|
||||
qDebug() << q.lastError().text();
|
||||
}
|
||||
}
|
||||
|
||||
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