Recent

Author Topic: TBufDataSet index  (Read 4296 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 1876
    • Lazarus and Free Pascal italian community
TBufDataSet index
« on: December 31, 2013, 02:49:17 pm »
Hello guys, I have a problem. Who tells me how to create indexes for the data contained in a TBufDataSet? For example, how do I change the following code to index both columns?


Code: [Select]
     VDSet_Chiavi.FieldDefs.Add('Id',ftInteger);
     VDSet_Chiavi.FieldDefs.Add('Chiave',ftString,50);
     VDSet_Chiavi.MaxIndexesCount:=10;

     VDSet_Chiavi.CreateDataset;{creo il dataset virtuale con la struttura sopra dichiarata}
     VDSet_Chiavi.Open; {apro la connessione al dataset virtuale}
     VDSet_Chiavi.Append; {dico che voglio accodare dei dati al dataset virtuale}
     VDSet_Chiavi.Fields[0].Value:=1;
     VDSet_Chiavi.Fields[1].Value:='value 1';
     VDSet_Chiavi.Post; {applico l'accodamento}
     VDSet_Chiavi.Append;{dico che voglio accodare dei dati al dataset virtuale}
     VDSet_Chiavi.Fields[0].Value:=2;
     VDSet_Chiavi.Fields[1].Value:='value 2';
     VDSet_Chiavi.Post; {applico l'accodamento}
     VDSet_Chiavi.Append;{dico che voglio accodare dei dati al dataset virtuale}
     VDSet_Chiavi.Fields[0].Value:=3;
     VDSet_Chiavi.Fields[1].Value:='value 3';
     VDSet_Chiavi.Post; {applico l'accodamento}
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

howardpc

  • Hero Member
  • *****
  • Posts: 3011
Re: TBufDataSet index
« Reply #1 on: January 01, 2014, 03:05:00 am »
The small project attached gives you example code for indexing and filtering two related TBufDatasets.

By "index both columns" do you mean you want two indexes, one on each field, or an expression index that combines the two fields?

xinyiman

  • Hero Member
  • *****
  • Posts: 1876
    • Lazarus and Free Pascal italian community
Re: TBufDataSet index
« Reply #2 on: January 01, 2014, 11:11:23 am »
Thank you :)

 expression index that combines the two fields

They also do not understand why at the start of my program which I am attaching does not auto populate the field NewLookUpField. You know tell me why, would he do if the fields were indexed.

Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

howardpc

  • Hero Member
  • *****
  • Posts: 3011
Re: TBufDataSet index
« Reply #3 on: January 01, 2014, 12:09:54 pm »
Here's an example of setting an expression index. You'll have to place a TBufDataset, datasource, and dbgrid on your form and hook them up appropriately, then add OnCreate and OnDestroy handlers for your form as follows. Of course in a proper program your datasets would be put in a separate datamodule.

Code: [Select]
unit mainExpressionIdx;

{$mode objfpc}{$H+}

interface

uses
  Forms, DBGrids, DbCtrls, db, BufDataset, Classes;

type

  { TForm1 }

  TForm1 = class(TForm)
    dataNames: TBufDataset;
    dsNames: TDatasource;
    grdNames: TDBGrid;
    navNames: TDBNavigator;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  dataNames.FieldDefs.Add('Forename',ftString,15,True);
  dataNames.FieldDefs.Add('Surname',ftString,15,True);
  dataNames.FieldDefs.Add('ID',ftAutoInc,0,True);
  dataNames.CreateDataset;
  dataNames.IndexDefs.Add('ForenameSurnameExprIdx','Surname;Forename',[ixCaseInsensitive,ixUnique]);
  dataNames.Open;
  dataNames.AppendRecord(['Arthur','Gates',0]);
  dataNames.AppendRecord(['Arthur','Jones',0]);
  dataNames.AppendRecord(['Bill','Gates',0]);
  dataNames.AppendRecord(['Emily','Gates',0]);
  dataNames.AppendRecord(['Tim','Avery',0]);
  dataNames.AppendRecord(['Sven','Obama',0]);
  dataNames.IndexFieldNames:='Surname;Forename';
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  dataNames.Close;
end;

end.