+Storage::State AbstractSqlStorage::init(const QVariantMap &settings)
+{
+ setConnectionProperties(settings);
+
+ _debug = Quassel::isOptionSet("debug");
+
+ QSqlDatabase db = logDb();
+ if (!db.isValid() || !db.isOpen())
+ return NotAvailable;
+
+ if (installedSchemaVersion() == -1) {
+ qCritical() << "Storage Schema is missing!";
+ return NeedsSetup;
+ }
+
+ if (installedSchemaVersion() > schemaVersion()) {
+ qCritical() << "Installed Schema is newer then any known Version.";
+ return NotAvailable;
+ }
+
+ if (installedSchemaVersion() < schemaVersion()) {
+ qWarning() << qPrintable(tr("Installed Schema (version %1) is not up to date. Upgrading to version %2...").arg(installedSchemaVersion()).arg(schemaVersion()));
+ if (!upgradeDb()) {
+ qWarning() << qPrintable(tr("Upgrade failed..."));
+ return NotAvailable;
+ }
+ }
+
+ quInfo() << qPrintable(displayName()) << "Storage Backend is ready. Quassel Schema Version:" << installedSchemaVersion();
+ return IsReady;
+}
+
+
+QString AbstractSqlStorage::queryString(const QString &queryName, int version)
+{
+ if (version == 0)
+ version = schemaVersion();
+
+ QFileInfo queryInfo(QString(":/SQL/%1/%2/%3.sql").arg(displayName()).arg(version).arg(queryName));
+ if (!queryInfo.exists() || !queryInfo.isFile() || !queryInfo.isReadable()) {
+ qCritical() << "Unable to read SQL-Query" << queryName << "for engine" << displayName();
+ return QString();
+ }
+
+ QFile queryFile(queryInfo.filePath());
+ if (!queryFile.open(QIODevice::ReadOnly | QIODevice::Text))
+ return QString();
+ QString query = QTextStream(&queryFile).readAll();
+ queryFile.close();
+
+ return query.trimmed();
+}
+
+
+QStringList AbstractSqlStorage::setupQueries()
+{
+ QStringList queries;
+ QDir dir = QDir(QString(":/SQL/%1/%2/").arg(displayName()).arg(schemaVersion()));
+ foreach(QFileInfo fileInfo, dir.entryInfoList(QStringList() << "setup*", QDir::NoFilter, QDir::Name)) {
+ queries << queryString(fileInfo.baseName());
+ }
+ return queries;
+}
+
+
+bool AbstractSqlStorage::setup(const QVariantMap &settings)
+{
+ setConnectionProperties(settings);
+ QSqlDatabase db = logDb();
+ if (!db.isOpen()) {
+ qCritical() << "Unable to setup Logging Backend!";
+ return false;
+ }
+
+ db.transaction();
+ foreach(QString queryString, setupQueries()) {
+ QSqlQuery query = db.exec(queryString);
+ if (!watchQuery(query)) {
+ qCritical() << "Unable to setup Logging Backend!";
+ db.rollback();
+ return false;
+ }
+ }
+ bool success = setupSchemaVersion(schemaVersion());
+ if (success)
+ db.commit();
+ else
+ db.rollback();
+ return success;
+}
+
+
+QStringList AbstractSqlStorage::upgradeQueries(int version)
+{
+ QStringList queries;
+ QDir dir = QDir(QString(":/SQL/%1/%2/").arg(displayName()).arg(version));
+ foreach(QFileInfo fileInfo, dir.entryInfoList(QStringList() << "upgrade*", QDir::NoFilter, QDir::Name)) {
+ queries << queryString(fileInfo.baseName(), version);
+ }
+ return queries;
+}
+
+
+bool AbstractSqlStorage::upgradeDb()
+{
+ if (schemaVersion() <= installedSchemaVersion())
+ return true;
+
+ QSqlDatabase db = logDb();
+
+ for (int ver = installedSchemaVersion() + 1; ver <= schemaVersion(); ver++) {
+ foreach(QString queryString, upgradeQueries(ver)) {
+ QSqlQuery query = db.exec(queryString);
+ if (!watchQuery(query)) {
+ qCritical() << "Unable to upgrade Logging Backend!";
+ return false;
+ }
+ }
+ }
+ return updateSchemaVersion(schemaVersion());
+}
+
+
+int AbstractSqlStorage::schemaVersion()
+{
+ // returns the newest Schema Version!
+ // not the currently used one! (though it can be the same)
+ if (_schemaVersion > 0)
+ return _schemaVersion;
+
+ int version;
+ bool ok;
+ QDir dir = QDir(":/SQL/" + displayName());
+ foreach(QFileInfo fileInfo, dir.entryInfoList()) {
+ if (!fileInfo.isDir())
+ continue;
+
+ version = fileInfo.fileName().toInt(&ok);
+ if (!ok)
+ continue;
+
+ if (version > _schemaVersion)
+ _schemaVersion = version;
+ }