fortune.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include <TreeFrogModel>
  2. #include "fortune.h"
  3. #include "fortuneobject.h"
  4. Fortune::Fortune()
  5. : TAbstractModel(), d(new FortuneObject)
  6. {
  7. d->id = 0;
  8. }
  9. Fortune::Fortune(const Fortune &other)
  10. : TAbstractModel(), d(other.d)
  11. { }
  12. Fortune::Fortune(const FortuneObject &object)
  13. : TAbstractModel(), d(new FortuneObject(object))
  14. { }
  15. Fortune::~Fortune()
  16. {
  17. // If the reference count becomes 0,
  18. // the shared data object 'FortuneObject' is deleted.
  19. }
  20. uint Fortune::id() const
  21. {
  22. return d->id;
  23. }
  24. QString Fortune::message() const
  25. {
  26. return d->message;
  27. }
  28. void Fortune::setMessage(const QString &message)
  29. {
  30. d->message = message;
  31. }
  32. Fortune &Fortune::operator=(const Fortune &other)
  33. {
  34. d = other.d; // increments the reference count of the data
  35. return *this;
  36. }
  37. Fortune Fortune::create(const QString &message)
  38. {
  39. FortuneObject obj;
  40. obj.message = message;
  41. if (!obj.create()) {
  42. return Fortune();
  43. }
  44. return Fortune(obj);
  45. }
  46. Fortune Fortune::create(const QVariantMap &values)
  47. {
  48. Fortune model;
  49. model.setProperties(values);
  50. if (!model.d->create()) {
  51. model.d->clear();
  52. }
  53. return model;
  54. }
  55. Fortune Fortune::get(uint id)
  56. {
  57. TSqlORMapper<FortuneObject> mapper;
  58. return Fortune(mapper.findByPrimaryKey(id));
  59. }
  60. int Fortune::count()
  61. {
  62. TSqlORMapper<FortuneObject> mapper;
  63. return mapper.findCount();
  64. }
  65. QList<Fortune> Fortune::getAll()
  66. {
  67. TSqlQueryORMapper<FortuneObject> mapper;
  68. mapper.prepare(QStringLiteral("SELECT * from fortune"));
  69. QList<Fortune> fortunes;
  70. if (mapper.exec()) {
  71. while (mapper.next()) {
  72. fortunes << Fortune(mapper.value());
  73. }
  74. }
  75. return fortunes;
  76. }
  77. TModelObject *Fortune::modelData()
  78. {
  79. return d.data();
  80. }
  81. const TModelObject *Fortune::modelData() const
  82. {
  83. return d.data();
  84. }