aboutsummaryrefslogtreecommitdiff
path: root/Src/nde/win/Table.h
blob: 02dc699088b1eb048d2016bcc0237386d3fabf1c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* ---------------------------------------------------------------------------
                           Nullsoft Database Engine
                             --------------------
                        codename: Near Death Experience
--------------------------------------------------------------------------- */

/* ---------------------------------------------------------------------------

 Table Class Prototypes
 Windows implementation
--------------------------------------------------------------------------- */

#ifndef __TABLE_H
#define __TABLE_H

#include <stdio.h>
//#include <io.h>
#include "../Scanner.h"
#include <map>
#include "../IndexRecord.h"
#include <assert.h>

class Table : private Scanner
{
public:
	// TODO: move these back to protected
	VFILE *Handle = NULL;
	using Scanner::index;
	bool use_row_cache   = false;
	BOOL GLocateUpToDate = FALSE;

private:
	void Init();
	void Reset();

private:
	LinkedList *Scanners;

protected:
	wchar_t *Name;
	wchar_t *IdxName;

	VFILE        *IdxHandle = NULL;
	BOOL          AutoCreate;
	Record       *FieldsRecord = NULL;
	IndexRecord  *IndexList = NULL;
	Database     *db;
	BOOL          Cached;
	int           numErrors = 0;
	using Scanner::Edition;
	bool          columns_cached = false;
	unsigned char column_ids[256];
	typedef std::map<int, Record*> RowCache;
	RowCache rowCache;

	// Tables
	static bool Compact_ColumnWalk(Record *record, Field *entry, void *context_in);
	static bool Compact_ColumnWalk2(Record *record, Field *entry, void *context_in);
	static bool Compact_IndexWalk(Table *table, IndexField *entry, void *context);
	static bool IndexWriteWalker(IndexRecord *record, Field *entry, void *context);
	static bool IndexWalkerThunk(IndexRecord *record, Field *entry, void *context);
	static bool IndexNewWalker(IndexRecord *record, Field *entry, void *context);
	static bool BuildColumnCache(Record *record, Field *entry, void *context);

public:
	typedef bool (*IndexWalker)(Table *table, IndexField *entry, void *context);
	Table(const wchar_t *TableName, const wchar_t *IdxName, BOOL Create, Database *db, BOOL Cached);
	~Table();
	BOOL Open(void);
	void Close(void);

	// Columns
	ColumnField *NewColumn(unsigned char Id, const wchar_t *name, unsigned char type, BOOL indexUniques);

	void DeleteColumn(ColumnField *field); // todo
	void DeleteColumnByName(const wchar_t *name); // todo
	void DeleteColumnById(unsigned char Id); // todo
	void PostColumns(void);
	NDE_API Record *GetColumns(void);
	ColumnField *GetColumnByName(const wchar_t *FieldName);
	ColumnField *GetColumnById(unsigned char Idx);
	unsigned char GetColumnType(unsigned char Id);

	// Fields
	using Scanner::NewFieldByName;
	using Scanner::NewFieldById;
	using Scanner::GetFieldByName;
	using Scanner::GetFieldById;
	using Scanner::DeleteField;
	using Scanner::DeleteFieldByName;
	using Scanner::DeleteFieldById;

	// Records
	using Scanner::First;
	using Scanner::Last;
	using Scanner::Next;
	using Scanner::Previous;
	using Scanner::Eof;
	using Scanner::Bof;
	using Scanner::New;
	using Scanner::Insert;
	using Scanner::Edit;
	using Scanner::Cancel;
	using Scanner::Post;
	using Scanner::Delete;
	using Scanner::GetRecordsCount;
	using Scanner::GetRecordById;
	using Scanner::GetRecordId;
	void Sync(void);
	using Scanner::LocateByName;
	using Scanner::LocateById;
	BOOL LocateByIdEx(int Id, int From, Field *field, int comp_mode);

	// Indexes
	void AddIndexByName(const wchar_t *FieldName, const wchar_t *KeyName);
	void AddIndexById(unsigned char Id, const wchar_t *KeyName);

	void WalkIndices(IndexWalker callback, void *context);

	IndexField *GetIndexByName(const wchar_t *name);
	IndexField *GetIndexById(unsigned char Id);
	using Scanner::SetWorkingIndexByName;
	using Scanner::SetWorkingIndexById;
	NDE_API BOOL CheckIndexing(void);
	void DropIndexByName(const wchar_t *desc);
	void DropIndexById(unsigned char Id);
	void DropIndex(IndexField *Ptr);
	void IndexModified(void);

	// Filters
	using Scanner::AddFilterByName;
	using Scanner::AddFilterById;
	using Scanner::AddFilterOp;
	using Scanner::RemoveFilters;

	// Scanners
	Scanner *NewScanner();
	Scanner *GetDefaultScanner();
	void DeleteScanner(Scanner *scan);

	// Misc
	using Scanner::FragmentationLevel;
	void Compact(int *progress = NULL);
	void SetGlobalLocateUpToDate(BOOL is);

	// Row Cache
	void RowCache_Delete(int position);
	void RowCache_Remove(int position);
	void RowCache_Add(Record *record, int position);
	Record *RowCache_Get(int position);
	NDE_API void EnableRowCache();

	// Searching
	void SetFieldSearchableById(unsigned char field_id, bool searchable);

	int HasErrors()
	{
		return numErrors > 0;
	}
	int NumErrors()
	{
		return numErrors;
	}
	void IncErrorCount()
	{
		numErrors++;
	}
};

#endif