unit Unit1;
                                                     
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, DB, FIBDataSet, pFIBDataSet, FIBDatabase, pFIBDatabase,
  ExtCtrls, StdCtrls, Grids, DBGrids;

type
  TForm1 = class(TForm)
    StatusBar1: TStatusBar;
    db: TpFIBDatabase;
    tr: TpFIBTransaction;
    dt: TpFIBDataSet;
    ds: TDataSource;
    Panel1: TPanel;
    Label2: TLabel;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    DBGrid1: TDBGrid;
    DBGrid2: TDBGrid;
    dtLim: TpFIBDataSet;
    dsLim: TDataSource;
    Button1: TButton;
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure PageControl1Change(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Unit2, uCommon;

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Memo1.Text := GetExampleNotes;
  Caption := 'FIBPlus Example - ' + Application.Title;
  db.DBName := 'localhost:' + ExtractFileDir(Application.ExeName) + '\db\employee.fdb';
  db.Connected := True;
  PageControl1Change(nil);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if not db.Connected then Exit;
  db.CloseDataSets;
  db.Close;
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := MessageDlg('This will end your ' + QuotedStr(Caption) + ' session. Proceed?',
    mtConfirmation, [mbOk, mbCancel], 0) = mrOk;
end;

procedure TForm1.PageControl1Change(Sender: TObject);
begin
  if PageControl1.ActivePageIndex = 1 then begin
    if dt.Active then dt.Close;
    dtLim.Open;
  end else begin
    if dtLim.Active then dtLim.Close;
    dt.Open;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  sCnt: string; iCnt: Integer;
begin
  sCnt := '1000000';
  if not InputQuery('Input record count',
    'record count for large table (recommended 500000 - 1000000)', sCnt)
  then Exit;
  try iCnt := StrToInt(sCnt)
  except
    ShowMessage('Enter valid integer value'); Exit;
  end;

  db.CloseDataSets;

  Form2 := TForm2.Create(Application);
  Form2.Show;
  try
    //deactivate indexes
    Form2.Label1.Caption := 'ALTER INDEX IDX_BIGTABLE_NAME INACTIVE';
    Application.ProcessMessages;
    db.Execute('ALTER INDEX IDX_BIGTABLE_NAME INACTIVE');

    Form2.Label1.Caption := 'ALTER INDEX IDX_BIGTABLE_NAME_DESC INACTIVE';
    Application.ProcessMessages;
    db.Execute('ALTER INDEX IDX_BIGTABLE_NAME_DESC INACTIVE');

    Form2.Label1.Caption := 'ALTER INDEX IDX_BIGTABLE_DETAIL_BIGTABLE_ID INACTIVE';
    Application.ProcessMessages;
    db.Execute('ALTER INDEX IDX_BIGTABLE_DETAIL_BIGTABLE_ID INACTIVE');

    //fill tables
    Form2.Label1.Caption := 'EXECUTE PROCEDURE GEN_BIGTABLE_CONTENT(' + sCnt + ')';
    Application.ProcessMessages;
    db.Execute('EXECUTE PROCEDURE GEN_BIGTABLE_CONTENT(' + sCnt + ')');

    //activate indexes
    Form2.Label1.Caption := 'ALTER INDEX IDX_BIGTABLE_NAME ACTIVE';
    Application.ProcessMessages;
    db.Execute('ALTER INDEX IDX_BIGTABLE_NAME ACTIVE');

    Form2.Label1.Caption := 'ALTER INDEX IDX_BIGTABLE_NAME_DESC ACTIVE';
    Application.ProcessMessages;
    db.Execute('ALTER INDEX IDX_BIGTABLE_NAME_DESC ACTIVE');

    Form2.Label1.Caption := 'ALTER INDEX IDX_BIGTABLE_DETAIL_BIGTABLE_ID ACTIVE';
    Application.ProcessMessages;
    db.Execute('ALTER INDEX IDX_BIGTABLE_DETAIL_BIGTABLE_ID ACTIVE');
  finally
    Form2.Hide;
    Form2.Free;
  end;

  db.Close;
  db.Open;

  PageControl1Change(nil);
end;

end.
