unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

uses 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;

  dt.SelectSQL.Text := 'SELECT * FROM EMPLOYEE';
  dt.AutoUpdateOptions.UpdateTableName := 'EMPLOYEE';
  dt.AutoUpdateOptions.KeyFields       := 'EMP_NO';
  dt.AutoUpdateOptions.AutoReWriteSqls := True;
  dt.AutoUpdateOptions.CanChangeSQLs   := False;
  dt.Open;
  dt.InsertSQL.Add(' RETURNING EMP_NO')
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.Button1Click(Sender: TObject);
begin
  if dt.Active then dt.Close;
  dt.AutoUpdateOptions.CanChangeSQLs   := True;
  dt.InsertSQL.Clear;
  dt.UpdateSQL.Clear;
  dt.DeleteSQL.Clear;
  dt.RefreshSQL.Clear;
  dt.SelectSQL.Text := 'SELECT EMP.RDB$DB_KEY, EMP.* FROM EMPLOYEE EMP';
  dt.AutoUpdateOptions.UpdateTableName := 'EMPLOYEE';
  dt.AutoUpdateOptions.KeyFields       := 'DB_KEY';
  dt.AutoUpdateOptions.AutoReWriteSqls := True;
  dt.AutoUpdateOptions.CanChangeSQLs   := False;
  dt.Open;
  dt.InsertSQL.Add(' RETURNING RDB$DB_KEY');
//  ShowMessage(dt.InsertSQL.Text);
end;

end.
