bool RunsTableModel::postRow(int row_no, bool throw_exc) { bool is_single_user = sqlConnection().driverName().endsWith(QLatin1String("SQLITE"), Qt::CaseInsensitive); if(is_single_user) return Super::postRow(row_no, throw_exc); int col_stime = columnIndex("startTimeMs"); QF_ASSERT(col_stime >= 0, "Bad startTimeMs column!", return false); if(isDirty(row_no, col_stime)) { int id = value(row_no, "runs.id").toInt(); int orig_msec = origValue(row_no, col_stime).toInt(); int db_msec = 0; qf::core::sql::Transaction transaction(sqlConnection()); QString qs = "SELECT id, startTimeMs FROM runs WHERE id=" QF_IARG(id) " FOR UPDATE"; qf::core::sql::Query q(transaction.connection()); q.exec(qs, qf::core::Exception::Throw); if(q.next()) { db_msec = q.value("startTimeMs").toInt(); } if(orig_msec == db_msec) { bool ret = Super::postRow(row_no, throw_exc); transaction.commit(); return ret; } else { QString err_msg = tr("Mid-air collision setting start time, reload table and try it again."); revertRow(row_no); if(throw_exc) QF_EXCEPTION(err_msg); return false; } } return Super::postRow(row_no, throw_exc); }
void RunsTableModel::switchStartTimes(int r1, int r2) { qfLogFuncFrame() << r1 << r2; int col_stime = columnIndex("startTimeMs"); QF_ASSERT(col_stime >= 0, "Bad startTimeMs column!", return); int id1 = value(r1, "runs.id").toInt(); int id2 = value(r2, "runs.id").toInt(); QString err_msg; QModelIndex ix1 = index(r1, col_stime); QVariant v1 = ix1.data(Qt::EditRole); QModelIndex ix2 = index(r2, col_stime); QVariant v2 = ix2.data(Qt::EditRole); bool is_single_user = sqlConnection().driverName().endsWith(QLatin1String("SQLITE"), Qt::CaseInsensitive); if(is_single_user) { setData(ix1, v2); setData(ix2, v1); postRow(r1, true); postRow(r2, true); } else { //qf::core::sql::Transaction transaction(sqlConnection()); quickevent::core::og::TimeMs t1 = v1.value<quickevent::core::og::TimeMs>(); quickevent::core::og::TimeMs t2 = v2.value<quickevent::core::og::TimeMs>(); int msec1 = -1, msec2 = -1; qf::core::sql::Query q(sqlConnection()); QString qs = "SELECT id, startTimeMs FROM runs WHERE id IN (" QF_IARG(id1) ", " QF_IARG(id2) ")"; q.exec(qs, qf::core::Exception::Throw); while(q.next()) { int id = q.value("id").toInt(); if(id == id1) msec1 = q.value("startTimeMs").toInt(); else if(id == id2) msec2 = q.value("startTimeMs").toInt(); } qfDebug() << t1.msec() << msec1 << t2.msec() << msec2; if(msec1 == t1.msec() && msec2 == t2.msec()) { setData(ix1, v2); setData(ix2, v1); postRow(r1, qf::core::Exception::Throw); postRow(r2, qf::core::Exception::Throw); //transaction.commit(); } else { err_msg = tr("Mid-air collision switching start times, reload table and try it again."); } } emit startTimesSwitched(id1, id2, err_msg); }