![]() |
Mixxx
|
00001 // cratedao.cpp 00002 // Created 10/22/2009 by RJ Ryan (rryan@mit.edu) 00003 00004 #include <QtDebug> 00005 #include <QSqlQuery> 00006 #include <QSqlError> 00007 #include <QVariant> 00008 00009 #include "library/dao/cratedao.h" 00010 #include "library/queryutil.h" 00011 00012 CrateDAO::CrateDAO(QSqlDatabase& database) 00013 : m_database(database) { 00014 } 00015 00016 CrateDAO::~CrateDAO() { 00017 } 00018 00019 void CrateDAO::initialize() { 00020 qDebug() << "CrateDAO::initialize()"; 00021 } 00022 00023 unsigned int CrateDAO::crateCount() { 00024 QSqlQuery query(m_database); 00025 query.prepare("SELECT count(*) FROM " CRATE_TABLE); 00026 00027 if (!query.exec() || !query.next()) { 00028 LOG_FAILED_QUERY(query); 00029 return 0; 00030 } 00031 return query.value(0).toInt(); 00032 } 00033 00034 bool CrateDAO::createCrate(const QString& name) { 00035 QSqlQuery query(m_database); 00036 query.prepare("INSERT INTO " CRATE_TABLE " (name) VALUES (:name)"); 00037 query.bindValue(":name", name); 00038 00039 if (!query.exec()) { 00040 LOG_FAILED_QUERY(query); 00041 return false; 00042 } 00043 00044 int crateId = query.lastInsertId().toInt(); 00045 emit(added(crateId)); 00046 return true; 00047 } 00048 00049 bool CrateDAO::renameCrate(int crateId, const QString& newName) { 00050 QSqlQuery query(m_database); 00051 query.prepare("UPDATE " CRATE_TABLE " SET name = :name WHERE id = :id"); 00052 query.bindValue(":name", newName); 00053 query.bindValue(":id", crateId); 00054 00055 if (!query.exec()) { 00056 LOG_FAILED_QUERY(query); 00057 return false; 00058 } 00059 return true; 00060 } 00061 00062 bool CrateDAO::setCrateLocked(int crateId, bool locked) { 00063 // SQLite3 doesn't support boolean value. Using integer instead. 00064 int lock = locked ? 1 : 0; 00065 QSqlQuery query(m_database); 00066 query.prepare("UPDATE " CRATE_TABLE " SET locked = :lock WHERE id = :id"); 00067 query.bindValue(":lock", lock); 00068 query.bindValue(":id", crateId); 00069 00070 if (!query.exec()) { 00071 LOG_FAILED_QUERY(query); 00072 return false; 00073 } 00074 00075 return true; 00076 } 00077 00078 bool CrateDAO::isCrateLocked(int crateId) { 00079 QSqlQuery query(m_database); 00080 query.prepare("SELECT locked FROM " CRATE_TABLE " WHERE id = :id"); 00081 query.bindValue(":id", crateId); 00082 00083 if (query.exec()) { 00084 if (query.next()) { 00085 int lockValue = query.value(0).toInt(); 00086 return lockValue == 1; 00087 } 00088 } else { 00089 LOG_FAILED_QUERY(query); 00090 } 00091 00092 return false; 00093 } 00094 00095 bool CrateDAO::deleteCrate(int crateId) { 00096 Q_ASSERT(m_database.transaction()); 00097 QSqlQuery query(m_database); 00098 query.prepare("DELETE FROM " CRATE_TRACKS_TABLE " WHERE crate_id = :id"); 00099 query.bindValue(":id", crateId); 00100 00101 if (!query.exec()) { 00102 LOG_FAILED_QUERY(query); 00103 Q_ASSERT(m_database.rollback()); 00104 return false; 00105 } 00106 00107 query.prepare("DELETE FROM " CRATE_TABLE " WHERE id = :id"); 00108 query.bindValue(":id", crateId); 00109 00110 if (!query.exec()) { 00111 LOG_FAILED_QUERY(query); 00112 Q_ASSERT(m_database.rollback()); 00113 return false; 00114 } 00115 Q_ASSERT(m_database.commit()); 00116 00117 emit(deleted(crateId)); 00118 return true; 00119 } 00120 00121 int CrateDAO::getCrateIdByName(const QString& name) { 00122 QSqlQuery query(m_database); 00123 query.prepare("SELECT id FROM " CRATE_TABLE " WHERE name = (:name)"); 00124 query.bindValue(":name", name); 00125 if (query.exec()) { 00126 if (query.next()) { 00127 int id = query.value(0).toInt(); 00128 return id; 00129 } 00130 } else { 00131 LOG_FAILED_QUERY(query); 00132 } 00133 return -1; 00134 } 00135 00136 int CrateDAO::getCrateId(int position) { 00137 QSqlQuery query(m_database); 00138 query.prepare("SELECT id FROM " CRATE_TABLE); 00139 if (query.exec()) { 00140 int currentRow = 0; 00141 while(query.next()) { 00142 if (currentRow++ == position) { 00143 int id = query.value(0).toInt(); 00144 return id; 00145 } 00146 } 00147 } else { 00148 LOG_FAILED_QUERY(query); 00149 } 00150 return -1; 00151 } 00152 00153 QString CrateDAO::crateName(int crateId) { 00154 QSqlQuery query(m_database); 00155 query.prepare("SELECT name FROM " CRATE_TABLE " WHERE id = (:id)"); 00156 query.bindValue(":id", crateId); 00157 if (query.exec()) { 00158 if (query.next()) { 00159 return query.value(0).toString(); 00160 } 00161 } else { 00162 LOG_FAILED_QUERY(query); 00163 } 00164 return QString(); 00165 } 00166 00167 unsigned int CrateDAO::crateSize(int crateId) { 00168 QSqlQuery query(m_database); 00169 query.prepare("SELECT COUNT(*) FROM " CRATE_TRACKS_TABLE " WHERE crate_id = (:id)"); 00170 query.bindValue(":id", crateId); 00171 if (query.exec()) { 00172 if (query.next()) { 00173 return query.value(0).toInt(); 00174 } 00175 } else { 00176 LOG_FAILED_QUERY(query); 00177 } 00178 return 0; 00179 } 00180 00181 bool CrateDAO::addTrackToCrate(int trackId, int crateId) { 00182 QSqlQuery query(m_database); 00183 query.prepare("INSERT INTO " CRATE_TRACKS_TABLE 00184 " (crate_id, track_id) VALUES (:crate_id, :track_id)"); 00185 query.bindValue(":crate_id", crateId); 00186 query.bindValue(":track_id", trackId); 00187 00188 if (!query.exec()) { 00189 // It's normal for this query to fail with a constraint violation 00190 // (e.g. the track is already in the crate) 00191 LOG_FAILED_QUERY(query); 00192 return false; 00193 } 00194 00195 emit(trackAdded(crateId, trackId)); 00196 emit(changed(crateId)); 00197 return true; 00198 } 00199 00200 void CrateDAO::removeTrackFromCrates(int trackId) { 00201 QSqlQuery query(m_database); 00202 QString queryString = QString("DELETE FROM %1 WHERE %2 = %3") 00203 .arg(CRATE_TRACKS_TABLE) 00204 .arg(CRATETRACKSTABLE_TRACKID) 00205 .arg(QString::number(trackId)); 00206 query.prepare(queryString); 00207 if (!query.exec()) { 00208 LOG_FAILED_QUERY(query); 00209 } 00210 } 00211 00212 bool CrateDAO::removeTrackFromCrate(int trackId, int crateId) { 00213 QSqlQuery query(m_database); 00214 query.prepare("DELETE FROM " CRATE_TRACKS_TABLE " WHERE " 00215 "crate_id = :crate_id AND track_id = :track_id"); 00216 query.bindValue(":crate_id", crateId); 00217 query.bindValue(":track_id", trackId); 00218 00219 if (!query.exec()) { 00220 LOG_FAILED_QUERY(query); 00221 return false; 00222 } 00223 00224 emit(trackRemoved(crateId, trackId)); 00225 emit(changed(crateId)); 00226 return true; 00227 }