unit Unit3;

interface

uses
  uFBServices,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, IB_Services, ExtCtrls;

type
  TForm3 = class(TForm)
    StatusBar1: TStatusBar;
    Memo1: TMemo;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    TabSheet3: TTabSheet;
    Label1: TLabel;
    edtServer: TEdit;
    Label2: TLabel;
    cbProtocol: TComboBox;
    Label3: TLabel;
    edtUser: TEdit;
    Label4: TLabel;
    edtPassword: TEdit;
    Label5: TLabel;
    edtRole: TEdit;
    btnServerProperties: TButton;
    btnServerLog: TButton;
    memServerProps: TMemo;
    lvUsers: TListView;
    Label6: TLabel;
    edtUSUserName: TEdit;
    Label7: TLabel;
    edtUSPassword: TEdit;
    Label8: TLabel;
    edtUSFirstName: TEdit;
    Label9: TLabel;
    edtUSMiddleName: TEdit;
    Label10: TLabel;
    edtUSLastName: TEdit;
    btnDisplayUser: TButton;
    btnAddUser: TButton;
    btnModifyUser: TButton;
    btnDeleteUser: TButton;
    btnDisplayUsers: TButton;
    Label11: TLabel;
    edtCfgDBName: TEdit;
    btnShutdown: TButton;
    btnOnline: TButton;
    btnSweepInterval: TButton;
    btnSQLDialect: TButton;
    btnPageBuffers: TButton;
    btnActivateShadow: TButton;
    btnReserveSpace: TButton;
    btnAsyncMode: TButton;
    btnReadOnly: TButton;
    TabSheet4: TTabSheet;
    Label12: TLabel;
    edtSweepInterval: TEdit;
    Label13: TLabel;
    cbDialect: TComboBox;
    Label14: TLabel;
    edtPageBuffers: TEdit;
    chkReserveSpace: TCheckBox;
    chkAsyncMode: TCheckBox;
    chkReadOnly: TCheckBox;
    Label15: TLabel;
    edtDatabase: TEdit;
    Label16: TLabel;
    edtBackup: TEdit;
    Label17: TLabel;
    edtRestore: TEdit;
    memMaintenanceLog: TMemo;
    btnBackup: TButton;
    btnRestore: TButton;
    btnSweep: TButton;
    btnRepair: TButton;
    btnCheck: TButton;
    btnStatistica: TButton;
    Label18: TLabel;
    edtClientLib: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure btnServerPropertiesClick(Sender: TObject);
    procedure btnServerLogClick(Sender: TObject);
    procedure btnDisplayUsersClick(Sender: TObject);
    procedure btnDisplayUserClick(Sender: TObject);
    procedure btnAddUserClick(Sender: TObject);
    procedure btnModifyUserClick(Sender: TObject);
    procedure btnDeleteUserClick(Sender: TObject);
    procedure btnShutdownClick(Sender: TObject);
    procedure btnOnlineClick(Sender: TObject);
    procedure btnSweepIntervalClick(Sender: TObject);
    procedure btnSQLDialectClick(Sender: TObject);
    procedure btnPageBuffersClick(Sender: TObject);
    procedure btnActivateShadowClick(Sender: TObject);
    procedure btnReserveSpaceClick(Sender: TObject);
    procedure btnAsyncModeClick(Sender: TObject);
    procedure btnReadOnlyClick(Sender: TObject);
    procedure btnBackupClick(Sender: TObject);
    procedure btnRestoreClick(Sender: TObject);
    procedure btnSweepClick(Sender: TObject);
    procedure btnRepairClick(Sender: TObject);
    procedure btnCheckClick(Sender: TObject);
    procedure btnStatisticaClick(Sender: TObject);
  private
    procedure OnServerLog(Sender: TObject; const Text: string);
    procedure OnDatabaseLog(Sender: TObject; const Text: string);
    function GetFirebirdServer: TFirebirdServer;
  public
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

uses uCommon;

{$R *.dfm}

procedure TForm3.FormCreate(Sender: TObject);
begin
  Memo1.Text := GetExampleNotes;
  Caption := 'FIBPlus Example - ' + Application.Title;
end;

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

function TForm3.GetFirebirdServer: TFirebirdServer;
begin
  Result := TFirebirdServer.Instance(edtServer.Text, edtUser.Text,
    edtPassword.Text, TProtocol(cbProtocol.ItemIndex), edtClientLib.Text);
end;

procedure TForm3.btnServerPropertiesClick(Sender: TObject);
var
  sp: TpFIBServerProperties;
  i: Integer;
begin
  sp := GetFirebirdServer.GetServerPropeties([Database, Version, ConfigParameters]);
  with memServerProps.Lines do begin
    Add('Server Version       : ' + sp.VersionInfo.ServerVersion);
    Add('Server Implementation: ' + sp.VersionInfo.ServerImplementation);
    Add('Service Version      : ' + IntToStr(sp.VersionInfo.ServiceVersion));
    Add('');
    Add('Number of Attachmets : ' + IntToStr(sp.DatabaseInfo.NoOfAttachments));
    Add('Number of Databases  : ' + IntToStr(sp.DatabaseInfo.NoOfDatabases));
    Add('Attached Databases   :');
    for i := 0 to sp.DatabaseInfo.NoOfDatabases do
      Add('  '  + sp.DatabaseInfo.DbName[i]);

    Add('Base location              : ' + sp.ConfigParams.BaseLocation);
    Add('Lock File location         : ' + sp.ConfigParams.LockFileLocation);
    Add('Message File location      : ' + sp.ConfigParams.MessageFileLocation);
    Add('Security Database location : ' + sp.ConfigParams.SecurityDatabaseLocation);
  end;
  sp.Free;
end;

procedure TForm3.OnServerLog(Sender: TObject; const Text: string);
begin
  memServerProps.Lines.Add(Text);
end;

procedure TForm3.btnServerLogClick(Sender: TObject);
begin
  OnServerLog(nil, 'Server log:');
  with GetFirebirdServer do
  begin
    OnFbTextNotify := OnServerLog;
    GetServerLog;
  end;
end;

procedure TForm3.btnDisplayUsersClick(Sender: TObject);
var
  secSrvc: TpFIBSecurityService;
  i: Integer;
begin
  secSrvc := GetFirebirdServer.GetSecurityService;
  lvUsers.Clear;
  for i := 0 to secSrvc.UserInfoCount - 1 do
  begin
    with lvUsers.Items.Add do
    begin
      Caption := secSrvc.UserInfo[i].UserName;
      SubItems.Add(secSrvc.UserInfo[i].FirstName);
      SubItems.Add(secSrvc.UserInfo[i].MiddleName);
      SubItems.Add(secSrvc.UserInfo[i].LastName);
    end;
  end;
  secSrvc.Free;
end;

procedure TForm3.btnDisplayUserClick(Sender: TObject);
var
  secSrvc: TpFIBSecurityService;
begin
  secSrvc := GetFirebirdServer.GetSecurityService;
  try
    secSrvc.Attach;
    secSrvc.DisplayUser(edtUSUserName.Text);
  finally
    secSrvc.Detach;
  end;
  if secSrvc.UserInfoCount = 0 then
    ShowMessage('User not found');
  edtUSFirstName.Text := secSrvc.UserInfo[0].FirstName;
  edtUSMiddleName.Text:= secSrvc.UserInfo[0].MiddleName;
  edtUSLastName.Text  := secSrvc.UserInfo[0].LastName;
  edtUSPassword.Text  := '';
  secSrvc.Free;
end;

procedure TForm3.btnAddUserClick(Sender: TObject);
var secSrvc: TpFIBSecurityService;
begin
  secSrvc := GetFirebirdServer.GetSecurityService;
  try
    secSrvc.Attach;
    secSrvc.UserName := edtUSUserName.Text;
    secSrvc.Password := edtUSPassword.Text;
    secsrvc.FirstName:= edtUSFirstName.Text;
    secSrvc.MiddleName:= edtUSMiddleName.Text;
    secSrvc.LastName := edtUSLastName.Text;
    secSrvc.AddUser;
  finally
    secSrvc.Detach;
  end;
end;

procedure TForm3.btnModifyUserClick(Sender: TObject);
var secSrvc: TpFIBSecurityService;
begin
  secSrvc := GetFirebirdServer.GetSecurityService;
  try
    secSrvc.Attach;
    secSrvc.UserName := edtUSUserName.Text;
    secSrvc.Password := edtUSPassword.Text;
    secsrvc.FirstName:= edtUSFirstName.Text;
    secSrvc.MiddleName:= edtUSMiddleName.Text;
    secSrvc.LastName := edtUSLastName.Text;
    secSrvc.ModifyUser;
  finally
    secSrvc.Detach;
  end;
end;

procedure TForm3.btnDeleteUserClick(Sender: TObject);
var
  secSrvc: TpFIBSecurityService;
begin
  secSrvc := GetFirebirdServer.GetSecurityService;
  try
    secSrvc.Attach;
    secSrvc.UserName := edtUSUserName.Text;
    secSrvc.DeleteUser;
  finally
    secSrvc.Detach;
  end;
end;

procedure TForm3.btnShutdownClick(Sender: TObject);
begin GetFirebirdServer.ShutdownDB(edtCfgDBName.Text);
end;

procedure TForm3.btnOnlineClick(Sender: TObject);
begin GetFirebirdServer.OnlineDB(edtCfgDBName.Text);
end;

procedure TForm3.btnSweepIntervalClick(Sender: TObject);
begin GetFirebirdServer.SetupSweepInterval(StrToInt(edtSweepInterval.Text));
end;

procedure TForm3.btnSQLDialectClick(Sender: TObject);
begin GetFirebirdServer.SetupSQLDialect(cbDialect.ItemIndex + 1);
end;

procedure TForm3.btnPageBuffersClick(Sender: TObject);
begin GetFirebirdServer.SetupPageBuffers(StrToInt(edtPageBuffers.Text));
end;

procedure TForm3.btnActivateShadowClick(Sender: TObject);
begin GetFirebirdServer.ExecActivateShadow;
end;

procedure TForm3.btnReserveSpaceClick(Sender: TObject);
begin GetFirebirdServer.SetupReserveSpace(chkReserveSpace.Checked);
end;

procedure TForm3.btnAsyncModeClick(Sender: TObject);
begin GetFirebirdServer.SetupAsyncMode(chkAsyncMode.Checked);
end;

procedure TForm3.btnReadOnlyClick(Sender: TObject);
begin GetFirebirdServer.SetupReadOnly(chkReadOnly.Checked);
end;

procedure TForm3.btnBackupClick(Sender: TObject);
begin
  GetFirebirdServer.OnFbTextNotify := OnDatabaseLog;
  GetFirebirdServer.BackupDB(edtDatabase.Text, edtBackup.Text);
end;

procedure TForm3.btnRestoreClick(Sender: TObject);
begin
  GetFirebirdServer.OnFbTextNotify := OnDatabaseLog;
  GetFirebirdServer.RestoreDB(edtBackup.Text, edtRestore.Text);
end;

procedure TForm3.btnSweepClick(Sender: TObject);
begin
  GetFirebirdServer.OnFbTextNotify := OnDatabaseLog;
  GetFirebirdServer.SweepDatabase(edtDatabase.Text);
end;

procedure TForm3.btnRepairClick(Sender: TObject);
begin
  GetFirebirdServer.OnFbTextNotify := OnDatabaseLog;
  GetFirebirdServer.MendDatabase(edtDatabase.Text);
end;

procedure TForm3.OnDatabaseLog(Sender: TObject; const Text: string);
begin
  memMaintenanceLog.Lines.Add(Text);
end;

procedure TForm3.btnCheckClick(Sender: TObject);
begin
  GetFirebirdServer.OnFbTextNotify := OnDatabaseLog;
  if GetFirebirdServer.CheckDatabase(edtDatabase.Text) then
    ShowMessage('Validation Successful. No errors found.');
end;

procedure TForm3.btnStatisticaClick(Sender: TObject);
begin
  GetFirebirdServer.OnFbTextNotify := OnDatabaseLog;
  GetFirebirdServer.GetDBStat(edtDatabase.Text);
end;

end.
