Record.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 ? 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. }
  54. ColumnField *Record::GetColumnByName(const char *name)
  55. {
  56. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  57. {
  58. ColumnField *p = (ColumnField *)*itr;
  59. if (!strcasecmp(p->GetFieldName(), name))
  60. return p;
  61. }
  62. return NULL;
  63. }
  64. //---------------------------------------------------------------------------
  65. int Record::WriteFields(Table *ParentTable, int RecordIndex)
  66. {
  67. Field *previous = 0;
  68. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  69. {
  70. Field *p = *itr;
  71. p->WriteField(ParentTable, previous, (Field *)p->next);
  72. previous = p;
  73. }
  74. return WriteIndex(ParentTable, RecordIndex);
  75. }
  76. //---------------------------------------------------------------------------
  77. int Record::WriteIndex(Table *ParentTable, int RecordIndex)
  78. {
  79. int P=0;
  80. if (RecordIndex == NEW_RECORD)
  81. RecordIndex = ParentTable->index->Insert(InsertionPoint);
  82. if (!Fields.empty())
  83. {
  84. Field *f = *Fields.begin();
  85. P=f->GetFieldPos();
  86. }
  87. return ParentTable->index->Update(RecordIndex, P, this, FALSE);
  88. }
  89. //---------------------------------------------------------------------------
  90. void Record::Delete(Table *ParentTable, int RecordIndex)
  91. {
  92. ParentTable->index->Delete(RecordIndex, ParentTable->index->Get(RecordIndex), this);
  93. }
  94. void Record::WalkFields(FieldsWalker callback, void *context)
  95. {
  96. if (callback)
  97. {
  98. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  99. {
  100. if (!callback(this, *itr, context))
  101. break;
  102. }
  103. }
  104. }