Memory Data Sample (Delphi)

From RemObjects Wiki
Jump to: navigation, search

This is a Data Abstract for Delphi Samples topic

Contents



This sample works only with the PCTrade Sample Server - Delphi.

Purpose

The Memory Data Sample demonstrates how to create and populate a virtual dataset dynamically.
The client displays two grids:

  • Top Grid: Displays the primary keys of one or more datasets from the PCTrade.
    Supply the names of the datasets required in a comma-separated list, e.g. Providers, Dealers, Groups.
  • Bottom Grid: Displays the names and sizes of the files contained in the specified disk folder.
MemoryDataSample.png

On the server side, virtual datasets are implemented via the TDAMemDataTable class and registered as TDataAbstractService.ExportedDataTables.

Examine the code

See the simple code in the ..\Server\DataService_Impl.pas:

  • ValidateDatasetAccess and onBeforeGetDatasetData events

procedure TDASampleService.DataAbstractServiceBeforeGetDatasetData(
  aSender: TObject; const aDataset: IDADataset; const aIncludeSchema: Boolean;
  const aMaxRecords: Integer);

  procedure ProcessMemoryDataCode;
  begin
    if SameText(aDataset.LogicalName, MemoryData_ComboDataset.LogicalName) then
      FillComboDataset((MemoryData_ComboDataset as IDAEditableDataset))
    else if SameText(aDataset.LogicalName, MemoryData_DirectoryData.LogicalName) then
      FillWinDirDataset((MemoryData_DirectoryData as IDAEditableDataset));
  end;

begin
  ProcessMemoryDataCode;
end;


procedure TDASampleService.DataAbstractServiceValidateDatasetAccess(
  Sender: TObject; const aConnection: IDAConnection; const aDatasetName: string;
  const aParamNames: array of string; const aParamValues: array of Variant;
  aSchema: TDASchema; var Allowed: Boolean);

  procedure FillParameters(aDataSet: TDADataTable);
  var
    i: integer;
  begin
    for i := Low(aParamNames) to High(aParamNames) do
      aDataset.ParamByName(aParamNames[i]).Value := aParamValues[i];
  end;

  procedure ProcessMemoryDataCode;
  begin
    if SameText(aDatasetName, MemoryData_ComboDataset.LogicalName) then
      FillParameters(MemoryData_ComboDataset)
    else if SameText(aDatasetName, MemoryData_DirectoryData.LogicalName) then
      FillParameters(MemoryData_DirectoryData);
  end;

begin
  ProcessMemoryDataCode;
... //skip the rest
end;
  • FillComboDataset and FillWinDirDataset methods

procedure TDASampleService.FillComboDataset(const aDataset: IDAEditableDataset);
var
  ltext: TDAField;

  procedure _FillCustomDataset(aDatasetName: string);
  var
    lDataset: IDADataset;
  begin
    if (Pos(UpperCase(aDatasetName), UpperCase(aDataset.ParamByName('CustomParameter').AsString)) > 0) then begin
      lDataset := Schema.NewDataset(Connection, aDatasetName, [], [], True);
      while not lDataset.EOF do begin
        aDataset.Append;
        ltext.AsWideString := aDatasetName + ': '+lDataset.Fields[1].AsWideString;
        aDataset.Post;

        lDataset.Next;
      end;
      lDataset:=nil;
    end;
  end;

begin
  with aDataset do begin
    // Opens the in memory data table
    Open;
    ltext := aDataset.FieldByName('TextField');

    // Adds 3 dummy records
    aDataset.Append; ltext.AsWideString := 'String A'; aDataset.Post;
    aDataset.Append; ltext.AsWideString := 'String B'; aDataset.Post;
    aDataset.Append; ltext.AsWideString := 'String C'; aDataset.Post;

    // Fetches data from other datasets, according to the value of the parameter "CustomParameter"
    _FillCustomDataset(ds_Providers);
    _FillCustomDataset(ds_Dealers);
    _FillCustomDataset(ds_Groups);

  end;
end;


procedure TDASampleService.FillWinDirDataset(
  const aDataset: IDAEditableDataset);
var
  dirinfo: TSearchRec;
begin
  aDataset.Open;
  if (FindFirst(IncludeTrailingPathDelimiter(aDataset.ParamByName('Directory').AsString) + '*.*', faArchive, dirinfo) = 0) then
  repeat
    aDataset.Append;

    aDataset.FieldByName('FileName').AsString := dirinfo.Name;
    aDataset.FieldByName('FileSize').AsInteger := dirinfo.Size;

    aDataset.Post;
  until (FindNext(dirinfo) <> 0);
  FindClose(dirinfo);
end;


Getting started

  • Run the PCTrade Sample Server - Delphi.
  • Run the Memory Data Sample.
  • Change the custom parameter, [re]open the table and review the results.
  • Change the Directory, [re]open the table and review the results.

Editions and Languages

Concepts Covered

See Also

Product: RemObjects Data Abstract
Available Editions: Data Abstract for .NET, Xcode, Delphi, Java and JavaScript

GlossaryArticlesFeaturesLibrarySamples

Personal tools
Namespaces

Variants
Actions
Navigation
products
platforms
special
Toolbox