#include "MindScoreService.h" #include "ClassSet.h" #include "SqlDBHelper.h" #include MindScoreService::MindScoreService(QObject *parent) : QObject(parent) { } bool MindScoreService::saveUniqueScoreData(int projId, const QString &score) { MindScoreInfo info; bool qRet = queryScoreData(&info, projId); if (qRet == false) { return false; } if (info.id < 0) { info.projectId = projId; info.score = score; return saveScoreData(&info); } else { return updateScoreData(info.id, score); } } bool MindScoreService::queryScoreData(MindScoreInfo *info, int projId) { bool ret = false; try { Transaction t(SqlDBHelper::getDatabase()); QString selectSql = QString("SELECT id, project_id, score " "from t_mind_score WHERE project_id = %1") .arg(projId); QueryResult queryResult = t.execQuery(selectSql); if (queryResult.next()) { info->id = queryResult.value(0).toInt(); info->projectId = queryResult.value(1).toInt(); info->score = queryResult.value(2).toString(); } ret = true; } catch (const DBException &ex) { qDebug() << ex.lastError.text(); } return ret; } bool MindScoreService::updateScoreData(int id, const QString &score) { bool ret = false; try { Transaction t(SqlDBHelper::getDatabase()); t.update("t_mind_score").set("score", score).where("id = ?", id); t.commit(); ret = true; } catch (const DBException &ex) { qDebug() << ex.lastError.text(); } return ret; } bool MindScoreService::saveScoreData(MindScoreInfo *info) { bool ret = false; try { Transaction t(SqlDBHelper::getDatabase()); InsertQuery q = t.insertInto("t_mind_score (project_id,score)"); NonQueryResult result = q.values(info->projectId, info->score).exec(); t.commit(); info->id = result.lastInsertId().toInt(); ret = true; } catch (const DBException &ex) { qDebug() << ex.lastError.text(); } return ret; }