(*****************************************************************************)
(* MODULE: SQLSESS                                                           *)
(* RIGHTS: Reggatta Systems, Inc. - COPYRIGHT 1995 - 2000                    *)
(* DESCR.: This module contains the Scalable SQL Class.                      *)
(*****************************************************************************)
Unit SqlSess;
{$I BSE.INC}
{$I BAPI.INC}
Interface
Uses
  BDE,

  BseColl,

  BtrSess;


Type
  TBtrSqlSession =
  Class( TBtrSession )
    Protected
      Function IsScalableSQLDatabase( Name : PChar ) : Boolean;

    Public
      Destructor  Destroy; Override;

      Procedure   SetDBType( BeHonest : Boolean; pDbDesc : pDBDesc ); Override;

      Procedure   OpenDatabaseList ( var hCur : hDBICur );  Override;

      Procedure   OpenDatabaseInternal (                      { Open a database }
                    pdbDesc       : pDBDesc;                  { Database description }
                    eOpenMode     : DBIOpenMode;              { Open type }
                    eShareMode    : DBIShareMode;             { Share type }
                    pszPassword   : PChar;                    { Password }
                    iOptFlds      : Word;                     { Number of optional Params }
                    pOptFldDesc   : pFLDDesc;                 { Optional Field Descriptors }
                    pOptParams    : Pointer;                  { Optional Params }
                    var hDb       : hDBIDb                    { Returned database handle }
                  ); Override;
  end;

  Function SQLEngineLoaded : Boolean;

Implementation
Uses
  SysUtils,
  Classes,
  Windows,

  BseErrs,
  BseTable,
  BseRgtry,
  BseUtils,

  BtrTypes,
  BtrConst,
  BtrConv,

  SqlErrs,
  SqlTable,
  SqlDbase,
  SqlApi;

{$I BTRIEVE.INC}

Const
  MadeSQLCall : Boolean = FALSE;


Function SQLEngineLoaded : Boolean;
begin
  Result := MadeSQLCall;
  if Not Result then
  begin
    MadeSQlCall := ( SQLInit = 0 );
    Result       := MadeSQLCall;
  end;
end;


Destructor TBtrSqlSession.Destroy;
begin
  Inherited Destroy;
  If MadeSQLCall and ( Container <> NIL ) and ( Container.Count = 0 ) then
  begin
    if ( MainThreadID = GetCurrentThreadID ) then
    begin
      XQLStop;
      SQLStop;
    end;
    MadeSQLCall := FALSE;
  end;
end;




Function TBtrSqlSession.IsScalableSQLDatabase( Name : PChar ) : Boolean;
begin
  If ( Name <> NIL ) then
  begin
    Result := ( StrPos( Name, SSQL_DB_PREFIX ) = Name );
  end
  else
    Result := FALSE;
end;


Procedure TBtrSqlSession.SetDBType( BeHonest : Boolean; pDbDesc : pDBDesc );
var
  tmp : DBIPATH;
begin
  if IsScalableSQLDatabase( @pdbDesc^.szDbType ) then
  begin
    If ( StrPos( @pdbDesc^.szName, SSQL_DB_PREFIX ) = NIL ) then
    begin
      StrLCopy( @tmp, @pDbDesc^.szPhyName, SizeOf( tmp ) - 1 );
      StrLCat(  @tmp, WBTRV_USER_FILE, SizeOf( tmp ) );
      If FileExists( StrPas( @tmp ) ) then
        Exit;
    end
    else
      Exit;
  end;
  Inherited SetDBType( BeHonest, pDbDesc );
end;


Procedure TBtrSqlSession.OpenDatabaseInternal (             { Open a database }
           pdbDesc       : pDBDesc;                         { Database description }
           eOpenMode     : DBIOpenMode;                     { Open type }
           eShareMode    : DBIShareMode;                    { Share type }
           pszPassword   : PChar;                           { Password }
           iOptFlds      : Word;                            { Number of optional Params }
           pOptFldDesc   : pFLDDesc;                        { Optional Field Descriptors }
           pOptParams    : Pointer;                         { Optional Params }
           var   hDb     : hDBIDb                           { Returned database handle }
         );
Const
  MAX_PARAMS = 16;
  MAX_BUFFER = 1024;
var
  Desc       : DBDesc;
  Path       : String;
  iFields    : Word;
  FldDescs   : Array[0..MAX_PARAMS] of FLDDesc;
  Buffer     : Array[0..MAX_BUFFER] of Char;
  P          : TBtrSqlDatabase;
begin
  hDb := NIL;
  Move( pDbDesc^, Desc, SizeOf( DBDesc ) );
  If ( iOptFlds = 0 ) then
  begin
    Path := '\DATABASES\' + StrPas( @Desc.szName ) + '\DB INFO';
    Try
      CfgGetRecord( NIL, PChar( Path ), iFields, @FldDescs, @Buffer );
      iOptFlds    := iFields;
      pOptFldDesc := @FldDescs;
      pOptParams  := @Buffer;
    Except
      on E:ETitanBoundryError do
        ;
    end;
  end;
  P     := TBtrSqlDatabase.Create( Self, Databases, pdbDesc, eOPenMode, eShareMode,
             pszPassword, iOptFlds, pOptFldDesc, pOptParams );
  hDb   := hDBIDb( P );
  ValidateHandle( hDB );
  P.ClientID := ClientIDAddress;
  OpenedDatabase := TRUE;
(*
  IsSQL := ( StrLen( P.FName ) > 0 ) and ( StrLen( P.Password ) > 0 );
  If IsSQL then
  begin
    If SQLEngineLoaded then
      P.Login
    else
      ThrowScalableSQLException( ISAM_SSQL_NOT_LOADED );
  end;
*)
end;


Procedure TBtrSqlSession.OpenDatabaseList ( var hCur : hDBICur );
begin
  If IGNORESQLDATABASES or Not IsSSql40 then
    Inherited OpenDatabaseList( hCur )
  else
  begin
    If SQLEngineLoaded and DBNamesInterfaceDLLLoaded then
    begin
      hCur := hDBICur( TSSQLDatabaseTable.Create( Self, SystemTables, CfgRecords, NIL, NIL ) );
      TBaseTable( hCur ).OpenTable;
    end
    else
    begin
      IGNORESQLDATABASES := TRUE;
      Inherited OpenDatabaseList( hCur );
    end;
  end;
end;



end.

