prettyficating the prompting for backend credentials
[quassel.git] / src / core / postgresqlstorage.cpp
index 96a60d3..c7d59b9 100644 (file)
@@ -35,6 +35,18 @@ PostgreSqlStorage::PostgreSqlStorage(QObject *parent)
 PostgreSqlStorage::~PostgreSqlStorage() {
 }
 
+AbstractSqlMigrationWriter *PostgreSqlStorage::createMigrationWriter() {
+  PostgreSqlMigrationWriter *writer = new PostgreSqlMigrationWriter();
+  QVariantMap properties;
+  properties["Username"] = _userName;
+  properties["Password"] = _password;
+  properties["Hostname"] = _hostName;
+  properties["Port"] = _port;
+  properties["Database"] = _databaseName;
+  writer->setConnectionProperties(properties);
+  return writer;
+}
+
 bool PostgreSqlStorage::isAvailable() const {
   if(!QSqlDatabase::isDriverAvailable("QPSQL")) return false;
   return true;
@@ -49,10 +61,18 @@ QString PostgreSqlStorage::description() const {
   return tr("PostgreSQL Turbo Bomber HD!");
 }
 
-QVariantMap PostgreSqlStorage::setupKeys() const {
+QStringList PostgreSqlStorage::setupKeys() const {
+  QStringList keys;
+  keys << "Username"
+       << "Password"
+       << "Hostname"
+       << "Port"
+       << "Database";
+  return keys;
+}
+QVariantMap PostgreSqlStorage::setupDefaults() const {
   QVariantMap map;
   map["Username"] = QVariant(QString("quassel"));
-  map["Password"] = QVariant(QString());
   map["Hostname"] = QVariant(QString("localhost"));
   map["Port"] = QVariant(5432);
   map["Database"] = QVariant(QString("quassel"));
@@ -1255,34 +1275,49 @@ QList<Message> PostgreSqlStorage::requestMsgs(UserId user, BufferId bufferId, Ms
     return messagelist;
   }
 
-  QSqlQuery query(db);
+  QString queryName;
+  QVariantList params;
   if(last == -1 && first == -1) {
-    query.prepare(queryString("select_messages"));
+    queryName = "select_messages";
   } else if(last == -1) {
-    query.prepare(queryString("select_messagesNewerThan"));
-    query.bindValue(":firstmsg", first.toInt());
+    queryName = "select_messagesNewerThan";
+    params << first.toInt();
   } else {
-    query.prepare(queryString("select_messagesRange"));
-    query.bindValue(":lastmsg", last.toInt());
-    query.bindValue(":firstmsg", first.toInt());
+    queryName = "select_messagesRange";
+    params << first.toInt();
+    params << last.toInt();
   }
-  query.bindValue(":bufferid", bufferId.toInt());
-  safeExec(query);
+  params << bufferId.toInt();
+  if(limit != -1)
+    params << limit;
+  else
+    params << "ALL";
+
+  if(!prepareQuery(queryName, queryString(queryName), db)) {
+    qWarning() << "PostgreSqlStorage::logMessages(): unable to prepare query:" << queryString(queryName);
+    qWarning() << "  Error:" << db.lastError().text();
+    db.rollback();
+    return messagelist;
+  }
+
+  QSqlQuery query = executePreparedQuery(queryName, params, db);
+
   if(!watchQuery(query)) {
+    qDebug() << "select_messages failed";
     db.rollback();
     return messagelist;
   }
 
   QDateTime timestamp;
-  for(int i = 0; i < limit && query.next(); i++) {
+  while(query.next()) {
     timestamp = query.value(1).toDateTime();
     timestamp.setTimeSpec(Qt::UTC);
     Message msg(timestamp,
-                bufferInfo,
-                (Message::Type)query.value(2).toUInt(),
-                query.value(5).toString(),
-                query.value(4).toString(),
-                (Message::Flags)query.value(3).toUInt());
+               bufferInfo,
+               (Message::Type)query.value(2).toUInt(),
+               query.value(5).toString(),
+               query.value(4).toString(),
+               (Message::Flags)query.value(3).toUInt());
     msg.setMsgId(query.value(0).toInt());
     messagelist << msg;
   }
@@ -1389,7 +1424,7 @@ bool PostgreSqlStorage::prepareQuery(const QString &handle, const QString &query
 QSqlQuery PostgreSqlStorage::executePreparedQuery(const QString &handle, const QVariantList &params, const QSqlDatabase &db) {
   if(!_preparedQueries.contains(db.connectionName()) || !_preparedQueries[db.connectionName()].contains(handle)) {
     qWarning() << "PostgreSqlStorage::executePreparedQuery() no prepared Query with handle" << handle << "on Database" << db.connectionName();
-    return QSqlQuery();
+    return QSqlQuery(db);
   }
 
   QSqlDriver *driver = db.driver();
@@ -1419,7 +1454,7 @@ QSqlQuery PostgreSqlStorage::executePreparedQuery(const QString &handle, const Q
 QSqlQuery PostgreSqlStorage::executePreparedQuery(const QString &handle, const QVariant &param, const QSqlDatabase &db) {
   if(!_preparedQueries.contains(db.connectionName()) || !_preparedQueries[db.connectionName()].contains(handle)) {
     qWarning() << "PostgreSqlStorage::executePreparedQuery() no prepared Query with handle" << handle << "on Database" << db.connectionName();
-    return QSqlQuery();
+    return QSqlQuery(db);
   }
 
   QSqlField field;
@@ -1445,10 +1480,6 @@ void PostgreSqlStorage::deallocateQuery(const QString &handle, const QSqlDatabas
   db.exec(QString("DEALLOCATE %1").arg(queryId));
 }
 
-
-
-
-
 // ========================================
 //  PostgreSqlMigrationWriter
 // ========================================
@@ -1467,6 +1498,7 @@ bool PostgreSqlMigrationWriter::prepareQuery(MigrationObject mo) {
     query = queryString("migrate_write_sender");
     break;
   case Identity:
+    _validIdentities.clear();
     query = queryString("migrate_write_identity");
     break;
   case IdentityNick:
@@ -1509,6 +1541,7 @@ bool PostgreSqlMigrationWriter::writeMo(const SenderMO &sender) {
 
 //bool PostgreSqlMigrationWriter::writeIdentity(const IdentityMO &identity) {
 bool PostgreSqlMigrationWriter::writeMo(const IdentityMO &identity) {
+  _validIdentities << identity.id.toInt();
   bindValue(0, identity.id.toInt());
   bindValue(1, identity.userid.toInt());
   bindValue(2, identity.identityname);
@@ -1546,7 +1579,10 @@ bool PostgreSqlMigrationWriter::writeMo(const NetworkMO &network) {
   bindValue(0, network.networkid.toInt());
   bindValue(1, network.userid.toInt());
   bindValue(2, network.networkname);
-  bindValue(3, network.identityid.toInt());
+  if(_validIdentities.contains(network.identityid.toInt()))
+    bindValue(3, network.identityid.toInt());
+  else
+    bindValue(3, QVariant());
   bindValue(4, network.encodingcodec);
   bindValue(5, network.decodingcodec);
   bindValue(6, network.servercodec);