The IDatasetInstance interface is used to work with the data of relational data sources. Properties and methods of this interface allow to receive information on source structure, and also the data of all source records. If it is required to change some records or to add new ones, the cache of relational data source is used in this case. The source cache is a clipboard in computer memory that contains records handled at the moment. The ICachedDataset interface is used to work with relational data source cache. The OpenCached method is used to open cache. The following operations with cache are available: filtering and search of records, changing existing and creating new records.
After finishing work with data or data cache, execute the IDatasetInstance.Close method.
The Filter and Filtered properties of data source cache are used to set up filtering.
Executing the example requires that the repository contains a table with the Table_1 identifier. This table contains the ID field that stores numeric identifiers of records, and also any other fields.
Sub UserProc;
Var
MB: IMetabase;
DSInst: IDatasetInstance;
Cache: ICachedDataset;
FieldsInst: IDatasetInstanceFields;
FieldInst: IDatasetInstanceField;
Begin
MB := MetabaseClass.Active;
DSInst := MB.ItemById("Table_1").Open(Null) As IDatasetInstance;
Cache := DSInst.OpenCached;
Cache.Filter.AsString := "(ID>=100)AND(ID<=1000)";
Cache.Filtered := True;
Cache.First;
While Not Cache.Eof Do
FieldsInst := Cache.Fields;
For Each FieldInst In FieldsInst Do
Debug.Write(FieldInst.Value + " ");
End For;
Debug.WriteLine("");
Cache.Next;
End While;
Cache.Close;
DSInst.Close;
End Sub UserProc;
The specified repository table is opened at the example execution. The cache, in which filtering is set up, is created for the data. Data of all records, satisfying to the set condition of filtering, are displayed in the console of the development environment.
Properties and methods of the ICachedDatasetLookup interface are used to search records in cache. Creation of the search object is provided on calling the CreateLookup method. The cache enables the user to search only for the first record with searched field values. If more than one record with required values exist in a source, it is recommended to set up cache filtering.
Executing the example requires that the repository contains a table with the Table_1 identifier. This table contains the ID and NAME fields and also any other fields.
Sub UserProc;
Var
MB: IMetabase;
DSInst: IDatasetInstance;
Cache: ICachedDataset;
CacheLookup: ICachedDatasetLookup;
SoughtFlds, FoundFlds: IDatasetInstanceFields;
Begin
MB := MetabaseClass.Active;
DSInst := MB.ItemById("Table_1").Open(Null) As IDatasetInstance;
Cache := DSInst.OpenCached;
//Creating object for search by field ID values
CacheLookup := Cache.CreateLookup("ID");
SoughtFlds := CacheLookup.Fields;
//Setting required value
SoughtFlds.Item(0).Value := 100;
//Record search
If CacheLookup.Locate Then
FoundFlds := CacheLookup.Lookup("NAME");
Debug.WriteLine(FoundFlds.Item(0).Value);
End If;
Cache.Close;
DSInst.Close;
End Sub UserProc;
The specified repository table is opened at the example execution. The record, in which value of the ID field equals to specified value, is searched in table data. In case of successful search the value of the NAME field of found record is displayed in the development environment console.
Methods of the ICachedDataset interface are used to edit existing and to delete and create new records in cache:
Append - add a new record to the end and open it for edit.
Insert - add a new record to the current position and open it for edit.
Delete - remove the current record.
Edit - open the current record for edit.
Post - save the made changes.
Cancel - undo the made changes and exit the edit mode.
Truncate - remove all records.
NOTE. These methods can be used only if the relational source allows editing records.
Executing the examples requires that the repository contains a table with the Table_1 identifier. This table contains the ID and NAME fields and also any other fields.
Example 1:
Sub UserProc;
Var
MB: IMetabase;
DSInst: IDatasetInstance;
Cache: ICachedDataset;
fID: IDatasetInstanceField;
Begin
MB := MetabaseClass.Active;
DSInst := MB.ItemById("Table_1").Open(Null) As IDatasetInstance;
Cache := DSInst.OpenCached;
While Not Cache.Eof Do
Cache.Edit;
fID := Cache.Fields.FindById("ID");
fID.Value := (fID.Value As Integer) + 1;
Cache.Post;
Cache.Next;
End While;
Cache.Close;
DSInst.Close;
End Sub UserProc;
After executing the example the values of the ID field in all records are increased by one.
Example 2:
Sub UserProc;
Var
MB: IMetabase;
DSInst: IDatasetInstance;
Cache: ICachedDataset;
fID: IDatasetInstanceField;
Begin
MB := MetabaseClass.Active;
DSInst := MB.ItemById("Table_1").Open(Null) As IDatasetInstance;
Cache := DSInst.OpenCached;
While Not Cache.Eof Do
fID := Cache.Fields.FindById("ID");
If ((fID.Value As Integer) Mod 2) = 0 Then
Cache.Delete
End If;
Cache.Next;
End While;
Cache.Close;
DSInst.Close;
End Sub UserProc;
After executing the example all records having value of the ID field divisible by two are removed from data source.
See also: