1
0

Record.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. int n=0;
  33. if (RecordPos != 0)
  34. {
  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. #ifdef _WIN32
  55. ColumnField *Record::GetColumnByName(const wchar_t *name)
  56. {
  57. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  58. {
  59. ColumnField *p = (ColumnField *)*itr;
  60. if (!_wcsicmp(p->GetFieldName(), name))
  61. return p;
  62. }
  63. return NULL;
  64. }
  65. #elif defined(__APPLE__)
  66. ColumnField *Record::GetColumnByName(CFStringRef name)
  67. {
  68. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  69. {
  70. ColumnField *p = (ColumnField *)*itr;
  71. if (CFStringCompare(p->GetFieldName(), name, kCFCompareCaseInsensitive) == kCFCompareEqualTo)
  72. return p;
  73. }
  74. return NULL;
  75. }
  76. #endif
  77. //---------------------------------------------------------------------------
  78. int Record::WriteFields(Table *ParentTable, int RecordIndex)
  79. {
  80. Field *previous = 0;
  81. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  82. {
  83. Field *p = *itr;
  84. p->WriteField(ParentTable, previous, (Field *)p->next);
  85. previous = p;
  86. }
  87. return WriteIndex(ParentTable, RecordIndex);
  88. }
  89. //---------------------------------------------------------------------------
  90. int Record::WriteIndex(Table *ParentTable, int RecordIndex)
  91. {
  92. int P=0;
  93. if (RecordIndex == NEW_RECORD)
  94. RecordIndex = ParentTable->index->Insert(InsertionPoint);
  95. if (!Fields.empty())
  96. {
  97. Field *f = *Fields.begin();
  98. P=f->GetFieldPos();
  99. }
  100. return ParentTable->index->Update(RecordIndex, P, this, FALSE);
  101. }
  102. //---------------------------------------------------------------------------
  103. void Record::Delete(Table *ParentTable, int RecordIndex)
  104. {
  105. ParentTable->index->Delete(RecordIndex, ParentTable->index->Get(RecordIndex), this);
  106. }
  107. void Record::WalkFields(FieldsWalker callback, void *context)
  108. {
  109. if (callback)
  110. {
  111. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  112. {
  113. if (!callback(this, *itr, context))
  114. break;
  115. }
  116. }
  117. }