unit i_ebss;

interface

uses
   DBTables, Sysutils;

const
   DATABASENAME = 'ebss';
   DATABASEUSER = 'ebss';
   DATABASEPASW = 'ebss';
   SESSIONNAME = 'session';

type
   _ebss= class
   public
      function openSQL(tmpSessionName, tmpDatabaseName, SQL:String):TQuery;overload;
      function execSQL(tmpSessionName, tmpDatabaseName, SQL:String):Boolean;overload;
   end;

function openDatabase(var tmpSession: TSession;var tmpDatabase: TDatabase; var tmpSessionName, tmpDatabaseName: String): Boolean;
procedure closeDatabase(var tmpSession: TSession;var tmpDatabase: TDatabase);

procedure TransactionStart(tmpDatabase: TDatabase);
procedure TransactionCommit(tmpDatabase: TDatabase);
procedure TransactionRollback(tmpDatabase: TDatabase);

function getDatabaseName: String;
function getSessionName: String;

implementation

function getSessionName: String;
var
   i: Integer;
   ch: Char;
begin
   Result := 'S_';
   Randomize;
   For i := 1 to 4 do
   begin
      Result := Result + Char(65 + Random(25));
      Result := Result + Char(48 + Random(9));
   end;
end;

function getDataBaseName: String;
var
   i: Integer;
   ch: Char;
begin
   Result := 'D_';
   Randomize;
   For i := 1 to 4 do
   begin
      Result := Result + Char(65 + Random(25));
      Result := Result + Char(48 + Random(9));
   end;
end;

procedure TransactionStart(tmpDatabase: TDatabase);
begin
   If Assigned(tmpDatabase) then
   begin
      tmpDatabase.StartTransaction;
   end;
end;

procedure TransactionCommit(tmpDatabase: TDatabase);
begin
   If Assigned(tmpDatabase) then
   begin
      tmpDatabase.Commit;
   end;
end;

procedure TransactionRollback(tmpDatabase: TDatabase);
begin
   If Assigned(tmpDatabase) then
   begin
      tmpDatabase.Rollback;
   end;
end;

function openDatabase(var tmpSession: TSession; var tmpDatabase: TDatabase; var tmpSessionName, tmpDatabaseName: String): Boolean;
begin
   Result := True;
   tmpSessionName := getSessionName;
   tmpDatabaseName := getDatabaseName;
   
   try
      tmpDatabase := nil;
      Sessions.OpenSession(tmpSessionName);
      tmpSession := Sessions.FindSession(tmpSessionName);
      If tSession <> nil then
      begin
         tmpDatabase := tmpSession.FindDatabase(tmpDatabaseName);
         If tmpDatabase = nil then
         begin
            tmpDatabase := TDatabase.Create(nil);
   
            tmpDatabase.Connected := False;
            tmpDatabase.AliasName := DATABASENAME;
            tmpDatabase.DatabaseName := tmpDatabaseName;
            tmpDatabase.SessionName := tmpSessionName;
            tmpDatabase.KeepConnection := True;
            tmpDatabase.LoginPrompt:=False;
            tmpDatabase.Params.Clear;
            tmpDatabase.Params.Add(Format('USER NAME=%s',[DATABASEUSER]));
            tmpDatabase.Params.Add(Format('PASSWORD=%s',[DATABASEPASW]));
         end;
         tmpDatabase:=tmpSession.OpenDatabase(tmpDatabaseName);
      end;
   except
      Result := False;
   end;
end;

procedure closeDatabase(var tmpSession: TSession; var tmpDatabase: TDatabase);
begin
   tmpDatabase.Close;
   tmpSession.DropConnections;

   tmpDatabase.Free;
   tmpSession.Close;
   tmpSession.Free;
end;

function _ebss.openSQL(tmpSessionName, tmpDatabaseName, SQL:String):TQuery;
begin
   Result := nil;
   If Length(SQL) > 0 then
   begin
     Result := TQuery.Create(nil);
     Result.DatabaseName := tmpDatabaseName;
     Result.SessionName := tmpSessionName;
     Result.SQL.Add(SQL);
     Result.Open;
   end;
end;

function _ebss.execSQL(tmpSessionName, tmpDatabaseName, SQL:String):Boolean;
var
   Query: TQuery;
begin
   Result := False;
   If Length(SQL) > 0 then
   begin
     Query := TQuery.Create(nil);
     Query.DatabaseName := tmpDatabaseName;
     Query.SessionName := tmpSessionName;
     Query.SQL.Add(SQL);
     Query.ExecSQL;
     Query.Free;

     Result := True;
   end;
end;

end.
