aboutsummaryrefslogtreecommitdiff
path: root/Src/nde/win/IndexRecord.cpp
blob: 06405ba14ac95ddd1e001c748111fdd70776ff35 (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
/* ---------------------------------------------------------------------------
Nullsoft Database Engine
--------------------
codename: Near Death Experience
--------------------------------------------------------------------------- */

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

IndexRecord Class

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

#include "../nde.h"
#include <stdio.h>

IndexRecord::IndexRecord(int RecordPos, int insertionPoint, VFILE *TableHandle, Table *ParentTable)
{
	InsertionPoint = insertionPoint;
	if (RecordPos != 0)
	{
		int n=0;
		uint32_t ThisPos = RecordPos;
		while (ThisPos)
		{
			if (n >= 128)
				break;
			Vfseek(TableHandle, ThisPos, SEEK_SET);
			Field Entry (ThisPos);
			Field *TypedEntry = Entry.ReadField(ParentTable, ThisPos, &ThisPos);

			if (!TypedEntry) break; // some db error?

			AddField(TypedEntry);
			//			ThisPos = TypedEntry->GetNextFieldPos();
			n++;
		}
	}
}

void IndexRecord::BuildCollaboration()
{
	for (FieldList::iterator itr = Fields.begin(); itr != Fields.end(); itr++)
	{
		IndexField *p = (IndexField *)(* itr);
		if ((itr + 1) != Fields.end())
			p->index->Colaborate((IndexField *)(*(itr+1)));
		else
			p->index->Colaborate((IndexField *)*(Fields.begin()));
	}
}

bool IndexRecord::NeedFix()
{
	for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
	{
		IndexField *p = (IndexField *)*itr;
		if (p->index->NeedFix())
			return true;
	}
	return false;
}

IndexField *IndexRecord::GetIndexByName(const wchar_t *name)
{
	for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
	{
		IndexField *p = (IndexField *)*itr;
		if (!_wcsicmp(p->GetIndexName(), name))
			return p;
	}
	return NULL;
}

bool IndexRecord::CheckIndexing(int v)
{
	int i = v;
	for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
	{
		IndexField *p = (IndexField *)*itr;
		v = p->index->GetCooperative(v);
	}
	return v == i;
}

Field *RecordBase::GetField( unsigned char ID )
{
	for ( FieldList::iterator itr = Fields.begin(); itr != Fields.end(); itr++ )
	{
		IndexField *p = (IndexField *)*itr;
		if ( p->GetFieldId() == ID )
			return p;
	}

	return NULL;
}

void RecordBase::AddField(Field *field)
{
	if (!field)
		return;

	if (GetField(field->ID))
		return;

	Fields.push_back(field);
}

int IndexRecord::WriteFields( Table *ParentTable )
{
	Field      *l_previous_field = NULL;
	IndexField *l_index_field    = NULL;

	for ( FieldList::iterator itr = Fields.begin(); itr != Fields.end(); itr++ )
	{
		l_index_field = (IndexField *)(* itr);

		Field* nextField = (itr + 1) != Fields.end() ? *(itr + 1) : nullptr;

		//l_index_field->WriteField(ParentTable, l_previous_field, (Field*)l_index_field->next);
		l_index_field->WriteField(ParentTable, l_previous_field, nextField);

		l_previous_field = l_index_field;
	}

	return WriteIndex( ParentTable );
}


int IndexRecord::WriteIndex( Table *ParentTable )
{
	int P = 0;

	if ( !Fields.empty() )
		P = ( *Fields.begin() )->GetFieldPos();

	return ParentTable->index->Update( INDEX_RECORD_NUM, P, this, FALSE );
}

void RecordBase::RemoveField(Field *field)
{
	if (!field)
		return;

	//Fields.erase(field);
	//delete field;

	for (auto it = Fields.begin(); it != Fields.end(); it++)
	{
		Field* f = *it;
		if (f == field)
		{
			Fields.erase(it);
			delete f;
			break;
		}
	}
}

void IndexRecord::WalkFields(FieldsWalker callback, void *context)
{
	if (callback)
	{
		for (FieldList::iterator itr=Fields.begin();itr!=Fields.end();itr++)
		{
			if (!callback(this, *itr, context))
				break;
		}
	}
}