summaryrefslogtreecommitdiffstats
path: root/src/library/dao
diff options
context:
space:
mode:
authorUwe Klotz <uklotz@mixxx.org>2020-11-10 12:48:25 +0100
committerUwe Klotz <uklotz@mixxx.org>2020-11-15 14:34:14 +0100
commitc9170a8e0b860a3906005c677e4674cbcc74c32a (patch)
tree6641189f6530118a977fa5215f67147055f4e3e9 /src/library/dao
parent3e1f1c44db104ccc806779802c00eb22cc35c74b (diff)
Cue/CueDAO: Improve type-safety and reduce code duplication
Diffstat (limited to 'src/library/dao')
-rw-r--r--src/library/dao/cuedao.cpp98
1 files changed, 47 insertions, 51 deletions
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<int>(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<int>(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<int>(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