(*****************************************************************************)
(* MODULE: BTRMAIN                                                           *)
(* AUTHOR: Peter Blair                                                       *)
(* RIGHTS: Reggatta Systems, Inc. - COPYRIGHT 1995 - 2000                    *)
(* DESCR.: This module contains the unit that defines the Titan components   *)
(*****************************************************************************)
Unit BtrMain;
{$I BSE.INC}
{$I BAPI.INC}

{..$DEFINE USE_TREE}
{..$DEFINE USE_LOCALE_CODE}
{..$DEFINE USE_DBTABLES_STUFF}
{..$DEFINE USE_CALLBACKS}
{$R-}

interface
uses
  Windows,
  SysUtils,
  Graphics,
  Classes,
  Controls,
  Db,
  DBCommon,
  Bde,
  SMIntf,
  StdVCL,
  DbTables,
  BseEng,
  BseTable,
  BseSess,
  BseMain,
  BseTypes,
  BseLook,

  SQLSess;

Const
  BtrTitanEnv : TTitanEnvInfo =
  (
    RootKey     : HKEY_LOCAL_MACHINE;
    ClientPath  : '\Software\Titan\Btrieve'
  );


Type
  TtbSessionList =
  Class( TttSessionList )
    Function  Engine : TVirtEng; Override;
    Function  GetSessionClass : TttSessionClass; Override;
    Function  DefaultSession : TttSession; Override;
    Procedure OpenCriticalSection; Override;
    Procedure CloseCriticalSection; Override;
  end;

  TtbSession =
  Class( TttSession )
    Function  Engine : TVirtEng; Override;
    Procedure SetEngineObject( FEngine : TVirtEng ); Override;
    Procedure SetSessionList; Override;
    Function  GetEngineClass : TBaseSessionClass; Override;
    Function  DefaultSession : TttSession; Override;
    Function  GetDatabaseClass : TttDatabaseClass; Override;
    Function  GetTableClass : TttTableClass; Override;
    Procedure CriticalSectionStart; Override;
    Procedure CriticalSectionEnd; Override;
  end;
  TtbSessionClass  = Class of TtbSession;

  TtbDatabase =
  Class( TttDatabase )
    Function  LoginPromptDefault : Boolean; Override;
    Function  Engine : TVirtEng; Override;
    Procedure SetSessionList; Override;
    Function  DefaultSession : TttSession; Override;
  end;
  TtbDatabaseClass = Class of TtbDatabase;

  TtbBatchMove = Class;
  TtbBatchMoveClass = Class of TtbBatchMove;

  TtbTable =
  Class( TttTable )
    Function  Engine : TVirtEng; Override;
    Procedure SetSessionList; Override;
    Function  DefaultSession : TttSession; Override;
    Function  GetBatchMoveClass : TttBatchMoveClass; Override;
    Function  CreateBlobStream( Field : TField; Mode : TBlobStreamMode ) : TStream; Override;
  end;
  TtbTableClass = Class of TtbTable;

  TtbBatchMove =
  Class( TttBatchMove )
    Function  Engine : TVirtEng; Override;
  end;

  TtbStoredProc =
  Class( TttStoredProc )
    Function  Engine : TVirtEng; Override;
    Function  CreateBlobStream( Field : TField; Mode : TBlobStreamMode ) : TStream; Override;
    Procedure SetSessionList; Override;
    Function  DefaultSession : TttSession; Override;
  end;

  TtbUpdateSQL =
  Class( TttUpdateSQL )
    Function GetQueryClass : TttQueryClass; Override;
  end;

  TtbQuery =
  Class( TttQuery )
    Function  Engine : TVirtEng; Override;
    Function  CreateBlobStream( Field : TField; Mode : TBlobStreamMode ) : TStream; Override;
    Procedure SetSessionList; Override;
    Function  DefaultSession : TttSession; Override;
  end;

  TtbBlobStream =
  Class( TttBlobStream )
    Function  Engine : TVirtEng; Override;
  end;


  { These are Win 3.1 controls}
  TtbDBLookupList =
  Class( TttDBLookupList )
  end;

  TtbDBLookupCombo =
  Class( TttDBLookupCombo )
  end;


var
  BtrEng     : TVirtEng;
  TbSession  : TtbSession;
  TbSessions : TtbSessionlist;

implementation
uses
  BseConst,
  BseErrs;

Var
  FCSect : TRTLCriticalSection;

Procedure TtbSessionlist.OpenCriticalSection;
begin
  InitializeCriticalSection( FCSect );
end;


Procedure TtbSessionlist.CloseCriticalSection;
begin
  DeleteCriticalSection( FCSect );
end;


Function TtbSessionList.Engine : TVirtEng;
begin
  Result := BtrEng;
end;


Function TtbSessionList.DefaultSession : TttSession;
begin
  Result := TbSession;
end;


Function TtbSessionList.GetSessionClass : TttSessionClass;
begin
  Result := TtbSession;
end;


Function TtbSession.Engine : TVirtEng;
begin
  Result := BtrEng;
end;


Procedure TtbSession.SetEngineObject( FEngine : TVirtEng );
begin
  BtrEng := FEngine;
end;


Procedure TtbSession.SetSessionList;
begin
  FSessionList := TbSessions;
end;


Function TtbSession.GetEngineClass : TBaseSessionClass;
begin
  Result := TBtrSQLSession;
end;


Function TtbSession.DefaultSession : TttSession;
begin
  Result := TbSession;
end;


Function TTbSession.GetDatabaseClass : TttDatabaseClass;
begin
  Result := TtbDatabase;
end;


Function TTbSession.GetTableClass : TttTableClass;
begin
  Result := TtbTable;
end;


Procedure TtbSession.CriticalSectionStart;
begin
  EnterCriticalSection( FCSect );
end;


Procedure TtbSession.CriticalSectionEnd;
begin
  LeaveCriticalSection( FCSect );
end;


Function TtbDatabase.LoginPromptDefault : Boolean;
begin
  Result := FALSE;
end;


Function TtbDatabase.Engine : TVirtEng;
begin
  Result := BtrEng;
end;


Procedure TtbDatabase.SetSessionList;
begin
  FSessionList := TbSessions;
end;


Function TtbDatabase.DefaultSession : TttSession;
begin
  Result := TbSession;
end;


Procedure TtbTable.SetSessionList;
begin
  FSessionList := TbSessions;
end;


Function TtbTable.Engine : TVirtEng;
begin
  Result := BtrEng;
end;


Function TtbTable.DefaultSession : TttSession;
begin
  Result := TbSession;
end;

Function TtbTable.GetBatchMoveClass : TttBatchMoveClass;
begin
  Result := TtbBatchMove;
end;


Function TtbTable.CreateBlobStream( Field : TField; Mode : TBlobStreamMode ) : TStream;
begin
  Result := TtbBlobStream.Create( Field as TBlobField, Mode );
end;




Function TtbBatchMove.Engine : TVirtEng;
begin
  Result := BtrEng;
end;


Function TtbStoredProc.Engine : TVirtEng;
begin
  Result := BtrEng;
end;


Function TtbStoredProc.CreateBlobStream( Field : TField; Mode : TBlobStreamMode ) : TStream;
begin
  Result := TtbBlobStream.Create( Field as TBlobField, Mode );
end;


Procedure TtbStoredProc.SetSessionList;
begin
  FSessionList := TbSessions;
end;


Function TtbStoredProc.DefaultSession : TttSession;
begin
  Result := TbSession;
end;




Function TtbUpdateSQL.GetQueryClass : TttQueryClass;
begin
  Result := TtbQuery;
end;


Function TtbQuery.Engine : TVirtEng;
begin
  Result := BtrEng;
end;


Function TtbQuery.CreateBlobStream( Field : TField; Mode : TBlobStreamMode ) : TStream;
begin
  Result := TtbBlobStream.Create( Field as TBlobField, Mode );
end;


Procedure TtbQuery.SetSessionList;
begin
  FSessionList := TbSessions;
end;


Function TtbQuery.DefaultSession : TttSession;
begin
  Result := TbSession;
end;




Function TtbBlobStream.Engine : TVirtEng;
begin
  Result := BtrEng;
end;






Initialization
  TbSessions := TtbSessionlist.Create;
  TbSession  := TtbSession.Create( NIL );
  TbSession.SessionName := 'Btrieve';
finalization
  TbSession.Free;
  TbSessions.Free;
end.
