Using the Client Dataset in File-Based Architecture - Adding IndexDefs
(Page 3 of 4 )
Indexes are useful for not only sorting and grouping data but searching for records and setting up relationship with other datasets as well. When working with the client dataset, indexes can be created at both design time and runtime, but before the dataset has been created. The process is fairly similar to creating field definitions except for the fact that, unlike field definitions, index definitions are optional.
When the client dataset represents server data it inherits the default index of the records, which can be used to order the records in the dataset. But it does not allow you to modify it; instead, the index of the modified data is maintained in the ChangeLog or Delta as CHANGEINDEX. This changed index too is maintained by the client dataset and does not allow you to change or modify it.
To work with an editable index you can create one using the IndexFieldName property. Specify the name of the fields on which you want to add an index to the IndexFieldNames property of the client dataset. Multiple field names can be specified by separating them with semicolons and specifying them in the order that you would want the index to be created with. This method can be used to create a temporary index during runtime and is useful only if you want to create an index for sorting only. It does not support grouping and cannot sort records in descending order.
To create an index during runtime that supports grouping you can use the AddIndex method. This method gives you a lot more flexibility, allowing you to specify the name of the index, the fields that make it up, the default level of grouping support and how the indexes are sorted. You can sort them case-insensitively or in descending order, neither of which is allowed when you create an index using the IndexFieldName property. Indexes created using this method are not persistent and are lost as soon as the dataset is closed.
To work with indexes that persist and are saved, you can use the IndexDefs property. You can create persistent indexes at design time by using the Object Inspector. Select the client dataset component and in the Object Inspector click on the ellipses button of the IndexDefs property. This brings up the Collection editor which allows you to add, modify or remove properties of field definitions visually. To add index definitions at runtime call the AddIndexDef method. This method adds a new IndexDef and requires you to specify the name, type, and options of the index that will be created.
CDS1.IndexDefs.Clear;
with CDS1.IndexDefs.AddIndexDef do
begin
Name := 'fnIndex';
Fields := 'fName';
Options := [ixPrimary];
end;
end;
As is evident from the code above, for every index definition added you also need to specify the attributes of the index by specifying it in the property of the IndexDefs object. It should be noted that the AddIndexDefs method needs to be called before the dataset is created.
After the field definitions and index definitions have been created, call the CreateDataset method to create an in memory dataset for the application.
Next: Loading and Saving Files >>
More Delphi-Kylix Articles
More By Danish Ahmed