Table.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* ---------------------------------------------------------------------------
  2. Nullsoft Database Engine
  3. --------------------
  4. codename: Near Death Experience
  5. --------------------------------------------------------------------------- */
  6. /* ---------------------------------------------------------------------------
  7. Table Class Prototypes
  8. Windows implementation
  9. --------------------------------------------------------------------------- */
  10. #ifndef __TABLE_H
  11. #define __TABLE_H
  12. #include <stdio.h>
  13. //#include <io.h>
  14. #include "../Scanner.h"
  15. #include <map>
  16. #include "../IndexRecord.h"
  17. #include <assert.h>
  18. class Table : private Scanner
  19. {
  20. public:
  21. // TODO: move these back to protected
  22. VFILE *Handle = NULL;
  23. using Scanner::index;
  24. bool use_row_cache = false;
  25. BOOL GLocateUpToDate = FALSE;
  26. private:
  27. void Init();
  28. void Reset();
  29. private:
  30. LinkedList *Scanners;
  31. protected:
  32. wchar_t *Name;
  33. wchar_t *IdxName;
  34. VFILE *IdxHandle = NULL;
  35. BOOL AutoCreate;
  36. Record *FieldsRecord = NULL;
  37. IndexRecord *IndexList = NULL;
  38. Database *db;
  39. BOOL Cached;
  40. int numErrors = 0;
  41. using Scanner::Edition;
  42. bool columns_cached = false;
  43. unsigned char column_ids[256];
  44. typedef std::map<int, Record*> RowCache;
  45. RowCache rowCache;
  46. // Tables
  47. static bool Compact_ColumnWalk(Record *record, Field *entry, void *context_in);
  48. static bool Compact_ColumnWalk2(Record *record, Field *entry, void *context_in);
  49. static bool Compact_IndexWalk(Table *table, IndexField *entry, void *context);
  50. static bool IndexWriteWalker(IndexRecord *record, Field *entry, void *context);
  51. static bool IndexWalkerThunk(IndexRecord *record, Field *entry, void *context);
  52. static bool IndexNewWalker(IndexRecord *record, Field *entry, void *context);
  53. static bool BuildColumnCache(Record *record, Field *entry, void *context);
  54. public:
  55. typedef bool (*IndexWalker)(Table *table, IndexField *entry, void *context);
  56. Table(const wchar_t *TableName, const wchar_t *IdxName, BOOL Create, Database *db, BOOL Cached);
  57. ~Table();
  58. BOOL Open(void);
  59. void Close(void);
  60. // Columns
  61. ColumnField *NewColumn(unsigned char Id, const wchar_t *name, unsigned char type, BOOL indexUniques);
  62. void DeleteColumn(ColumnField *field); // todo
  63. void DeleteColumnByName(const wchar_t *name); // todo
  64. void DeleteColumnById(unsigned char Id); // todo
  65. void PostColumns(void);
  66. NDE_API Record *GetColumns(void);
  67. ColumnField *GetColumnByName(const wchar_t *FieldName);
  68. ColumnField *GetColumnById(unsigned char Idx);
  69. unsigned char GetColumnType(unsigned char Id);
  70. // Fields
  71. using Scanner::NewFieldByName;
  72. using Scanner::NewFieldById;
  73. using Scanner::GetFieldByName;
  74. using Scanner::GetFieldById;
  75. using Scanner::DeleteField;
  76. using Scanner::DeleteFieldByName;
  77. using Scanner::DeleteFieldById;
  78. // Records
  79. using Scanner::First;
  80. using Scanner::Last;
  81. using Scanner::Next;
  82. using Scanner::Previous;
  83. using Scanner::Eof;
  84. using Scanner::Bof;
  85. using Scanner::New;
  86. using Scanner::Insert;
  87. using Scanner::Edit;
  88. using Scanner::Cancel;
  89. using Scanner::Post;
  90. using Scanner::Delete;
  91. using Scanner::GetRecordsCount;
  92. using Scanner::GetRecordById;
  93. using Scanner::GetRecordId;
  94. void Sync(void);
  95. using Scanner::LocateByName;
  96. using Scanner::LocateById;
  97. BOOL LocateByIdEx(int Id, int From, Field *field, int comp_mode);
  98. // Indexes
  99. void AddIndexByName(const wchar_t *FieldName, const wchar_t *KeyName);
  100. void AddIndexById(unsigned char Id, const wchar_t *KeyName);
  101. void WalkIndices(IndexWalker callback, void *context);
  102. IndexField *GetIndexByName(const wchar_t *name);
  103. IndexField *GetIndexById(unsigned char Id);
  104. using Scanner::SetWorkingIndexByName;
  105. using Scanner::SetWorkingIndexById;
  106. NDE_API BOOL CheckIndexing(void);
  107. void DropIndexByName(const wchar_t *desc);
  108. void DropIndexById(unsigned char Id);
  109. void DropIndex(IndexField *Ptr);
  110. void IndexModified(void);
  111. // Filters
  112. using Scanner::AddFilterByName;
  113. using Scanner::AddFilterById;
  114. using Scanner::AddFilterOp;
  115. using Scanner::RemoveFilters;
  116. // Scanners
  117. Scanner *NewScanner();
  118. Scanner *GetDefaultScanner();
  119. void DeleteScanner(Scanner *scan);
  120. // Misc
  121. using Scanner::FragmentationLevel;
  122. void Compact(int *progress = NULL);
  123. void SetGlobalLocateUpToDate(BOOL is);
  124. // Row Cache
  125. void RowCache_Delete(int position);
  126. void RowCache_Remove(int position);
  127. void RowCache_Add(Record *record, int position);
  128. Record *RowCache_Get(int position);
  129. NDE_API void EnableRowCache();
  130. // Searching
  131. void SetFieldSearchableById(unsigned char field_id, bool searchable);
  132. int HasErrors()
  133. {
  134. return numErrors > 0;
  135. }
  136. int NumErrors()
  137. {
  138. return numErrors;
  139. }
  140. void IncErrorCount()
  141. {
  142. numErrors++;
  143. }
  144. };
  145. #endif