ScaleInfoService.cpp 2.2 KB

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