Changeset f0f9fdc in confclerk_git


Ignore:
Timestamp:
09/13/17 23:28:44 (5 years ago)
Author:
Philipp Spitzer <philipp@…>
Branches:
master
Children:
7ba0378
Parents:
a4d3f7b
git-author:
Philipp Spitzer <philipp@…> (09/13/17 23:17:32)
git-committer:
Philipp Spitzer <philipp@…> (09/13/17 23:28:44)
Message:

Avoid duplicate code when inserting/updating a conference.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sql/sqlengine.cpp

    ra4d3f7b rf0f9fdc  
    142142void SqlEngine::addConferenceToDB(QHash<QString,QString> &aConference, int conferenceId) {
    143143    QSqlQuery query(db);
    144     if (conferenceId <= 0) // insert conference
    145     {
     144    bool insert = conferenceId <= 0;
     145    if (insert) { // insert conference
    146146        query.prepare("INSERT INTO CONFERENCE (title,url,subtitle,venue,city,start,end,"
    147147                                                "day_change,timeslot_duration,active) "
    148148                        " VALUES (:title,:url,:subtitle,:venue,:city,:start,:end,"
    149149                                                ":day_change,:timeslot_duration,:active)");
    150         foreach (QString prop_name, (QList<QString>() << "title" << "url" << "subtitle" << "venue" << "city")) {
    151             query.bindValue(QString(":") + prop_name, aConference[prop_name]);
    152         }
    153         query.bindValue(":start", QDateTime(QDate::fromString(aConference["start"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t());
    154         query.bindValue(":end", QDateTime(QDate::fromString(aConference["end"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t());
    155         query.bindValue(":day_change", -QTime::fromString(aConference["day_change"],TIME_FORMAT).secsTo(QTime(0,0)));
    156         query.bindValue(":timeslot_duration", -QTime::fromString(aConference["timeslot_duration"],TIME_FORMAT).secsTo(QTime(0,0)));
    157         query.bindValue(":active", 1);
    158         query.exec();
    159         emitSqlQueryError(query);
    160         aConference["id"] = query.lastInsertId().toString(); // 'id' is assigned automatically
    161     }
    162     else // update conference
    163     {
     150    } else { // update conference
    164151        query.prepare("UPDATE CONFERENCE set title=:title, url=:url, subtitle=:subtitle, venue=:venue, city=:city, start=:start, end=:end,"
    165152                                            "day_change=:day_change, timeslot_duration=:timeslot_duration, active=:active "
    166153                      "WHERE id=:id");
    167         foreach (QString prop_name, (QList<QString>() << "title" << "url" << "subtitle" << "venue" << "city")) {
    168             query.bindValue(QString(":") + prop_name, aConference[prop_name]);
    169         }
    170         query.bindValue(":start", QDateTime(QDate::fromString(aConference["start"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t());
    171         query.bindValue(":end", QDateTime(QDate::fromString(aConference["end"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t());
    172         query.bindValue(":day_change", -QTime::fromString(aConference["day_change"],TIME_FORMAT).secsTo(QTime(0,0)));
    173         query.bindValue(":timeslot_duration", -QTime::fromString(aConference["timeslot_duration"],TIME_FORMAT).secsTo(QTime(0,0)));
    174         query.bindValue(":active", 1);
    175         query.bindValue(":id", conferenceId);
    176         query.exec();
    177         emitSqlQueryError(query);
     154    }
     155    foreach (QString prop_name, (QList<QString>() << "title" << "url" << "subtitle" << "venue" << "city")) {
     156        query.bindValue(QString(":") + prop_name, aConference[prop_name]);
     157    }
     158    query.bindValue(":start", QDateTime(QDate::fromString(aConference["start"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t());
     159    query.bindValue(":end", QDateTime(QDate::fromString(aConference["end"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t());
     160    query.bindValue(":day_change", -QTime::fromString(aConference["day_change"],TIME_FORMAT).secsTo(QTime(0,0)));
     161    query.bindValue(":timeslot_duration", -QTime::fromString(aConference["timeslot_duration"],TIME_FORMAT).secsTo(QTime(0,0)));
     162    query.bindValue(":active", 1);
     163    if (!insert) query.bindValue(":id", conferenceId);
     164    query.exec();
     165    emitSqlQueryError(query);
     166    if (insert) {
     167        aConference["id"] = query.lastInsertId().toString(); // 'id' is assigned automatically
     168    } else {
    178169        aConference["id"] = QVariant(conferenceId).toString();
    179170    }
Note: See TracChangeset for help on using the changeset viewer.