QxModelBase.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #pragma once
  2. #include "QxModelView.h"
  3. #include "QxOrm.h"
  4. #include "OrmModelBase.h"
  5. template<typename T>
  6. class QxModelBase : public qx::QxModel<T, OrmModelBase>
  7. {
  8. public :
  9. QxModelBase(QObject * parent = nullptr)
  10. :qx::QxModel<T, OrmModelBase>(parent),
  11. isAutoInsert(false){};
  12. virtual ~QxModelBase(){ ; };
  13. void setAutoInsert(bool isAutoInsert = true){ this->isAutoInsert = isAutoInsert; }
  14. protected:
  15. bool isAutoInsert;
  16. public:
  17. virtual QSqlError qxSaveRowData(int row, const QStringList & column = QStringList(), QSqlDatabase * pDatabase = nullptr)
  18. {
  19. if (!this->m_pDataMemberId) { this->m_lastError = QSqlError("[QxOrm] problem with 'qxSaveRowData()' method : 'data member id not registered'", "", QSqlError::UnknownError); return this->m_lastError; }
  20. type_ptr pItem = getRowItemAt(row); if (!pItem) { return QSqlError(); }
  21. QVariant id = this->m_pDataMemberId->toVariant(pItem.get());
  22. bool bExist = qx::trait::is_valid_primary_key(id);
  23. if (bExist) { bExist = qx::dao::exist((*pItem), this->database(pDatabase)); }
  24. if (bExist) { this->m_lastError = qx::dao::update((*pItem), this->database(pDatabase), column); }
  25. else if (isAutoInsert){ this->m_lastError = qx::dao::insert((*pItem), this->database(pDatabase)); }
  26. return this->m_lastError;
  27. }
  28. virtual void insertDirtyRowToModel()
  29. {
  30. qx::QxModel<T, OrmModelBase>::insertDirtyRowToModel();
  31. }
  32. void countWithRelation(const qx::QxSqlQuery &query, const QStringList &relation)
  33. {
  34. qx::dao::count_with_relation<T>(totalCount, relation, query);
  35. }
  36. virtual QSqlError qxFetchRow(int row, const QStringList & relation, QSqlDatabase * pDatabase)
  37. {
  38. using namespace qx;
  39. type_ptr pItem = getRowItemAt(row); if (!pItem) { return QSqlError(); }
  40. if (relation.count() == 0)
  41. {
  42. this->m_lastError = qx::dao::fetch_by_id((*pItem), this->database(pDatabase), this->m_lstColumns);
  43. }
  44. else
  45. {
  46. QxSqlQuery query;
  47. query.where("t." + m_pDataMemberId->getKey()).isEqualTo(m_pDataMemberId->toVariant(pItem.get()));
  48. QStringList relations;
  49. relations << "<t>";
  50. QString placeholder[] = { "b", "c", "d", "e", "f", "g", "h" };
  51. int i = 0;
  52. for (auto dataMember : m_lstDataMember)
  53. {
  54. if (dataMember->hasSqlRelation())
  55. {
  56. relations << dataMember->getKey() + "<" + placeholder[i] + ">";
  57. i++;
  58. }
  59. }
  60. this->m_lastError = qx::dao::fetch_by_query_with_relation(relations, query, (*pItem), this->database(pDatabase));
  61. }
  62. if (this->m_lastError.isValid())
  63. {
  64. return this->m_lastError;
  65. }
  66. QModelIndex idxTopLeft = this->index(row, 0);
  67. QModelIndex idxBottomRight = this->index(row, (this->m_lstDataMember.count() - 1));
  68. this->raiseEvent_dataChanged(idxTopLeft, idxBottomRight);
  69. updateKey(row);
  70. return this->m_lastError;
  71. }
  72. std::shared_ptr<T> getByRow(int row) const
  73. {
  74. return m_model.getByIndex(row);
  75. }
  76. };