Avoid duplicate code when inserting/updating a conference.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Wed, 13 Sep 2017 21:17:32 +0000 (23:17 +0200)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Wed, 13 Sep 2017 21:28:44 +0000 (23:28 +0200)
src/sql/sqlengine.cpp

index 9f5b8cd..5097082 100644 (file)
@@ -141,40 +141,31 @@ bool SqlEngine::applySqlFile(const QString sqlFile) {
 
 void SqlEngine::addConferenceToDB(QHash<QString,QString> &aConference, int conferenceId) {
     QSqlQuery query(db);
-    if (conferenceId <= 0) // insert conference
-    {
+    bool insert = conferenceId <= 0;
+    if (insert) { // insert conference
         query.prepare("INSERT INTO CONFERENCE (title,url,subtitle,venue,city,start,end,"
                                                 "day_change,timeslot_duration,active) "
                         " VALUES (:title,:url,:subtitle,:venue,:city,:start,:end,"
                                                 ":day_change,:timeslot_duration,:active)");
-        foreach (QString prop_name, (QList<QString>() << "title" << "url" << "subtitle" << "venue" << "city")) {
-            query.bindValue(QString(":") + prop_name, aConference[prop_name]);
-        }
-        query.bindValue(":start", QDateTime(QDate::fromString(aConference["start"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t());
-        query.bindValue(":end", QDateTime(QDate::fromString(aConference["end"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t());
-        query.bindValue(":day_change", -QTime::fromString(aConference["day_change"],TIME_FORMAT).secsTo(QTime(0,0)));
-        query.bindValue(":timeslot_duration", -QTime::fromString(aConference["timeslot_duration"],TIME_FORMAT).secsTo(QTime(0,0)));
-        query.bindValue(":active", 1);
-        query.exec();
-        emitSqlQueryError(query);
-        aConference["id"] = query.lastInsertId().toString(); // 'id' is assigned automatically
-    }
-    else // update conference
-    {
+    } else { // update conference
         query.prepare("UPDATE CONFERENCE set title=:title, url=:url, subtitle=:subtitle, venue=:venue, city=:city, start=:start, end=:end,"
                                             "day_change=:day_change, timeslot_duration=:timeslot_duration, active=:active "
                       "WHERE id=:id");
-        foreach (QString prop_name, (QList<QString>() << "title" << "url" << "subtitle" << "venue" << "city")) {
-            query.bindValue(QString(":") + prop_name, aConference[prop_name]);
-        }
-        query.bindValue(":start", QDateTime(QDate::fromString(aConference["start"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t());
-        query.bindValue(":end", QDateTime(QDate::fromString(aConference["end"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t());
-        query.bindValue(":day_change", -QTime::fromString(aConference["day_change"],TIME_FORMAT).secsTo(QTime(0,0)));
-        query.bindValue(":timeslot_duration", -QTime::fromString(aConference["timeslot_duration"],TIME_FORMAT).secsTo(QTime(0,0)));
-        query.bindValue(":active", 1);
-        query.bindValue(":id", conferenceId);
-        query.exec();
-        emitSqlQueryError(query);
+    }
+    foreach (QString prop_name, (QList<QString>() << "title" << "url" << "subtitle" << "venue" << "city")) {
+        query.bindValue(QString(":") + prop_name, aConference[prop_name]);
+    }
+    query.bindValue(":start", QDateTime(QDate::fromString(aConference["start"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t());
+    query.bindValue(":end", QDateTime(QDate::fromString(aConference["end"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t());
+    query.bindValue(":day_change", -QTime::fromString(aConference["day_change"],TIME_FORMAT).secsTo(QTime(0,0)));
+    query.bindValue(":timeslot_duration", -QTime::fromString(aConference["timeslot_duration"],TIME_FORMAT).secsTo(QTime(0,0)));
+    query.bindValue(":active", 1);
+    if (!insert) query.bindValue(":id", conferenceId);
+    query.exec();
+    emitSqlQueryError(query);
+    if (insert) {
+        aConference["id"] = query.lastInsertId().toString(); // 'id' is assigned automatically
+    } else {
         aConference["id"] = QVariant(conferenceId).toString();
     }
 }