1
0

IndexRecord.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* ---------------------------------------------------------------------------
  2. Nullsoft Database Engine
  3. --------------------
  4. codename: Near Death Experience
  5. --------------------------------------------------------------------------- */
  6. /* ---------------------------------------------------------------------------
  7. IndexRecord Class
  8. --------------------------------------------------------------------------- */
  9. #include "nde.h"
  10. #include <stdio.h>
  11. IndexRecord::IndexRecord(int RecordPos, int insertionPoint, VFILE *TableHandle, Table *ParentTable)
  12. {
  13. InsertionPoint = insertionPoint;
  14. if (RecordPos != 0)
  15. {
  16. int n=0;
  17. uint32_t ThisPos = RecordPos;
  18. while (ThisPos)
  19. {
  20. if (n >= 128)
  21. break;
  22. Vfseek(TableHandle, ThisPos, SEEK_SET);
  23. Field Entry (ThisPos);
  24. Field *TypedEntry = Entry.ReadField(ParentTable, ThisPos, &ThisPos);
  25. if (!TypedEntry) break; // some db error?
  26. AddField(TypedEntry);
  27. // ThisPos = TypedEntry->GetNextFieldPos();
  28. n++;
  29. }
  30. }
  31. }
  32. void IndexRecord::BuildCollaboration()
  33. {
  34. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  35. {
  36. IndexField *p = (IndexField *)*itr;
  37. if (p->next)
  38. p->index->Colaborate((IndexField *)p->next);
  39. else
  40. p->index->Colaborate((IndexField *)*Fields.begin());
  41. }
  42. }
  43. bool IndexRecord::NeedFix()
  44. {
  45. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  46. {
  47. IndexField *p = (IndexField *)*itr;
  48. if (p->index->NeedFix())
  49. return true;
  50. }
  51. return false;
  52. }
  53. #ifdef _WIN32
  54. IndexField *IndexRecord::GetIndexByName(const wchar_t *name)
  55. {
  56. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  57. {
  58. IndexField *p = (IndexField *)*itr;
  59. if (!_wcsicmp(p->GetIndexName(), name))
  60. return p;
  61. }
  62. return NULL;
  63. }
  64. #else
  65. IndexField *IndexRecord::GetIndexByName(CFStringRef name)
  66. {
  67. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  68. {
  69. IndexField *p = (IndexField *)*itr;
  70. if (CFStringCompare(p->GetIndexName(), name, kCFCompareCaseInsensitive) == kCFCompareEqualTo)
  71. return p;
  72. }
  73. return NULL;
  74. }
  75. #endif
  76. bool IndexRecord::CheckIndexing(int v)
  77. {
  78. int i = v;
  79. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  80. {
  81. IndexField *p = (IndexField *)*itr;
  82. v = p->index->GetCooperative(v);
  83. }
  84. return v == i;
  85. }
  86. void IndexRecord::SetModified(BOOL modifiedVal)
  87. {
  88. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  89. {
  90. IndexField *p = (IndexField *)*itr;
  91. p->index->Modified = modifiedVal;
  92. }
  93. }
  94. Field *RecordBase::GetField(unsigned char ID)
  95. {
  96. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  97. {
  98. IndexField *p = (IndexField *)*itr;
  99. if (p->GetFieldId() == ID)
  100. return p;
  101. }
  102. return NULL;
  103. }
  104. void RecordBase::AddField(Field *field)
  105. {
  106. if (!field) return;
  107. if (GetField(field->ID))
  108. return;
  109. Fields.push_back(field);
  110. }
  111. int IndexRecord::WriteFields(Table *ParentTable)
  112. {
  113. Field *previous = 0;
  114. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  115. {
  116. IndexField *p = (IndexField *)*itr;
  117. p->WriteField(ParentTable, previous, (Field *)p->next);
  118. previous = p;
  119. }
  120. return WriteIndex(ParentTable);
  121. }
  122. int IndexRecord::WriteIndex(Table *ParentTable)
  123. {
  124. int P=0;
  125. if (!Fields.empty())
  126. P=(*Fields.begin())->GetFieldPos();
  127. return ParentTable->index->Update(INDEX_RECORD_NUM, P, this, FALSE);
  128. }
  129. void RecordBase::RemoveField(Field *field)
  130. {
  131. if (!field)
  132. return;
  133. Fields.erase(field);
  134. delete field;
  135. }
  136. void IndexRecord::WalkFields(FieldsWalker callback, void *context)
  137. {
  138. if (callback)
  139. {
  140. for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
  141. {
  142. if (!callback(this, *itr, context))
  143. break;
  144. }
  145. }
  146. }