MindWeightService.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "MindWeightService.h"
  2. #include "ClassSet.h"
  3. #include "SqlDBHelper.h"
  4. #include <QDebug>
  5. MindWeightService::MindWeightService(QObject *parent) : QObject(parent) { }
  6. bool MindWeightService::saveUniqueWeightData(int projId, int indexType, int dataSource, int algorithm,
  7. const QString &weight)
  8. {
  9. MindWeightInfo info;
  10. bool qRet = queryWeightData(&info, projId, indexType);
  11. if (qRet == false) {
  12. return false;
  13. }
  14. if (info.id < 0) {
  15. info.projectId = projId;
  16. info.indexType = indexType;
  17. info.dataSource = dataSource;
  18. info.algorithm = algorithm;
  19. info.weight = weight;
  20. return saveWeightData(&info);
  21. } else {
  22. return updateWeightData(info.id, weight);
  23. }
  24. }
  25. bool MindWeightService::queryWeightData(MindWeightInfo *info, int projId, int indexType, int dataSource, int algorithm)
  26. {
  27. bool ret = false;
  28. try {
  29. Transaction t(SqlDBHelper::getDatabase());
  30. QString selectSql = QString("SELECT id, project_id, index_type, data_source, algorithm, weight "
  31. "from t_mind_weight WHERE project_id = %1 and index_type = %2 and "
  32. "data_source = %3 and algorithm = %4")
  33. .arg(projId)
  34. .arg(indexType)
  35. .arg(dataSource)
  36. .arg(algorithm);
  37. QueryResult queryResult = t.execQuery(selectSql);
  38. if (queryResult.next()) {
  39. info->id = queryResult.value(0).toInt();
  40. info->projectId = queryResult.value(1).toInt();
  41. info->indexType = queryResult.value(2).toInt();
  42. info->dataSource = queryResult.value(3).toInt();
  43. info->algorithm = queryResult.value(4).toInt();
  44. info->weight = queryResult.value(5).toString();
  45. }
  46. ret = true;
  47. } catch (const DBException &ex) {
  48. qDebug() << ex.lastError.text();
  49. }
  50. return ret;
  51. }
  52. bool MindWeightService::queryWeightData(MindWeightInfo *info, int projId, int indexType)
  53. {
  54. bool ret = false;
  55. try {
  56. Transaction t(SqlDBHelper::getDatabase());
  57. QString selectSql = QString("SELECT id, project_id, index_type, weight "
  58. "from t_mind_weight WHERE project_id = %1 and index_type = %2")
  59. .arg(projId)
  60. .arg(indexType);
  61. QueryResult queryResult = t.execQuery(selectSql);
  62. if (queryResult.next()) {
  63. info->id = queryResult.value(0).toInt();
  64. info->projectId = queryResult.value(1).toInt();
  65. info->indexType = queryResult.value(2).toInt();
  66. info->weight = queryResult.value(3).toString();
  67. }
  68. ret = true;
  69. } catch (const DBException &ex) {
  70. qDebug() << ex.lastError.text();
  71. }
  72. return ret;
  73. }
  74. bool MindWeightService::updateWeightData(int id, const QString &weight)
  75. {
  76. bool ret = false;
  77. try {
  78. Transaction t(SqlDBHelper::getDatabase());
  79. t.update("t_mind_weight").set("weight", weight).where("id = ?", id);
  80. t.commit();
  81. ret = true;
  82. } catch (const DBException &ex) {
  83. qDebug() << ex.lastError.text();
  84. }
  85. return ret;
  86. }
  87. bool MindWeightService::deleteWeightData(int id)
  88. {
  89. bool ret = false;
  90. try {
  91. Transaction t(SqlDBHelper::getDatabase());
  92. t.deleteFrom("t_mind_weight").where("id = ?", id);
  93. t.commit();
  94. ret = true;
  95. } catch (const DBException &ex) {
  96. qDebug() << ex.lastError.text();
  97. }
  98. return ret;
  99. }
  100. bool MindWeightService::saveWeightData(MindWeightInfo *info)
  101. {
  102. bool ret = false;
  103. try {
  104. Transaction t(SqlDBHelper::getDatabase());
  105. InsertQuery q = t.insertInto("t_mind_weight (project_id,index_type,data_source,algorithm,weight)");
  106. NonQueryResult result =
  107. q.values(info->projectId, info->indexType, info->dataSource, info->algorithm, info->weight).exec();
  108. t.commit();
  109. info->id = result.lastInsertId().toInt();
  110. ret = true;
  111. } catch (const DBException &ex) {
  112. qDebug() << ex.lastError.text();
  113. }
  114. return ret;
  115. }