Make your own free website on Tripod.com

File : oci-thread.adb


-- with TExt_IO; use Text_IO;
package body OCI.Thread is

   -----------------
   -- Environment --
   -----------------
   protected body Synch is

      procedure Same_Environment_In(
            Task_ID1 : Task_ID;
            Task_ID2 : Task_ID := Current_Task) is
         Env1 : Thread_Environment := Thread.Environment_Attr.Value
            (Task_ID1);
         Env2 : Thread_Environment := Thread.Environment_Attr.Value
            (Task_ID2);
      begin
         if Task_ID1=Task_ID2 then
            raise THE_SAME_TASKS;
         end if;
         if Env1.Environment=OCIEnv(Empty_Handle)
               and Env2.Environment=OCIEnv(Empty_Handle) then
            Env1 := Create;
            Environment_Attr.Set_Value(Env1,Task_ID1);
            Environment_Attr.Set_Value(Env1,Task_ID2);
         elsif Env1.Environment/=OCIEnv(Empty_Handle)
               and Env2.Environment/=OCIEnv(Empty_Handle) then
            raise BOTH_ENVIRONMENT_ALREADY_CREATED;
         elsif Env1.Environment/=OCIEnv(Empty_Handle) then
            Environment_Attr.Set_Value(Env1,Task_ID2);
         else
            Environment_Attr.Set_Value(Env2,Task_ID1);
         end if;
      end Same_Environment_In;

      function Environment return Thread_Environment is
         Envr : OCIEnv renames Thread.Environment_Attr.Reference.Environment
            ;
      begin
         if Envr=OCIEnv(Empty_Handle) then
            Environment_Attr.Set_Value(Create);
         end if;
         return Thread.Environment_Attr.Value;
      end Environment;

   end Synch;

   function Environment return OCIEnv is
      Envr : OCIEnv renames Thread.Environment_Attr.Reference.Environment
         ;
      Env : OCIEnv := Envr;
   begin
      if Env=OCIEnv(Empty_Handle) then
         return Synch.Environment.Environment;
      else
         return Env;
      end if;
   end Environment;

   procedure Create_Error is
   begin
      Error_Attr.Set_Value(Create(Synch.Environment));
   end;

   -----------
   -- Error --
   -----------

   function Error return OCIError is
      Errr : OCIError renames Thread.Error_Attr.Reference.Error;
      Err : OCIError := Errr;
   begin
      if Err=OCIError(Empty_Handle) then
         Create_Error;
         return Errr;
      else
         return Err;
      end if;
   end Error;

end OCI.Thread;