unit FRM_Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Mask, DBCtrls, DB, ExtCtrls, DBClient, Provider,
  IBODataset, IB_Components, IB_UtilityBar, IB_StatementGrid, IB_Samples,
  IB_Process, IB_Parse, IB_Script;

type
  TForm1 = class(TForm)
    db: TIBODatabase;
    qT2: TIBOQuery;
    dsp: TDataSetProvider;
    c1: TClientDataSet;
    c2: TClientDataSet;
    dT1: TDataSource;
    dc1: TDataSource;
    DBNavigator1: TDBNavigator;
    DBNavigator2: TDBNavigator;
    dc2: TDataSource;
    Label1: TLabel;
    DBEdit1: TDBEdit;
    Label2: TLabel;
    DBEdit2: TDBEdit;
    Label3: TLabel;
    DBEdit3: TDBEdit;
    Label4: TLabel;
    DBEdit4: TDBEdit;
    Label5: TLabel;
    DBEdit5: TDBEdit;
    qT1: TIBOQuery;
    ub: TIB_UtilityBar;
    Panel1: TPanel;
    lbT2_CODE: TLabel;
    lbT3_CODE: TLabel;
    edT2_CODE: TEdit;
    btInsertRecs: TButton;
    btApply: TButton;
    btPurge: TButton;
    edT3_CODE: TEdit;
    sc: TIB_Script;
    procedure btInsertRecsClick(Sender: TObject);
    procedure btApplyClick(Sender: TObject);
    procedure dspUpdateError(Sender: TObject;
      DataSet: TCustomClientDataSet; E: EUpdateError;
      UpdateKind: TUpdateKind; var Response: TResolverResponse);
    procedure btPurgeClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure c1AfterOpen(DataSet: TDataSet);
    procedure scMacroSubstitute(Sender: TComponent;
      const ATextBlock: String; var ATextResult: String);
    procedure dbBeforeConnect(Sender: TIB_Connection);
    procedure dbAfterConnect(Sender: TIB_Connection);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  ReadDefaultPath( db, 'IBOClientDatasetsPath' );
  if not db.Connected then
    db.Connect;
  if not db.Connected then
  begin
    ShowMessage( 'Unable to connect to server.'#13#10 +
                 'See your system administrator.');
    Application.Terminate;
  end;
  c1.Open;
  c2.Open;
end;

procedure TForm1.dbBeforeConnect(Sender: TIB_Connection);
begin
  if NeedToCreateDataBase then
    sc.Execute;
end;

procedure TForm1.scMacroSubstitute(Sender: TComponent;
  const ATextBlock: String; var ATextResult: String);
begin
  DefaultMacroSubstitute( ATextBlock, ATextResult );
end;

procedure TForm1.dbAfterConnect(Sender: TIB_Connection);
begin
  WriteDefaultPath;
end;

procedure TForm1.c1AfterOpen(DataSet: TDataSet);
begin
  c2.DataSetField := c1.FieldByName( 'qT2' ) as TDataSetField;
end;

procedure TForm1.btPurgeClick(Sender: TObject);
begin
  c2.Close;
  c1.Close;
  db.StartTransaction;
  try
    db.ExecSQL( 'DELETE FROM TEST2' );
    db.ExecSQL( 'DELETE FROM TEST1' );
    db.Commit;
  except
    db.Rollback;
    raise;
  end;
  c1.Open;
  c2.Open;
end;

procedure TForm1.btInsertRecsClick(Sender: TObject);
begin
  c1.Close;
  c1.Params.ParamByName( 'T1.CODE' ).AsInteger := StrToInt( edT2_CODE.Text );
  c1.Open;
  if not c1.Locate( 'CODE', edT2_CODE.Text, [] ) then
  begin
    c1.Insert;
    c1.FieldByName('CODE').AsString := edT2_CODE.Text;
    try
      c1.Post;
    except
      c1.Cancel;
      raise;
    end;
  end;
  c2.Open;
  c2.Insert;
  c2.FieldByName('HDR_CODE').AsString := edT2_CODE.Text;
  c2.FieldByName('CODE').AsString := edT3_CODE.Text;
  try
    c2.Post;
  except
    c2.Cancel;
    raise;
  end;
  btApply.Click;
  edT3_CODE.Text := IntToStr( StrToInt( edT3_CODE.Text ) + 1 );
end;

procedure TForm1.btApplyClick(Sender: TObject);
begin
  if c1.ChangeCount > 0 then
    c1.ApplyUpdates(0);
end;

procedure TForm1.dspUpdateError(Sender: TObject;
  DataSet: TCustomClientDataSet; E: EUpdateError; UpdateKind: TUpdateKind;
  var Response: TResolverResponse);
begin
  ShowMessage(E.Message);
  Response := rrAbort;
end;

end.


