Working with Data Source Cache

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 there is necessity to change any records or to add new, the cache of relational data source is used in this case. The cache of source is a clipboard in computer memory, that contains records handled at this moment. The ICachedDataset interface is used to work with a cache of a relational data . The OpenCached method is used to open cache, In cache the filtering and search of records, change existing and creation of new records are available.

Data filtering

The Filter and the 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. The table contains the ID field, storing 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(NullAs 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;
End Sub UserProc;

The specified repository table is opened at the example execution. The cache, filtering in which 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.

Data search

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 the cache filtering.

Executing the example requires that the repository contains a table with the Table_1 identifier. The 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(NullAs 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;
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.

Data change

Methods of the ICachedDataset interface are used to edit existing, and also remove and create new records in cache.

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. The 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(NullAs 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;
End Sub UserProc;

At example execution, 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(NullAs 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;
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:

Examples