#include "ScaleInfoService.h" #include "SqlDBHelper.h" #include ScaleInfoService::ScaleInfoService(QObject *parent) { } int ScaleInfoService::AddScaleInfo(const ScaleInfo &scaleInfo) { int ret = -1; try { Transaction t(SqlDBHelper::getDatabase()); InsertQuery query = t.insertInto("t_scale_info(`project_id`, `scale_name`, `scale_value`)"); NonQueryResult result = query.values(scaleInfo.projectId, scaleInfo.scaleName, scaleInfo.scaleValue).exec(); qDebug() << result.lastQuery(); t.commit(); ret = result.lastInsertId().toInt(); } catch (const DBException &ex) { qDebug() << ex.lastError.text(); } return ret; } bool ScaleInfoService::UpdateScaleInfo(const ScaleInfo &scaleInfo) { bool ret = false; try { Transaction t(SqlDBHelper::getDatabase()); t.update("t_scale_info") .set("scale_name", scaleInfo.scaleName) .set("scale_value", scaleInfo.scaleValue) .where("id = ?", scaleInfo.id); t.commit(); ret = true; } catch (const DBException &ex) { qDebug() << ex.lastError.text(); } return ret; } bool ScaleInfoService::QueryScaleByProjectId(QList *scaleInfoList, int projectId) { QSqlDatabase db = SqlDBHelper::getDatabase(); QSqlQuery query(db); bool ret = false; QString selectSql = QString("SELECT id, project_id,scale_name,scale_value FROM " "t_scale_info WHERE project_id = %1") .arg(projectId); if (query.exec(selectSql)) { while (query.next()) { if (query.isNull(0) == false) { ScaleInfo *scaleInfo = new ScaleInfo(); scaleInfo->id = query.value(0).toInt(); scaleInfo->projectId = query.value(1).toInt(); scaleInfo->scaleName = query.value(2).toString(); scaleInfo->scaleValue = query.value(3).toString(); scaleInfoList->append(scaleInfo); } } ret = true; } else { qDebug() << query.lastError(); } return ret; }