GradeIndexInfoService.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "GradeIndexInfoService.h"
  2. #include "SqlDBHelper.h"
  3. #include <QDebug>
  4. GradeIndexInfoService::GradeIndexInfoService(QObject *parent) { }
  5. bool GradeIndexInfoService::QueryGradeIndexInfoByProjectId(GradeIndexInfo *gradeIndexInfo, int projectId)
  6. {
  7. QSqlDatabase db = SqlDBHelper::getDatabase();
  8. QSqlQuery query(db);
  9. bool ret = false;
  10. QString selectSql = QString("SELECT id, project_id,grade_name,grade_value FROM "
  11. "t_grade_index_info WHERE project_id = %1")
  12. .arg(projectId);
  13. if (query.exec(selectSql)) {
  14. if (query.next()) {
  15. if (query.isNull(0) == false) {
  16. gradeIndexInfo->id = query.value(0).toInt();
  17. gradeIndexInfo->projectId = query.value(1).toInt();
  18. gradeIndexInfo->gradeIndexName = query.value(2).toString();
  19. gradeIndexInfo->gradeIndexValue = query.value(3).toString();
  20. }
  21. ret = true;
  22. }
  23. } else {
  24. qDebug() << query.lastError();
  25. }
  26. return ret;
  27. }
  28. int GradeIndexInfoService::AddGradeIndexInfo(const GradeIndexInfo &gradeIndexInfo)
  29. {
  30. int ret = -1;
  31. try {
  32. Transaction t(SqlDBHelper::getDatabase());
  33. InsertQuery query = t.insertInto("t_grade_index_info(`project_id`, `grade_index_name`, `grade_index_value`)");
  34. NonQueryResult result =
  35. query.values(gradeIndexInfo.projectId, gradeIndexInfo.gradeIndexName, gradeIndexInfo.gradeIndexValue)
  36. .exec();
  37. t.commit();
  38. ret = result.lastInsertId().toInt();
  39. } catch (const DBException &ex) {
  40. qDebug() << ex.lastError.text();
  41. }
  42. return ret;
  43. }
  44. bool GradeIndexInfoService::UpdateGradeIndexInfo(const GradeIndexInfo &gradeIndexInfo)
  45. {
  46. bool ret = false;
  47. try {
  48. Transaction t(SqlDBHelper::getDatabase());
  49. t.update("t_grade_index_info")
  50. .set("grade_index_name", gradeIndexInfo.gradeIndexName)
  51. .set("grade_index_value", gradeIndexInfo.gradeIndexValue)
  52. .where("id = ?", gradeIndexInfo.id);
  53. t.commit();
  54. ret = true;
  55. } catch (const DBException &ex) {
  56. qDebug() << ex.lastError.text();
  57. }
  58. return ret;
  59. }