1
0

Record.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* ---------------------------------------------------------------------------
  2. Nullsoft Database Engine
  3. --------------------
  4. codename: Near Death Experience
  5. --------------------------------------------------------------------------- */
  6. /* ---------------------------------------------------------------------------
  7. Record Class
  8. --------------------------------------------------------------------------- */
  9. //#include "record.h"
  10. #include "../nde.h"
  11. #include <stdio.h>
  12. void RecordBase::Retain()
  13. {
  14. ref_count++;
  15. }
  16. void RecordBase::Release()
  17. {
  18. if (--ref_count == 0)
  19. delete this;
  20. }
  21. RecordBase::RecordBase()
  22. {
  23. ref_count = 1;
  24. InsertionPoint = 0;
  25. }
  26. //---------------------------------------------------------------------------
  27. Record::Record(int RecordPos, int insertionPoint, VFILE *TableHandle, Table *ParentTable)
  28. {
  29. InsertionPoint = insertionPoint;
  30. Record *columns = ParentTable->GetColumns();
  31. int max=columns ? (int)columns->Fields.size() : 128;
  32. if (RecordPos != 0)
  33. {
  34. int n=0;
  35. uint32_t ThisPos = RecordPos;
  36. while (ThisPos)
  37. {
  38. if (n >= max)
  39. break;
  40. Vfseek(TableHandle, ThisPos, SEEK_SET);
  41. Field Entry (ThisPos);
  42. Field *TypedEntry = Entry.ReadField(ParentTable, ThisPos, &ThisPos);
  43. if (!TypedEntry) break; // some db error?
  44. AddField(TypedEntry);
  45. n++;
  46. }
  47. }
  48. }
  49. //---------------------------------------------------------------------------
  50. RecordBase::~RecordBase()
  51. {
  52. //Fields.deleteAll();
  53. for (Field* field : Fields)
  54. {
  55. if (field)
  56. {
  57. delete field;
  58. }
  59. }
  60. Fields.clear();
  61. }
  62. ColumnField *Record::GetColumnByName(const wchar_t *name)
  63. {
  64. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  65. {
  66. ColumnField *p = (ColumnField *)*itr;
  67. if (!_wcsicmp(p->GetFieldName(), name))
  68. return p;
  69. }
  70. return NULL;
  71. }
  72. //---------------------------------------------------------------------------
  73. int Record::WriteFields(Table *ParentTable, int RecordIndex)
  74. {
  75. Field *previous = 0;
  76. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  77. {
  78. Field *p = *itr;
  79. Field* nextField = (itr + 1) != Fields.end() ? *(itr + 1) : nullptr;
  80. //p->WriteField(ParentTable, previous, (Field*)p->next);
  81. p->WriteField(ParentTable, previous, nextField);
  82. previous = p;
  83. }
  84. return WriteIndex(ParentTable, RecordIndex);
  85. }
  86. //---------------------------------------------------------------------------
  87. int Record::WriteIndex(Table *ParentTable, int RecordIndex)
  88. {
  89. int P=0;
  90. if (RecordIndex == NEW_RECORD)
  91. RecordIndex = ParentTable->index->Insert(InsertionPoint);
  92. if (!Fields.empty())
  93. {
  94. Field *f = *Fields.begin();
  95. P=f->GetFieldPos();
  96. }
  97. return ParentTable->index->Update(RecordIndex, P, this, FALSE);
  98. }
  99. //---------------------------------------------------------------------------
  100. void Record::Delete(Table *ParentTable, int RecordIndex)
  101. {
  102. ParentTable->index->Delete(RecordIndex, ParentTable->index->Get(RecordIndex), this);
  103. }
  104. void Record::WalkFields(FieldsWalker callback, void *context)
  105. {
  106. if (callback)
  107. {
  108. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  109. {
  110. if (!callback(this, *itr, context))
  111. break;
  112. }
  113. }
  114. }