+void AbstractSqlStorage::addConnectionToPool()
+{
+ QMutexLocker locker(&_connectionPoolMutex);
+ // we have to recheck if the connection pool already contains a connection for
+ // this thread. Since now (after the lock) we can only tell for sure
+ if (_connectionPool.contains(QThread::currentThread()))
+ return;
+
+ QThread *currentThread = QThread::currentThread();
+
+ int connectionId = _nextConnectionId++;
+
+ Connection *connection = new Connection(QLatin1String(QString("quassel_%1_con_%2").arg(driverName()).arg(connectionId).toLatin1()));
+ connection->moveToThread(currentThread);
+ connect(this, SIGNAL(destroyed()), connection, SLOT(deleteLater()));
+ connect(currentThread, SIGNAL(destroyed()), connection, SLOT(deleteLater()));
+ connect(connection, SIGNAL(destroyed()), this, SLOT(connectionDestroyed()));
+ _connectionPool[currentThread] = connection;
+
+ QSqlDatabase db = QSqlDatabase::addDatabase(driverName(), connection->name());
+ db.setDatabaseName(databaseName());
+
+ if (!hostName().isEmpty())
+ db.setHostName(hostName());
+
+ if (port() != -1)
+ db.setPort(port());
+
+ if (!userName().isEmpty()) {
+ db.setUserName(userName());
+ db.setPassword(password());
+ }
+
+ dbConnect(db);
+}
+
+
+void AbstractSqlStorage::dbConnect(QSqlDatabase &db)
+{
+ if (!db.open()) {
+ quWarning() << "Unable to open database" << displayName() << "for thread" << QThread::currentThread();
+ quWarning() << "-" << db.lastError().text();
+ }
+ else {
+ if (!initDbSession(db)) {
+ quWarning() << "Unable to initialize database" << displayName() << "for thread" << QThread::currentThread();
+ db.close();
+ }
+ }
+}
+
+
+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. Schema version:" << installedSchemaVersion();
+ return IsReady;