From c9170a8e0b860a3906005c677e4674cbcc74c32a Mon Sep 17 00:00:00 2001 From: Uwe Klotz Date: Tue, 10 Nov 2020 12:48:25 +0100 Subject: Cue/CueDAO: Improve type-safety and reduce code duplication --- src/library/dao/cuedao.cpp | 98 ++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 51 deletions(-) (limited to 'src/library/dao') diff --git a/src/library/dao/cuedao.cpp b/src/library/dao/cuedao.cpp index fb76e915c6..a65d08711f 100644 --- a/src/library/dao/cuedao.cpp +++ b/src/library/dao/cuedao.cpp @@ -40,7 +40,7 @@ inline QString labelFromQVariant(const QVariant& value) { } CuePointer cueFromRow(const QSqlRecord& row) { - int id = row.value(row.indexOf("id")).toInt(); + const auto id = DbId(row.value(row.indexOf("id"))); TrackId trackId(row.value(row.indexOf("track_id"))); int type = row.value(row.indexOf("type")).toInt(); int position = row.value(row.indexOf("position")).toInt(); @@ -142,28 +142,11 @@ bool CueDAO::saveCue(Cue* cue) const { VERIFY_OR_DEBUG_ASSERT(cue) { return false; } - if (cue->getId() == -1) { - // New cue - QSqlQuery query(m_database); - query.prepare(QStringLiteral("INSERT INTO " CUE_TABLE " (track_id, type, position, length, hotcue, label, color) VALUES (:track_id, :type, :position, :length, :hotcue, :label, :color)")); - query.bindValue(":track_id", cue->getTrackId().toVariant()); - query.bindValue(":type", static_cast(cue->getType())); - query.bindValue(":position", cue->getPosition()); - query.bindValue(":length", cue->getLength()); - query.bindValue(":hotcue", cue->getHotCue()); - query.bindValue(":label", labelToQVariant(cue->getLabel())); - query.bindValue(":color", mixxx::RgbColor::toQVariant(cue->getColor())); - if (query.exec()) { - int id = query.lastInsertId().toInt(); - cue->setId(id); - cue->setDirty(false); - return true; - } - qDebug() << query.executedQuery() << query.lastError(); - } else { + // Prepare query + QSqlQuery query(m_database); + if (cue->getId().isValid()) { // Update cue - QSqlQuery query(m_database); query.prepare(QStringLiteral("UPDATE " CUE_TABLE " SET " "track_id=:track_id," "type=:type," @@ -173,40 +156,53 @@ bool CueDAO::saveCue(Cue* cue) const { "label=:label," "color=:color" " WHERE id=:id")); - query.bindValue(":id", cue->getId()); - query.bindValue(":track_id", cue->getTrackId().toVariant()); - query.bindValue(":type", static_cast(cue->getType())); - query.bindValue(":position", cue->getPosition()); - query.bindValue(":length", cue->getLength()); - query.bindValue(":hotcue", cue->getHotCue()); - query.bindValue(":label", labelToQVariant(cue->getLabel())); - query.bindValue(":color", mixxx::RgbColor::toQVariant(cue->getColor())); + query.bindValue(":id", cue->getId().toVariant()); + } else { + // New cue + query.prepare( + QStringLiteral("INSERT INTO " CUE_TABLE + " (track_id, type, position, length, hotcue, " + "label, color) VALUES (:track_id, :type, " + ":position, :length, :hotcue, :label, :color)")); + } - if (query.exec()) { - cue->setDirty(false); - return true; - } else { - LOG_FAILED_QUERY(query); - } + // Bind values and execute query + query.bindValue(":track_id", cue->getTrackId().toVariant()); + query.bindValue(":type", static_cast(cue->getType())); + query.bindValue(":position", cue->getPosition()); + query.bindValue(":length", cue->getLength()); + query.bindValue(":hotcue", cue->getHotCue()); + query.bindValue(":label", labelToQVariant(cue->getLabel())); + query.bindValue(":color", mixxx::RgbColor::toQVariant(cue->getColor())); + if (!query.exec()) { + LOG_FAILED_QUERY(query); + return false; } - return false; + + if (!cue->getId().isValid()) { + // New cue + const auto newId = DbId(query.lastInsertId()); + DEBUG_ASSERT(newId.isValid()); + cue->setId(newId); + } + DEBUG_ASSERT(cue->getId().isValid()); + cue->setDirty(false); + return true; } bool CueDAO::deleteCue(Cue* cue) const { //qDebug() << "CueDAO::deleteCue" << QThread::currentThread() << m_database.connectionName(); - if (cue->getId() != -1) { - QSqlQuery query(m_database); - query.prepare(QStringLiteral("DELETE FROM " CUE_TABLE " WHERE id=:id")); - query.bindValue(":id", cue->getId()); - if (query.exec()) { - return true; - } else { - LOG_FAILED_QUERY(query); - } - } else { - return true; + if (!cue->getId().isValid()) { + return false; } - return false; + QSqlQuery query(m_database); + query.prepare(QStringLiteral("DELETE FROM " CUE_TABLE " WHERE id=:id")); + query.bindValue(":id", cue->getId().toVariant()); + if (!query.exec()) { + LOG_FAILED_QUERY(query); + return false; + } + return true; } void CueDAO::saveTrackCues( @@ -219,16 +215,16 @@ void CueDAO::saveTrackCues( pCue->setTrackId(trackId); } // New cues (without an id) must always be marked as dirty - DEBUG_ASSERT(pCue->getId() >= 0 || pCue->isDirty()); + DEBUG_ASSERT(pCue->getId().isValid() || pCue->isDirty()); // Update or save cue if (pCue->isDirty()) { saveCue(pCue.get()); } // After saving each cue must have a valid id - VERIFY_OR_DEBUG_ASSERT(pCue->getId() >= 0) { + VERIFY_OR_DEBUG_ASSERT(pCue->getId().isValid()) { continue; } - cueIds.append(QString::number(pCue->getId())); + cueIds.append(pCue->getId().toString()); } // Delete orphaned cues -- cgit v1.2.3