12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- #include "GradeIndexInfoService.h"
- #include "SqlDBHelper.h"
- #include <QDebug>
- GradeIndexInfoService::GradeIndexInfoService(QObject *parent) { }
- bool GradeIndexInfoService::QueryGradeIndexInfoByProjectId(QList<GradeIndexInfo *> *gradeIndexInfoList, int projectId)
- {
- QSqlDatabase db = SqlDBHelper::getDatabase();
- QSqlQuery query(db);
- bool ret = false;
- QString selectSql =
- QString("SELECT id, project_id,grade_index_name,grade_index_value,grade_parent_name,grade_level FROM "
- "t_grade_index_info WHERE project_id = %1")
- .arg(projectId);
- if (query.exec(selectSql)) {
- while (query.next()) {
- if (query.isNull(0) == false) {
- GradeIndexInfo *gradeIndexInfo = new GradeIndexInfo;
- gradeIndexInfo->id = query.value(0).toInt();
- gradeIndexInfo->projectId = query.value(1).toInt();
- gradeIndexInfo->gradeIndexName = query.value(2).toString();
- gradeIndexInfo->gradeIndexValue = query.value(3).toString();
- gradeIndexInfo->gradeParentName = query.value(4).toString();
- gradeIndexInfo->gradeLevel = query.value(5).toInt();
- gradeIndexInfoList->append(gradeIndexInfo);
- }
- }
- ret = true;
- } else {
- qDebug() << query.lastError();
- }
- return ret;
- }
- int GradeIndexInfoService::AddGradeIndexInfo(const GradeIndexInfo &gradeIndexInfo)
- {
- int ret = -1;
- try {
- Transaction t(SqlDBHelper::getDatabase());
- InsertQuery query = t.insertInto("t_grade_index_info(`project_id`, `grade_index_name`, `grade_index_value`, "
- "`grade_parent_name`, `grade_level`)");
- NonQueryResult result =
- query.values(gradeIndexInfo.projectId, gradeIndexInfo.gradeIndexName, gradeIndexInfo.gradeIndexValue,
- gradeIndexInfo.gradeParentName, gradeIndexInfo.gradeLevel)
- .exec();
- t.commit();
- ret = result.lastInsertId().toInt();
- } catch (const DBException &ex) {
- qDebug() << ex.lastError.text();
- }
- return ret;
- }
- bool GradeIndexInfoService::UpdateGradeIndexInfo(const GradeIndexInfo &gradeIndexInfo)
- {
- bool ret = false;
- try {
- Transaction t(SqlDBHelper::getDatabase());
- t.update("t_grade_index_info")
- .set("grade_index_name", gradeIndexInfo.gradeIndexName)
- .set("grade_index_value", gradeIndexInfo.gradeIndexValue)
- .set("grade_parent_name", gradeIndexInfo.gradeParentName)
- .set("grade_level", gradeIndexInfo.gradeLevel)
- .where("id = ?", gradeIndexInfo.id);
- t.commit();
- ret = true;
- } catch (const DBException &ex) {
- qDebug() << ex.lastError.text();
- }
- return ret;
- }
|